]> granicus.if.org Git - clang/commitdiff
Support tentative definitions in precompiled headers. This isn't likely
authorDouglas Gregor <dgregor@apple.com>
Wed, 22 Apr 2009 22:02:47 +0000 (22:02 +0000)
committerDouglas Gregor <dgregor@apple.com>
Wed, 22 Apr 2009 22:02:47 +0000 (22:02 +0000)
to happen (ever), but at least we'll do the right thing when it does.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@69829 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Frontend/PCHBitCodes.h
include/clang/Frontend/PCHReader.h
lib/Frontend/PCHReader.cpp
lib/Frontend/PCHWriter.cpp
test/PCH/tentative-defs.c [new file with mode: 0644]
test/PCH/tentative-defs.h [new file with mode: 0644]

index 41369b37d70cf701c32b4c31d607729dd8e4cabc..a2f4dae490a76c799ee6423cc4a5c8b5c60507e3 100644 (file)
@@ -134,7 +134,7 @@ namespace clang {
 
       /// \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 @@ namespace clang {
       /// 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.
index cade8af2259f420f41c053c7f563bf6eb30bee29..9bb5ca4f0f978697623f0f6ddbca7b9b8786d43d 100644 (file)
@@ -149,6 +149,10 @@ private:
   /// 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;
index 6de13045bb437ae4b9bf6984e6bbdc79b35395ae..28cca4d0ff221be0fe0eca097861837bfbfb72d1 100644 (file)
@@ -1713,6 +1713,13 @@ PCHReader::ReadPCHBlock(uint64_t &PreprocessorBlockOffset) {
       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 @@ void PCHReader::InitializeSema(Sema &S) {
     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) {
index b081a2813f9b1794877b3e13b7e1d9e740f4058c..097ced2363cd37b07a68093a3e5485f351abe35d 100644 (file)
@@ -2024,6 +2024,15 @@ void PCHWriter::WritePCH(Sema &SemaRef) {
       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 @@ void PCHWriter::WritePCH(Sema &SemaRef) {
   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();
diff --git a/test/PCH/tentative-defs.c b/test/PCH/tentative-defs.c
new file mode 100644 (file)
index 0000000..980cfab
--- /dev/null
@@ -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}}
diff --git a/test/PCH/tentative-defs.h b/test/PCH/tentative-defs.h
new file mode 100644 (file)
index 0000000..4675d9a
--- /dev/null
@@ -0,0 +1,9 @@
+// Header for PCH test tentative-defs.c
+int variable;
+
+
+
+
+
+
+int incomplete_array[];