Coverage Report

Created: 2020-09-22 08:39

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