]> granicus.if.org Git - clang/commitdiff
- Move static function IsNonPragmaNonMacroLexer into Preprocessor.h.
authorTed Kremenek <kremenek@apple.com>
Wed, 19 Nov 2008 21:57:25 +0000 (21:57 +0000)
committerTed Kremenek <kremenek@apple.com>
Wed, 19 Nov 2008 21:57:25 +0000 (21:57 +0000)
- Add variants of IsNonPragmaNonMacroLexer to accept an IncludeMacroStack entry
  (simplifies some uses).
- Use IsNonPragmaNonMacroLexer in Preprocessor::LookupFile.
- Add 'FileID' to PreprocessorLexer, and have Preprocessor query this fileid
  when looking up the FileEntry for a file

Performance testing of -Eonly on Cocoa.h shows no performance regression because
of this patch.

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

include/clang/Lex/Preprocessor.h
include/clang/Lex/PreprocessorLexer.h
lib/Lex/Lexer.cpp
lib/Lex/PPDirectives.cpp
lib/Lex/PPLexerChange.cpp
lib/Lex/PTHLexer.cpp
lib/Lex/PreprocessorLexer.cpp

index 4d31be4ecf4edead3d4f903025231ba34aa674fb..d803cb6a97ae0f7bd0e7554a2a0b6a8a75067f84 100644 (file)
@@ -608,7 +608,25 @@ private:
   const FileEntry *LookupFile(const char *FilenameStart,const char *FilenameEnd,
                               bool isAngled, const DirectoryLookup *FromDir,
                               const DirectoryLookup *&CurDir);
-    
+
+  
+
+  static bool IsNonPragmaNonMacroLexer(const Lexer* L,
+                                       const PreprocessorLexer* P) {
+    if (L)
+      return !L->isPragmaLexer();
+    else
+      return P != 0;
+  }
+
+  static bool IsNonPragmaNonMacroLexer(const IncludeStackInfo& I) {
+    return IsNonPragmaNonMacroLexer(I.TheLexer, I.ThePPLexer);
+  }
+
+  bool IsNonPragmaNonMacroLexer() const {
+    return IsNonPragmaNonMacroLexer(CurLexer.get(), CurPPLexer);
+  }
+  
   //===--------------------------------------------------------------------===//
   // Caching stuff.
   void CachingLex(Token &Result);
index de9c6b780a0c1fd5631626b9e93f892c16e03fdc..df8049e214eec32fb3472409f00b8ad8619a3b3c 100644 (file)
@@ -26,6 +26,9 @@ class Preprocessor;
 class PreprocessorLexer {
 protected:
   Preprocessor *PP;              // Preprocessor object controlling lexing.
+
+  /// The SourceManager fileID corresponding to the file being lexed.
+  const unsigned FileID;
   
   //===--------------------------------------------------------------------===//
   // Context-specific lexing flags set by the preprocessor.
@@ -64,7 +67,9 @@ protected:
   void operator=(const PreprocessorLexer&); // DO NOT IMPLEMENT
   friend class Preprocessor;
   
-  PreprocessorLexer(Preprocessor* pp) : PP(pp) {}
+  PreprocessorLexer(Preprocessor* pp, SourceLocation L);
+  PreprocessorLexer() : PP(0), FileID(0) {}
+  
   virtual ~PreprocessorLexer();
   
   virtual void IndirectLex(Token& Result) = 0;
@@ -119,6 +124,13 @@ protected:
   /// (potentially) macro expand the filename.  If the sequence parsed is not
   /// lexically legal, emit a diagnostic and return a result EOM token.
   void LexIncludeFilename(Token &Result);
+  
+public:
+  unsigned getFileID() const { 
+    assert(PP &&
+      "PreprocessorLexer::getFileID() should only be used with a Preprocessor");
+    return FileID;
+  }
 };
 
 }  // end namespace clang
index 8ddd62fe998314ac39779ca63d63ced6d7d6151a..eebdd1e2eb3cec2ee05c2430d4891e040873a2e3 100644 (file)
@@ -63,7 +63,8 @@ tok::ObjCKeywordKind Token::getObjCKeywordID() const {
 /// outlive it, so it doesn't take ownership of either of them.
 Lexer::Lexer(SourceLocation fileloc, Preprocessor &pp,
              const char *BufStart, const char *BufEnd)
-  : PreprocessorLexer(&pp), FileLoc(fileloc), Features(pp.getLangOptions()) {
+  : PreprocessorLexer(&pp, fileloc), FileLoc(fileloc),
+    Features(pp.getLangOptions()) {
       
   SourceManager &SourceMgr = PP->getSourceManager();
   unsigned InputFileID = SourceMgr.getPhysicalLoc(FileLoc).getFileID();
@@ -110,7 +111,9 @@ Lexer::Lexer(SourceLocation fileloc, Preprocessor &pp,
 Lexer::Lexer(SourceLocation fileloc, const LangOptions &features,
              const char *BufStart, const char *BufEnd,
              const llvm::MemoryBuffer *FromFile)
-  : PreprocessorLexer(0), FileLoc(fileloc), Features(features) {
+  : PreprocessorLexer(), FileLoc(fileloc),
+    Features(features) {
+      
   Is_PragmaLexer = false;
   InitCharacterInfo();
   
index ced7673ca23bfad329a54c001b80bc8641636a80..a1216b15bccd5b918dd254b13b3a2dbefd9b5dd7 100644 (file)
@@ -303,8 +303,8 @@ const FileEntry *Preprocessor::LookupFile(const char *FilenameStart,
   // info about where the current file is.
   const FileEntry *CurFileEnt = 0;
   if (!FromDir) {
-    SourceLocation FileLoc = getCurrentFileLexer()->getFileLoc();
-    CurFileEnt = SourceMgr.getFileEntryForLoc(FileLoc);
+    unsigned FileID = getCurrentFileLexer()->getFileID();
+    CurFileEnt = SourceMgr.getFileEntryForID(FileID);
   }
   
   // Do a standard file entry lookup.
@@ -317,8 +317,8 @@ const FileEntry *Preprocessor::LookupFile(const char *FilenameStart,
   // Otherwise, see if this is a subframework header.  If so, this is relative
   // to one of the headers on the #include stack.  Walk the list of the current
   // headers on the #include stack and pass them to HeaderInfo.
-  if (CurLexer && !CurLexer->Is_PragmaLexer) {
-    if ((CurFileEnt = SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc())))
+  if (IsNonPragmaNonMacroLexer()) {
+    if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID())))
       if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd,
                                                     CurFileEnt)))
         return FE;
@@ -326,9 +326,9 @@ const FileEntry *Preprocessor::LookupFile(const char *FilenameStart,
   
   for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
     IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
-    if (ISEntry.TheLexer && !ISEntry.TheLexer->Is_PragmaLexer) {
+    if (IsNonPragmaNonMacroLexer(ISEntry)) {
       if ((CurFileEnt = 
-           SourceMgr.getFileEntryForLoc(ISEntry.TheLexer->getFileLoc())))
+           SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID())))
         if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart,
                                                       FilenameEnd, CurFileEnt)))
           return FE;
index 87330983d658ff5b0bf9d1084bac9a8db825a75e..3a2ed658fb3df1e7d974088cb8b0eb5ad126d61a 100644 (file)
@@ -25,27 +25,17 @@ PPCallbacks::~PPCallbacks() {}
 // Miscellaneous Methods.
 //===----------------------------------------------------------------------===//
 
-static inline bool IsNonPragmaNonMacroLexer(const Lexer* L,
-                                            const PreprocessorLexer* P) {
-  if (L)
-    return !L->isPragmaLexer();
-  else
-    return P != 0;
-}
-
 /// isInPrimaryFile - Return true if we're in the top-level file, not in a
 /// #include.  This looks through macro expansions and active _Pragma lexers.
 bool Preprocessor::isInPrimaryFile() const {
-  if (IsNonPragmaNonMacroLexer(CurLexer.get(), CurPPLexer))
+  if (IsNonPragmaNonMacroLexer())
     return IncludeMacroStack.empty();
   
   // If there are any stacked lexers, we're in a #include.
-  assert(IsNonPragmaNonMacroLexer(IncludeMacroStack[0].TheLexer,
-                                  IncludeMacroStack[0].ThePPLexer) &&
+  assert(IsNonPragmaNonMacroLexer(IncludeMacroStack[0]) &&
          "Top level include stack isn't our primary lexer?");
   for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i)
-    if (IsNonPragmaNonMacroLexer(IncludeMacroStack[i].TheLexer,
-                                 IncludeMacroStack[i].ThePPLexer))
+    if (IsNonPragmaNonMacroLexer(IncludeMacroStack[i]))
       return false;
   return true;
 }
@@ -91,7 +81,7 @@ void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
                                             const DirectoryLookup *CurDir) {
     
   // Add the current lexer to the include stack.
-  if (CurLexer || CurTokenLexer)
+  if (CurPPLexer || CurTokenLexer)
     PushIncludeMacroStack();
 
   CurLexer.reset(TheLexer);
@@ -212,6 +202,7 @@ bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
   
   // We're done with the #included file.
   CurLexer.reset();
+  CurPPLexer = 0;
 
   // This is the end of the top-level file.  If the diag::pp_macro_not_used
   // diagnostic is enabled, look for macros that have not been used.
index 8062102e6cc0f3b4cd989963673aabb7f5ba4aac..d22285cae5c6735c0c0ce1ef70db6841ddbc0da9 100644 (file)
@@ -18,8 +18,8 @@ using namespace clang;
 
 PTHLexer::PTHLexer(Preprocessor& pp, SourceLocation fileloc,
                    const Token *TokArray, unsigned NumToks)
-  : PreprocessorLexer(&pp), FileLoc(fileloc), Tokens(TokArray),
-    NumTokens(NumToks), CurToken(0) {
+  : PreprocessorLexer(&pp, fileloc), FileLoc(fileloc),
+    Tokens(TokArray), NumTokens(NumToks), CurToken(0) {
 
   assert (Tokens[NumTokens-1].is(tok::eof));
   --NumTokens;
index 20bce413b74db32b760667c4ee79c3819362e93c..2ce181ef83a2afb17c48dd9ede4f0af9f9daa3bb 100644 (file)
@@ -18,6 +18,9 @@
 
 using namespace clang;
 
+PreprocessorLexer::PreprocessorLexer(Preprocessor* pp, SourceLocation L)
+  :  PP(pp), FileID(pp->getSourceManager().getPhysicalLoc(L).getFileID()) {}
+
 PreprocessorLexer::~PreprocessorLexer() {}
 
 void PreprocessorLexer::Diag(SourceLocation Loc, unsigned DiagID,