Coverage Report

Created: 2021-08-24 07:12

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