Coverage Report

Created: 2019-07-24 05:18

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