[cfe-commits] r69005 - in /cfe/trunk: include/clang/AST/ExternalASTSource.h include/clang/Frontend/PCHBitCodes.h include/clang/Frontend/PCHReader.h include/clang/Frontend/PCHWriter.h lib/Frontend/PCHReader.cpp lib/Frontend/PCHWriter.cpp lib/Sema/ParseAST.cpp test/PCH/external-defs.c test/PCH/external-defs.h

Douglas Gregor dgregor at apple.com
Mon Apr 13 17:24:21 PDT 2009


Author: dgregor
Date: Mon Apr 13 19:24:19 2009
New Revision: 69005

URL: http://llvm.org/viewvc/llvm-project?rev=69005&view=rev
Log:
When writing a PCH file, keep track of all of the non-static,
non-inline external definitions (and tentative definitions) that are
found at the top level. The corresponding declarations are stored in a
record in the PCH file, so that they can be provided to the
ASTConsumer (via HandleTopLevelDecl) when the PCH file is read.

Added:
    cfe/trunk/test/PCH/external-defs.c
    cfe/trunk/test/PCH/external-defs.h
Modified:
    cfe/trunk/include/clang/AST/ExternalASTSource.h
    cfe/trunk/include/clang/Frontend/PCHBitCodes.h
    cfe/trunk/include/clang/Frontend/PCHReader.h
    cfe/trunk/include/clang/Frontend/PCHWriter.h
    cfe/trunk/lib/Frontend/PCHReader.cpp
    cfe/trunk/lib/Frontend/PCHWriter.cpp
    cfe/trunk/lib/Sema/ParseAST.cpp

Modified: cfe/trunk/include/clang/AST/ExternalASTSource.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ExternalASTSource.h?rev=69005&r1=69004&r2=69005&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/ExternalASTSource.h (original)
+++ cfe/trunk/include/clang/AST/ExternalASTSource.h Mon Apr 13 19:24:19 2009
@@ -18,6 +18,7 @@
 #include "llvm/ADT/SmallVector.h"
 namespace clang {
 
+class ASTConsumer;
 class Decl;
 class DeclContext;
 
@@ -83,6 +84,10 @@
   virtual bool ReadDeclsVisibleInContext(DeclContext *DC,
                        llvm::SmallVectorImpl<VisibleDeclaration> & Decls) = 0;
 
+  /// \brief Function that will be invoked when we begin parsing a new
+  /// translation unit involving this external AST source.
+  virtual void StartTranslationUnit(ASTConsumer *Consumer);
+
   /// \brief Print any statistics that have been gathered regarding
   /// the external AST source.
   virtual void PrintStats();

Modified: cfe/trunk/include/clang/Frontend/PCHBitCodes.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHBitCodes.h?rev=69005&r1=69004&r2=69005&view=diff

==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHBitCodes.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHBitCodes.h Mon Apr 13 19:24:19 2009
@@ -130,7 +130,17 @@
       /// between offsets (for unresolved identifier IDs) and
       /// IdentifierInfo pointers (for already-resolved identifier
       /// IDs).
-      IDENTIFIER_TABLE = 6
+      IDENTIFIER_TABLE = 6,
+
+      /// \brief Record code for the array of external definitions.
+      ///
+      /// The PCH file contains a list of all of the external
+      /// definitions present within the parsed headers, stored as an
+      /// array of declaration IDs. These external definitions will be
+      /// reported to the AST consumer after the PCH file has been
+      /// read, since their presence can affect the semantics of the
+      /// program (e.g., for code generation).
+      EXTERNAL_DEFINITIONS = 7
     };
 
     /// \brief Record types used within a source manager block.
@@ -300,9 +310,6 @@
       TYPE_OBJC_QUALIFIED_CLASS     = 24
     };
 
-    /// \brief Record code for the offsets of each type.
-    ///
-
     /// \brief Record codes for each kind of declaration.
     ///
     /// These constants describe the records that can occur within a

Modified: cfe/trunk/include/clang/Frontend/PCHReader.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHReader.h?rev=69005&r1=69004&r2=69005&view=diff

==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHReader.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHReader.h Mon Apr 13 19:24:19 2009
@@ -120,6 +120,10 @@
   /// is set) or is an IdentifierInfo* that has already been resolved.
   llvm::SmallVector<uint64_t, 16> IdentifierData;
 
+  /// \brief The set of external definitions stored in the the PCH
+  /// file.
+  llvm::SmallVector<uint64_t, 16> ExternalDefinitions;
+
   PCHReadResult ReadPCHBlock();
   bool CheckPredefinesBuffer(const char *PCHPredef, 
                              unsigned PCHPredefLen,
@@ -188,6 +192,13 @@
   virtual bool ReadDeclsVisibleInContext(DeclContext *DC,
                        llvm::SmallVectorImpl<VisibleDeclaration> & Decls);
 
+  /// \brief Function that will be invoked when we begin parsing a new
+  /// translation unit involving this external AST source.
+  ///
+  /// This function will provide all of the external definitions to
+  /// the ASTConsumer.
+  virtual void StartTranslationUnit(ASTConsumer *Consumer);
+
   /// \brief Print some statistics about PCH usage.
   virtual void PrintStats();
 

Modified: cfe/trunk/include/clang/Frontend/PCHWriter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHWriter.h?rev=69005&r1=69004&r2=69005&view=diff

==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHWriter.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHWriter.h Mon Apr 13 19:24:19 2009
@@ -21,6 +21,7 @@
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SmallVector.h"
 #include <queue>
+#include <vector>
 
 namespace llvm {
   class APInt;
@@ -85,6 +86,20 @@
   /// IdentifierInfo.
   llvm::DenseMap<const IdentifierInfo *, pch::IdentID> IdentifierIDs;
 
+  /// \brief Declarations encountered that might be external
+  /// definitions.
+  ///
+  /// We keep track of external definitions (as well as tentative
+  /// definitions) as we are emitting declarations to the PCH
+  /// file. The PCH file contains a separate record for these external
+  /// definitions, which are provided to the AST consumer by the PCH
+  /// reader. This is behavior is required to properly cope with,
+  /// e.g., tentative variable definitions that occur within
+  /// headers. The declarations themselves are stored as declaration
+  /// IDs, since they will be written out to an EXTERNAL_DEFINITIONS
+  /// record.
+  llvm::SmallVector<uint64_t, 16> ExternalDefinitions;
+
   void WriteTargetTriple(const TargetInfo &Target);
   void WriteLanguageOptions(const LangOptions &LangOpts);
   void WriteSourceManagerBlock(SourceManager &SourceMgr);

Modified: cfe/trunk/lib/Frontend/PCHReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHReader.cpp?rev=69005&r1=69004&r2=69005&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PCHReader.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReader.cpp Mon Apr 13 19:24:19 2009
@@ -12,8 +12,10 @@
 //===----------------------------------------------------------------------===//
 #include "clang/Frontend/PCHReader.h"
 #include "clang/Frontend/FrontendDiagnostic.h"
+#include "clang/AST/ASTConsumer.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Decl.h"
+#include "clang/AST/DeclGroup.h"
 #include "clang/AST/Type.h"
 #include "clang/Lex/MacroInfo.h"
 #include "clang/Lex/Preprocessor.h"
@@ -663,6 +665,14 @@
       }
 #endif
       break;
+
+    case pch::EXTERNAL_DEFINITIONS:
+      if (!ExternalDefinitions.empty()) {
+        Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
+        return Failure;
+      }
+      ExternalDefinitions.swap(Record);
+      break;
     }
   }
 
@@ -1276,6 +1286,17 @@
   return false;
 }
 
+void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
+  if (!Consumer)
+    return;
+
+  for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
+    Decl *D = GetDecl(ExternalDefinitions[I]);
+    DeclGroupRef DG(D);
+    Consumer->HandleTopLevelDecl(DG);
+  }
+}
+
 void PCHReader::PrintStats() {
   std::fprintf(stderr, "*** PCH Statistics:\n");
 

Modified: cfe/trunk/lib/Frontend/PCHWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHWriter.cpp?rev=69005&r1=69004&r2=69005&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriter.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHWriter.cpp Mon Apr 13 19:24:19 2009
@@ -934,8 +934,29 @@
     W.Code = (pch::DeclCode)0;
     W.Visit(D);
     if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
-    assert(W.Code && "Visitor did not set record code");
+    assert(W.Code && "Unhandled declaration kind while generating PCH");
     S.EmitRecord(W.Code, Record);
+
+    // Note external declarations so that we can add them to a record
+    // in the PCH file later.
+    if (isa<FileScopeAsmDecl>(D))
+      ExternalDefinitions.push_back(ID);
+    else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
+      if (// Non-static file-scope variables with initializers or that
+          // are tentative definitions.
+          (Var->isFileVarDecl() &&
+           (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
+          // Out-of-line definitions of static data members (C++).
+          (Var->getDeclContext()->isRecord() && 
+           !Var->getLexicalDeclContext()->isRecord() && 
+           Var->getStorageClass() == VarDecl::Static))
+        ExternalDefinitions.push_back(ID);
+    } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
+      if (Func->isThisDeclarationADefinition() &&
+          Func->getStorageClass() != FunctionDecl::Static &&
+          !Func->isInline())
+        ExternalDefinitions.push_back(ID);
+    }
   }
 
   // Exit the declarations block
@@ -1013,9 +1034,11 @@
   WritePreprocessor(PP);
   WriteTypesBlock(Context);
   WriteDeclsBlock(Context);
+  WriteIdentifierTable();
   S.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
   S.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
-  WriteIdentifierTable();
+  if (!ExternalDefinitions.empty())
+    S.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
   S.ExitBlock();
 }
 

Modified: cfe/trunk/lib/Sema/ParseAST.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/ParseAST.cpp?rev=69005&r1=69004&r2=69005&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/ParseAST.cpp (original)
+++ cfe/trunk/lib/Sema/ParseAST.cpp Mon Apr 13 19:24:19 2009
@@ -14,6 +14,7 @@
 #include <llvm/ADT/OwningPtr.h>
 #include "clang/Sema/ParseAST.h"
 #include "clang/AST/ASTConsumer.h"
+#include "clang/AST/ExternalASTSource.h"
 #include "clang/AST/Stmt.h"
 #include "Sema.h"
 #include "clang/Parse/Parser.h"
@@ -44,6 +45,9 @@
   
   Consumer->Initialize(Ctx);
   
+  if (Ctx.getExternalSource())
+    Ctx.getExternalSource()->StartTranslationUnit(Consumer);
+
   Parser::DeclGroupPtrTy ADecl;
   
   while (!P.ParseTopLevelDecl(ADecl)) {  // Not end of file.

Added: cfe/trunk/test/PCH/external-defs.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/external-defs.c?rev=69005&view=auto

==============================================================================
--- cfe/trunk/test/PCH/external-defs.c (added)
+++ cfe/trunk/test/PCH/external-defs.c Mon Apr 13 19:24:19 2009
@@ -0,0 +1,8 @@
+// Test with pch.
+// RUN: clang-cc -triple x86_64-apple-darwin9 -emit-pch -o %t.pch %S/external-defs.h &&
+// RUN: clang-cc -triple x86_64-apple-darwin9 -include-pch %t.pch -emit-llvm -o %t %s &&
+
+// RUN: grep "@x = common global i32 0, align 4" %t | count 1 &&
+// FIXME below: should be i32 17, but we don't serialize y's value yet
+// RUN: grep "@y = common global i32 0, align 4"  %t | count 1 &&
+// RUN: grep "@z" %t | count 0

Added: cfe/trunk/test/PCH/external-defs.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/external-defs.h?rev=69005&view=auto

==============================================================================
--- cfe/trunk/test/PCH/external-defs.h (added)
+++ cfe/trunk/test/PCH/external-defs.h Mon Apr 13 19:24:19 2009
@@ -0,0 +1,10 @@
+// Helper for external-defs.c test
+
+// Tentative definition
+int x;
+
+// FIXME: check this, once we actually serialize it
+int y = 17;
+
+// Should not show up
+static int z;





More information about the cfe-commits mailing list