r268082 - Fix crash in BuildCXXDefaultInitExpr.

Reid Kleckner via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 29 11:06:54 PDT 2016


Author: rnk
Date: Fri Apr 29 13:06:53 2016
New Revision: 268082

URL: http://llvm.org/viewvc/llvm-project?rev=268082&view=rev
Log:
Fix crash in BuildCXXDefaultInitExpr.

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

Based on patch by Raphael "Teemperor" Isemann!

Differential Revision: http://reviews.llvm.org/D19721

Added:
    cfe/trunk/test/SemaCXX/pr27047-default-init-expr-name-conflict.cpp
Modified:
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp

Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=268082&r1=268081&r2=268082&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Fri Apr 29 13:06:53 2016
@@ -11412,8 +11412,19 @@ ExprResult Sema::BuildCXXDefaultInitExpr
     CXXRecordDecl *ClassPattern = ParentRD->getTemplateInstantiationPattern();
     DeclContext::lookup_result Lookup =
         ClassPattern->lookup(Field->getDeclName());
-    assert(Lookup.size() == 1);
-    FieldDecl *Pattern = cast<FieldDecl>(Lookup[0]);
+
+    // Lookup can return at most two results: the pattern for the field, or the
+    // injected class name of the parent record. No other member can have the
+    // same name as the field.
+    assert(!Lookup.empty() && Lookup.size() <= 2 &&
+           "more than two lookup results for field name");
+    FieldDecl *Pattern = dyn_cast<FieldDecl>(Lookup[0]);
+    if (!Pattern) {
+      assert(isa<CXXRecordDecl>(Lookup[0]) &&
+             "cannot have other non-field member with same name");
+      Pattern = cast<FieldDecl>(Lookup[1]);
+    }
+
     if (InstantiateInClassInitializer(Loc, Field, Pattern,
                                       getTemplateInstantiationArgs(Field)))
       return ExprError();

Added: cfe/trunk/test/SemaCXX/pr27047-default-init-expr-name-conflict.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/pr27047-default-init-expr-name-conflict.cpp?rev=268082&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/pr27047-default-init-expr-name-conflict.cpp (added)
+++ cfe/trunk/test/SemaCXX/pr27047-default-init-expr-name-conflict.cpp Fri Apr 29 13:06:53 2016
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -fsyntax-only -std=c++11 %s
+
+template <typename T>
+struct A {
+  // Used to crash when field was named after class.
+  int A = 0;
+};
+A<int> a;




More information about the cfe-commits mailing list