blob: 7aefea5095c78640ac5d3966e2d5bf443dc832f4 [file] [log] [blame]
Jeffrey Yasskin782f63e2011-08-26 00:41:31 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=core -analyzer-store=region -verify %s
Ted Kremenek68957a92010-08-04 20:01:07 +00002
3// FIXME: Only the stack-address checking in Sema catches this right now, and
4// the stack analyzer doesn't handle the ImplicitCastExpr (lvalue).
5const int& g() {
6 int s;
Jeffrey Yasskin782f63e2011-08-26 00:41:31 +00007 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 Kremenek68957a92010-08-04 20:01:07 +00008}
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00009
10const int& g2() {
11 int s1;
12 int &s2 = s1; // expected-note {{binding reference variable 's2' here}}
Jeffrey Yasskin782f63e2011-08-26 00:41:31 +000013 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 Kyrtzidis26e10be2010-11-30 22:57:32 +000014}
15
16const 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 Yasskin782f63e2011-08-26 00:41:31 +000020 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 Kyrtzidis26e10be2010-11-30 22:57:32 +000021}
22
23int get_value();
24
Jordan Rose5e5440b2013-02-22 01:51:15 +000025const int &get_reference1() { return get_value(); } // expected-warning{{Address of stack memory associated with temporary object of type 'int' returned}} expected-warning {{returning reference to local temporary}}
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +000026
27const int &get_reference2() {
28 const int &x = get_value(); // expected-note {{binding reference variable 'x' here}}
Jordan Rose5e5440b2013-02-22 01:51:15 +000029 return x; // expected-warning{{Address of stack memory associated with temporary object of type 'int' returned}} expected-warning {{returning reference to local temporary}}
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +000030}
31
32const 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}}
Jordan Rose5e5440b2013-02-22 01:51:15 +000035 return x2; // expected-warning{{Address of stack memory associated with temporary object of type 'int' returned}} expected-warning {{returning reference to local temporary}}
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +000036}
37
38int global_var;
39int *f1() {
40 int &y = global_var;
41 return &y;
42}
43
44int *f2() {
45 int x1;
46 int &x2 = x1; // expected-note {{binding reference variable 'x2' here}}
Jeffrey Yasskin782f63e2011-08-26 00:41:31 +000047 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 Kyrtzidis26e10be2010-11-30 22:57:32 +000048}
49
50int *f3() {
51 int x1;
52 int *const &x2 = &x1; // expected-note {{binding reference variable 'x2' here}}
Ted Kremenek6a835dd2011-10-02 00:54:48 +000053 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 Kyrtzidis26e10be2010-11-30 22:57:32 +000054}
55
56const 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}}
Jordan Rose5e5440b2013-02-22 01:51:15 +000059 return &x2; // expected-warning{{Address of stack memory associated with temporary object of type 'int' returned}} expected-warning {{returning address of local temporary}}
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +000060}
61
62struct S {
63 int x;
64};
65
66int *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 Yasskin782f63e2011-08-26 00:41:31 +000070 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 Kyrtzidis26e10be2010-11-30 22:57:32 +000071}
72
73void *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 Kyrtzidis26e10be2010-11-30 22:57:32 +000079template <typename T>
80struct TS {
81 int *get();
82 int *m() {
83 int *&x = get();
84 return x;
85 }
86};
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +000087
88// rdar://11345441
89int* f5() {
Hans Wennborg5965b7c2012-08-20 08:52:22 +000090 int& i = i; // expected-warning {{Assigned value is garbage or undefined}} expected-note {{binding reference variable 'i' here}} expected-warning{{reference 'i' is not yet bound to a value when used within its own initialization}}
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +000091 return &i; // expected-warning {{address of stack memory associated with local variable 'i' returned}}
92}
Ted Kremenekf08740b2013-02-22 01:39:26 +000093
94void *radar13226577() {
95 void *p = &p;
96 return p; // expected-warning {{stack memory associated with local variable 'p' returned to caller}}
97}
98