Coverage Report

Created: 2021-01-26 06:56

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