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