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