clang API Documentation

PPCaching.cpp
Go to the documentation of this file.
00001 //===--- PPCaching.cpp - Handle caching lexed tokens ----------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements pieces of the Preprocessor interface that manage the
00011 // caching of lexed tokens.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "clang/Lex/Preprocessor.h"
00016 using namespace clang;
00017 
00018 // EnableBacktrackAtThisPos - From the point that this method is called, and
00019 // until CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor
00020 // keeps track of the lexed tokens so that a subsequent Backtrack() call will
00021 // make the Preprocessor re-lex the same tokens.
00022 //
00023 // Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can
00024 // be called multiple times and CommitBacktrackedTokens/Backtrack calls will
00025 // be combined with the EnableBacktrackAtThisPos calls in reverse order.
00026 void Preprocessor::EnableBacktrackAtThisPos() {
00027   BacktrackPositions.push_back(CachedLexPos);
00028   EnterCachingLexMode();
00029 }
00030 
00031 // Disable the last EnableBacktrackAtThisPos call.
00032 void Preprocessor::CommitBacktrackedTokens() {
00033   assert(!BacktrackPositions.empty()
00034          && "EnableBacktrackAtThisPos was not called!");
00035   BacktrackPositions.pop_back();
00036 }
00037 
00038 // Make Preprocessor re-lex the tokens that were lexed since
00039 // EnableBacktrackAtThisPos() was previously called.
00040 void Preprocessor::Backtrack() {
00041   assert(!BacktrackPositions.empty()
00042          && "EnableBacktrackAtThisPos was not called!");
00043   CachedLexPos = BacktrackPositions.back();
00044   BacktrackPositions.pop_back();
00045   recomputeCurLexerKind();
00046 }
00047 
00048 void Preprocessor::CachingLex(Token &Result) {
00049   if (!InCachingLexMode())
00050     return;
00051 
00052   if (CachedLexPos < CachedTokens.size()) {
00053     Result = CachedTokens[CachedLexPos++];
00054     return;
00055   }
00056 
00057   ExitCachingLexMode();
00058   Lex(Result);
00059 
00060   if (isBacktrackEnabled()) {
00061     // Cache the lexed token.
00062     EnterCachingLexMode();
00063     CachedTokens.push_back(Result);
00064     ++CachedLexPos;
00065     return;
00066   }
00067 
00068   if (CachedLexPos < CachedTokens.size()) {
00069     EnterCachingLexMode();
00070   } else {
00071     // All cached tokens were consumed.
00072     CachedTokens.clear();
00073     CachedLexPos = 0;
00074   }
00075 }
00076 
00077 void Preprocessor::EnterCachingLexMode() {
00078   if (InCachingLexMode())
00079     return;
00080 
00081   PushIncludeMacroStack();
00082   CurLexerKind = CLK_CachingLexer;
00083 }
00084 
00085 
00086 const Token &Preprocessor::PeekAhead(unsigned N) {
00087   assert(CachedLexPos + N > CachedTokens.size() && "Confused caching.");
00088   ExitCachingLexMode();
00089   for (unsigned C = CachedLexPos + N - CachedTokens.size(); C > 0; --C) {
00090     CachedTokens.push_back(Token());
00091     Lex(CachedTokens.back());
00092   }
00093   EnterCachingLexMode();
00094   return CachedTokens.back();
00095 }
00096 
00097 void Preprocessor::AnnotatePreviousCachedTokens(const Token &Tok) {
00098   assert(Tok.isAnnotation() && "Expected annotation token");
00099   assert(CachedLexPos != 0 && "Expected to have some cached tokens");
00100   assert(CachedTokens[CachedLexPos-1].getLastLoc() == Tok.getAnnotationEndLoc()
00101          && "The annotation should be until the most recent cached token");
00102 
00103   // Start from the end of the cached tokens list and look for the token
00104   // that is the beginning of the annotation token.
00105   for (CachedTokensTy::size_type i = CachedLexPos; i != 0; --i) {
00106     CachedTokensTy::iterator AnnotBegin = CachedTokens.begin() + i-1;
00107     if (AnnotBegin->getLocation() == Tok.getLocation()) {
00108       assert((BacktrackPositions.empty() || BacktrackPositions.back() < i) &&
00109              "The backtrack pos points inside the annotated tokens!");
00110       // Replace the cached tokens with the single annotation token.
00111       if (i < CachedLexPos)
00112         CachedTokens.erase(AnnotBegin + 1, CachedTokens.begin() + CachedLexPos);
00113       *AnnotBegin = Tok;
00114       CachedLexPos = i;
00115       return;
00116     }
00117   }
00118 }