suspicious-clones.cpp
2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// RUN: %clang_analyze_cc1 -verify %s \
// RUN: -analyzer-checker=alpha.clone.CloneChecker \
// RUN: -analyzer-config alpha.clone.CloneChecker:ReportNormalClones=false \
// RUN: -analyzer-config alpha.clone.CloneChecker:MinimumCloneComplexity=10
// Tests finding a suspicious clone that references local variables.
void log();
int max(int a, int b) {
log();
if (a > b)
return a;
return b; // expected-note{{Similar code using 'b' here}}
}
int maxClone(int x, int y, int z) {
log();
if (x > y)
return x;
return z; // expected-warning{{Potential copy-paste error; did you really mean to use 'z' here?}}
}
// Tests finding a suspicious clone that references global variables.
struct mutex {
bool try_lock();
void unlock();
};
mutex m1;
mutex m2;
int i;
void busyIncrement() {
while (true) {
if (m1.try_lock()) {
++i;
m1.unlock(); // expected-note{{Similar code using 'm1' here}}
if (i > 1000) {
return;
}
}
}
}
void faultyBusyIncrement() {
while (true) {
if (m1.try_lock()) {
++i;
m2.unlock(); // expected-warning{{Potential copy-paste error; did you really mean to use 'm2' here?}}
if (i > 1000) {
return;
}
}
}
}
// Tests that we provide two suggestions in cases where two fixes are possible.
int foo(int a, int b, int c) {
a += b + c;
b /= a + b;
c -= b * a; // expected-warning{{Potential copy-paste error; did you really mean to use 'b' here?}}
return c;
}
int fooClone(int a, int b, int c) {
a += b + c;
b /= a + b;
c -= a * a; // expected-note{{Similar code using 'a' here}}
return c;
}
// Tests that for clone groups with a many possible suspicious clone pairs, at
// most one warning per clone group is generated and every relevant clone is
// reported through either a warning or a note.
long bar1(long a, long b, long c, long d) {
c = a - b;
c = c / d * a;
d = b * b - c; // expected-warning{{Potential copy-paste error; did you really mean to use 'b' here?}}
return d;
}
long bar2(long a, long b, long c, long d) {
c = a - b;
c = c / d * a;
d = c * b - c; // expected-note{{Similar code using 'c' here}} \
// expected-warning{{Potential copy-paste error; did you really mean to use 'c' here?}}
return d;
}
long bar3(long a, long b, long c, long d) {
c = a - b;
c = c / d * a;
d = a * b - c; // expected-note{{Similar code using 'a' here}}
return d;
}