Jeffrey Yasskin | 782f63e | 2011-08-26 00:41:31 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core -analyzer-store=region -verify %s |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 2 | |
| 3 | // FIXME: Only the stack-address checking in Sema catches this right now, and |
| 4 | // the stack analyzer doesn't handle the ImplicitCastExpr (lvalue). |
| 5 | const int& g() { |
| 6 | int s; |
Jeffrey Yasskin | 782f63e | 2011-08-26 00:41:31 +0000 | [diff] [blame] | 7 | return s; // expected-warning{{Address of stack memory associated with local variable 's' returned}} expected-warning{{reference to stack memory associated with local variable 's' returned}} |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 8 | } |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 9 | |
| 10 | const int& g2() { |
| 11 | int s1; |
| 12 | int &s2 = s1; // expected-note {{binding reference variable 's2' here}} |
Jeffrey Yasskin | 782f63e | 2011-08-26 00:41:31 +0000 | [diff] [blame] | 13 | return s2; // expected-warning{{Address of stack memory associated with local variable 's1' returned}} expected-warning {{reference to stack memory associated with local variable 's1' returned}} |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 14 | } |
| 15 | |
| 16 | const int& g3() { |
| 17 | int s1; |
| 18 | int &s2 = s1; // expected-note {{binding reference variable 's2' here}} |
| 19 | int &s3 = s2; // expected-note {{binding reference variable 's3' here}} |
Jeffrey Yasskin | 782f63e | 2011-08-26 00:41:31 +0000 | [diff] [blame] | 20 | return s3; // expected-warning{{Address of stack memory associated with local variable 's1' returned}} expected-warning {{reference to stack memory associated with local variable 's1' returned}} |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | int get_value(); |
| 24 | |
Jeffrey Yasskin | 782f63e | 2011-08-26 00:41:31 +0000 | [diff] [blame] | 25 | const int &get_reference1() { return get_value(); } // expected-warning{{Address of stack memory associated with temporary object of type 'const int' returned}} expected-warning {{returning reference to local temporary}} |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 26 | |
| 27 | const int &get_reference2() { |
| 28 | const int &x = get_value(); // expected-note {{binding reference variable 'x' here}} |
Jeffrey Yasskin | 782f63e | 2011-08-26 00:41:31 +0000 | [diff] [blame] | 29 | return x; // expected-warning{{Address of stack memory associated with temporary object of type 'const int' returned}} expected-warning {{returning reference to local temporary}} |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | const int &get_reference3() { |
| 33 | const int &x1 = get_value(); // expected-note {{binding reference variable 'x1' here}} |
| 34 | const int &x2 = x1; // expected-note {{binding reference variable 'x2' here}} |
Jeffrey Yasskin | 782f63e | 2011-08-26 00:41:31 +0000 | [diff] [blame] | 35 | return x2; // expected-warning{{Address of stack memory associated with temporary object of type 'const int' returned}} expected-warning {{returning reference to local temporary}} |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | int global_var; |
| 39 | int *f1() { |
| 40 | int &y = global_var; |
| 41 | return &y; |
| 42 | } |
| 43 | |
| 44 | int *f2() { |
| 45 | int x1; |
| 46 | int &x2 = x1; // expected-note {{binding reference variable 'x2' here}} |
Jeffrey Yasskin | 782f63e | 2011-08-26 00:41:31 +0000 | [diff] [blame] | 47 | return &x2; // expected-warning{{Address of stack memory associated with local variable 'x1' returned}} expected-warning {{address of stack memory associated with local variable 'x1' returned}} |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | int *f3() { |
| 51 | int x1; |
| 52 | int *const &x2 = &x1; // expected-note {{binding reference variable 'x2' here}} |
Ted Kremenek | 6a835dd | 2011-10-02 00:54:48 +0000 | [diff] [blame] | 53 | return x2; // expected-warning {{address of stack memory associated with local variable 'x1' returned}} expected-warning {{Address of stack memory associated with local variable 'x1' returned to caller}} |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | const int *f4() { |
| 57 | const int &x1 = get_value(); // expected-note {{binding reference variable 'x1' here}} |
| 58 | const int &x2 = x1; // expected-note {{binding reference variable 'x2' here}} |
Jeffrey Yasskin | 782f63e | 2011-08-26 00:41:31 +0000 | [diff] [blame] | 59 | return &x2; // expected-warning{{Address of stack memory associated with temporary object of type 'const int' returned}} expected-warning {{returning address of local temporary}} |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | struct S { |
| 63 | int x; |
| 64 | }; |
| 65 | |
| 66 | int *mf() { |
| 67 | S s1; |
| 68 | S &s2 = s1; // expected-note {{binding reference variable 's2' here}} |
| 69 | int &x = s2.x; // expected-note {{binding reference variable 'x' here}} |
Jeffrey Yasskin | 782f63e | 2011-08-26 00:41:31 +0000 | [diff] [blame] | 70 | return &x; // expected-warning{{Address of stack memory associated with local variable 's1' returned}} expected-warning {{address of stack memory associated with local variable 's1' returned}} |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void *lf() { |
| 74 | label: |
| 75 | void *const &x = &&label; // expected-note {{binding reference variable 'x' here}} |
| 76 | return x; // expected-warning {{returning address of label, which is local}} |
| 77 | } |
| 78 | |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 79 | template <typename T> |
| 80 | struct TS { |
| 81 | int *get(); |
| 82 | int *m() { |
| 83 | int *&x = get(); |
| 84 | return x; |
| 85 | } |
| 86 | }; |