]> granicus.if.org Git - clang/commitdiff
[CFG] Fix crash finding destructor of lifetime-extended temporary.
authorDevin Coughlin <dcoughlin@apple.com>
Tue, 2 Aug 2016 21:07:23 +0000 (21:07 +0000)
committerDevin Coughlin <dcoughlin@apple.com>
Tue, 2 Aug 2016 21:07:23 +0000 (21:07 +0000)
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
test/SemaCXX/warn-thread-safety-analysis.cpp

index d7a9bdb3d82cd47d970f1b530179596b214564a2..a67f0910e15a7581bef8c5f0cb48688094fce67e 100644 (file)
@@ -3902,7 +3902,17 @@ CFGImplicitDtor::getDestructorDecl(ASTContext &astContext) const {
     case CFGElement::AutomaticObjectDtor: {
       const VarDecl *var = castAs<CFGAutomaticObjDtor>().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();
       }
index b5d2f8e1de83403a267281dd19bb3aac3f990640..bbb4f9b48d365df24abb9e7abb881bbad76d78ec 100644 (file)
@@ -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 {