PPCaching.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //===--- PPCaching.cpp - Handle caching lexed tokens ----------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements pieces of the Preprocessor interface that manage the
  11. // caching of lexed tokens.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/Lex/Preprocessor.h"
  15. using namespace clang;
  16. // EnableBacktrackAtThisPos - From the point that this method is called, and
  17. // until CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor
  18. // keeps track of the lexed tokens so that a subsequent Backtrack() call will
  19. // make the Preprocessor re-lex the same tokens.
  20. //
  21. // Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can
  22. // be called multiple times and CommitBacktrackedTokens/Backtrack calls will
  23. // be combined with the EnableBacktrackAtThisPos calls in reverse order.
  24. void Preprocessor::EnableBacktrackAtThisPos() {
  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. if (CachedLexPos < CachedTokens.size()) {
  47. Result = CachedTokens[CachedLexPos++];
  48. return;
  49. }
  50. ExitCachingLexMode();
  51. Lex(Result);
  52. if (isBacktrackEnabled()) {
  53. // Cache the lexed token.
  54. EnterCachingLexMode();
  55. CachedTokens.push_back(Result);
  56. ++CachedLexPos;
  57. return;
  58. }
  59. if (CachedLexPos < CachedTokens.size()) {
  60. EnterCachingLexMode();
  61. } else {
  62. // All cached tokens were consumed.
  63. CachedTokens.clear();
  64. CachedLexPos = 0;
  65. }
  66. }
  67. void Preprocessor::EnterCachingLexMode() {
  68. if (InCachingLexMode())
  69. return;
  70. PushIncludeMacroStack();
  71. CurLexerKind = CLK_CachingLexer;
  72. }
  73. const Token &Preprocessor::PeekAhead(unsigned N) {
  74. assert(CachedLexPos + N > CachedTokens.size() && "Confused caching.");
  75. ExitCachingLexMode();
  76. for (unsigned C = CachedLexPos + N - CachedTokens.size(); C > 0; --C) {
  77. CachedTokens.push_back(Token());
  78. Lex(CachedTokens.back());
  79. }
  80. EnterCachingLexMode();
  81. return CachedTokens.back();
  82. }
  83. void Preprocessor::AnnotatePreviousCachedTokens(const Token &Tok) {
  84. assert(Tok.isAnnotation() && "Expected annotation token");
  85. assert(CachedLexPos != 0 && "Expected to have some cached tokens");
  86. assert(CachedTokens[CachedLexPos-1].getLastLoc() == Tok.getAnnotationEndLoc()
  87. && "The annotation should be until the most recent cached token");
  88. // Start from the end of the cached tokens list and look for the token
  89. // that is the beginning of the annotation token.
  90. for (CachedTokensTy::size_type i = CachedLexPos; i != 0; --i) {
  91. CachedTokensTy::iterator AnnotBegin = CachedTokens.begin() + i-1;
  92. if (AnnotBegin->getLocation() == Tok.getLocation()) {
  93. assert((BacktrackPositions.empty() || BacktrackPositions.back() < i) &&
  94. "The backtrack pos points inside the annotated tokens!");
  95. // Replace the cached tokens with the single annotation token.
  96. if (i < CachedLexPos)
  97. CachedTokens.erase(AnnotBegin + 1, CachedTokens.begin() + CachedLexPos);
  98. *AnnotBegin = Tok;
  99. CachedLexPos = i;
  100. return;
  101. }
  102. }
  103. }