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