From 5bd4129674050a155bcb76cc22289bbffaf2f930 Mon Sep 17 00:00:00 2001 From: Devin Coughlin Date: Tue, 2 Aug 2016 21:07:23 +0000 Subject: [PATCH] [CFG] Fix crash finding destructor of lifetime-extended temporary. Fix a crash under -Wthread-safety when finding the destructor for a lifetime-extending reference. A patch by Nandor Licker! Differential Revision: https://reviews.llvm.org/D22419 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@277522 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/CFG.cpp | 12 +++++++++++- test/SemaCXX/warn-thread-safety-analysis.cpp | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/Analysis/CFG.cpp b/lib/Analysis/CFG.cpp index d7a9bdb3d8..a67f0910e1 100644 --- a/lib/Analysis/CFG.cpp +++ b/lib/Analysis/CFG.cpp @@ -3902,7 +3902,17 @@ CFGImplicitDtor::getDestructorDecl(ASTContext &astContext) const { case CFGElement::AutomaticObjectDtor: { const VarDecl *var = castAs().getVarDecl(); QualType ty = var->getType(); - ty = ty.getNonReferenceType(); + + // FIXME: See CFGBuilder::addLocalScopeForVarDecl. + // + // Lifetime-extending constructs are handled here. This works for a single + // temporary in an initializer expression. + if (ty->isReferenceType()) { + if (const Expr *Init = var->getInit()) { + ty = getReferenceInitTemporaryType(astContext, Init); + } + } + while (const ArrayType *arrayType = astContext.getAsArrayType(ty)) { ty = arrayType->getElementType(); } diff --git a/test/SemaCXX/warn-thread-safety-analysis.cpp b/test/SemaCXX/warn-thread-safety-analysis.cpp index b5d2f8e1de..bbb4f9b48d 100644 --- a/test/SemaCXX/warn-thread-safety-analysis.cpp +++ b/test/SemaCXX/warn-thread-safety-analysis.cpp @@ -5160,6 +5160,21 @@ void test3() { } // end namespace GlobalAcquiredBeforeAfterTest +namespace LifetimeExtensionText { + +struct Holder { + virtual ~Holder() throw() {} + int i = 0; +}; + +void test() { + // Should not crash. + const auto &value = Holder().i; +} + +} // end namespace LifetimeExtensionTest + + namespace LockableUnions { union LOCKABLE MutexUnion { -- 2.40.0