[cfe-commits] r69829 - in /cfe/trunk: include/clang/Frontend/PCHBitCodes.h include/clang/Frontend/PCHReader.h lib/Frontend/PCHReader.cpp lib/Frontend/PCHWriter.cpp test/PCH/tentative-defs.c test/PCH/tentative-defs.h

Douglas Gregor dgregor at apple.com
Wed Apr 22 15:02:47 PDT 2009


Author: dgregor
Date: Wed Apr 22 17:02:47 2009
New Revision: 69829

URL: http://llvm.org/viewvc/llvm-project?rev=69829&view=rev
Log:
Support tentative definitions in precompiled headers. This isn't likely
to happen (ever), but at least we'll do the right thing when it does.

Added:
    cfe/trunk/test/PCH/tentative-defs.c
    cfe/trunk/test/PCH/tentative-defs.h
Modified:
    cfe/trunk/include/clang/Frontend/PCHBitCodes.h
    cfe/trunk/include/clang/Frontend/PCHReader.h
    cfe/trunk/lib/Frontend/PCHReader.cpp
    cfe/trunk/lib/Frontend/PCHWriter.cpp

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

==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHBitCodes.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHBitCodes.h Wed Apr 22 17:02:47 2009
@@ -134,7 +134,7 @@
 
       /// \brief Record code for the array of external definitions.
       ///
-      /// The PCH file contains a list of all of the external
+      /// The PCH file contains a list of all of the unnamed 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
@@ -151,9 +151,12 @@
       /// offsets into this record.
       SPECIAL_TYPES = 8,
 
-      /// \brief Record code for the block of extra statistics we
-      /// gather while generating a PCH file.
-      STATISTICS = 9
+      /// \brief Record code for the extra statistics we gather while
+      /// generating a PCH file.
+      STATISTICS = 9,
+
+      /// \brief Record code for the array of tentative definitions.
+      TENTATIVE_DEFINITIONS = 10
     };
 
     /// \brief Record types used within a source manager block.

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

==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHReader.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHReader.h Wed Apr 22 17:02:47 2009
@@ -149,6 +149,10 @@
   /// file.
   llvm::SmallVector<uint64_t, 16> ExternalDefinitions;
 
+  /// \brief The set of tentative definitions stored in the the PCH
+  /// file.
+  llvm::SmallVector<uint64_t, 16> TentativeDefinitions;
+
   /// \brief Mapping from switch-case IDs in the PCH file to
   /// switch-case statements.
   std::map<unsigned, SwitchCase *> SwitchCaseStmts;

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

==============================================================================
--- cfe/trunk/lib/Frontend/PCHReader.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReader.cpp Wed Apr 22 17:02:47 2009
@@ -1713,6 +1713,13 @@
       TotalNumMacros = Record[1];
       break;
 
+    case pch::TENTATIVE_DEFINITIONS:
+      if (!TentativeDefinitions.empty()) {
+        Error("Duplicate TENTATIVE_DEFINITIONS record in PCH file");
+        return Failure;
+      }
+      TentativeDefinitions.swap(Record);
+      break;
     }
   }
 
@@ -2523,6 +2530,13 @@
     SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
   }
   PreloadedDecls.clear();
+
+  // If there were any tentative definitions, deserialize them and add
+  // them to Sema's table of tentative definitions.
+  for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
+    VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
+    SemaObj->TentativeDefinitions[Var->getDeclName()] = Var;
+  }
 }
 
 IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {

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

==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriter.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHWriter.cpp Wed Apr 22 17:02:47 2009
@@ -2024,6 +2024,15 @@
       getIdentifierRef(&Table.get(BuiltinNames[I]));
   }
 
+  // Build a record containing all of the tentative definitions in
+  // this header file. Generally, this record will be empty.
+  RecordData TentativeDefinitions;
+  for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator 
+         TD = SemaRef.TentativeDefinitions.begin(),
+         TDEnd = SemaRef.TentativeDefinitions.end();
+       TD != TDEnd; ++TD)
+    AddDeclRef(TD->second, TentativeDefinitions);
+
   // Write the remaining PCH contents.
   RecordData Record;
   Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
@@ -2042,8 +2051,13 @@
   AddTypeRef(Context.getBuiltinVaListType(), Record);
   Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
 
+  // Write the record containing external, unnamed definitions.
   if (!ExternalDefinitions.empty())
     Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
+
+  // Write the record containing tentative definitions.
+  if (!TentativeDefinitions.empty())
+    Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
   
   // Some simple statistics
   Record.clear();

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

==============================================================================
--- cfe/trunk/test/PCH/tentative-defs.c (added)
+++ cfe/trunk/test/PCH/tentative-defs.c Wed Apr 22 17:02:47 2009
@@ -0,0 +1,9 @@
+// Test with pch.
+// RUN: clang-cc -triple x86_64-apple-darwin9 -emit-pch -o %t.pch %S/tentative-defs.h &&
+// RUN: clang-cc -triple x86_64-apple-darwin9 -include-pch %t.pch -verify -emit-llvm -o %t %s &&
+
+// RUN: grep "@variable = common global i32 0" %t | count 1 &&
+// RUN: grep "@incomplete_array = common global .*1 x i32" %t | count 1
+
+
+// FIXME: tentative-defs.h expected-warning{{tentative}}

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

==============================================================================
--- cfe/trunk/test/PCH/tentative-defs.h (added)
+++ cfe/trunk/test/PCH/tentative-defs.h Wed Apr 22 17:02:47 2009
@@ -0,0 +1,9 @@
+// Header for PCH test tentative-defs.c
+int variable;
+
+
+
+
+
+
+int incomplete_array[];





More information about the cfe-commits mailing list