[cfe-commits] r59666 - in /cfe/trunk: 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

Ted Kremenek kremenek at apple.com
Wed Nov 19 13:57:27 PST 2008


Author: kremenek
Date: Wed Nov 19 15:57:25 2008
New Revision: 59666

URL: http://llvm.org/viewvc/llvm-project?rev=59666&view=rev
Log:
- Move static function IsNonPragmaNonMacroLexer into Preprocessor.h.
- 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.

Modified:
    cfe/trunk/include/clang/Lex/Preprocessor.h
    cfe/trunk/include/clang/Lex/PreprocessorLexer.h
    cfe/trunk/lib/Lex/Lexer.cpp
    cfe/trunk/lib/Lex/PPDirectives.cpp
    cfe/trunk/lib/Lex/PPLexerChange.cpp
    cfe/trunk/lib/Lex/PTHLexer.cpp
    cfe/trunk/lib/Lex/PreprocessorLexer.cpp

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

==============================================================================
--- cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/trunk/include/clang/Lex/Preprocessor.h Wed Nov 19 15:57:25 2008
@@ -608,7 +608,25 @@
   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);

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

==============================================================================
--- cfe/trunk/include/clang/Lex/PreprocessorLexer.h (original)
+++ cfe/trunk/include/clang/Lex/PreprocessorLexer.h Wed Nov 19 15:57:25 2008
@@ -26,6 +26,9 @@
 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 @@
   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 @@
   /// (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

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

==============================================================================
--- cfe/trunk/lib/Lex/Lexer.cpp (original)
+++ cfe/trunk/lib/Lex/Lexer.cpp Wed Nov 19 15:57:25 2008
@@ -63,7 +63,8 @@
 /// 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, 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();
   

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

==============================================================================
--- cfe/trunk/lib/Lex/PPDirectives.cpp (original)
+++ cfe/trunk/lib/Lex/PPDirectives.cpp Wed Nov 19 15:57:25 2008
@@ -303,8 +303,8 @@
   // 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 @@
   // 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 @@
   
   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;

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

==============================================================================
--- cfe/trunk/lib/Lex/PPLexerChange.cpp (original)
+++ cfe/trunk/lib/Lex/PPLexerChange.cpp Wed Nov 19 15:57:25 2008
@@ -25,27 +25,17 @@
 // 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 @@
                                             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 @@
   
   // 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.

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

==============================================================================
--- cfe/trunk/lib/Lex/PTHLexer.cpp (original)
+++ cfe/trunk/lib/Lex/PTHLexer.cpp Wed Nov 19 15:57:25 2008
@@ -18,8 +18,8 @@
 
 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;

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

==============================================================================
--- cfe/trunk/lib/Lex/PreprocessorLexer.cpp (original)
+++ cfe/trunk/lib/Lex/PreprocessorLexer.cpp Wed Nov 19 15:57:25 2008
@@ -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,





More information about the cfe-commits mailing list