Argyrios Kyrtzidis | c4d2c90 | 2011-02-28 19:49:42 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core -analyzer-inline-call -analyzer-store region -verify %s |
Zhongxing Xu | 1c625f2 | 2010-05-06 03:38:27 +0000 | [diff] [blame] | 2 | |
Ted Kremenek | d4f482a | 2011-01-14 20:29:43 +0000 | [diff] [blame] | 3 | int test1_f1() { |
Zhongxing Xu | 06079d1 | 2010-02-27 02:44:37 +0000 | [diff] [blame] | 4 | int y = 1; |
| 5 | y++; |
| 6 | return y; |
| 7 | } |
| 8 | |
Ted Kremenek | d4f482a | 2011-01-14 20:29:43 +0000 | [diff] [blame] | 9 | void test1_f2() { |
Zhongxing Xu | 06079d1 | 2010-02-27 02:44:37 +0000 | [diff] [blame] | 10 | int x = 1; |
Ted Kremenek | d4f482a | 2011-01-14 20:29:43 +0000 | [diff] [blame] | 11 | x = test1_f1(); |
Zhongxing Xu | 06079d1 | 2010-02-27 02:44:37 +0000 | [diff] [blame] | 12 | if (x == 1) { |
| 13 | int *p = 0; |
| 14 | *p = 3; // no-warning |
| 15 | } |
| 16 | if (x == 2) { |
| 17 | int *p = 0; |
Ted Kremenek | 452b84d | 2010-03-23 01:11:38 +0000 | [diff] [blame] | 18 | *p = 3; // expected-warning{{Dereference of null pointer (loaded from variable 'p')}} |
Zhongxing Xu | 06079d1 | 2010-02-27 02:44:37 +0000 | [diff] [blame] | 19 | } |
| 20 | } |
Ted Kremenek | d4f482a | 2011-01-14 20:29:43 +0000 | [diff] [blame] | 21 | |
| 22 | // Test that inlining works when the declared function has less arguments |
| 23 | // than the actual number in the declaration. |
| 24 | void test2_f1() {} |
| 25 | int test2_f2(); |
| 26 | |
| 27 | void test2_f3() { |
| 28 | test2_f1(test2_f2()); // expected-warning{{too many arguments in call to 'test2_f1'}} |
| 29 | } |
| 30 | |
Ted Kremenek | 0849ade | 2012-01-12 19:25:46 +0000 | [diff] [blame] | 31 | // Test that inlining works with recursive functions. |
| 32 | |
| 33 | unsigned factorial(unsigned x) { |
| 34 | if (x <= 1) |
| 35 | return 1; |
| 36 | return x * factorial(x - 1); |
| 37 | } |
| 38 | |
| 39 | void test_factorial() { |
| 40 | if (factorial(3) == 6) { |
| 41 | int *p = 0; |
| 42 | *p = 0xDEADBEEF; // expected-warning {{null}} |
| 43 | } |
| 44 | else { |
| 45 | int *p = 0; |
| 46 | *p = 0xDEADBEEF; // no-warning |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void test_factorial_2() { |
| 51 | unsigned x = factorial(3); |
| 52 | if (x == factorial(3)) { |
| 53 | int *p = 0; |
| 54 | *p = 0xDEADBEEF; // expected-warning {{null}} |
| 55 | } |
| 56 | else { |
| 57 | int *p = 0; |
| 58 | *p = 0xDEADBEEF; // no-warning |
| 59 | } |
| 60 | } |
Ted Kremenek | 7e86783 | 2012-03-03 01:22:03 +0000 | [diff] [blame] | 61 | |
| 62 | // Test that returning stack memory from a parent stack frame does |
| 63 | // not trigger a warning. |
| 64 | static char *return_buf(char *buf) { |
| 65 | return buf + 10; |
| 66 | } |
| 67 | |
| 68 | void test_return_stack_memory_ok() { |
| 69 | char stack_buf[100]; |
| 70 | char *pos = return_buf(stack_buf); |
| 71 | (void) pos; |
| 72 | } |
| 73 | |
| 74 | char *test_return_stack_memory_bad() { |
| 75 | char stack_buf[100]; |
| 76 | char *x = stack_buf; |
| 77 | return x; // expected-warning {{stack memory associated}} |
| 78 | } |
| 79 | |
Ted Kremenek | e4d653b | 2012-03-05 23:57:14 +0000 | [diff] [blame^] | 80 | // Test that passing a struct value with an uninitialized field does |
| 81 | // not trigger a warning if we are inlining and the body is available. |
| 82 | struct rdar10977037 { int x, y; }; |
| 83 | int test_rdar10977037_aux(struct rdar10977037 v) { return v.y; } |
| 84 | int test_rdar10977037_aux_2(struct rdar10977037 v); |
| 85 | int test_rdar10977037() { |
| 86 | struct rdar10977037 v; |
| 87 | v.y = 1; |
| 88 | v. y += test_rdar10977037_aux(v); // no-warning |
| 89 | return test_rdar10977037_aux_2(v); // expected-warning {{Passed-by-value struct argument contains uninitialized data}} |
| 90 | } |
| 91 | |
| 92 | |