Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang/lib/Lex/PPCaching.cpp
Line
Count
Source (jump to first uncovered line)
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
14
#include "clang/Lex/Preprocessor.h"
15
using namespace clang;
16
17
// EnableBacktrackAtThisPos - From the point that this method is called, and
18
// until CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor
19
// keeps track of the lexed tokens so that a subsequent Backtrack() call will
20
// make the Preprocessor re-lex the same tokens.
21
//
22
// Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can
23
// be called multiple times and CommitBacktrackedTokens/Backtrack calls will
24
// be combined with the EnableBacktrackAtThisPos calls in reverse order.
25
411
void Preprocessor::EnableBacktrackAtThisPos() {
26
411
  assert(LexLevel == 0 && "cannot use lookahead while lexing");
27
0
  BacktrackPositions.push_back(CachedLexPos);
28
411
  EnterCachingLexMode();
29
411
}
30
31
// Disable the last EnableBacktrackAtThisPos call.
32
0
void Preprocessor::CommitBacktrackedTokens() {
33
0
  assert(!BacktrackPositions.empty()
34
0
         && "EnableBacktrackAtThisPos was not called!");
35
0
  BacktrackPositions.pop_back();
36
0
}
37
38
// Make Preprocessor re-lex the tokens that were lexed since
39
// EnableBacktrackAtThisPos() was previously called.
40
411
void Preprocessor::Backtrack() {
41
411
  assert(!BacktrackPositions.empty()
42
411
         && "EnableBacktrackAtThisPos was not called!");
43
0
  CachedLexPos = BacktrackPositions.back();
44
411
  BacktrackPositions.pop_back();
45
411
  recomputeCurLexerKind();
46
411
}
47
48
84.8k
void Preprocessor::CachingLex(Token &Result) {
49
84.8k
  if (!InCachingLexMode())
50
0
    return;
51
52
  // The assert in EnterCachingLexMode should prevent this from happening.
53
84.8k
  assert(LexLevel == 1 &&
54
84.8k
         "should not use token caching within the preprocessor");
55
56
84.8k
  if (CachedLexPos < CachedTokens.size()) {
57
54.4k
    Result = CachedTokens[CachedLexPos++];
58
54.4k
    Result.setFlag(Token::IsReinjected);
59
54.4k
    return;
60
54.4k
  }
61
62
30.3k
  ExitCachingLexMode();
63
30.3k
  Lex(Result);
64
65
30.3k
  if (isBacktrackEnabled()) {
66
    // Cache the lexed token.
67
13.6k
    EnterCachingLexModeUnchecked();
68
13.6k
    CachedTokens.push_back(Result);
69
13.6k
    ++CachedLexPos;
70
13.6k
    return;
71
13.6k
  }
72
73
16.7k
  if (CachedLexPos < CachedTokens.size()) {
74
0
    EnterCachingLexModeUnchecked();
75
16.7k
  } else {
76
    // All cached tokens were consumed.
77
16.7k
    CachedTokens.clear();
78
16.7k
    CachedLexPos = 0;
79
16.7k
  }
80
16.7k
}
81
82
20.5k
void Preprocessor::EnterCachingLexMode() {
83
  // The caching layer sits on top of all the other lexers, so it's incorrect
84
  // to cache tokens while inside a nested lex action. The cached tokens would
85
  // be retained after returning to the enclosing lex action and, at best,
86
  // would appear at the wrong position in the token stream.
87
20.5k
  assert(LexLevel == 0 &&
88
20.5k
         "entered caching lex mode while lexing something else");
89
90
20.5k
  if (InCachingLexMode()) {
91
397
    assert(CurLexerCallback == CLK_CachingLexer && "Unexpected lexer kind");
92
0
    return;
93
397
  }
94
95
20.1k
  EnterCachingLexModeUnchecked();
96
20.1k
}
97
98
33.7k
void Preprocessor::EnterCachingLexModeUnchecked() {
99
33.7k
  assert(CurLexerCallback != CLK_CachingLexer && "already in caching lex mode");
100
0
  PushIncludeMacroStack();
101
33.7k
  CurLexerCallback = CLK_CachingLexer;
102
33.7k
}
103
104
105
20.1k
const Token &Preprocessor::PeekAhead(unsigned N) {
106
20.1k
  assert(CachedLexPos + N > CachedTokens.size() && "Confused caching.");
107
0
  ExitCachingLexMode();
108
40.2k
  for (size_t C = CachedLexPos + N - CachedTokens.size(); C > 0; --C) {
109
20.1k
    CachedTokens.push_back(Token());
110
20.1k
    Lex(CachedTokens.back());
111
20.1k
  }
112
20.1k
  EnterCachingLexMode();
113
20.1k
  return CachedTokens.back();
114
20.1k
}
115
116
15
void Preprocessor::AnnotatePreviousCachedTokens(const Token &Tok) {
117
15
  assert(Tok.isAnnotation() && "Expected annotation token");
118
0
  assert(CachedLexPos != 0 && "Expected to have some cached tokens");
119
0
  assert(CachedTokens[CachedLexPos-1].getLastLoc() == Tok.getAnnotationEndLoc()
120
15
         && "The annotation should be until the most recent cached token");
121
122
  // Start from the end of the cached tokens list and look for the token
123
  // that is the beginning of the annotation token.
124
18
  for (CachedTokensTy::size_type i = CachedLexPos; i != 0; --i) {
125
18
    CachedTokensTy::iterator AnnotBegin = CachedTokens.begin() + i-1;
126
18
    if (AnnotBegin->getLocation() == Tok.getLocation()) {
127
15
      assert((BacktrackPositions.empty() || BacktrackPositions.back() <= i) &&
128
15
             "The backtrack pos points inside the annotated tokens!");
129
      // Replace the cached tokens with the single annotation token.
130
15
      if (i < CachedLexPos)
131
1
        CachedTokens.erase(AnnotBegin + 1, CachedTokens.begin() + CachedLexPos);
132
15
      *AnnotBegin = Tok;
133
15
      CachedLexPos = i;
134
15
      return;
135
15
    }
136
18
  }
137
15
}
138
139
0
bool Preprocessor::IsPreviousCachedToken(const Token &Tok) const {
140
  // There's currently no cached token...
141
0
  if (!CachedLexPos)
142
0
    return false;
143
144
0
  const Token LastCachedTok = CachedTokens[CachedLexPos - 1];
145
0
  if (LastCachedTok.getKind() != Tok.getKind())
146
0
    return false;
147
148
0
  SourceLocation::IntTy RelOffset = 0;
149
0
  if ((!getSourceManager().isInSameSLocAddrSpace(
150
0
          Tok.getLocation(), getLastCachedTokenLocation(), &RelOffset)) ||
151
0
      RelOffset)
152
0
    return false;
153
154
0
  return true;
155
0
}
156
157
0
void Preprocessor::ReplacePreviousCachedToken(ArrayRef<Token> NewToks) {
158
0
  assert(CachedLexPos != 0 && "Expected to have some cached tokens");
159
0
  CachedTokens.insert(CachedTokens.begin() + CachedLexPos - 1, NewToks.begin(),
160
0
                      NewToks.end());
161
0
  CachedTokens.erase(CachedTokens.begin() + CachedLexPos - 1 + NewToks.size());
162
0
  CachedLexPos += NewToks.size() - 1;
163
0
}