]> granicus.if.org Git - clang/commitdiff
[analyzer] Fix crash when building CFG with variable of incomplete type
authorMartin Bohme <mboehme@google.com>
Tue, 7 Mar 2017 08:42:37 +0000 (08:42 +0000)
committerMartin Bohme <mboehme@google.com>
Tue, 7 Mar 2017 08:42:37 +0000 (08:42 +0000)
Summary:
I've included a unit test with a function template containing a variable
of incomplete type. Clang compiles this without errors (the standard
does not require a diagnostic in this case). Without the fix, this case
triggers the crash.

Reviewers: klimek

Reviewed By: klimek

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D30636

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@297129 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/CFG.cpp
unittests/Analysis/CFGTest.cpp

index d56e0e8fa1d0d402eb522732d188fd2353fa58a5..2a2b3d73b5caf6af76211ddb23d9c48f297f97b9 100644 (file)
@@ -1390,7 +1390,7 @@ LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl *VD,
 
   // Check if type is a C++ class with non-trivial destructor.
   if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl())
-    if (!CD->hasTrivialDestructor()) {
+    if (CD->hasDefinition() && !CD->hasTrivialDestructor()) {
       // Add the variable to scope
       Scope = createOrReuseLocalScope(Scope);
       Scope->addVar(VD);
index e691110050a22dbefdbb1ff2ef299459e1f44cb1..768705f46f29f7e55fadd93574dc4c2eff9492a2 100644 (file)
@@ -35,7 +35,9 @@ public:
     if (!Body)
       return;
     TheBuildResult = SawFunctionBody;
-    if (CFG::buildCFG(nullptr, Body, Result.Context, CFG::BuildOptions()))
+    CFG::BuildOptions Options;
+    Options.AddImplicitDtors = true;
+    if (CFG::buildCFG(nullptr, Body, Result.Context, Options))
         TheBuildResult = BuiltCFG;
   }
 };
@@ -75,6 +77,16 @@ TEST(CFG, DeleteExpressionOnDependentType) {
   EXPECT_EQ(BuiltCFG, BuildCFG(Code));
 }
 
+// Constructing a CFG on a function template with a variable of incomplete type
+// should not crash.
+TEST(CFG, VariableOfIncompleteType) {
+  const char *Code = "template<class T> void f() {\n"
+                     "  class Undefined;\n"
+                     "  Undefined u;\n"
+                     "}\n";
+  EXPECT_EQ(BuiltCFG, BuildCFG(Code));
+}
+
 } // namespace
 } // namespace analysis
 } // namespace clang