Clang Project

clang_source_code/test/SemaCXX/return-stack-addr-2.cpp
1// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify -std=c++11 %s
2
3namespace PR26599 {
4template <typename>
5struct S;
6
7struct I {};
8
9template <typename T>
10void *&non_pointer() {
11  void *&r = S<T>()[I{}];
12  return r;
13}
14
15template <typename T>
16void *&pointer() {
17  void *&r = S<T>()[nullptr];
18  return r;
19}
20}
21
22namespace LocalTemporary {
23
24template <class T>
25class QMap {
26public:
27  T value(const T &t = T()) const {
28    return t;
29  }
30};
31
32struct A {};
33
34void test() {
35  QMap<A *> map;
36  map.value();
37}
38
39typedef int* ptr;
40ptr int1(const ptr &p = ptr()) {
41  return (p);
42}
43
44ptr int2(const ptr &p = nullptr) {
45  return p;
46}
47
48ptr int3() {
49  const ptr &p = ptr();
50  return p;
51}
52
53const int *int4(const int &x = 5) {
54  return &x;
55}
56
57const int *int5(const int &x) {
58  return &x;
59}
60
61const int *int6() {
62  const int &x = 11;  //expected-note{{binding reference variable 'x' here}}
63  return &x;  //expected-warning{{returning address of local temporary object}}
64}
65
66const int *int7(int x) {
67  const int &x2 = x;  // expected-note{{binding reference variable 'x2' here}}
68  return &x2;  //  expected-warning{{address of stack memory associated with parameter 'x' returned}}
69}
70
71const int *int8(const int &x = 5) {
72  const int &x2 = x;
73  return &x2;
74}
75
76const int *int9() {
77  const int &x = 5;  // expected-note{{binding reference variable 'x' here}}
78  const int &x2 = x;  // expected-note{{binding reference variable 'x2' here}}
79  return &x2;  // expected-warning{{returning address of local temporary object}}
80}
81}
82