blob: e1f06835c784f00e632f0b1f4941296fbfc728e2 [file] [log] [blame]
Dominic Chen2cfd9012017-03-03 18:02:02 +00001// RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-store=region -verify %s -Wno-undefined-bool-conversion
Ted Kremenek68957a92010-08-04 20:01:07 +00002
Jordan Rose112344a2013-05-01 18:19:59 +00003typedef __INTPTR_TYPE__ intptr_t;
4
Ted Kremenek68957a92010-08-04 20:01:07 +00005const 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
Pavel Labath76b5dd42013-07-26 11:50:42 +000023void g4() {
24 static const int &x = 3; // no warning
25}
26
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +000027int get_value();
28
Jordan Rose5e5440b2013-02-22 01:51:15 +000029const 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 +000030
31const int &get_reference2() {
32 const int &x = get_value(); // expected-note {{binding reference variable 'x' here}}
Jordan Rose5e5440b2013-02-22 01:51:15 +000033 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 +000034}
35
36const int &get_reference3() {
37 const int &x1 = get_value(); // expected-note {{binding reference variable 'x1' here}}
38 const int &x2 = x1; // expected-note {{binding reference variable 'x2' here}}
Jordan Rose5e5440b2013-02-22 01:51:15 +000039 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 +000040}
41
42int global_var;
43int *f1() {
44 int &y = global_var;
45 return &y;
46}
47
48int *f2() {
49 int x1;
50 int &x2 = x1; // expected-note {{binding reference variable 'x2' here}}
Jeffrey Yasskin782f63e2011-08-26 00:41:31 +000051 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 +000052}
53
54int *f3() {
55 int x1;
56 int *const &x2 = &x1; // expected-note {{binding reference variable 'x2' here}}
Ted Kremenek6a835dd2011-10-02 00:54:48 +000057 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 +000058}
59
60const int *f4() {
61 const int &x1 = get_value(); // expected-note {{binding reference variable 'x1' here}}
62 const int &x2 = x1; // expected-note {{binding reference variable 'x2' here}}
Jordan Rose5e5440b2013-02-22 01:51:15 +000063 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 +000064}
65
66struct S {
67 int x;
68};
69
70int *mf() {
71 S s1;
72 S &s2 = s1; // expected-note {{binding reference variable 's2' here}}
73 int &x = s2.x; // expected-note {{binding reference variable 'x' here}}
Jeffrey Yasskin782f63e2011-08-26 00:41:31 +000074 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 +000075}
76
77void *lf() {
78 label:
79 void *const &x = &&label; // expected-note {{binding reference variable 'x' here}}
80 return x; // expected-warning {{returning address of label, which is local}}
81}
82
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +000083template <typename T>
84struct TS {
85 int *get();
86 int *m() {
87 int *&x = get();
88 return x;
89 }
90};
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +000091
92// rdar://11345441
93int* f5() {
Richard Smith354438d2018-07-23 21:21:22 +000094 int& i = i; // expected-warning {{Assigned value is garbage or undefined}} expected-warning{{reference 'i' is not yet bound to a value when used within its own initialization}}
95 return &i;
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +000096}
Ted Kremenekf08740b2013-02-22 01:39:26 +000097
98void *radar13226577() {
99 void *p = &p;
100 return p; // expected-warning {{stack memory associated with local variable 'p' returned to caller}}
101}
102
Jordan Rose112344a2013-05-01 18:19:59 +0000103namespace rdar13296133 {
104 class ConvertsToBool {
105 public:
106 operator bool() const { return this; }
107 };
108
109 class ConvertsToIntptr {
110 public:
111 operator intptr_t() const { return reinterpret_cast<intptr_t>(this); }
112 };
113
114 class ConvertsToPointer {
115 public:
116 operator const void *() const { return this; }
117 };
118
119 intptr_t returnAsNonLoc() {
120 ConvertsToIntptr obj;
121 return obj; // expected-warning{{Address of stack memory associated with local variable 'obj' returned to caller}}
122 }
123
124 bool returnAsBool() {
125 ConvertsToBool obj;
126 return obj; // no-warning
127 }
128
129 intptr_t returnAsNonLocViaPointer() {
130 ConvertsToPointer obj;
131 return reinterpret_cast<intptr_t>(static_cast<const void *>(obj)); // expected-warning{{Address of stack memory associated with local variable 'obj' returned to caller}}
132 }
133
134 bool returnAsBoolViaPointer() {
135 ConvertsToPointer obj;
136 return obj; // no-warning
137 }
138}
139