-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dupfind #22
Open
jcdkiki
wants to merge
2
commits into
main
Choose a base branch
from
dupfind
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Dupfind #22
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
.PHONY: all install clean uninstall | ||
|
||
CC = g++ | ||
|
||
all: dupfind | ||
|
||
dupfind: main.cpp | ||
$(CC) main.cpp -o dupfind | ||
|
||
clean: | ||
rm -f dupfind | ||
|
||
install: dupfind | ||
cp dupfind /usr/local/bin | ||
|
||
uninstall: | ||
rm -f /usr/local/bin/dupfind |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,245 @@ | ||
#include <iostream> | ||
#include <algorithm> | ||
#include <string> | ||
#include <unordered_map> | ||
#include <vector> | ||
#include <fstream> | ||
#include <string.h> | ||
|
||
struct { | ||
bool show = false; | ||
int maxskip = 2; | ||
float similarity_threshold = 0.7f; | ||
int report_len = 4; | ||
} cfg; | ||
|
||
struct linedata_t { | ||
std::string str; | ||
std::string tab; | ||
size_t group_ind; | ||
}; | ||
|
||
struct groupdata_t { | ||
std::vector<size_t> members; | ||
const char *color; | ||
}; | ||
|
||
std::vector<linedata_t> lines; | ||
std::string file_name; | ||
std::unordered_map<int, groupdata_t> groups; | ||
|
||
inline void rtrim(std::string &s) | ||
{ | ||
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { | ||
return !std::isspace(ch); | ||
}).base(), s.end()); | ||
} | ||
|
||
int str_distance(std::string &a, std::string &b) | ||
{ | ||
if (a.size() == 0) | ||
return b.size(); | ||
|
||
if (b.size() == 0) | ||
return a.size(); | ||
|
||
std::vector<std::vector<int>> dp(a.size(), std::vector<int>(b.size())); | ||
dp[0][0] = 0; | ||
for (size_t i = 1; i < a.size(); i++) | ||
dp[i][0] = i; | ||
for (size_t j = 1; j < b.size(); j++) | ||
dp[0][j] = j; | ||
|
||
for (size_t i = 1; i < a.size(); i++) { | ||
for (size_t j = 1; j < b.size(); j++) { | ||
dp[i][j] = std::min(std::min(dp[i][j - 1] + 1, dp[i - 1][j] + 1), dp[i - 1][j - 1] + (a[i] != b[j])); | ||
} | ||
} | ||
|
||
return dp.back().back(); | ||
} | ||
|
||
void assign_line_to_group(int n, float threshold) | ||
{ | ||
bool ok = false; | ||
for (int i = 0; i < n; i++) { | ||
float similarity = 1.f - (float)str_distance(lines[n].str, lines[i].str) / (float)std::max(lines[n].str.size(), lines[i].str.size()); | ||
|
||
if (similarity >= threshold) { | ||
groups[i].members.push_back(n); | ||
lines[n].group_ind = i; | ||
ok = true; | ||
break; | ||
} | ||
} | ||
|
||
if (!ok) { | ||
groups[n].members.push_back(n); | ||
} | ||
} | ||
|
||
void print_lines(int first, int last) | ||
{ | ||
for (int i = first; i <= last; i++) { | ||
std::cout << lines[i].tab << ' ' << lines[i].str << std::endl; | ||
} | ||
} | ||
|
||
void report_if_needed(int len, int first, int last) | ||
{ | ||
if (len >= cfg.report_len) { | ||
std::cout << file_name << ": [" << first+1 << ", " << last+1 << "] duplicates [" << | ||
lines[first].group_ind+1 << ", " << lines[last].group_ind+1 << "]\n"; | ||
|
||
if (cfg.show) { | ||
std::cout << "...\n"; | ||
print_lines(lines[first].group_ind, lines[last].group_ind); | ||
std::cout << "...\n"; | ||
print_lines(first, last); | ||
std::cout << "...\n\n"; | ||
} | ||
} | ||
} | ||
|
||
void find_sequences() | ||
{ | ||
size_t seq_len = 1; | ||
size_t seq_first = 0; | ||
size_t seq_last = 0; | ||
|
||
for (size_t i = 1; i < lines.size(); i++) { | ||
if ((int)(i - seq_last) > cfg.maxskip + 1) { | ||
report_if_needed(seq_len, seq_first, seq_last); | ||
seq_len = 1; | ||
seq_last = i; | ||
seq_first = i; | ||
} | ||
|
||
int diff = lines[i].group_ind - lines[seq_last].group_ind; | ||
if (diff == 1) { | ||
seq_last = i; | ||
seq_len++; | ||
} | ||
} | ||
|
||
report_if_needed(seq_len, seq_first, seq_last); | ||
} | ||
|
||
bool is_line_regular(int n) | ||
{ | ||
if (lines[n].str.size() == 0) | ||
return false; | ||
|
||
if (lines[n].str == "}" || lines[n].str == "{") | ||
return false; | ||
|
||
if (lines[n].str.size() > 0 && lines[n].str[0] == '#') | ||
return false; | ||
|
||
if (lines[n].str.size() >= 2 && lines[n].str[0] == '/' && lines[n].str[1] == '/') | ||
return false; | ||
|
||
if (lines[n].str.size() >= 2 && lines[n].str[0] == '/' && lines[n].str[1] == '*') | ||
return false; | ||
|
||
if (lines[n].str.size() >= 6 && lines[n].str.substr(0, 6) == "break;") | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
void print_help() | ||
{ | ||
std::cout << "Usage: dupfind [options] <file1> <file2>...\n" | ||
"options:\n" | ||
" --show show code that was duplicated\n" | ||
" --maxskip X maximum allowed number of lines to skip\n" | ||
" --similarity X similarity threshold (in percent)\n" | ||
" --reportlen X minimal length of sequence to report it\n"; | ||
} | ||
|
||
char **parse_arg(char **argv) | ||
{ | ||
if (strcmp(argv[0], "-h") == 0 || strcmp(argv[0], "--help") == 0) { | ||
print_help(); | ||
exit(0); | ||
} | ||
|
||
if (argv[0][0] != '-' || argv[0][1] != '-') | ||
return argv; | ||
|
||
if (strcmp(argv[0], "--show") == 0) { | ||
cfg.show = true; | ||
return argv + 1; | ||
} | ||
|
||
if (argv[1] == NULL) | ||
return NULL; | ||
|
||
if (strcmp(argv[0], "--maxskip") == 0) { | ||
cfg.maxskip = atoi(argv[1]); | ||
return argv + 2; | ||
} | ||
if (strcmp(argv[0], "--similarity") == 0) { | ||
cfg.similarity_threshold = atof(argv[1]) / 100.f; | ||
return argv + 2; | ||
} | ||
if (strcmp(argv[0], "--reportlen") == 0) { | ||
cfg.report_len = atoi(argv[1]); | ||
return argv + 2; | ||
} | ||
|
||
return argv; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
if (argc < 2) { | ||
print_help(); | ||
exit(1); | ||
} | ||
|
||
argv++; | ||
while (*argv != NULL) { | ||
char **next = parse_arg(argv); | ||
if (next == NULL) { | ||
std::cout << "Input error\n"; | ||
exit(1); | ||
} | ||
else if (next > argv) { | ||
argv = next; | ||
continue; | ||
} | ||
|
||
lines.clear(); | ||
groups.clear(); | ||
|
||
file_name = *argv; | ||
std::ifstream file(*argv); | ||
if (!file.is_open()) { | ||
std::cout << "Failed to open file\n"; | ||
exit(1); | ||
} | ||
|
||
std::string line; | ||
while (std::getline(file, line)) { | ||
rtrim(line); | ||
|
||
size_t i = 0; | ||
while (i < line.size() && isspace(line[i])) i++; | ||
std::string tab = line.substr(0, i); | ||
line = line.substr(i, line.size() - i); | ||
|
||
lines.push_back({ line, tab, 0 }); | ||
} | ||
file.close(); | ||
|
||
for (size_t j = 0; j < lines.size(); j++) { | ||
if (is_line_regular(j)) | ||
assign_line_to_group(j, cfg.similarity_threshold); | ||
} | ||
|
||
find_sequences(); | ||
argv++; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/bash | ||
for i in $(find $1 -iname "*.c") | ||
do | ||
./dupfind --similarity 90 --show $i | ||
done | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Как планируется этот прототип подключать к oclint? |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Учтено ли в алгоритме то что в строках может быть разное число whitespace символов?