Daniel Dunbar | d7d5f02 | 2009-03-24 02:24:46 +0000 | [diff] [blame] | 1 | // RUN: clang-cc -analyze -warn-dead-stores -verify %s && |
Daniel Dunbar | d7d5f02 | 2009-03-24 02:24:46 +0000 | [diff] [blame] | 2 | // RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=basic -warn-dead-stores -verify %s && |
Ted Kremenek | c037eac | 2009-07-10 00:41:58 +0000 | [diff] [blame] | 3 | // RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic-old-cast -analyzer-constraints=basic -warn-dead-stores -verify %s && |
Daniel Dunbar | d7d5f02 | 2009-03-24 02:24:46 +0000 | [diff] [blame] | 4 | // RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=range -warn-dead-stores -verify %s && |
Ted Kremenek | c037eac | 2009-07-10 00:41:58 +0000 | [diff] [blame] | 5 | // RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic-old-cast -analyzer-constraints=range -warn-dead-stores -verify %s && |
Daniel Dunbar | d7d5f02 | 2009-03-24 02:24:46 +0000 | [diff] [blame] | 6 | // RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=basic -warn-dead-stores -verify %s && |
| 7 | // RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=range -warn-dead-stores -verify %s |
Ted Kremenek | 8aefcbf | 2007-11-19 06:38:23 +0000 | [diff] [blame] | 8 | |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 9 | void f1() { |
Ted Kremenek | aa395ba | 2007-11-18 20:06:35 +0000 | [diff] [blame] | 10 | int k, y; |
Ted Kremenek | 0fdf06e | 2008-03-19 07:31:52 +0000 | [diff] [blame] | 11 | int abc=1; |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 12 | long idx=abc+3*5; // expected-warning {{never read}} |
Ted Kremenek | aa395ba | 2007-11-18 20:06:35 +0000 | [diff] [blame] | 13 | } |
Ted Kremenek | 8aefcbf | 2007-11-19 06:38:23 +0000 | [diff] [blame] | 14 | |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 15 | void f2(void *b) { |
Ted Kremenek | 8aefcbf | 2007-11-19 06:38:23 +0000 | [diff] [blame] | 16 | char *c = (char*)b; // no-warning |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 17 | char *d = b+1; // expected-warning {{never read}} |
Douglas Gregor | a316e7b | 2009-02-14 00:32:47 +0000 | [diff] [blame] | 18 | printf("%s", c); // expected-warning{{implicitly declaring C library function 'printf' with type 'int (char const *, ...)'}} \ |
| 19 | // 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] | 20 | } |
Ted Kremenek | 74c43a0 | 2007-11-20 03:03:00 +0000 | [diff] [blame] | 21 | |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 22 | void f3() { |
Ted Kremenek | 0fdf06e | 2008-03-19 07:31:52 +0000 | [diff] [blame] | 23 | int r; |
| 24 | if ((r = f()) != 0) { // no-warning |
| 25 | int y = r; // no-warning |
| 26 | printf("the error is: %d\n", y); |
| 27 | } |
Ted Kremenek | 74c43a0 | 2007-11-20 03:03:00 +0000 | [diff] [blame] | 28 | } |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 29 | |
| 30 | void f4(int k) { |
| 31 | |
| 32 | k = 1; |
| 33 | |
| 34 | if (k) |
| 35 | f1(); |
| 36 | |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 37 | k = 2; // expected-warning {{never read}} |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 38 | } |
Ted Kremenek | f87821c | 2008-04-15 18:37:29 +0000 | [diff] [blame] | 39 | |
| 40 | void f5() { |
| 41 | |
| 42 | int x = 4; // no-warning |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 43 | int *p = &x; // expected-warning{{never read}} |
Ted Kremenek | f87821c | 2008-04-15 18:37:29 +0000 | [diff] [blame] | 44 | |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | int f6() { |
| 48 | |
| 49 | int x = 4; |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 50 | ++x; // expected-warning{{never read}} |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 51 | return 1; |
| 52 | } |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 53 | |
| 54 | int f7(int *p) { |
| 55 | // This is allowed for defensive programming. |
| 56 | p = 0; // no-warning |
| 57 | return 1; |
| 58 | } |
| 59 | |
| 60 | int f8(int *p) { |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 61 | extern int *baz(); |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 62 | if (p = baz()) // expected-warning{{Although the value}} |
| 63 | return 1; |
| 64 | return 0; |
| 65 | } |
| 66 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 67 | int f9() { |
| 68 | int x = 4; |
| 69 | x = x + 10; // expected-warning{{never read}} |
| 70 | return 1; |
| 71 | } |
| 72 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 73 | int f10() { |
| 74 | int x = 4; |
| 75 | x = 10 + x; // expected-warning{{never read}} |
| 76 | return 1; |
| 77 | } |
| 78 | |
Ted Kremenek | 8b00b6e | 2008-07-23 23:18:43 +0000 | [diff] [blame] | 79 | int f11() { |
| 80 | int x = 4; |
Ted Kremenek | 380277e | 2008-10-15 05:23:41 +0000 | [diff] [blame] | 81 | return x++; // expected-warning{{never read}} |
Ted Kremenek | 8b00b6e | 2008-07-23 23:18:43 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Ted Kremenek | 380277e | 2008-10-15 05:23:41 +0000 | [diff] [blame] | 84 | int f11b() { |
| 85 | int x = 4; |
Ted Kremenek | 7f5fce7 | 2009-01-20 00:47:45 +0000 | [diff] [blame] | 86 | return ((((++x)))); // no-warning |
Ted Kremenek | 380277e | 2008-10-15 05:23:41 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Ted Kremenek | fc7ff55 | 2008-07-25 04:47:34 +0000 | [diff] [blame] | 89 | int f12a(int y) { |
| 90 | int x = y; // expected-warning{{never read}} |
| 91 | return 1; |
| 92 | } |
| 93 | int f12b(int y) { |
| 94 | int x __attribute__((unused)) = y; // no-warning |
| 95 | return 1; |
| 96 | } |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 97 | |
Ted Kremenek | efe88f5 | 2008-08-06 23:26:31 +0000 | [diff] [blame] | 98 | // Filed with PR 2630. This code should produce no warnings. |
| 99 | int f13(void) |
| 100 | { |
| 101 | int a = 1; |
| 102 | int b, c = b = a + a; |
| 103 | |
| 104 | if (b > 0) |
| 105 | return (0); |
| 106 | |
| 107 | return (a + b + c); |
| 108 | } |
| 109 | |
Ted Kremenek | b497ebd | 2008-09-04 21:52:52 +0000 | [diff] [blame] | 110 | // Filed with PR 2763. |
Ted Kremenek | 84fa6b9 | 2008-09-26 05:52:45 +0000 | [diff] [blame] | 111 | int f14(int count) { |
Ted Kremenek | b497ebd | 2008-09-04 21:52:52 +0000 | [diff] [blame] | 112 | int index, nextLineIndex; |
| 113 | for (index = 0; index < count; index = nextLineIndex+1) { |
| 114 | nextLineIndex = index+1; // no-warning |
| 115 | continue; |
| 116 | } |
| 117 | return index; |
| 118 | } |
Ted Kremenek | 84fa6b9 | 2008-09-26 05:52:45 +0000 | [diff] [blame] | 119 | |
| 120 | // Test case for <rdar://problem/6248086> |
| 121 | void f15(unsigned x, unsigned y) { |
| 122 | int count = x * y; // no-warning |
| 123 | int z[count]; |
| 124 | } |
| 125 | |
Ted Kremenek | 610a09e | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 126 | int f16(int x) { |
| 127 | x = x * 2; |
Ted Kremenek | d2025e2 | 2008-09-26 23:05:47 +0000 | [diff] [blame] | 128 | x = sizeof(int [x = (x || x + 1) * 2]) // expected-warning{{Although the value stored to 'x' is used}} |
| 129 | ? 5 : 8; |
Ted Kremenek | 610a09e | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 130 | return x; |
| 131 | } |
| 132 | |
Ted Kremenek | 3b58786 | 2009-01-09 22:15:01 +0000 | [diff] [blame] | 133 | // Self-assignments should not be flagged as dead stores. |
| 134 | int f17() { |
| 135 | int x = 1; |
| 136 | x = x; // no-warning |
| 137 | } |
Ted Kremenek | 7f5fce7 | 2009-01-20 00:47:45 +0000 | [diff] [blame] | 138 | |
| 139 | // <rdar://problem/6506065> |
| 140 | // The values of dead stores are only "consumed" in an enclosing expression |
Mike Stump | cd7bf23 | 2009-07-17 01:04:31 +0000 | [diff] [blame^] | 141 | // what that value is actually used. In other words, don't say "Although the |
| 142 | // value stored to 'x' is used...". |
Ted Kremenek | 7f5fce7 | 2009-01-20 00:47:45 +0000 | [diff] [blame] | 143 | int f18() { |
| 144 | int x = 0; // no-warning |
| 145 | if (1) |
| 146 | x = 10; // expected-warning{{Value stored to 'x' is never read}} |
| 147 | while (1) |
| 148 | x = 10; // expected-warning{{Value stored to 'x' is never read}} |
| 149 | do |
| 150 | x = 10; // expected-warning{{Value stored to 'x' is never read}} |
| 151 | while (1); |
| 152 | |
| 153 | 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'}} |
| 154 | } |
Ted Kremenek | d3098ee | 2009-02-09 18:01:00 +0000 | [diff] [blame] | 155 | |
| 156 | // PR 3514: false positive `dead initialization` warning for init to global |
| 157 | // http://llvm.org/bugs/show_bug.cgi?id=3514 |
| 158 | extern const int MyConstant; |
| 159 | int f19(void) { |
| 160 | int x = MyConstant; // no-warning |
| 161 | x = 1; |
| 162 | return x; |
| 163 | } |
| 164 | |
Ted Kremenek | 28433ff | 2009-03-31 03:34:38 +0000 | [diff] [blame] | 165 | int f19b(void) { // This case is the same as f19. |
Ted Kremenek | d3098ee | 2009-02-09 18:01:00 +0000 | [diff] [blame] | 166 | const int MyConstant = 0; |
Ted Kremenek | 28433ff | 2009-03-31 03:34:38 +0000 | [diff] [blame] | 167 | int x = MyConstant; // no-warning |
Ted Kremenek | d3098ee | 2009-02-09 18:01:00 +0000 | [diff] [blame] | 168 | x = 1; |
| 169 | return x; |
| 170 | } |
Ted Kremenek | 632d1ec | 2009-03-23 22:30:58 +0000 | [diff] [blame] | 171 | |
| 172 | void f20(void) { |
| 173 | int x = 1; // no-warning |
| 174 | #pragma unused(x) |
| 175 | } |
| 176 | |
Mike Stump | cd7bf23 | 2009-07-17 01:04:31 +0000 | [diff] [blame^] | 177 | void halt() __attribute__((noreturn)); |
| 178 | int f21() { |
| 179 | int x = 4; |
| 180 | |
| 181 | ++x; // expected-warning{{never read}} |
| 182 | if (1) { |
| 183 | halt(); |
| 184 | (void)x; |
| 185 | } |
| 186 | return 1; |
| 187 | } |