Coverage Report

Created: 2019-02-23 12:57

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/Lex/PreprocessorLexer.h
Line
Count
Source (jump to first uncovered line)
1
//===- PreprocessorLexer.h - C Language Family Lexer ------------*- C++ -*-===//
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
/// \file
10
/// Defines the PreprocessorLexer interface.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_CLANG_LEX_PREPROCESSORLEXER_H
15
#define LLVM_CLANG_LEX_PREPROCESSORLEXER_H
16
17
#include "clang/Lex/MultipleIncludeOpt.h"
18
#include "clang/Lex/Token.h"
19
#include "clang/Basic/SourceLocation.h"
20
#include "llvm/ADT/ArrayRef.h"
21
#include "llvm/ADT/SmallVector.h"
22
#include <cassert>
23
24
namespace clang {
25
26
class FileEntry;
27
class Preprocessor;
28
29
class PreprocessorLexer {
30
  virtual void anchor();
31
32
protected:
33
  friend class Preprocessor;
34
35
  // Preprocessor object controlling lexing.
36
  Preprocessor *PP = nullptr;
37
38
  /// The SourceManager FileID corresponding to the file being lexed.
39
  const FileID FID;
40
41
  /// Number of SLocEntries before lexing the file.
42
  unsigned InitialNumSLocEntries = 0;
43
44
  //===--------------------------------------------------------------------===//
45
  // Context-specific lexing flags set by the preprocessor.
46
  //===--------------------------------------------------------------------===//
47
48
  /// True when parsing \#XXX; turns '\\n' into a tok::eod token.
49
  bool ParsingPreprocessorDirective = false;
50
51
  /// True after \#include; turns \<xx> into a tok::angle_string_literal
52
  /// token.
53
  bool ParsingFilename = false;
54
55
  /// True if in raw mode.
56
  ///
57
  /// Raw mode disables interpretation of tokens and is a far faster mode to
58
  /// lex in than non-raw-mode.  This flag:
59
  ///  1. If EOF of the current lexer is found, the include stack isn't popped.
60
  ///  2. Identifier information is not looked up for identifier tokens.  As an
61
  ///     effect of this, implicit macro expansion is naturally disabled.
62
  ///  3. "#" tokens at the start of a line are treated as normal tokens, not
63
  ///     implicitly transformed by the lexer.
64
  ///  4. All diagnostic messages are disabled.
65
  ///  5. No callbacks are made into the preprocessor.
66
  ///
67
  /// Note that in raw mode that the PP pointer may be null.
68
  bool LexingRawMode = false;
69
70
  /// A state machine that detects the \#ifndef-wrapping a file
71
  /// idiom for the multiple-include optimization.
72
  MultipleIncludeOpt MIOpt;
73
74
  /// Information about the set of \#if/\#ifdef/\#ifndef blocks
75
  /// we are currently in.
76
  SmallVector<PPConditionalInfo, 4> ConditionalStack;
77
78
1.55M
  PreprocessorLexer() : FID() {}
79
  PreprocessorLexer(Preprocessor *pp, FileID fid);
80
2.13M
  virtual ~PreprocessorLexer() = default;
81
82
  virtual void IndirectLex(Token& Result) = 0;
83
84
  /// Return the source location for the next observable location.
85
  virtual SourceLocation getSourceLocation() = 0;
86
87
  //===--------------------------------------------------------------------===//
88
  // #if directive handling.
89
90
  /// pushConditionalLevel - When we enter a \#if directive, this keeps track of
91
  /// what we are currently in for diagnostic emission (e.g. \#if with missing
92
  /// \#endif).
93
  void pushConditionalLevel(SourceLocation DirectiveStart, bool WasSkipping,
94
10.7M
                            bool FoundNonSkip, bool FoundElse) {
95
10.7M
    PPConditionalInfo CI;
96
10.7M
    CI.IfLoc = DirectiveStart;
97
10.7M
    CI.WasSkipping = WasSkipping;
98
10.7M
    CI.FoundNonSkip = FoundNonSkip;
99
10.7M
    CI.FoundElse = FoundElse;
100
10.7M
    ConditionalStack.push_back(CI);
101
10.7M
  }
102
0
  void pushConditionalLevel(const PPConditionalInfo &CI) {
103
0
    ConditionalStack.push_back(CI);
104
0
  }
105
106
  /// popConditionalLevel - Remove an entry off the top of the conditional
107
  /// stack, returning information about it.  If the conditional stack is empty,
108
  /// this returns true and does not fill in the arguments.
109
10.7M
  bool popConditionalLevel(PPConditionalInfo &CI) {
110
10.7M
    if (ConditionalStack.empty())
111
2
      return true;
112
10.7M
    CI = ConditionalStack.pop_back_val();
113
10.7M
    return false;
114
10.7M
  }
115
116
  /// Return the top of the conditional stack.
117
  /// \pre This requires that there be a conditional active.
118
4.55M
  PPConditionalInfo &peekConditionalLevel() {
119
4.55M
    assert(!ConditionalStack.empty() && "No conditionals active!");
120
4.55M
    return ConditionalStack.back();
121
4.55M
  }
122
123
10.4M
  unsigned getConditionalStackDepth() const { return ConditionalStack.size(); }
124
125
public:
126
  PreprocessorLexer(const PreprocessorLexer &) = delete;
127
  PreprocessorLexer &operator=(const PreprocessorLexer &) = delete;
128
129
  //===--------------------------------------------------------------------===//
130
  // Misc. lexing methods.
131
132
  /// After the preprocessor has parsed a \#include, lex and
133
  /// (potentially) macro expand the filename.
134
  ///
135
  /// If the sequence parsed is not lexically legal, emit a diagnostic and
136
  /// return a result EOD token.
137
  void LexIncludeFilename(Token &FilenameTok);
138
139
  /// Inform the lexer whether or not we are currently lexing a
140
  /// preprocessor directive.
141
32.7k
  void setParsingPreprocessorDirective(bool f) {
142
32.7k
    ParsingPreprocessorDirective = f;
143
32.7k
  }
144
145
  /// Return true if this lexer is in raw mode or not.
146
1.39G
  bool isLexingRawMode() const { return LexingRawMode; }
147
148
  /// Return the preprocessor object for this lexer.
149
0
  Preprocessor *getPP() const { return PP; }
150
151
2.92M
  FileID getFileID() const {
152
2.92M
    assert(PP &&
153
2.92M
      "PreprocessorLexer::getFileID() should only be used with a Preprocessor");
154
2.92M
    return FID;
155
2.92M
  }
156
157
  /// Number of SLocEntries before lexing the file.
158
466k
  unsigned getInitialNumSLocEntries() const {
159
466k
    return InitialNumSLocEntries;
160
466k
  }
161
162
  /// getFileEntry - Return the FileEntry corresponding to this FileID.  Like
163
  /// getFileID(), this only works for lexers with attached preprocessors.
164
  const FileEntry *getFileEntry() const;
165
166
  /// Iterator that traverses the current stack of preprocessor
167
  /// conditional directives (\#if/\#ifdef/\#ifndef).
168
  using conditional_iterator =
169
      SmallVectorImpl<PPConditionalInfo>::const_iterator;
170
171
0
  conditional_iterator conditional_begin() const {
172
0
    return ConditionalStack.begin();
173
0
  }
174
175
0
  conditional_iterator conditional_end() const {
176
0
    return ConditionalStack.end();
177
0
  }
178
179
28
  void setConditionalLevels(ArrayRef<PPConditionalInfo> CL) {
180
28
    ConditionalStack.clear();
181
28
    ConditionalStack.append(CL.begin(), CL.end());
182
28
  }
183
};
184
185
} // namespace clang
186
187
#endif // LLVM_CLANG_LEX_PREPROCESSORLEXER_H