[cfe-commits] r70321 - in /cfe/trunk: include/clang/Basic/DiagnosticFrontendKinds.td include/clang/Frontend/PCHReader.h lib/Frontend/PCHReader.cpp lib/Frontend/PCHWriter.cpp test/PCH/fuzzy-pch.c

Douglas Gregor dgregor at apple.com
Tue Apr 28 13:33:11 PDT 2009


Author: dgregor
Date: Tue Apr 28 15:33:11 2009
New Revision: 70321

URL: http://llvm.org/viewvc/llvm-project?rev=70321&view=rev
Log:
Implement checking for macro definitions that occur on the command
line when using a PCH that were not provided when building the PCH
file. If those names were used as identifiers somewhere in the PCH
file, reject the PCH file.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticFrontendKinds.td
    cfe/trunk/include/clang/Frontend/PCHReader.h
    cfe/trunk/lib/Frontend/PCHReader.cpp
    cfe/trunk/lib/Frontend/PCHWriter.cpp
    cfe/trunk/test/PCH/fuzzy-pch.c

Modified: cfe/trunk/include/clang/Basic/DiagnosticFrontendKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticFrontendKinds.td?rev=70321&r1=70320&r2=70321&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticFrontendKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticFrontendKinds.td Tue Apr 28 15:33:11 2009
@@ -123,5 +123,7 @@
     "macro definitions used to build the precompiled header are missing">;
 def note_using_macro_def_from_pch : Note<
     "using this macro definition from precompiled header">;
-
+def warn_macro_name_used_in_pch : Warning<
+    "definition of macro %0 conflicts with an identifier used in the "
+    "precompiled header">;
 }

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

==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHReader.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHReader.h Tue Apr 28 15:33:11 2009
@@ -280,6 +280,16 @@
   /// Objective-C protocols.
   llvm::SmallVector<Decl *, 16> InterestingDecls;
 
+  /// \brief The file ID for the predefines buffer in the PCH file.
+  FileID PCHPredefinesBufferID;
+
+  /// \brief Pointer to the beginning of the predefines buffer in the
+  /// PCH file.
+  const char *PCHPredefines;
+
+  /// \brief Length of the predefines buffer in the PCH file.
+  unsigned PCHPredefinesLen;
+
   /// \brief Suggested contents of the predefines buffer, after this
   /// PCH file has been processed.
   ///

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

==============================================================================
--- cfe/trunk/lib/Frontend/PCHReader.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReader.cpp Tue Apr 28 15:33:11 2009
@@ -507,9 +507,17 @@
     std::string MacroName = Extra.substr(StartOfMacroName,
                                          EndOfMacroName - StartOfMacroName);
 
-    // FIXME: Perform this check!
-    fprintf(stderr, "FIXME: check whether '%s' was used in the PCH file\n",
-            MacroName.c_str());
+    // Check whether this name was used somewhere in the PCH file. If
+    // so, defining it as a macro could change behavior, so we reject
+    // the PCH file.
+    if (IdentifierInfo *II = get(MacroName.c_str(),
+                                 MacroName.c_str() + MacroName.size())) {
+      Diag(diag::warn_macro_name_used_in_pch)
+        << II;
+      Diag(diag::note_ignoring_pch)
+        << FileName;
+      return true;
+    }
 
     // Add this definition to the suggested predefines buffer.
     SuggestedPredefines += Extra;
@@ -818,9 +826,11 @@
                                          Name);
     FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
       
-    if (strcmp(Name, "<built-in>") == 0
-        && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
-      return IgnorePCH;
+    if (strcmp(Name, "<built-in>") == 0) {
+      PCHPredefinesBufferID = BufferID;
+      PCHPredefines = BlobStart;
+      PCHPredefinesLen = BlobLen - 1;
+    }
 
     break;
   }
@@ -1287,7 +1297,12 @@
   // Load the translation unit declaration
   if (Context)
     ReadDeclRecord(DeclOffsets[0], 0);
-
+  
+  // Check the predefines buffer.
+  if (CheckPredefinesBuffer(PCHPredefines, PCHPredefinesLen, 
+                            PCHPredefinesBufferID))
+    return IgnorePCH;
+  
   // Initialization of builtins and library builtins occurs before the
   // PCH file is read, so there may be some identifiers that were
   // loaded into the IdentifierTable before we intercepted the

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

==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriter.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHWriter.cpp Tue Apr 28 15:33:11 2009
@@ -1378,11 +1378,21 @@
 
   // Create and write out the blob that contains the identifier
   // strings.
-  IdentifierOffsets.resize(IdentifierIDs.size());
   {
     OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
     
+    // Look for any identifiers that were named while processing the
+    // headers, but are otherwise not needed. We add these to the hash
+    // table to enable checking of the predefines buffer in the case
+    // where the user adds new macro definitions when building the PCH
+    // file.
+    for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
+                                IDEnd = PP.getIdentifierTable().end();
+         ID != IDEnd; ++ID)
+      getIdentifierRef(ID->second);
+
     // Create the on-disk hash table representation.
+    IdentifierOffsets.resize(IdentifierIDs.size());
     for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
            ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
          ID != IDEnd; ++ID) {

Modified: cfe/trunk/test/PCH/fuzzy-pch.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/fuzzy-pch.c?rev=70321&r1=70320&r2=70321&view=diff

==============================================================================
--- cfe/trunk/test/PCH/fuzzy-pch.c (original)
+++ cfe/trunk/test/PCH/fuzzy-pch.c Tue Apr 28 15:33:11 2009
@@ -1,6 +1,8 @@
 // Test with pch.
 // RUN: clang-cc -emit-pch -DFOO -o %t %S/variables.h &&
-// RUN: clang-cc -DBAR=int -include-pch %t -fsyntax-only -pedantic %s 
+// RUN: clang-cc -DBAR=int -include-pch %t -fsyntax-only -pedantic %s &&
+// RUN: clang-cc -DFOO -DBAR=int -include-pch %t -Werror %s &&
+// RUN: not clang-cc -DFOO -DBAR=int -DX=5 -include-pch %t -Werror %s 
 
 BAR bar = 17;
 





More information about the cfe-commits mailing list