[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

llvm-svn: 277522

clang/lib/Analysis/CFG.cpp
clang/test/SemaCXX/warn-thread-safety-analysis.cpp

index d7a9bdb..a67f091 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 b5d2f8e..bbb4f9b 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 {