Daniel Dunbar | d427023 | 2009-01-20 23:17:32 +0000 | [diff] [blame] | 1 | // RUN: clang -analyze -warn-dead-stores -verify %s && |
| 2 | // RUN: clang -analyze -checker-simple -warn-dead-stores -verify %s && |
| 3 | // RUN: clang -analyze -warn-dead-stores -checker-simple -verify %s |
Ted Kremenek | e81da50 | 2008-07-02 23:18:22 +0000 | [diff] [blame] | 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}} |
Douglas Gregor | a316e7b | 2009-02-14 00:32:47 +0000 | [diff] [blame^] | 15 | printf("%s", c); // expected-warning{{implicitly declaring C library function 'printf' with type 'int (char const *, ...)'}} \ |
| 16 | // expected-note{{please include the header <stdio.h> or explicitly provide a declaration for 'printf'}} |
Ted Kremenek | 8aefcbf | 2007-11-19 06:38:23 +0000 | [diff] [blame] | 17 | } |
Ted Kremenek | 74c43a0 | 2007-11-20 03:03:00 +0000 | [diff] [blame] | 18 | |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 19 | void f3() { |
Ted Kremenek | 0fdf06e | 2008-03-19 07:31:52 +0000 | [diff] [blame] | 20 | int r; |
| 21 | if ((r = f()) != 0) { // no-warning |
| 22 | int y = r; // no-warning |
| 23 | printf("the error is: %d\n", y); |
| 24 | } |
Ted Kremenek | 74c43a0 | 2007-11-20 03:03:00 +0000 | [diff] [blame] | 25 | } |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 26 | |
| 27 | void f4(int k) { |
| 28 | |
| 29 | k = 1; |
| 30 | |
| 31 | if (k) |
| 32 | f1(); |
| 33 | |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 34 | k = 2; // expected-warning {{never read}} |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 35 | } |
Ted Kremenek | f87821c | 2008-04-15 18:37:29 +0000 | [diff] [blame] | 36 | |
| 37 | void f5() { |
| 38 | |
| 39 | int x = 4; // no-warning |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 40 | int *p = &x; // expected-warning{{never read}} |
Ted Kremenek | f87821c | 2008-04-15 18:37:29 +0000 | [diff] [blame] | 41 | |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | int f6() { |
| 45 | |
| 46 | int x = 4; |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 47 | ++x; // expected-warning{{never read}} |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 48 | return 1; |
| 49 | } |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 50 | |
| 51 | int f7(int *p) { |
| 52 | // This is allowed for defensive programming. |
| 53 | p = 0; // no-warning |
| 54 | return 1; |
| 55 | } |
| 56 | |
| 57 | int f8(int *p) { |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 58 | extern int *baz(); |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 59 | if (p = baz()) // expected-warning{{Although the value}} |
| 60 | return 1; |
| 61 | return 0; |
| 62 | } |
| 63 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 64 | int f9() { |
| 65 | int x = 4; |
| 66 | x = x + 10; // expected-warning{{never read}} |
| 67 | return 1; |
| 68 | } |
| 69 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 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 | 380277e | 2008-10-15 05:23:41 +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 | 380277e | 2008-10-15 05:23:41 +0000 | [diff] [blame] | 81 | int f11b() { |
| 82 | int x = 4; |
Ted Kremenek | 7f5fce7 | 2009-01-20 00:47:45 +0000 | [diff] [blame] | 83 | return ((((++x)))); // no-warning |
Ted Kremenek | 380277e | 2008-10-15 05:23:41 +0000 | [diff] [blame] | 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 | } |
Ted Kremenek | 7f5fce7 | 2009-01-20 00:47:45 +0000 | [diff] [blame] | 135 | |
| 136 | // <rdar://problem/6506065> |
| 137 | // The values of dead stores are only "consumed" in an enclosing expression |
| 138 | // what that value is actually used. In other words, don't say "Although the value stored to 'x' is used...". |
| 139 | int f18() { |
| 140 | int x = 0; // no-warning |
| 141 | if (1) |
| 142 | x = 10; // expected-warning{{Value stored to 'x' is never read}} |
| 143 | while (1) |
| 144 | x = 10; // expected-warning{{Value stored to 'x' is never read}} |
| 145 | do |
| 146 | x = 10; // expected-warning{{Value stored to 'x' is never read}} |
| 147 | while (1); |
| 148 | |
| 149 | return (x = 10); // expected-warning{{Although the value stored to 'x' is used in the enclosing expression, the value is never actually read from 'x'}} |
| 150 | } |
Ted Kremenek | d3098ee | 2009-02-09 18:01:00 +0000 | [diff] [blame] | 151 | |
| 152 | // PR 3514: false positive `dead initialization` warning for init to global |
| 153 | // http://llvm.org/bugs/show_bug.cgi?id=3514 |
| 154 | extern const int MyConstant; |
| 155 | int f19(void) { |
| 156 | int x = MyConstant; // no-warning |
| 157 | x = 1; |
| 158 | return x; |
| 159 | } |
| 160 | |
| 161 | int f19b(void) { // FIXME: Should this case be considered the same as f19? |
| 162 | const int MyConstant = 0; |
| 163 | int x = MyConstant; // expected-warning{{never read}} |
| 164 | x = 1; |
| 165 | return x; |
| 166 | } |