[cfe-commits] r59723 - /cfe/trunk/lib/Lex/PPLexerChange.cpp

Ted Kremenek kremenek at apple.com
Wed Nov 19 23:56:33 PST 2008


Author: kremenek
Date: Thu Nov 20 01:56:33 2008
New Revision: 59723

URL: http://llvm.org/viewvc/llvm-project?rev=59723&view=rev
Log:
Add ugly "test harness" for PTHLexer that is not enabled by default. The
(temporary hack) to test the PTHLexer is that whenever we would create a Lexer
object we instead raw lex a memory buffer first and then use the PTHLexer. This
logic exists only to driver the PTHLexer and will be removed/changed in the
future. Note that the regular path using normal Lexer objects is what is used by
default.

Modified:
    cfe/trunk/lib/Lex/PPLexerChange.cpp

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

==============================================================================
--- cfe/trunk/lib/Lex/PPLexerChange.cpp (original)
+++ cfe/trunk/lib/Lex/PPLexerChange.cpp Thu Nov 20 01:56:33 2008
@@ -17,6 +17,8 @@
 #include "clang/Lex/MacroInfo.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/SourceManager.h"
+#include "llvm/Support/MemoryBuffer.h"
+
 using namespace clang;
 
 PPCallbacks::~PPCallbacks() {}
@@ -72,8 +74,50 @@
   if (MaxIncludeStackDepth < IncludeMacroStack.size())
     MaxIncludeStackDepth = IncludeMacroStack.size();
 
+#if 1
   Lexer *TheLexer = new Lexer(SourceLocation::getFileLoc(FileID, 0), *this);
   EnterSourceFileWithLexer(TheLexer, CurDir);
+#else
+  const llvm::MemoryBuffer* B = getSourceManager().getBuffer(FileID);
+  
+  // Create a raw lexer.
+  Lexer L(SourceLocation::getFileLoc(FileID, 0), getLangOptions(),
+          B->getBufferStart(), B->getBufferEnd(), B);
+  
+  // Ignore whitespace.
+  L.SetKeepWhitespaceMode(false);
+  L.SetCommentRetentionState(false);
+  
+  // Lex the file, populating our data structures.
+  std::vector<Token>* Tokens = new std::vector<Token>();
+  Token Tok;  
+  
+  do {
+    L.LexFromRawLexer(Tok);
+    
+    if (Tok.is(tok::identifier))
+      Tok.setIdentifierInfo(LookUpIdentifierInfo(Tok));
+    
+    // Store the token.
+    Tokens->push_back(Tok);
+  }
+  while (Tok.isNot(tok::eof));
+  
+  if (CurPPLexer || CurTokenLexer)
+    PushIncludeMacroStack();
+  
+  CurDirLookup = CurDir;
+  SourceLocation Loc = SourceLocation::getFileLoc(FileID, 0);
+  CurPTHLexer.reset(new PTHLexer(*this, Loc, &(*Tokens)[0], Tokens->size()));
+  CurPPLexer = CurPTHLexer.get();
+  
+  // Notify the client, if desired, that we are in a new source file.
+  if (Callbacks) {
+    SrcMgr::CharacteristicKind FileType =
+    SourceMgr.getFileCharacteristic(CurPPLexer->getFileID());    
+    Callbacks->FileChanged(Loc, PPCallbacks::EnterFile, FileType);
+  }
+#endif
 }  
 
 /// EnterSourceFile - Add a source file to the top of the include stack and
@@ -172,14 +216,15 @@
     if (Callbacks && !isEndOfMacro && CurPPLexer) {
       SrcMgr::CharacteristicKind FileType =
         SourceMgr.getFileCharacteristic(CurPPLexer->getFileID());
-      
+
       if (CurLexer) {
-        // FIXME: Should we use the location of 'Result'?
         Callbacks->FileChanged(CurLexer->getSourceLocation(CurLexer->BufferPtr),
                                PPCallbacks::ExitFile, FileType);
       }
       else {
-        assert (0 && "FIXME: Add callback support for PTHLexer.");
+        // FIXME: Is this okay to use the location of 'Result'?
+        Callbacks->FileChanged(Result.getLocation(), PPCallbacks::ExitFile,
+                               FileType);  
       }
     }
 
@@ -258,9 +303,7 @@
       CurTokenLexer.reset();
     else
       TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.take();
-  } else {
-    CurLexer.reset();
-  }
+  }   
   
   PopIncludeMacroStack();
 }





More information about the cfe-commits mailing list