[PATCH] D19721: Fix crash in BuildCXXDefaultInitExpr.

Raphael Isemann via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 29 05:22:44 PDT 2016


teemperor created this revision.
teemperor added reviewers: cfe-commits, rnk.

  Fix crash in BuildCXXDefaultInitExpr when member of template class
  has same name as the class itself.

http://reviews.llvm.org/D19721

Files:
  lib/Sema/SemaDeclCXX.cpp
  test/SemaCXX/pr27047-default-init-expr-name-conflict.cpp

Index: test/SemaCXX/pr27047-default-init-expr-name-conflict.cpp
===================================================================
--- /dev/null
+++ test/SemaCXX/pr27047-default-init-expr-name-conflict.cpp
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -fsyntax-only -std=c++11 %s
+
+struct A {};
+
+template <typename T>
+struct B {
+
+  // Don't crash here
+  A B{};
+};
+
+int main() {
+  B<int> b;
+}
Index: lib/Sema/SemaDeclCXX.cpp
===================================================================
--- lib/Sema/SemaDeclCXX.cpp
+++ lib/Sema/SemaDeclCXX.cpp
@@ -11412,8 +11412,31 @@
     CXXRecordDecl *ClassPattern = ParentRD->getTemplateInstantiationPattern();
     DeclContext::lookup_result Lookup =
         ClassPattern->lookup(Field->getDeclName());
-    assert(Lookup.size() == 1);
-    FieldDecl *Pattern = cast<FieldDecl>(Lookup[0]);
+
+    FieldDecl *Pattern = nullptr;
+
+    // It's possible that both the current CXXRecordDecl and
+    // the wanted FieldDecl appear in Lookup, so we iterate
+    // over it and check that there is exactly one FieldDecl
+    // and an optional unused CXXRecordDecl.
+    for (NamedDecl *PossibleDecl : Lookup) {
+      FieldDecl *PossibleFieldDecl = dyn_cast<FieldDecl>(PossibleDecl);
+
+      if (PossibleFieldDecl) {
+        // Will fail if there is more than one FieldDecl in Lookup.
+        assert(Pattern == nullptr);
+
+        Pattern = PossibleFieldDecl;
+      } else {
+        // Assert that the other declaration is the unused
+        // parent CXXRecordDecl.
+        assert(isa<CXXRecordDecl>(PossibleDecl));
+      }
+    }
+
+    // Will fail if there is no FieldDecl in Lookup.
+    assert(Pattern != nullptr);
+
     if (InstantiateInClassInitializer(Loc, Field, Pattern,
                                       getTemplateInstantiationArgs(Field)))
       return ExprError();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D19721.55580.patch
Type: text/x-patch
Size: 1841 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160429/8d2c70aa/attachment.bin>


More information about the cfe-commits mailing list