[cfe-commits] r69987 - in /cfe/trunk: include/clang/Frontend/PCHBitCodes.h include/clang/Lex/HeaderSearch.h lib/Frontend/PCHReader.cpp lib/Frontend/PCHWriter.cpp lib/Lex/HeaderSearch.cpp lib/Lex/PPLexerChange.cpp test/PCH/objc_import.h test/PCH/objc_import.m

Steve Naroff snaroff at apple.com
Fri Apr 24 13:03:21 PDT 2009


Author: snaroff
Date: Fri Apr 24 15:03:17 2009
New Revision: 69987

URL: http://llvm.org/viewvc/llvm-project?rev=69987&view=rev
Log:
Add PCH support for #import.

Added:
    cfe/trunk/test/PCH/objc_import.h
    cfe/trunk/test/PCH/objc_import.m
Modified:
    cfe/trunk/include/clang/Frontend/PCHBitCodes.h
    cfe/trunk/include/clang/Lex/HeaderSearch.h
    cfe/trunk/lib/Frontend/PCHReader.cpp
    cfe/trunk/lib/Frontend/PCHWriter.cpp
    cfe/trunk/lib/Lex/HeaderSearch.cpp
    cfe/trunk/lib/Lex/PPLexerChange.cpp

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

==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHBitCodes.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHBitCodes.h Fri Apr 24 15:03:17 2009
@@ -211,7 +211,11 @@
       
       /// \brief The value of the next __COUNTER__ to dispense.
       /// [PP_COUNTER_VALUE, Val]
-      PP_COUNTER_VALUE = 4
+      PP_COUNTER_VALUE = 4,
+      
+      /// \brief Describes one header file info [isImport, DirInfo, NumIncludes]
+      /// ControlloingMacro is optional.
+      PP_HEADER_FILE_INFO = 5
     };
 
     /// \defgroup PCHAST Precompiled header AST constants

Modified: cfe/trunk/include/clang/Lex/HeaderSearch.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/HeaderSearch.h?rev=69987&r1=69986&r2=69987&view=diff

==============================================================================
--- cfe/trunk/include/clang/Lex/HeaderSearch.h (original)
+++ cfe/trunk/include/clang/Lex/HeaderSearch.h Fri Apr 24 15:03:17 2009
@@ -23,7 +23,31 @@
 class FileManager;
 class IdentifierInfo;
 
+/// HeaderFileInfo - The preprocessor keeps track of this information for each
+/// file that is #included.
+struct HeaderFileInfo {
+  /// isImport - True if this is a #import'd or #pragma once file.
+  bool isImport : 1;
+  
+  /// DirInfo - Keep track of whether this is a system header, and if so,
+  /// whether it is C++ clean or not.  This can be set by the include paths or
+  /// by #pragma gcc system_header.  This is an instance of
+  /// SrcMgr::CharacteristicKind.
+  unsigned DirInfo : 2;
+  
+  /// NumIncludes - This is the number of times the file has been included
+  /// already.
+  unsigned short NumIncludes;
+  
+  /// ControllingMacro - If this file has a #ifndef XXX (or equivalent) guard
+  /// that protects the entire contents of the file, this is the identifier
+  /// for the macro that controls whether or not it has any effect.
+  const IdentifierInfo *ControllingMacro;
   
+  HeaderFileInfo() : isImport(false), DirInfo(SrcMgr::C_User),
+    NumIncludes(0), ControllingMacro(0) {}
+};
+
 /// HeaderSearch - This class encapsulates the information needed to find the
 /// file referenced by a #include or #include_next, (sub-)framework lookup, etc.
 class HeaderSearch {
@@ -39,35 +63,10 @@
   unsigned SystemDirIdx;
   bool NoCurDirSearch;
   
-  /// PreFileInfo - The preprocessor keeps track of this information for each
-  /// file that is #included.
-  struct PerFileInfo {
-    /// isImport - True if this is a #import'd or #pragma once file.
-    bool isImport : 1;
-    
-    /// DirInfo - Keep track of whether this is a system header, and if so,
-    /// whether it is C++ clean or not.  This can be set by the include paths or
-    /// by #pragma gcc system_header.  This is an instance of
-    /// SrcMgr::CharacteristicKind.
-    unsigned DirInfo : 2;
-    
-    /// NumIncludes - This is the number of times the file has been included
-    /// already.
-    unsigned short NumIncludes;
-    
-    /// ControllingMacro - If this file has a #ifndef XXX (or equivalent) guard
-    /// that protects the entire contents of the file, this is the identifier
-    /// for the macro that controls whether or not it has any effect.
-    const IdentifierInfo *ControllingMacro;
-    
-    PerFileInfo() : isImport(false), DirInfo(SrcMgr::C_User),
-      NumIncludes(0), ControllingMacro(0) {}
-  };
-  
   /// FileInfo - This contains all of the preprocessor-specific data about files
   /// that are included.  The vector is indexed by the FileEntry's UID.
   ///
-  std::vector<PerFileInfo> FileInfo;
+  std::vector<HeaderFileInfo> FileInfo;
 
   /// LookupFileCache - This is keeps track of each lookup performed by
   /// LookupFile.  The first part of the value is the starting index in
@@ -190,13 +189,20 @@
   const HeaderMap *CreateHeaderMap(const FileEntry *FE);
   
   void IncrementFrameworkLookupCount() { ++NumFrameworkLookups; }
+
+  typedef std::vector<HeaderFileInfo>::iterator header_file_iterator;
+  header_file_iterator header_file_begin() { return FileInfo.begin(); }
+  header_file_iterator header_file_end() { return FileInfo.end(); }
+
+  // Used by PCHReader.
+  void setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID);
   
   void PrintStats();
 private:
       
-  /// getFileInfo - Return the PerFileInfo structure for the specified
+  /// getFileInfo - Return the HeaderFileInfo structure for the specified
   /// FileEntry.
-  PerFileInfo &getFileInfo(const FileEntry *FE);
+  HeaderFileInfo &getFileInfo(const FileEntry *FE);
 };
 
 }  // end namespace clang

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

==============================================================================
--- cfe/trunk/lib/Frontend/PCHReader.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReader.cpp Fri Apr 24 15:03:17 2009
@@ -23,6 +23,7 @@
 #include "clang/AST/Type.h"
 #include "clang/Lex/MacroInfo.h"
 #include "clang/Lex/Preprocessor.h"
+#include "clang/Lex/HeaderSearch.h"
 #include "clang/Basic/OnDiskHashTable.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Basic/SourceManagerInternals.h"
@@ -1444,6 +1445,7 @@
   RecordData Record;
   llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
   MacroInfo *Macro = 0;
+  
   while (true) {
     unsigned Code = Stream.ReadCode();
     switch (Code) {
@@ -1536,7 +1538,9 @@
       Macro->AddTokenToBody(Tok);
       break;
     }
-    }
+    case pch::PP_HEADER_FILE_INFO:
+      break; // Already processed by ReadPreprocessorBlock().
+  }
   }
 }
 
@@ -1545,6 +1549,7 @@
     return Error("Malformed preprocessor block record");
   
   RecordData Record;
+  unsigned NumHeaderInfos = 0;
   while (true) {
     unsigned Code = Stream.ReadCode();
     switch (Code) {
@@ -1581,8 +1586,16 @@
     case pch::PP_MACRO_OBJECT_LIKE:
     case pch::PP_MACRO_FUNCTION_LIKE:
     case pch::PP_TOKEN:
-      // Once we've hit a macro definition or a token, we're done.
-      return false;
+      break;
+    case pch::PP_HEADER_FILE_INFO: {
+      HeaderFileInfo HFI;
+      HFI.isImport = Record[0];
+      HFI.DirInfo = Record[1];
+      HFI.NumIncludes = Record[2];
+      HFI.ControllingMacro = DecodeIdentifierInfo(Record[3]);
+      PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
+      break;
+    }
     }
   }
 }

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

==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriter.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHWriter.cpp Fri Apr 24 15:03:17 2009
@@ -23,6 +23,7 @@
 #include "clang/AST/Type.h"
 #include "clang/Lex/MacroInfo.h"
 #include "clang/Lex/Preprocessor.h"
+#include "clang/Lex/HeaderSearch.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/OnDiskHashTable.h"
 #include "clang/Basic/SourceManager.h"
@@ -1547,7 +1548,22 @@
     }
     ++NumMacros;
   }
-  
+
+  // Loop over all the header files.
+  HeaderSearch &HS = PP.getHeaderSearchInfo();  
+  for (HeaderSearch::header_file_iterator I = HS.header_file_begin(), 
+                                          E = HS.header_file_end();
+       I != E; ++I) {
+    Record.push_back((*I).isImport);
+    Record.push_back((*I).DirInfo);
+    Record.push_back((*I).NumIncludes);
+    if ((*I).ControllingMacro)
+      AddIdentifierRef((*I).ControllingMacro, Record);
+    else
+      Record.push_back(0);
+    Stream.EmitRecord(pch::PP_HEADER_FILE_INFO, Record);
+    Record.clear();
+  }
   Stream.ExitBlock();
 }
 

Modified: cfe/trunk/lib/Lex/HeaderSearch.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/HeaderSearch.cpp?rev=69987&r1=69986&r2=69987&view=diff

==============================================================================
--- cfe/trunk/lib/Lex/HeaderSearch.cpp (original)
+++ cfe/trunk/lib/Lex/HeaderSearch.cpp Fri Apr 24 15:03:17 2009
@@ -377,14 +377,20 @@
 //===----------------------------------------------------------------------===//
 
 
-/// getFileInfo - Return the PerFileInfo structure for the specified
+/// getFileInfo - Return the HeaderFileInfo structure for the specified
 /// FileEntry.
-HeaderSearch::PerFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
+HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
   if (FE->getUID() >= FileInfo.size())
     FileInfo.resize(FE->getUID()+1);
   return FileInfo[FE->getUID()];
 }  
 
+void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
+  if (UID >= FileInfo.size())
+    FileInfo.resize(UID+1);
+  FileInfo[UID] = HFI;
+}
+
 /// ShouldEnterIncludeFile - Mark the specified file as a target of of a
 /// #include, #include_next, or #import directive.  Return false if #including
 /// the file will have no effect or true if we should include it.
@@ -392,7 +398,7 @@
   ++NumIncluded; // Count # of attempted #includes.
 
   // Get information about this file.
-  PerFileInfo &FileInfo = getFileInfo(File);
+  HeaderFileInfo &FileInfo = getFileInfo(File);
   
   // If this is a #import directive, check that we have not already imported
   // this header.

Modified: cfe/trunk/lib/Lex/PPLexerChange.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPLexerChange.cpp?rev=69987&r1=69986&r2=69987&view=diff

==============================================================================
--- cfe/trunk/lib/Lex/PPLexerChange.cpp (original)
+++ cfe/trunk/lib/Lex/PPLexerChange.cpp Fri Apr 24 15:03:17 2009
@@ -179,7 +179,7 @@
   if (CurPPLexer) {  // Not ending a macro, ignore it.
     if (const IdentifierInfo *ControllingMacro = 
           CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
-      // Okay, this has a controlling macro, remember in PerFileInfo.
+      // Okay, this has a controlling macro, remember in HeaderFileInfo.
       if (const FileEntry *FE = 
             SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))
         HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);

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

==============================================================================
--- cfe/trunk/test/PCH/objc_import.h (added)
+++ cfe/trunk/test/PCH/objc_import.h Fri Apr 24 15:03:17 2009
@@ -0,0 +1,7 @@
+/* For use with the objc_import.m test */
+
+ at interface TestPCH
++ alloc;
+- (void)instMethod;
+ at end
+

Added: cfe/trunk/test/PCH/objc_import.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/objc_import.m?rev=69987&view=auto

==============================================================================
--- cfe/trunk/test/PCH/objc_import.m (added)
+++ cfe/trunk/test/PCH/objc_import.m Fri Apr 24 15:03:17 2009
@@ -0,0 +1,15 @@
+// Test this without pch.
+// RUN: clang-cc -include %S/objc_import.h -fsyntax-only -verify %s &&
+
+// Test with pch.
+// RUN: clang-cc -x=objective-c -emit-pch -o %t %S/objc_import.h &&
+// RUN: clang-cc -include-pch %t -fsyntax-only -verify %s 
+
+#import "objc_import.h"
+
+void func() {
+ TestPCH *xx;
+
+ xx = [TestPCH alloc];
+ [xx instMethod];
+}





More information about the cfe-commits mailing list