r269551 - Handle injected class names in the ASTImporter.

Sean Callanan via cfe-commits cfe-commits at lists.llvm.org
Fri May 13 22:43:58 PDT 2016


Author: spyffe
Date: Sat May 14 00:43:57 2016
New Revision: 269551

URL: http://llvm.org/viewvc/llvm-project?rev=269551&view=rev
Log:
Handle injected class names in the ASTImporter.

Every class as parsed by Clang has a forward declaration of itself as a member:

class A {
  class A;
  ...
}

but when the parser generates this it ensures that the RecordTypes for the two 
are the same.  This makes (among other things) inheritance work.  This patch
fixes a bug where the ASTImporter generated two separate RecordTypes when
importing the class and the contained forward declaration, and adds a test case.

Thanks to Doug Gregor for advice on this.

Added:
    cfe/trunk/test/ASTMerge/Inputs/inheritance-base.cpp
    cfe/trunk/test/ASTMerge/inheritance.cpp
Modified:
    cfe/trunk/lib/AST/ASTImporter.cpp

Modified: cfe/trunk/lib/AST/ASTImporter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTImporter.cpp?rev=269551&r1=269550&r2=269551&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTImporter.cpp (original)
+++ cfe/trunk/lib/AST/ASTImporter.cpp Sat May 14 00:43:57 2016
@@ -2820,8 +2820,17 @@ Decl *ASTNodeImporter::VisitRecordDecl(R
         Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
         if (DCXX->getLambdaContextDecl() && !CDecl)
           return nullptr;
-        D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(),
-                                 CDecl);
+        D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
+      } else if (DCXX->isInjectedClassName()) {                                                 
+        // We have to be careful to do a similar dance to the one in                            
+        // Sema::ActOnStartCXXMemberDeclarations                                                
+        CXXRecordDecl *const PrevDecl = nullptr;                                                
+        const bool DelayTypeCreation = true;                                                    
+        D2CXX = CXXRecordDecl::Create(                                                          
+            Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc,                        
+            Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation);                           
+        Importer.getToContext().getTypeDeclType(                                                
+            D2CXX, llvm::dyn_cast<CXXRecordDecl>(DC));                                          
       } else {
         D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
                                       D->getTagKind(),

Added: cfe/trunk/test/ASTMerge/Inputs/inheritance-base.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ASTMerge/Inputs/inheritance-base.cpp?rev=269551&view=auto
==============================================================================
--- cfe/trunk/test/ASTMerge/Inputs/inheritance-base.cpp (added)
+++ cfe/trunk/test/ASTMerge/Inputs/inheritance-base.cpp Sat May 14 00:43:57 2016
@@ -0,0 +1,7 @@
+class A
+{
+public:
+  int x;
+  A(int _x) : x(_x) {
+  }
+};

Added: cfe/trunk/test/ASTMerge/inheritance.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ASTMerge/inheritance.cpp?rev=269551&view=auto
==============================================================================
--- cfe/trunk/test/ASTMerge/inheritance.cpp (added)
+++ cfe/trunk/test/ASTMerge/inheritance.cpp Sat May 14 00:43:57 2016
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++1z -emit-pch -o %t.1.ast %S/Inputs/inheritance-base.cpp
+// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++1z -ast-merge %t.1.ast -fsyntax-only -verify %s
+// expected-no-diagnostics
+
+class B : public A {
+  B(int _a) : A(_a) {
+  }
+};




More information about the cfe-commits mailing list