Coverage Report

Created: 2020-02-15 09:57

/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.14M
void Preprocessor::EnableBacktrackAtThisPos() {
26
3.14M
  assert(LexLevel == 0 && "cannot use lookahead while lexing");
27
3.14M
  BacktrackPositions.push_back(CachedLexPos);
28
3.14M
  EnterCachingLexMode();
29
3.14M
}
30
31
// Disable the last EnableBacktrackAtThisPos call.
32
34.3k
void Preprocessor::CommitBacktrackedTokens() {
33
34.3k
  assert(!BacktrackPositions.empty()
34
34.3k
         && "EnableBacktrackAtThisPos was not called!");
35
34.3k
  BacktrackPositions.pop_back();
36
34.3k
}
37
38
// Make Preprocessor re-lex the tokens that were lexed since
39
// EnableBacktrackAtThisPos() was previously called.
40
3.11M
void Preprocessor::Backtrack() {
41
3.11M
  assert(!BacktrackPositions.empty()
42
3.11M
         && "EnableBacktrackAtThisPos was not called!");
43
3.11M
  CachedLexPos = BacktrackPositions.back();
44
3.11M
  BacktrackPositions.pop_back();
45
3.11M
  recomputeCurLexerKind();
46
3.11M
}
47
48
106M
void Preprocessor::CachingLex(Token &Result) {
49
106M
  if (!InCachingLexMode())
50
0
    return;
51
106M
52
106M
  // The assert in EnterCachingLexMode should prevent this from happening.
53
106M
  assert(LexLevel == 1 &&
54
106M
         "should not use token caching within the preprocessor");
55
106M
56
106M
  if (CachedLexPos < CachedTokens.size()) {
57
62.3M
    Result = CachedTokens[CachedLexPos++];
58
62.3M
    Result.setFlag(Token::IsReinjected);
59
62.3M
    return;
60
62.3M
  }
61
43.9M
62
43.9M
  ExitCachingLexMode();
63
43.9M
  Lex(Result);
64
43.9M
65
43.9M
  if (isBacktrackEnabled()) {
66
2.43M
    // 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
41.5M
73
41.5M
  if (CachedLexPos < CachedTokens.size()) {
74
0
    EnterCachingLexModeUnchecked();
75
41.5M
  } else {
76
41.5M
    // All cached tokens were consumed.
77
41.5M
    CachedTokens.clear();
78
41.5M
    CachedLexPos = 0;
79
41.5M
  }
80
41.5M
}
81
82
61.4M
void Preprocessor::EnterCachingLexMode() {
83
61.4M
  // The caching layer sits on top of all the other lexers, so it's incorrect
84
61.4M
  // to cache tokens while inside a nested lex action. The cached tokens would
85
61.4M
  // be retained after returning to the enclosing lex action and, at best,
86
61.4M
  // would appear at the wrong position in the token stream.
87
61.4M
  assert(LexLevel == 0 &&
88
61.4M
         "entered caching lex mode while lexing something else");
89
61.4M
90
61.4M
  if (InCachingLexMode()) {
91
5.70M
    assert(CurLexerKind == CLK_CachingLexer && "Unexpected lexer kind");
92
5.70M
    return;
93
5.70M
  }
94
55.7M
95
55.7M
  EnterCachingLexModeUnchecked();
96
55.7M
}
97
98
58.2M
void Preprocessor::EnterCachingLexModeUnchecked() {
99
58.2M
  assert(CurLexerKind != CLK_CachingLexer && "already in caching lex mode");
100
58.2M
  PushIncludeMacroStack();
101
58.2M
  CurLexerKind = CLK_CachingLexer;
102
58.2M
}
103
104
105
55.4M
const Token &Preprocessor::PeekAhead(unsigned N) {
106
55.4M
  assert(CachedLexPos + N > CachedTokens.size() && "Confused caching.");
107
55.4M
  ExitCachingLexMode();
108
110M
  for (size_t C = CachedLexPos + N - CachedTokens.size(); C > 0; 
--C55.4M
) {
109
55.4M
    CachedTokens.push_back(Token());
110
55.4M
    Lex(CachedTokens.back());
111
55.4M
  }
112
55.4M
  EnterCachingLexMode();
113
55.4M
  return CachedTokens.back();
114
55.4M
}
115
116
907k
void Preprocessor::AnnotatePreviousCachedTokens(const Token &Tok) {
117
907k
  assert(Tok.isAnnotation() && "Expected annotation token");
118
907k
  assert(CachedLexPos != 0 && "Expected to have some cached tokens");
119
907k
  assert(CachedTokens[CachedLexPos-1].getLastLoc() == Tok.getAnnotationEndLoc()
120
907k
         && "The annotation should be until the most recent cached token");
121
907k
122
907k
  // Start from the end of the cached tokens list and look for the token
123
907k
  // that is the beginning of the annotation token.
124
1.27M
  for (CachedTokensTy::size_type i = CachedLexPos; i != 0; 
--i364k
) {
125
1.27M
    CachedTokensTy::iterator AnnotBegin = CachedTokens.begin() + i-1;
126
1.27M
    if (AnnotBegin->getLocation() == Tok.getLocation()) {
127
907k
      assert((BacktrackPositions.empty() || BacktrackPositions.back() <= i) &&
128
907k
             "The backtrack pos points inside the annotated tokens!");
129
907k
      // Replace the cached tokens with the single annotation token.
130
907k
      if (i < CachedLexPos)
131
114k
        CachedTokens.erase(AnnotBegin + 1, CachedTokens.begin() + CachedLexPos);
132
907k
      *AnnotBegin = Tok;
133
907k
      CachedLexPos = i;
134
907k
      return;
135
907k
    }
136
1.27M
  }
137
907k
}
138
139
17.6k
bool Preprocessor::IsPreviousCachedToken(const Token &Tok) const {
140
17.6k
  // There's currently no cached token...
141
17.6k
  if (!CachedLexPos)
142
4.24k
    return false;
143
13.3k
144
13.3k
  const Token LastCachedTok = CachedTokens[CachedLexPos - 1];
145
13.3k
  if (LastCachedTok.getKind() != Tok.getKind())
146
0
    return false;
147
13.3k
148
13.3k
  int RelOffset = 0;
149
13.3k
  if ((!getSourceManager().isInSameSLocAddrSpace(
150
13.3k
          Tok.getLocation(), getLastCachedTokenLocation(), &RelOffset)) ||
151
13.3k
      RelOffset)
152
0
    return false;
153
13.3k
154
13.3k
  return true;
155
13.3k
}
156
157
13.4k
void Preprocessor::ReplacePreviousCachedToken(ArrayRef<Token> NewToks) {
158
13.4k
  assert(CachedLexPos != 0 && "Expected to have some cached tokens");
159
13.4k
  CachedTokens.insert(CachedTokens.begin() + CachedLexPos - 1, NewToks.begin(),
160
13.4k
                      NewToks.end());
161
13.4k
  CachedTokens.erase(CachedTokens.begin() + CachedLexPos - 1 + NewToks.size());
162
13.4k
  CachedLexPos += NewToks.size() - 1;
163
13.4k
}