[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / Analysis / missing-bind-temporary.cpp
blobf79677e3cdae224960e1eb532eb415ae536e4443
1 // RUN: %clang_analyze_cc1 -triple x86_64-unknown-linux -analyzer-checker=debug.DumpCFG -std=c++14 -analyzer-config eagerly-assume=false %s > %t 2>&1
2 // RUN: FileCheck --input-file=%t %s
3 // RUN: %clang_analyze_cc1 -triple x86_64-unknown-linux -analyzer-checker=core,debug.ExprInspection -std=c++14 -verify -analyzer-config eagerly-assume=false %s
5 void clang_analyzer_eval(bool);
7 int global;
9 namespace variant_0 {
10 // This variant of the code works correctly. Function foo() is not a template
11 // function. Note that there are two destructors within foo().
13 class A {
14 public:
15 ~A() { ++global; }
18 class B {
19 A a;
22 // CHECK: void foo(int)
23 // CHECK: [B1]
24 // CHECK-NEXT: 1: (CXXConstructExpr, [B1.2], B)
25 // CHECK-NEXT: 2: B i;
26 // CHECK-NEXT: 3: operator=
27 // CHECK-NEXT: 4: [B1.3] (ImplicitCastExpr, FunctionToPointerDecay, B &(*)(B &&) noexcept)
28 // CHECK-NEXT: 5: i
29 // CHECK-NEXT: 6: {} (CXXConstructExpr, [B1.7], [B1.8], B)
30 // CHECK-NEXT: 7: [B1.6] (BindTemporary)
31 // CHECK-NEXT: 8: [B1.7]
32 // CHECK-NEXT: 9: [B1.5] = [B1.8] (OperatorCall)
33 // CHECK-NEXT: 10: ~B() (Temporary object destructor)
34 // CHECK-NEXT: 11: [B1.2].~B() (Implicit destructor)
35 void foo(int) {
36 B i;
37 i = {};
40 void bar() {
41 global = 0;
42 foo(1);
43 clang_analyzer_eval(global == 2); // expected-warning{{TRUE}}
46 } // end namespace variant_0
48 namespace variant_1 {
49 // Suddenly, if we turn foo() into a template, we are missing a
50 // CXXBindTemporaryExpr in the AST, and therefore we're missing a
51 // temporary destructor in the CFG.
53 class A {
54 public:
55 ~A() { ++global; }
58 class B {
59 A a;
62 // FIXME: Find the construction context for {} and enforce the temporary
63 // destructor.
64 // CHECK: template<> void foo<int>(int)
65 // CHECK: [B1]
66 // CHECK-NEXT: 1: (CXXConstructExpr, [B1.2], B)
67 // CHECK-NEXT: 2: B i;
68 // CHECK-NEXT: 3: operator=
69 // CHECK-NEXT: 4: [B1.3] (ImplicitCastExpr, FunctionToPointerDecay, B &(*)(B &&) noexcept)
70 // CHECK-NEXT: 5: i
71 // CHECK-NEXT: 6: {} (CXXConstructExpr, B)
72 // CHECK-NEXT: 7: [B1.6]
73 // CHECK-NEXT: 8: [B1.5] = [B1.7] (OperatorCall)
74 // CHECK-NEXT: 9: [B1.2].~B() (Implicit destructor)
75 template <typename T> void foo(T) {
76 B i;
77 i = {};
80 void bar() {
81 global = 0;
82 foo(1);
83 // FIXME: Should be TRUE, i.e. we should call (and inline) two destructors.
84 clang_analyzer_eval(global == 2); // expected-warning{{UNKNOWN}}
87 } // end namespace variant_1
89 namespace variant_2 {
90 // Making field 'a' in class 'B' public turns the class into an aggregate.
91 // In this case there is no constructor at {} - only an aggregate
92 // initialization. Aggregate initialization is unsupported for now.
94 class A {
95 public:
96 ~A() { ++global; }
99 class B {
100 public:
101 A a;
104 // CHECK: template<> void foo<int>(int)
105 // CHECK: [B1]
106 // CHECK-NEXT: 1: (CXXConstructExpr, [B1.2], B)
107 // CHECK-NEXT: 2: B i;
108 // CHECK-NEXT: 3: operator=
109 // CHECK-NEXT: 4: [B1.3] (ImplicitCastExpr, FunctionToPointerDecay, B &(*)(B &&) noexcept)
110 // CHECK-NEXT: 5: i
111 // CHECK-NEXT: 6: {}
112 // CHECK-NEXT: 7: {}
113 // CHECK-NEXT: 8: [B1.7] (BindTemporary)
114 // CHECK-NEXT: 9: [B1.8]
115 // CHECK-NEXT: 10: [B1.5] = [B1.9] (OperatorCall)
116 // CHECK-NEXT: 11: ~B() (Temporary object destructor)
117 // CHECK-NEXT: 12: [B1.2].~B() (Implicit destructor)
118 template <typename T> void foo(T) {
119 B i;
120 i = {};
123 void bar() {
124 global = 0;
125 foo(1);
126 // FIXME: Should be TRUE, i.e. we should call (and inline) two destructors.
127 clang_analyzer_eval(global == 2); // expected-warning{{UNKNOWN}}
130 } // end namespace variant_2