Coverage Report

Created: 2022-01-25 06:29

/Users/buildslave/jenkins/workspace/coverage/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
11.0M
void Preprocessor::EnableBacktrackAtThisPos() {
26
11.0M
  assert(LexLevel == 0 && "cannot use lookahead while lexing");
27
0
  BacktrackPositions.push_back(CachedLexPos);
28
11.0M
  EnterCachingLexMode();
29
11.0M
}
30
31
// Disable the last EnableBacktrackAtThisPos call.
32
41.9k
void Preprocessor::CommitBacktrackedTokens() {
33
41.9k
  assert(!BacktrackPositions.empty()
34
41.9k
         && "EnableBacktrackAtThisPos was not called!");
35
0
  BacktrackPositions.pop_back();
36
41.9k
}
37
38
// Make Preprocessor re-lex the tokens that were lexed since
39
// EnableBacktrackAtThisPos() was previously called.
40
11.0M
void Preprocessor::Backtrack() {
41
11.0M
  assert(!BacktrackPositions.empty()
42
11.0M
         && "EnableBacktrackAtThisPos was not called!");
43
0
  CachedLexPos = BacktrackPositions.back();
44
11.0M
  BacktrackPositions.pop_back();
45
11.0M
  recomputeCurLexerKind();
46
11.0M
}
47
48
231M
void Preprocessor::CachingLex(Token &Result) {
49
231M
  if (!InCachingLexMode())
50
0
    return;
51
52
  // The assert in EnterCachingLexMode should prevent this from happening.
53
231M
  assert(LexLevel == 1 &&
54
231M
         "should not use token caching within the preprocessor");
55
56
231M
  if (CachedLexPos < CachedTokens.size()) {
57
133M
    Result = CachedTokens[CachedLexPos++];
58
133M
    Result.setFlag(Token::IsReinjected);
59
133M
    return;
60
133M
  }
61
62
98.5M
  ExitCachingLexMode();
63
98.5M
  Lex(Result);
64
65
98.5M
  if (isBacktrackEnabled()) {
66
    // Cache the lexed token.
67
10.2M
    EnterCachingLexModeUnchecked();
68
10.2M
    CachedTokens.push_back(Result);
69
10.2M
    ++CachedLexPos;
70
10.2M
    return;
71
10.2M
  }
72
73
88.2M
  if (CachedLexPos < CachedTokens.size()) {
74
0
    EnterCachingLexModeUnchecked();
75
88.2M
  } else {
76
    // All cached tokens were consumed.
77
88.2M
    CachedTokens.clear();
78
88.2M
    CachedLexPos = 0;
79
88.2M
  }
80
88.2M
}
81
82
132M
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
132M
  assert(LexLevel == 0 &&
88
132M
         "entered caching lex mode while lexing something else");
89
90
132M
  if (InCachingLexMode()) {
91
13.6M
    assert(CurLexerKind == CLK_CachingLexer && "Unexpected lexer kind");
92
0
    return;
93
13.6M
  }
94
95
118M
  EnterCachingLexModeUnchecked();
96
118M
}
97
98
128M
void Preprocessor::EnterCachingLexModeUnchecked() {
99
128M
  assert(CurLexerKind != CLK_CachingLexer && "already in caching lex mode");
100
0
  PushIncludeMacroStack();
101
128M
  CurLexerKind = CLK_CachingLexer;
102
128M
}
103
104
105
118M
const Token &Preprocessor::PeekAhead(unsigned N) {
106
118M
  assert(CachedLexPos + N > CachedTokens.size() && "Confused caching.");
107
0
  ExitCachingLexMode();
108
236M
  for (size_t C = CachedLexPos + N - CachedTokens.size(); C > 0; 
--C118M
) {
109
118M
    CachedTokens.push_back(Token());
110
118M
    Lex(CachedTokens.back());
111
118M
  }
112
118M
  EnterCachingLexMode();
113
118M
  return CachedTokens.back();
114
118M
}
115
116
8.35M
void Preprocessor::AnnotatePreviousCachedTokens(const Token &Tok) {
117
8.35M
  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
8.35M
         && "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
8.74M
  for (CachedTokensTy::size_type i = CachedLexPos; i != 0; 
--i386k
) {
125
8.74M
    CachedTokensTy::iterator AnnotBegin = CachedTokens.begin() + i-1;
126
8.74M
    if (AnnotBegin->getLocation() == Tok.getLocation()) {
127
8.35M
      assert((BacktrackPositions.empty() || BacktrackPositions.back() <= i) &&
128
8.35M
             "The backtrack pos points inside the annotated tokens!");
129
      // Replace the cached tokens with the single annotation token.
130
8.35M
      if (i < CachedLexPos)
131
120k
        CachedTokens.erase(AnnotBegin + 1, CachedTokens.begin() + CachedLexPos);
132
8.35M
      *AnnotBegin = Tok;
133
8.35M
      CachedLexPos = i;
134
8.35M
      return;
135
8.35M
    }
136
8.74M
  }
137
8.35M
}
138
139
15.9k
bool Preprocessor::IsPreviousCachedToken(const Token &Tok) const {
140
  // There's currently no cached token...
141
15.9k
  if (!CachedLexPos)
142
4.64k
    return false;
143
144
11.3k
  const Token LastCachedTok = CachedTokens[CachedLexPos - 1];
145
11.3k
  if (LastCachedTok.getKind() != Tok.getKind())
146
0
    return false;
147
148
11.3k
  SourceLocation::IntTy RelOffset = 0;
149
11.3k
  if ((!getSourceManager().isInSameSLocAddrSpace(
150
11.3k
          Tok.getLocation(), getLastCachedTokenLocation(), &RelOffset)) ||
151
11.3k
      RelOffset)
152
0
    return false;
153
154
11.3k
  return true;
155
11.3k
}
156
157
11.3k
void Preprocessor::ReplacePreviousCachedToken(ArrayRef<Token> NewToks) {
158
11.3k
  assert(CachedLexPos != 0 && "Expected to have some cached tokens");
159
0
  CachedTokens.insert(CachedTokens.begin() + CachedLexPos - 1, NewToks.begin(),
160
11.3k
                      NewToks.end());
161
11.3k
  CachedTokens.erase(CachedTokens.begin() + CachedLexPos - 1 + NewToks.size());
162
11.3k
  CachedLexPos += NewToks.size() - 1;
163
11.3k
}