diff options
author | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2008-08-10 13:15:22 +0000 |
---|---|---|
committer | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2008-08-10 13:15:22 +0000 |
commit | 03db1b31dd926409b7defc1c90b66549464652c0 (patch) | |
tree | ea9d1600a9098e36c5ec0da6632e44180d1ad9d4 /lib/Lex/PPCaching.cpp | |
parent | 9ca8bb0996bbb8b9dbf69c51cb3d1523559e47e3 (diff) |
Allow the preprocessor to cache the lexed tokens, so that we can do efficient lookahead and backtracking.
1) New public methods added:
-EnableBacktrackAtThisPos
-DisableBacktrack
-Backtrack
-isBacktrackEnabled
2) LookAhead() implementation is replaced with a more efficient one.
3) LookNext() is removed.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@54611 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Lex/PPCaching.cpp')
-rw-r--r-- | lib/Lex/PPCaching.cpp | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/lib/Lex/PPCaching.cpp b/lib/Lex/PPCaching.cpp new file mode 100644 index 0000000000..794e9c4e76 --- /dev/null +++ b/lib/Lex/PPCaching.cpp @@ -0,0 +1,63 @@ +//===--- PPCaching.cpp - Handle caching lexed tokens ----------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements pieces of the Preprocessor interface that manage the +// caching of lexed tokens. +// +//===----------------------------------------------------------------------===// + +#include "clang/Lex/Preprocessor.h" +using namespace clang; + +void Preprocessor::CachingLex(Token &Result) { + if (CachedLexPos < CachedTokens.size()) { + Result = CachedTokens[CachedLexPos++]; + return; + } + + ExitCachingLexMode(); + Lex(Result); + + if (!CacheTokens) { + // All cached tokens were consumed. + CachedTokens.clear(); + CachedLexPos = 0; + return; + } + + // We should cache the lexed token. + + EnterCachingLexMode(); + if (Result.isNot(tok::eof)) { + CachedTokens.push_back(Result); + ++CachedLexPos; + } +} + +void Preprocessor::EnterCachingLexMode() { + if (InCachingLexMode()) + return; + + IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup, + CurTokenLexer)); + CurLexer = 0; + CurTokenLexer = 0; +} + + +const Token &Preprocessor::PeekAhead(unsigned N) { + assert(CachedLexPos + N > CachedTokens.size() && "Confused caching."); + ExitCachingLexMode(); + for (unsigned C = CachedLexPos + N - CachedTokens.size(); C > 0; --C) { + CachedTokens.push_back(Token()); + Lex(CachedTokens.back()); + } + EnterCachingLexMode(); + return CachedTokens.back(); +} |