[cfe-commits] r107598 - in /cfe/trunk: include/clang/AST/ASTContext.h include/clang/AST/Decl.h lib/AST/ASTContext.cpp lib/Frontend/PCHReader.cpp test/PCH/types.c test/PCH/types.h

Argyrios Kyrtzidis akyrtzi at gmail.com
Sun Jul 4 14:44:48 PDT 2010


Author: akirtzidis
Date: Sun Jul  4 16:44:47 2010
New Revision: 107598

URL: http://llvm.org/viewvc/llvm-project?rev=107598&view=rev
Log:
Fix a regression of a previous commit of mine (rdar://8158953).

Some of the invariant checks for creating Record/Enum types don't hold true during PCH reading.
Introduce more suitable ASTContext::getRecordType() and getEnumType().

Modified:
    cfe/trunk/include/clang/AST/ASTContext.h
    cfe/trunk/include/clang/AST/Decl.h
    cfe/trunk/lib/AST/ASTContext.cpp
    cfe/trunk/lib/Frontend/PCHReader.cpp
    cfe/trunk/test/PCH/types.c
    cfe/trunk/test/PCH/types.h

Modified: cfe/trunk/include/clang/AST/ASTContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTContext.h?rev=107598&r1=107597&r2=107598&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Sun Jul  4 16:44:47 2010
@@ -622,6 +622,10 @@
   /// specified typename decl.
   QualType getTypedefType(const TypedefDecl *Decl, QualType Canon = QualType());
 
+  QualType getRecordType(const RecordDecl *Decl);
+
+  QualType getEnumType(const EnumDecl *Decl);
+
   QualType getInjectedClassNameType(CXXRecordDecl *Decl, QualType TST);
 
   QualType getSubstTemplateTypeParmType(const TemplateTypeParmType *Replaced,

Modified: cfe/trunk/include/clang/AST/Decl.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Decl.h?rev=107598&r1=107597&r2=107598&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Sun Jul  4 16:44:47 2010
@@ -1993,6 +1993,13 @@
     return cast<EnumDecl>(TagDecl::getCanonicalDecl());
   }
 
+  const EnumDecl *getPreviousDeclaration() const {
+    return cast_or_null<EnumDecl>(TagDecl::getPreviousDeclaration());
+  }
+  EnumDecl *getPreviousDeclaration() {
+    return cast_or_null<EnumDecl>(TagDecl::getPreviousDeclaration());
+  }
+
   static EnumDecl *Create(ASTContext &C, DeclContext *DC,
                           SourceLocation L, IdentifierInfo *Id,
                           SourceLocation TKL, EnumDecl *PrevDecl);
@@ -2116,6 +2123,13 @@
                             RecordDecl* PrevDecl = 0);
   static RecordDecl *Create(ASTContext &C, EmptyShell Empty);
 
+  const RecordDecl *getPreviousDeclaration() const {
+    return cast_or_null<RecordDecl>(TagDecl::getPreviousDeclaration());
+  }
+  RecordDecl *getPreviousDeclaration() {
+    return cast_or_null<RecordDecl>(TagDecl::getPreviousDeclaration());
+  }
+
   virtual void Destroy(ASTContext& C);
 
   bool hasFlexibleArrayMember() const { return HasFlexibleArrayMember; }

Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=107598&r1=107597&r2=107598&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Sun Jul  4 16:44:47 2010
@@ -1802,11 +1802,11 @@
     assert(!Record->getPreviousDeclaration() &&
            "struct/union has previous declaration");
     assert(!NeedsInjectedClassNameType(Record));
-    Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
+    return getRecordType(Record);
   } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
     assert(!Enum->getPreviousDeclaration() &&
            "enum has previous declaration");
-    Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
+    return getEnumType(Enum);
   } else if (const UnresolvedUsingTypenameDecl *Using =
                dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
     Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
@@ -1831,6 +1831,30 @@
   return QualType(Decl->TypeForDecl, 0);
 }
 
+QualType ASTContext::getRecordType(const RecordDecl *Decl) {
+  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
+
+  if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration())
+    if (PrevDecl->TypeForDecl)
+      return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); 
+
+  Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Decl);
+  Types.push_back(Decl->TypeForDecl);
+  return QualType(Decl->TypeForDecl, 0);
+}
+
+QualType ASTContext::getEnumType(const EnumDecl *Decl) {
+  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
+
+  if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration())
+    if (PrevDecl->TypeForDecl)
+      return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); 
+
+  Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Decl);
+  Types.push_back(Decl->TypeForDecl);
+  return QualType(Decl->TypeForDecl, 0);
+}
+
 /// \brief Retrieve a substitution-result type.
 QualType
 ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,

Modified: cfe/trunk/lib/Frontend/PCHReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHReader.cpp?rev=107598&r1=107597&r2=107598&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PCHReader.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReader.cpp Sun Jul  4 16:44:47 2010
@@ -2149,14 +2149,14 @@
       Error("incorrect encoding of record type");
       return QualType();
     }
-    return Context->getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
+    return Context->getRecordType(cast<RecordDecl>(GetDecl(Record[0])));
 
   case pch::TYPE_ENUM:
     if (Record.size() != 1) {
       Error("incorrect encoding of enum type");
       return QualType();
     }
-    return Context->getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
+    return Context->getEnumType(cast<EnumDecl>(GetDecl(Record[0])));
 
   case pch::TYPE_ELABORATED: {
     unsigned Idx = 0;

Modified: cfe/trunk/test/PCH/types.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/types.c?rev=107598&r1=107597&r2=107598&view=diff
==============================================================================
--- cfe/trunk/test/PCH/types.c (original)
+++ cfe/trunk/test/PCH/types.c Sun Jul  4 16:44:47 2010
@@ -3,7 +3,7 @@
 
 // Test with pch.
 // RUN: %clang_cc1 -emit-pch -fblocks -o %t %S/types.h
-// RUN: %clang_cc1 -fblocks -include-pch %t -fsyntax-only -verify %s 
+// RUN: %clang_cc1 -fblocks -include-pch %t -fsyntax-only -verify %s -ast-print
 
 typedef int INT;
 INT int_value;

Modified: cfe/trunk/test/PCH/types.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/types.h?rev=107598&r1=107597&r2=107598&view=diff
==============================================================================
--- cfe/trunk/test/PCH/types.h (original)
+++ cfe/trunk/test/PCH/types.h Sun Jul  4 16:44:47 2010
@@ -42,3 +42,8 @@
 
 // TYPE_TYPEOF
 typedef typeof(int_ptr *) int_ptr_ptr2;
+
+struct S2;
+struct S2 {};
+enum E;
+enum E { myenum };





More information about the cfe-commits mailing list