From dc65685eb3652226b2888f9fc8800b39664dba00 Mon Sep 17 00:00:00 2001 From: Martin Bohme Date: Tue, 7 Mar 2017 08:42:37 +0000 Subject: [PATCH] [analyzer] Fix crash when building CFG with variable of incomplete type 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 | 2 +- unittests/Analysis/CFGTest.cpp | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/Analysis/CFG.cpp b/lib/Analysis/CFG.cpp index d56e0e8fa1..2a2b3d73b5 100644 --- a/lib/Analysis/CFG.cpp +++ b/lib/Analysis/CFG.cpp @@ -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); diff --git a/unittests/Analysis/CFGTest.cpp b/unittests/Analysis/CFGTest.cpp index e691110050..768705f46f 100644 --- a/unittests/Analysis/CFGTest.cpp +++ b/unittests/Analysis/CFGTest.cpp @@ -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 void f() {\n" + " class Undefined;\n" + " Undefined u;\n" + "}\n"; + EXPECT_EQ(BuiltCFG, BuildCFG(Code)); +} + } // namespace } // namespace analysis } // namespace clang -- 2.50.1