PPCaching.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //===--- PPCaching.cpp - Handle caching lexed tokens ----------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements pieces of the Preprocessor interface that manage the
  10. // caching of lexed tokens.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Lex/Preprocessor.h"
  14. using namespace clang;
  15. // EnableBacktrackAtThisPos - From the point that this method is called, and
  16. // until CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor
  17. // keeps track of the lexed tokens so that a subsequent Backtrack() call will
  18. // make the Preprocessor re-lex the same tokens.
  19. //
  20. // Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can
  21. // be called multiple times and CommitBacktrackedTokens/Backtrack calls will
  22. // be combined with the EnableBacktrackAtThisPos calls in reverse order.
  23. void Preprocessor::EnableBacktrackAtThisPos() {
  24. assert(LexLevel == 0 && "cannot use lookahead while lexing");
  25. BacktrackPositions.push_back(CachedLexPos);
  26. EnterCachingLexMode();
  27. }
  28. // Disable the last EnableBacktrackAtThisPos call.
  29. void Preprocessor::CommitBacktrackedTokens() {
  30. assert(!BacktrackPositions.empty()
  31. && "EnableBacktrackAtThisPos was not called!");
  32. BacktrackPositions.pop_back();
  33. }
  34. // Make Preprocessor re-lex the tokens that were lexed since
  35. // EnableBacktrackAtThisPos() was previously called.
  36. void Preprocessor::Backtrack() {
  37. assert(!BacktrackPositions.empty()
  38. && "EnableBacktrackAtThisPos was not called!");
  39. CachedLexPos = BacktrackPositions.back();
  40. BacktrackPositions.pop_back();
  41. recomputeCurLexerKind();
  42. }
  43. void Preprocessor::CachingLex(Token &Result) {
  44. if (!InCachingLexMode())
  45. return;
  46. // The assert in EnterCachingLexMode should prevent this from happening.
  47. assert(LexLevel == 1 &&
  48. "should not use token caching within the preprocessor");
  49. if (CachedLexPos < CachedTokens.size()) {
  50. Result = CachedTokens[CachedLexPos++];
  51. Result.setFlag(Token::IsReinjected);
  52. return;
  53. }
  54. ExitCachingLexMode();
  55. Lex(Result);
  56. if (isBacktrackEnabled()) {
  57. // Cache the lexed token.
  58. EnterCachingLexModeUnchecked();
  59. CachedTokens.push_back(Result);
  60. ++CachedLexPos;
  61. return;
  62. }
  63. if (CachedLexPos < CachedTokens.size()) {
  64. EnterCachingLexModeUnchecked();
  65. } else {
  66. // All cached tokens were consumed.
  67. CachedTokens.clear();
  68. CachedLexPos = 0;
  69. }
  70. }
  71. void Preprocessor::EnterCachingLexMode() {
  72. // The caching layer sits on top of all the other lexers, so it's incorrect
  73. // to cache tokens while inside a nested lex action. The cached tokens would
  74. // be retained after returning to the enclosing lex action and, at best,
  75. // would appear at the wrong position in the token stream.
  76. assert(LexLevel == 0 &&
  77. "entered caching lex mode while lexing something else");
  78. if (InCachingLexMode()) {
  79. assert(CurLexerKind == CLK_CachingLexer && "Unexpected lexer kind");
  80. return;
  81. }
  82. EnterCachingLexModeUnchecked();
  83. }
  84. void Preprocessor::EnterCachingLexModeUnchecked() {
  85. assert(CurLexerKind != CLK_CachingLexer && "already in caching lex mode");
  86. PushIncludeMacroStack();
  87. CurLexerKind = CLK_CachingLexer;
  88. }
  89. const Token &Preprocessor::PeekAhead(unsigned N) {
  90. assert(CachedLexPos + N > CachedTokens.size() && "Confused caching.");
  91. ExitCachingLexMode();
  92. for (size_t C = CachedLexPos + N - CachedTokens.size(); C > 0; --C) {
  93. CachedTokens.push_back(Token());
  94. Lex(CachedTokens.back());
  95. }
  96. EnterCachingLexMode();
  97. return CachedTokens.back();
  98. }
  99. void Preprocessor::AnnotatePreviousCachedTokens(const Token &Tok) {
  100. assert(Tok.isAnnotation() && "Expected annotation token");
  101. assert(CachedLexPos != 0 && "Expected to have some cached tokens");
  102. assert(CachedTokens[CachedLexPos-1].getLastLoc() == Tok.getAnnotationEndLoc()
  103. && "The annotation should be until the most recent cached token");
  104. // Start from the end of the cached tokens list and look for the token
  105. // that is the beginning of the annotation token.
  106. for (CachedTokensTy::size_type i = CachedLexPos; i != 0; --i) {
  107. CachedTokensTy::iterator AnnotBegin = CachedTokens.begin() + i-1;
  108. if (AnnotBegin->getLocation() == Tok.getLocation()) {
  109. assert((BacktrackPositions.empty() || BacktrackPositions.back() <= i) &&
  110. "The backtrack pos points inside the annotated tokens!");
  111. // Replace the cached tokens with the single annotation token.
  112. if (i < CachedLexPos)
  113. CachedTokens.erase(AnnotBegin + 1, CachedTokens.begin() + CachedLexPos);
  114. *AnnotBegin = Tok;
  115. CachedLexPos = i;
  116. return;
  117. }
  118. }
  119. }
  120. bool Preprocessor::IsPreviousCachedToken(const Token &Tok) const {
  121. // There's currently no cached token...
  122. if (!CachedLexPos)
  123. return false;
  124. const Token LastCachedTok = CachedTokens[CachedLexPos - 1];
  125. if (LastCachedTok.getKind() != Tok.getKind())
  126. return false;
  127. SourceLocation::IntTy RelOffset = 0;
  128. if ((!getSourceManager().isInSameSLocAddrSpace(
  129. Tok.getLocation(), getLastCachedTokenLocation(), &RelOffset)) ||
  130. RelOffset)
  131. return false;
  132. return true;
  133. }
  134. void Preprocessor::ReplacePreviousCachedToken(ArrayRef<Token> NewToks) {
  135. assert(CachedLexPos != 0 && "Expected to have some cached tokens");
  136. CachedTokens.insert(CachedTokens.begin() + CachedLexPos - 1, NewToks.begin(),
  137. NewToks.end());
  138. CachedTokens.erase(CachedTokens.begin() + CachedLexPos - 1 + NewToks.size());
  139. CachedLexPos += NewToks.size() - 1;
  140. }