[PATCH] D43056: [CFG] [analyzer] Add construction context for CXXBindTemporaryExpr.

2018-02-14 Thread Phabricator via Phabricator via cfe-commits
This revision was not accepted when it landed; it landed in state "Needs 
Review".
This revision was automatically updated to reflect the committed changes.
Closed by commit rL325210: [CFG] Provide construction contexts for temproary 
objects. (authored by dergachev, committed by ).
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D43056?vs=133341=134366#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D43056

Files:
  cfe/trunk/lib/Analysis/CFG.cpp
  cfe/trunk/test/Analysis/auto-obj-dtors-cfg-output.cpp
  cfe/trunk/test/Analysis/cfg-rich-constructors.cpp
  cfe/trunk/test/Analysis/temp-obj-dtors-cfg-output.cpp

Index: cfe/trunk/test/Analysis/auto-obj-dtors-cfg-output.cpp
===
--- cfe/trunk/test/Analysis/auto-obj-dtors-cfg-output.cpp
+++ cfe/trunk/test/Analysis/auto-obj-dtors-cfg-output.cpp
@@ -49,7 +49,8 @@
 // CHECK-NEXT:   3: a
 // CHECK-NEXT:   4: [B1.3] (ImplicitCastExpr, NoOp, const class A)
 // CHECK-NEXT:   5: const A  = a;
-// CHECK-NEXT:   6: A() (CXXConstructExpr, class A)
+// WARNINGS-NEXT:   6: A() (CXXConstructExpr, class A)
+// ANALYZER-NEXT:   6: A() (CXXConstructExpr, [B1.7], class A)
 // CHECK-NEXT:   7: [B1.6] (BindTemporary)
 // CHECK-NEXT:   8: [B1.7] (ImplicitCastExpr, NoOp, const class A)
 // CHECK-NEXT:   9: [B1.8]
Index: cfe/trunk/test/Analysis/cfg-rich-constructors.cpp
===
--- cfe/trunk/test/Analysis/cfg-rich-constructors.cpp
+++ cfe/trunk/test/Analysis/cfg-rich-constructors.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_analyze_cc1 -analyzer-checker=debug.DumpCFG -triple x86_64-apple-darwin12 -analyzer-config cfg-temporary-dtors=true -std=c++11 %s > %t 2>&1
+// RUN: %clang_analyze_cc1 -analyzer-checker=debug.DumpCFG -triple x86_64-apple-darwin12 -analyzer-config cfg-temporary-dtors=true -std=c++11 -w %s > %t 2>&1
 // RUN: FileCheck --input-file=%t %s
 
 class C {
@@ -8,6 +8,7 @@
   C(int, int);
 
   static C get();
+  operator bool() const;
 };
 
 typedef __typeof(sizeof(int)) size_t;
@@ -226,7 +227,7 @@
 
 } // end namespace ctor_initializers
 
-namespace return_stmt {
+namespace return_stmt_without_dtor {
 
 // CHECK: C returnVariable()
 // CHECK:  1:  (CXXConstructExpr, [B1.2], class C)
@@ -315,4 +316,140 @@
   return C(C::get());
 }
 
-} // end namespace return_stmt
+} // end namespace return_stmt_without_dtor
+
+namespace return_stmt_with_dtor {
+
+class D {
+public:
+  D();
+  ~D();
+};
+
+// CHECK:  return_stmt_with_dtor::D returnTemporary()
+// CHECK:  1: return_stmt_with_dtor::D() (CXXConstructExpr, [B1.2], class return_stmt_with_dtor::D)
+// CHECK-NEXT: 2: [B1.1] (BindTemporary)
+// CHECK-NEXT: 3: [B1.2] (ImplicitCastExpr, NoOp, const class return_stmt_with_dtor::D)
+// CHECK-NEXT: 4: [B1.3]
+// CHECK-NEXT: 5: [B1.4] (CXXConstructExpr, [B1.7], class return_stmt_with_dtor::D)
+// CHECK-NEXT: 6: ~return_stmt_with_dtor::D() (Temporary object destructor)
+// CHECK-NEXT: 7: return [B1.5];
+D returnTemporary() {
+  return D();
+}
+
+} // end namespace return_stmt_with_dtor
+
+namespace temporary_object_expr_without_dtors {
+
+// TODO: Should provide construction context for the constructor,
+// even if there is no specific trigger statement here.
+// CHECK: void simpleTemporary()
+// CHECK   1: C() (CXXConstructExpr, class C)
+void simpleTemporary() {
+  C();
+}
+
+// TODO: Should provide construction context for the constructor,
+// CHECK: void temporaryInCondition()
+// CHECK:  1: C() (CXXConstructExpr, class C)
+// CHECK-NEXT: 2: [B2.1] (ImplicitCastExpr, NoOp, const class C)
+// CHECK-NEXT: 3: [B2.2].operator bool
+// CHECK-NEXT: 4: [B2.2]
+// CHECK-NEXT: 5: [B2.4] (ImplicitCastExpr, UserDefinedConversion, _Bool)
+// CHECK-NEXT: T: if [B2.5]
+void temporaryInCondition() {
+  if (C());
+}
+
+} // end namespace temporary_object_expr_without_dtors
+
+namespace temporary_object_expr_with_dtors {
+
+class D {
+public:
+  D();
+  D(int);
+  ~D();
+
+  static D get();
+
+  operator bool() const;
+};
+
+// CHECK: void simpleTemporary()
+// CHECK:  1: temporary_object_expr_with_dtors::D() (CXXConstructExpr, [B1.2], class temporary_object_expr_with_dtors::D)
+// CHECK-NEXT: 2: [B1.1] (BindTemporary)
+// CHECK-NEXT: 3: ~temporary_object_expr_with_dtors::D() (Temporary object destructor)
+void simpleTemporary() {
+  D();
+}
+
+// CHECK:  void temporaryInCondition()
+// CHECK:  1: temporary_object_expr_with_dtors::D() (CXXConstructExpr, [B2.2], class temporary_object_expr_with_dtors::D)
+// CHECK-NEXT: 2: [B2.1] (BindTemporary)
+// CHECK-NEXT: 3: [B2.2] (ImplicitCastExpr, NoOp, const class temporary_object_expr_with_dtors::D)
+// CHECK-NEXT: 4: [B2.3].operator bool
+// CHECK-NEXT: 5: [B2.3]
+// CHECK-NEXT: 6: [B2.5] (ImplicitCastExpr, UserDefinedConversion, _Bool)
+// CHECK-NEXT: 7: 

[PATCH] D43056: [CFG] [analyzer] Add construction context for CXXBindTemporaryExpr.

2018-02-14 Thread Phabricator via Phabricator via cfe-commits
This revision was not accepted when it landed; it landed in state "Needs 
Review".
This revision was automatically updated to reflect the committed changes.
Closed by commit rC325210: [CFG] Provide construction contexts for temproary 
objects. (authored by dergachev, committed by ).

Repository:
  rC Clang

https://reviews.llvm.org/D43056

Files:
  lib/Analysis/CFG.cpp
  test/Analysis/auto-obj-dtors-cfg-output.cpp
  test/Analysis/cfg-rich-constructors.cpp
  test/Analysis/temp-obj-dtors-cfg-output.cpp

Index: test/Analysis/temp-obj-dtors-cfg-output.cpp
===
--- test/Analysis/temp-obj-dtors-cfg-output.cpp
+++ test/Analysis/temp-obj-dtors-cfg-output.cpp
@@ -216,7 +216,8 @@
 // CHECK:   [B2 (ENTRY)]
 // CHECK: Succs (1): B1
 // CHECK:   [B1]
-// CHECK: 1: A() (CXXConstructExpr, class A)
+// WARNINGS: 1: A() (CXXConstructExpr, class A)
+// ANALYZER: 1: A() (CXXConstructExpr, [B1.2], class A)
 // CHECK: 2: [B1.1] (BindTemporary)
 // CHECK: 3: [B1.2] (ImplicitCastExpr, NoOp, const class A)
 // CHECK: 4: [B1.3]
@@ -275,7 +276,8 @@
 // CHECK:   [B2 (ENTRY)]
 // CHECK: Succs (1): B1
 // CHECK:   [B1]
-// CHECK: 1: A() (CXXConstructExpr, class A)
+// WARNINGS: 1: A() (CXXConstructExpr, class A)
+// ANALYZER: 1: A() (CXXConstructExpr, [B1.2], class A)
 // CHECK: 2: [B1.1] (BindTemporary)
 // CHECK: 3: [B1.2] (ImplicitCastExpr, NoOp, const class A)
 // CHECK: 4: [B1.3]
@@ -290,13 +292,15 @@
 // CHECK:   [B2 (ENTRY)]
 // CHECK: Succs (1): B1
 // CHECK:   [B1]
-// CHECK: 1: A() (CXXConstructExpr, class A)
+// WARNINGS: 1: A() (CXXConstructExpr, class A)
+// ANALYZER: 1: A() (CXXConstructExpr, [B1.2], class A)
 // CHECK: 2: [B1.1] (BindTemporary)
 // CHECK: 3: [B1.2].operator int
 // CHECK: 4: [B1.2]
 // CHECK: 5: [B1.4] (ImplicitCastExpr, UserDefinedConversion, int)
 // CHECK: 6: int([B1.5]) (CXXFunctionalCastExpr, NoOp, int)
-// CHECK: 7: B() (CXXConstructExpr, class B)
+// WARNINGS: 7: B() (CXXConstructExpr, class B)
+// ANALYZER: 7: B() (CXXConstructExpr, [B1.8], class B)
 // CHECK: 8: [B1.7] (BindTemporary)
 // CHECK: 9: [B1.8].operator int
 // CHECK:10: [B1.8]
@@ -308,13 +312,15 @@
 // CHECK:16: ~A() (Temporary object destructor)
 // CHECK:17: foo
 // CHECK:18: [B1.17] (ImplicitCastExpr, FunctionToPointerDecay, void (*)(int))
-// CHECK:19: A() (CXXConstructExpr, class A)
+// WARNINGS:19: A() (CXXConstructExpr, class A)
+// ANALYZER:19: A() (CXXConstructExpr, [B1.20], class A)
 // CHECK:20: [B1.19] (BindTemporary)
 // CHECK:21: [B1.20].operator int
 // CHECK:22: [B1.20]
 // CHECK:23: [B1.22] (ImplicitCastExpr, UserDefinedConversion, int)
 // CHECK:24: int([B1.23]) (CXXFunctionalCastExpr, NoOp, int)
-// CHECK:25: B() (CXXConstructExpr, class B)
+// WARNINGS:25: B() (CXXConstructExpr, class B)
+// ANALYZER:25: B() (CXXConstructExpr, [B1.26], class B)
 // CHECK:26: [B1.25] (BindTemporary)
 // CHECK:27: [B1.26].operator int
 // CHECK:28: [B1.26]
@@ -347,7 +353,8 @@
 // CHECK: Preds (2): B4 B5
 // CHECK: Succs (2): B2 B1
 // CHECK:   [B4]
-// CHECK: 1: B() (CXXConstructExpr, class B)
+// WARNINGS: 1: B() (CXXConstructExpr, class B)
+// ANALYZER: 1: B() (CXXConstructExpr, [B4.2], class B)
 // CHECK: 2: [B4.1] (BindTemporary)
 // CHECK: 3: [B4.2].operator bool
 // CHECK: 4: [B4.2]
@@ -358,7 +365,8 @@
 // CHECK: 1: ~A() (Temporary object destructor)
 // CHECK: 2: foo
 // CHECK: 3: [B5.2] (ImplicitCastExpr, FunctionToPointerDecay, void (*)(_Bool))
-// CHECK: 4: A() (CXXConstructExpr, class A)
+// WARNINGS: 4: A() (CXXConstructExpr, class A)
+// ANALYZER: 4: A() (CXXConstructExpr, [B5.5], class A)
 // CHECK: 5: [B5.4] (BindTemporary)
 // CHECK: 6: [B5.5].operator bool
 // CHECK: 7: [B5.5]
@@ -377,15 +385,17 @@
 // CHECK: Preds (2): B8 B9
 // CHECK: Succs (2): B6 B5
 // CHECK:   [B8]
-// CHECK: 1: B() (CXXConstructExpr, class B)
+// WARNINGS: 1: B() (CXXConstructExpr, class B)
+// ANALYZER: 1: B() (CXXConstructExpr, [B8.2], class B)
 // CHECK: 2: [B8.1] (BindTemporary)
 // CHECK: 3: [B8.2].operator bool
 // CHECK: 4: [B8.2]
 // CHECK: 5: [B8.4] (ImplicitCastExpr, UserDefinedConversion, _Bool)
 // CHECK: Preds (1): B9
 // CHECK: Succs (1): B7
 // CHECK:   [B9]
-// CHECK: 1: A() (CXXConstructExpr, class A)
+// WARNINGS: 1: A() (CXXConstructExpr, class A)
+// ANALYZER: 1: A() (CXXConstructExpr, [B9.2], class A)
 // CHECK: 2: [B9.1] (BindTemporary)
 // CHECK: 3: [B9.2].operator bool
 // CHECK: 4: [B9.2]
@@ -413,7 +423,8 @@
 // CHECK: Preds (2): B4 B5
 // CHECK: Succs (2): B2 B1
 // CHECK:   [B4]
-// CHECK: 1: B() (CXXConstructExpr, class B)
+// WARNINGS: 1: B() (CXXConstructExpr, class B)
+// ANALYZER: 1: B() (CXXConstructExpr, [B4.2], 

[PATCH] D43056: [CFG] [analyzer] Add construction context for CXXBindTemporaryExpr.

2018-02-07 Thread Artem Dergachev via Phabricator via cfe-commits
NoQ created this revision.
NoQ added reviewers: dcoughlin, xazax.hun, a.sidorin, george.karpenkov, szepet.
Herald added subscribers: cfe-commits, rnkovacs.

This covers temporary constructors for which a destructor is necessary. It is 
necessary to support `CXXTemporaryObjectExpr` here, which is a "constructor in 
the middle of nowhere" expression like `C()` - it's a sub-class of 
`CXXConstructExpr` which we didn't even notice yet because it didn't appear in 
construction contexts we added support for in the previous patches, but it does 
commonly appear in `CXXBindTemporaryExpr`s and failing to consume the 
construction context would trigger the construction context overwrite assertion.

Note that when the destructor is trivial, `CXXBindTemporaryExpr` is omitted, 
and in that case we don't provide any construction context yet. In such cases 
there's no trigger statement at all, for example the following code:

  struct S {};
  
  void foo() {
S();
  }

would produce the following AST:

  `-FunctionDecl 0x7fea280366a0  line:3:6 foo 'void ()'
`-CompoundStmt 0x7fea28036d98 
  `-CXXTemporaryObjectExpr 0x7fea28036c70  'S' 'void () 
noexcept' zeroing

... and that's it.

So in that case we'd have to make a special kind of construction context that 
merely conveys that we are sure that we are indeed constructing a simple 
temporary object because we were directly told to do so by the programmer. But 
that's another story.


Repository:
  rC Clang

https://reviews.llvm.org/D43056

Files:
  lib/Analysis/CFG.cpp
  test/Analysis/auto-obj-dtors-cfg-output.cpp
  test/Analysis/cfg-rich-constructors.cpp
  test/Analysis/temp-obj-dtors-cfg-output.cpp

Index: test/Analysis/temp-obj-dtors-cfg-output.cpp
===
--- test/Analysis/temp-obj-dtors-cfg-output.cpp
+++ test/Analysis/temp-obj-dtors-cfg-output.cpp
@@ -216,7 +216,8 @@
 // CHECK:   [B2 (ENTRY)]
 // CHECK: Succs (1): B1
 // CHECK:   [B1]
-// CHECK: 1: A() (CXXConstructExpr, class A)
+// WARNINGS: 1: A() (CXXConstructExpr, class A)
+// ANALYZER: 1: A() (CXXConstructExpr, [B1.2], class A)
 // CHECK: 2: [B1.1] (BindTemporary)
 // CHECK: 3: [B1.2] (ImplicitCastExpr, NoOp, const class A)
 // CHECK: 4: [B1.3]
@@ -275,7 +276,8 @@
 // CHECK:   [B2 (ENTRY)]
 // CHECK: Succs (1): B1
 // CHECK:   [B1]
-// CHECK: 1: A() (CXXConstructExpr, class A)
+// WARNINGS: 1: A() (CXXConstructExpr, class A)
+// ANALYZER: 1: A() (CXXConstructExpr, [B1.2], class A)
 // CHECK: 2: [B1.1] (BindTemporary)
 // CHECK: 3: [B1.2] (ImplicitCastExpr, NoOp, const class A)
 // CHECK: 4: [B1.3]
@@ -290,13 +292,15 @@
 // CHECK:   [B2 (ENTRY)]
 // CHECK: Succs (1): B1
 // CHECK:   [B1]
-// CHECK: 1: A() (CXXConstructExpr, class A)
+// WARNINGS: 1: A() (CXXConstructExpr, class A)
+// ANALYZER: 1: A() (CXXConstructExpr, [B1.2], class A)
 // CHECK: 2: [B1.1] (BindTemporary)
 // CHECK: 3: [B1.2].operator int
 // CHECK: 4: [B1.2]
 // CHECK: 5: [B1.4] (ImplicitCastExpr, UserDefinedConversion, int)
 // CHECK: 6: int([B1.5]) (CXXFunctionalCastExpr, NoOp, int)
-// CHECK: 7: B() (CXXConstructExpr, class B)
+// WARNINGS: 7: B() (CXXConstructExpr, class B)
+// ANALYZER: 7: B() (CXXConstructExpr, [B1.8], class B)
 // CHECK: 8: [B1.7] (BindTemporary)
 // CHECK: 9: [B1.8].operator int
 // CHECK:10: [B1.8]
@@ -308,13 +312,15 @@
 // CHECK:16: ~A() (Temporary object destructor)
 // CHECK:17: foo
 // CHECK:18: [B1.17] (ImplicitCastExpr, FunctionToPointerDecay, void (*)(int))
-// CHECK:19: A() (CXXConstructExpr, class A)
+// WARNINGS:19: A() (CXXConstructExpr, class A)
+// ANALYZER:19: A() (CXXConstructExpr, [B1.20], class A)
 // CHECK:20: [B1.19] (BindTemporary)
 // CHECK:21: [B1.20].operator int
 // CHECK:22: [B1.20]
 // CHECK:23: [B1.22] (ImplicitCastExpr, UserDefinedConversion, int)
 // CHECK:24: int([B1.23]) (CXXFunctionalCastExpr, NoOp, int)
-// CHECK:25: B() (CXXConstructExpr, class B)
+// WARNINGS:25: B() (CXXConstructExpr, class B)
+// ANALYZER:25: B() (CXXConstructExpr, [B1.26], class B)
 // CHECK:26: [B1.25] (BindTemporary)
 // CHECK:27: [B1.26].operator int
 // CHECK:28: [B1.26]
@@ -347,7 +353,8 @@
 // CHECK: Preds (2): B4 B5
 // CHECK: Succs (2): B2 B1
 // CHECK:   [B4]
-// CHECK: 1: B() (CXXConstructExpr, class B)
+// WARNINGS: 1: B() (CXXConstructExpr, class B)
+// ANALYZER: 1: B() (CXXConstructExpr, [B4.2], class B)
 // CHECK: 2: [B4.1] (BindTemporary)
 // CHECK: 3: [B4.2].operator bool
 // CHECK: 4: [B4.2]
@@ -358,7 +365,8 @@
 // CHECK: 1: ~A() (Temporary object destructor)
 // CHECK: 2: foo
 // CHECK: 3: [B5.2] (ImplicitCastExpr, FunctionToPointerDecay, void (*)(_Bool))
-// CHECK: 4: A() (CXXConstructExpr, class A)
+// WARNINGS: 4: A() (CXXConstructExpr,