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 | |||||
69 | |||||
70 | int f10() { | ||||
71 | int x = 4; | ||||
72 | x = 10 + x; // expected-warning{{never read}} | ||||
73 | return 1; | ||||
74 | } | ||||
75 | |||||
Ted Kremenek | 8b00b6e | 2008-07-23 23:18:43 +0000 | [diff] [blame] | 76 | int f11() { |
77 | int x = 4; | ||||
Ted Kremenek | b0f3632 | 2008-07-24 17:01:17 +0000 | [diff] [blame] | 78 | return ++x; // expected-warning{{never read}} |
Ted Kremenek | 8b00b6e | 2008-07-23 23:18:43 +0000 | [diff] [blame] | 79 | } |
80 | |||||
Ted Kremenek | fc7ff55 | 2008-07-25 04:47:34 +0000 | [diff] [blame] | 81 | int f12a(int y) { |
82 | int x = y; // expected-warning{{never read}} | ||||
83 | return 1; | ||||
84 | } | ||||
85 | int f12b(int y) { | ||||
86 | int x __attribute__((unused)) = y; // no-warning | ||||
87 | return 1; | ||||
88 | } | ||||
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 89 | |
Ted Kremenek | efe88f5 | 2008-08-06 23:26:31 +0000 | [diff] [blame] | 90 | // Filed with PR 2630. This code should produce no warnings. |
91 | int f13(void) | ||||
92 | { | ||||
93 | int a = 1; | ||||
94 | int b, c = b = a + a; | ||||
95 | |||||
96 | if (b > 0) | ||||
97 | return (0); | ||||
98 | |||||
99 | return (a + b + c); | ||||
100 | } | ||||
101 | |||||
Ted Kremenek | b497ebd | 2008-09-04 21:52:52 +0000 | [diff] [blame] | 102 | // Filed with PR 2763. |
Ted Kremenek | 84fa6b9 | 2008-09-26 05:52:45 +0000 | [diff] [blame^] | 103 | int f14(int count) { |
Ted Kremenek | b497ebd | 2008-09-04 21:52:52 +0000 | [diff] [blame] | 104 | int index, nextLineIndex; |
105 | for (index = 0; index < count; index = nextLineIndex+1) { | ||||
106 | nextLineIndex = index+1; // no-warning | ||||
107 | continue; | ||||
108 | } | ||||
109 | return index; | ||||
110 | } | ||||
Ted Kremenek | 84fa6b9 | 2008-09-26 05:52:45 +0000 | [diff] [blame^] | 111 | |
112 | // Test case for <rdar://problem/6248086> | ||||
113 | void f15(unsigned x, unsigned y) { | ||||
114 | int count = x * y; // no-warning | ||||
115 | int z[count]; | ||||
116 | } | ||||
117 |