Ted Kremenek | 489f7b6 | 2008-07-04 04:38:48 +0000 | [diff] [blame] | 1 | // RUN: clang -warn-dead-stores -verify %s && |
2 | // RUN: clang -checker-simple -warn-dead-stores -verify %s && | ||||
Ted Kremenek | e81da50 | 2008-07-02 23:18:22 +0000 | [diff] [blame] | 3 | // RUN: clang -warn-dead-stores -checker-simple -verify %s |
4 | |||||
Ted Kremenek | 8aefcbf | 2007-11-19 06:38:23 +0000 | [diff] [blame] | 5 | |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 6 | void f1() { |
Ted Kremenek | aa395ba | 2007-11-18 20:06:35 +0000 | [diff] [blame] | 7 | int k, y; |
Ted Kremenek | 0fdf06e | 2008-03-19 07:31:52 +0000 | [diff] [blame] | 8 | int abc=1; |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 9 | long idx=abc+3*5; // expected-warning {{never read}} |
Ted Kremenek | aa395ba | 2007-11-18 20:06:35 +0000 | [diff] [blame] | 10 | } |
Ted Kremenek | 8aefcbf | 2007-11-19 06:38:23 +0000 | [diff] [blame] | 11 | |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 12 | void f2(void *b) { |
Ted Kremenek | 8aefcbf | 2007-11-19 06:38:23 +0000 | [diff] [blame] | 13 | char *c = (char*)b; // no-warning |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 14 | char *d = b+1; // expected-warning {{never read}} |
Ted Kremenek | 8aefcbf | 2007-11-19 06:38:23 +0000 | [diff] [blame] | 15 | printf("%s", c); |
16 | } | ||||
Ted Kremenek | 74c43a0 | 2007-11-20 03:03:00 +0000 | [diff] [blame] | 17 | |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 18 | void f3() { |
Ted Kremenek | 0fdf06e | 2008-03-19 07:31:52 +0000 | [diff] [blame] | 19 | int r; |
20 | if ((r = f()) != 0) { // no-warning | ||||
21 | int y = r; // no-warning | ||||
22 | printf("the error is: %d\n", y); | ||||
23 | } | ||||
Ted Kremenek | 74c43a0 | 2007-11-20 03:03:00 +0000 | [diff] [blame] | 24 | } |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 25 | |
26 | void f4(int k) { | ||||
27 | |||||
28 | k = 1; | ||||
29 | |||||
30 | if (k) | ||||
31 | f1(); | ||||
32 | |||||
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 33 | k = 2; // expected-warning {{never read}} |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 34 | } |
Ted Kremenek | f87821c | 2008-04-15 18:37:29 +0000 | [diff] [blame] | 35 | |
36 | void f5() { | ||||
37 | |||||
38 | int x = 4; // no-warning | ||||
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 39 | int *p = &x; // expected-warning{{never read}} |
Ted Kremenek | f87821c | 2008-04-15 18:37:29 +0000 | [diff] [blame] | 40 | |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 41 | } |
42 | |||||
43 | int f6() { | ||||
44 | |||||
45 | int x = 4; | ||||
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 46 | ++x; // expected-warning{{never read}} |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 47 | return 1; |
48 | } | ||||
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 49 | |
50 | int f7(int *p) { | ||||
51 | // This is allowed for defensive programming. | ||||
52 | p = 0; // no-warning | ||||
53 | return 1; | ||||
54 | } | ||||
55 | |||||
56 | int f8(int *p) { | ||||
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 57 | extern int *baz(); |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 58 | if (p = baz()) // expected-warning{{Although the value}} |
59 | return 1; | ||||
60 | return 0; | ||||
61 | } | ||||
62 | |||||
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 63 | int f9() { |
64 | int x = 4; | ||||
65 | x = x + 10; // expected-warning{{never read}} | ||||
66 | return 1; | ||||
67 | } | ||||
68 | |||||
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 69 | int f10() { |
70 | int x = 4; | ||||
71 | x = 10 + x; // expected-warning{{never read}} | ||||
72 | return 1; | ||||
73 | } | ||||
74 | |||||
Ted Kremenek | 8b00b6e | 2008-07-23 23:18:43 +0000 | [diff] [blame] | 75 | int f11() { |
76 | int x = 4; | ||||
Ted Kremenek | 380277e | 2008-10-15 05:23:41 +0000 | [diff] [blame] | 77 | return x++; // expected-warning{{never read}} |
Ted Kremenek | 8b00b6e | 2008-07-23 23:18:43 +0000 | [diff] [blame] | 78 | } |
79 | |||||
Ted Kremenek | 380277e | 2008-10-15 05:23:41 +0000 | [diff] [blame] | 80 | int f11b() { |
81 | int x = 4; | ||||
82 | return ++x; // no-warning | ||||
83 | } | ||||
84 | |||||
85 | |||||
Ted Kremenek | fc7ff55 | 2008-07-25 04:47:34 +0000 | [diff] [blame] | 86 | int f12a(int y) { |
87 | int x = y; // expected-warning{{never read}} | ||||
88 | return 1; | ||||
89 | } | ||||
90 | int f12b(int y) { | ||||
91 | int x __attribute__((unused)) = y; // no-warning | ||||
92 | return 1; | ||||
93 | } | ||||
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 94 | |
Ted Kremenek | efe88f5 | 2008-08-06 23:26:31 +0000 | [diff] [blame] | 95 | // Filed with PR 2630. This code should produce no warnings. |
96 | int f13(void) | ||||
97 | { | ||||
98 | int a = 1; | ||||
99 | int b, c = b = a + a; | ||||
100 | |||||
101 | if (b > 0) | ||||
102 | return (0); | ||||
103 | |||||
104 | return (a + b + c); | ||||
105 | } | ||||
106 | |||||
Ted Kremenek | b497ebd | 2008-09-04 21:52:52 +0000 | [diff] [blame] | 107 | // Filed with PR 2763. |
Ted Kremenek | 84fa6b9 | 2008-09-26 05:52:45 +0000 | [diff] [blame] | 108 | int f14(int count) { |
Ted Kremenek | b497ebd | 2008-09-04 21:52:52 +0000 | [diff] [blame] | 109 | int index, nextLineIndex; |
110 | for (index = 0; index < count; index = nextLineIndex+1) { | ||||
111 | nextLineIndex = index+1; // no-warning | ||||
112 | continue; | ||||
113 | } | ||||
114 | return index; | ||||
115 | } | ||||
Ted Kremenek | 84fa6b9 | 2008-09-26 05:52:45 +0000 | [diff] [blame] | 116 | |
117 | // Test case for <rdar://problem/6248086> | ||||
118 | void f15(unsigned x, unsigned y) { | ||||
119 | int count = x * y; // no-warning | ||||
120 | int z[count]; | ||||
121 | } | ||||
122 | |||||
Ted Kremenek | 610a09e | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 123 | int f16(int x) { |
124 | x = x * 2; | ||||
Ted Kremenek | d2025e2 | 2008-09-26 23:05:47 +0000 | [diff] [blame] | 125 | x = sizeof(int [x = (x || x + 1) * 2]) // expected-warning{{Although the value stored to 'x' is used}} |
126 | ? 5 : 8; | ||||
Ted Kremenek | 610a09e | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 127 | return x; |
128 | } | ||||
129 | |||||
Ted Kremenek | 3b58786 | 2009-01-09 22:15:01 +0000 | [diff] [blame^] | 130 | // Self-assignments should not be flagged as dead stores. |
131 | int f17() { | ||||
132 | int x = 1; | ||||
133 | x = x; // no-warning | ||||
134 | } |