aboutsummaryrefslogtreecommitdiff
path: root/lib/Lex/PPLexerChange.cpp
diff options
context:
space:
mode:
authorTed Kremenek <kremenek@apple.com>2008-11-20 07:56:33 +0000
committerTed Kremenek <kremenek@apple.com>2008-11-20 07:56:33 +0000
commit15ba2af0f9ad52e67e908179b8ec2eb404e11c61 (patch)
tree441a556ee7ead0ff4a216817e0a77b5b6444508a /lib/Lex/PPLexerChange.cpp
parentac2dda65d2d39c0a83d95bdf6237edbf4685bc58 (diff)
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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@59723 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Lex/PPLexerChange.cpp')
-rw-r--r--lib/Lex/PPLexerChange.cpp55
1 files changed, 49 insertions, 6 deletions
diff --git a/lib/Lex/PPLexerChange.cpp b/lib/Lex/PPLexerChange.cpp
index 1df4d210b8..973eced1cd 100644
--- a/lib/Lex/PPLexerChange.cpp
+++ b/lib/Lex/PPLexerChange.cpp
@@ -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 @@ void Preprocessor::EnterSourceFile(unsigned FileID,
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 @@ bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
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 @@ void Preprocessor::RemoveTopOfLexerStack() {
CurTokenLexer.reset();
else
TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.take();
- } else {
- CurLexer.reset();
- }
+ }
PopIncludeMacroStack();
}