r253107 - CFG: Delay creating Dtors for CompoundStmts which end in ReturnStmt

Matthias Gehre via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 13 16:36:50 PST 2015


Author: mgehre
Date: Fri Nov 13 18:36:50 2015
New Revision: 253107

URL: http://llvm.org/viewvc/llvm-project?rev=253107&view=rev
Log:
CFG: Delay creating Dtors for CompoundStmts which end in ReturnStmt

Summary:
VisitReturnStmt would create a new block with including Dtors, so the Dtors created
in VisitCompoundStmts would be in an unreachable block.

Example:

struct S {
  ~S();
};

void f()
{
  S s;
  return;
}

void g()
{
  S s;
}

Before this patch, f has one additional unreachable block containing just the
destructor of S. With this patch, both f and g have the same blocks.

Reviewers: krememek

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D13973

Added:
    cfe/trunk/test/Analysis/no-unreachable-dtors.cpp
Modified:
    cfe/trunk/lib/Analysis/CFG.cpp

Modified: cfe/trunk/lib/Analysis/CFG.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/CFG.cpp?rev=253107&r1=253106&r2=253107&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/CFG.cpp (original)
+++ cfe/trunk/lib/Analysis/CFG.cpp Fri Nov 13 18:36:50 2015
@@ -1942,7 +1942,15 @@ CFGBlock *CFGBuilder::VisitChooseExpr(Ch
 
 
 CFGBlock *CFGBuilder::VisitCompoundStmt(CompoundStmt *C) {
-  addLocalScopeAndDtors(C);
+  LocalScope::const_iterator scopeBeginPos = ScopePos;
+  if (BuildOpts.AddImplicitDtors) {
+    addLocalScopeForStmt(C);
+  }
+  if (!C->body_empty() && !isa<ReturnStmt>(*C->body_rbegin())) {
+    // If the body ends with a ReturnStmt, the dtors will be added in VisitReturnStmt
+    addAutomaticObjDtors(ScopePos, scopeBeginPos, C);
+  }
+
   CFGBlock *LastBlock = Block;
 
   for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();

Added: cfe/trunk/test/Analysis/no-unreachable-dtors.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/no-unreachable-dtors.cpp?rev=253107&view=auto
==============================================================================
--- cfe/trunk/test/Analysis/no-unreachable-dtors.cpp (added)
+++ cfe/trunk/test/Analysis/no-unreachable-dtors.cpp Fri Nov 13 18:36:50 2015
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -analyze -analyzer-checker=debug.Stats -verify -Wno-unreachable-code %s
+
+struct S {
+  ~S();
+};
+
+// the return at the end of an CompoundStmt does not lead to an unreachable block containing the dtors
+void test() { // expected-warning-re{{test -> Total CFGBlocks: {{[0-9]+}} | Unreachable CFGBlocks: 0 | Exhausted Block: no | Empty WorkList: yes}}
+  S s;
+  return;
+}




More information about the cfe-commits mailing list