blob: 8cba63f17e0da3498770371839a13e408f22b5f0 [file] [log] [blame]
Jordan Roseee049592012-08-21 21:44:07 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=core,debug.ExprInspection -verify %s
Jordan Roseee158bc2012-07-09 16:54:49 +00002
3void clang_analyzer_eval(int);
Jordan Rosee5399f12012-08-10 22:26:29 +00004void clang_analyzer_checkInlined(int);
Zhongxing Xu1c625f22010-05-06 03:38:27 +00005
Ted Kremenekd4f482a2011-01-14 20:29:43 +00006int test1_f1() {
Zhongxing Xu06079d12010-02-27 02:44:37 +00007 int y = 1;
8 y++;
Jordan Rosee5399f12012-08-10 22:26:29 +00009 clang_analyzer_checkInlined(1); // expected-warning{{TRUE}}
Zhongxing Xu06079d12010-02-27 02:44:37 +000010 return y;
11}
12
Ted Kremenekd4f482a2011-01-14 20:29:43 +000013void test1_f2() {
Zhongxing Xu06079d12010-02-27 02:44:37 +000014 int x = 1;
Ted Kremenekd4f482a2011-01-14 20:29:43 +000015 x = test1_f1();
Zhongxing Xu06079d12010-02-27 02:44:37 +000016 if (x == 1) {
17 int *p = 0;
18 *p = 3; // no-warning
19 }
20 if (x == 2) {
21 int *p = 0;
Ted Kremenek452b84d2010-03-23 01:11:38 +000022 *p = 3; // expected-warning{{Dereference of null pointer (loaded from variable 'p')}}
Zhongxing Xu06079d12010-02-27 02:44:37 +000023 }
24}
Ted Kremenekd4f482a2011-01-14 20:29:43 +000025
26// Test that inlining works when the declared function has less arguments
27// than the actual number in the declaration.
28void test2_f1() {}
29int test2_f2();
30
31void test2_f3() {
32 test2_f1(test2_f2()); // expected-warning{{too many arguments in call to 'test2_f1'}}
33}
34
Ted Kremenek0849ade2012-01-12 19:25:46 +000035// Test that inlining works with recursive functions.
36
37unsigned factorial(unsigned x) {
38 if (x <= 1)
39 return 1;
40 return x * factorial(x - 1);
41}
42
43void test_factorial() {
44 if (factorial(3) == 6) {
45 int *p = 0;
46 *p = 0xDEADBEEF; // expected-warning {{null}}
47 }
48 else {
49 int *p = 0;
50 *p = 0xDEADBEEF; // no-warning
51 }
52}
53
54void test_factorial_2() {
55 unsigned x = factorial(3);
56 if (x == factorial(3)) {
57 int *p = 0;
58 *p = 0xDEADBEEF; // expected-warning {{null}}
59 }
60 else {
61 int *p = 0;
62 *p = 0xDEADBEEF; // no-warning
63 }
64}
Ted Kremenek7e867832012-03-03 01:22:03 +000065
66// Test that returning stack memory from a parent stack frame does
67// not trigger a warning.
68static char *return_buf(char *buf) {
69 return buf + 10;
70}
71
72void test_return_stack_memory_ok() {
73 char stack_buf[100];
74 char *pos = return_buf(stack_buf);
75 (void) pos;
76}
77
78char *test_return_stack_memory_bad() {
79 char stack_buf[100];
80 char *x = stack_buf;
81 return x; // expected-warning {{stack memory associated}}
82}
83
Ted Kremeneke4d653b2012-03-05 23:57:14 +000084// Test that passing a struct value with an uninitialized field does
85// not trigger a warning if we are inlining and the body is available.
86struct rdar10977037 { int x, y; };
87int test_rdar10977037_aux(struct rdar10977037 v) { return v.y; }
88int test_rdar10977037_aux_2(struct rdar10977037 v);
89int test_rdar10977037() {
90 struct rdar10977037 v;
91 v.y = 1;
92 v. y += test_rdar10977037_aux(v); // no-warning
93 return test_rdar10977037_aux_2(v); // expected-warning {{Passed-by-value struct argument contains uninitialized data}}
94}
95
96
Jordan Roseee158bc2012-07-09 16:54:49 +000097// Test inlining a forward-declared function.
98// This regressed when CallEvent was first introduced.
99int plus1(int x);
100void test() {
101 clang_analyzer_eval(plus1(2) == 3); // expected-warning{{TRUE}}
102}
103
104int plus1(int x) {
105 return x + 1;
106}
107
Jordan Rosee5399f12012-08-10 22:26:29 +0000108
109void never_called_by_anyone() {
110 clang_analyzer_checkInlined(0); // no-warning
111}
112