1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
#pragma once
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif
//===- PreprocessorLexer.h - C Language Family Lexer ------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
/// \file
/// Defines the PreprocessorLexer interface.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_LEX_PREPROCESSORLEXER_H
#define LLVM_CLANG_LEX_PREPROCESSORLEXER_H
#include "clang/Basic/SourceLocation.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Lex/MultipleIncludeOpt.h"
#include "clang/Lex/Token.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
#include <cassert>
namespace clang {
class FileEntry;
class Preprocessor;
class PreprocessorLexer {
virtual void anchor();
protected:
friend class Preprocessor;
// Preprocessor object controlling lexing.
Preprocessor *PP = nullptr;
/// The SourceManager FileID corresponding to the file being lexed.
const FileID FID;
/// Number of SLocEntries before lexing the file.
unsigned InitialNumSLocEntries = 0;
//===--------------------------------------------------------------------===//
// Context-specific lexing flags set by the preprocessor.
//===--------------------------------------------------------------------===//
/// True when parsing \#XXX; turns '\\n' into a tok::eod token.
bool ParsingPreprocessorDirective = false;
/// True after \#include; turns \<xx> or "xxx" into a tok::header_name token.
bool ParsingFilename = false;
/// True if in raw mode.
///
/// Raw mode disables interpretation of tokens and is a far faster mode to
/// lex in than non-raw-mode. This flag:
/// 1. If EOF of the current lexer is found, the include stack isn't popped.
/// 2. Identifier information is not looked up for identifier tokens. As an
/// effect of this, implicit macro expansion is naturally disabled.
/// 3. "#" tokens at the start of a line are treated as normal tokens, not
/// implicitly transformed by the lexer.
/// 4. All diagnostic messages are disabled.
/// 5. No callbacks are made into the preprocessor.
///
/// Note that in raw mode that the PP pointer may be null.
bool LexingRawMode = false;
/// A state machine that detects the \#ifndef-wrapping a file
/// idiom for the multiple-include optimization.
MultipleIncludeOpt MIOpt;
/// Information about the set of \#if/\#ifdef/\#ifndef blocks
/// we are currently in.
SmallVector<PPConditionalInfo, 4> ConditionalStack;
struct IncludeInfo {
const FileEntry *File;
SourceLocation Location;
};
// A complete history of all the files included by the current file.
llvm::StringMap<IncludeInfo> IncludeHistory;
PreprocessorLexer() : FID() {}
PreprocessorLexer(Preprocessor *pp, FileID fid);
virtual ~PreprocessorLexer() = default;
virtual void IndirectLex(Token& Result) = 0;
/// Return the source location for the next observable location.
virtual SourceLocation getSourceLocation() = 0;
//===--------------------------------------------------------------------===//
// #if directive handling.
/// pushConditionalLevel - When we enter a \#if directive, this keeps track of
/// what we are currently in for diagnostic emission (e.g. \#if with missing
/// \#endif).
void pushConditionalLevel(SourceLocation DirectiveStart, bool WasSkipping,
bool FoundNonSkip, bool FoundElse) {
PPConditionalInfo CI;
CI.IfLoc = DirectiveStart;
CI.WasSkipping = WasSkipping;
CI.FoundNonSkip = FoundNonSkip;
CI.FoundElse = FoundElse;
ConditionalStack.push_back(CI);
}
void pushConditionalLevel(const PPConditionalInfo &CI) {
ConditionalStack.push_back(CI);
}
/// popConditionalLevel - Remove an entry off the top of the conditional
/// stack, returning information about it. If the conditional stack is empty,
/// this returns true and does not fill in the arguments.
bool popConditionalLevel(PPConditionalInfo &CI) {
if (ConditionalStack.empty())
return true;
CI = ConditionalStack.pop_back_val();
return false;
}
/// Return the top of the conditional stack.
/// \pre This requires that there be a conditional active.
PPConditionalInfo &peekConditionalLevel() {
assert(!ConditionalStack.empty() && "No conditionals active!");
return ConditionalStack.back();
}
unsigned getConditionalStackDepth() const { return ConditionalStack.size(); }
public:
PreprocessorLexer(const PreprocessorLexer &) = delete;
PreprocessorLexer &operator=(const PreprocessorLexer &) = delete;
//===--------------------------------------------------------------------===//
// Misc. lexing methods.
/// Lex a token, producing a header-name token if possible.
void LexIncludeFilename(Token &FilenameTok);
/// Inform the lexer whether or not we are currently lexing a
/// preprocessor directive.
void setParsingPreprocessorDirective(bool f) {
ParsingPreprocessorDirective = f;
}
/// Return true if this lexer is in raw mode or not.
bool isLexingRawMode() const { return LexingRawMode; }
/// Return the preprocessor object for this lexer.
Preprocessor *getPP() const { return PP; }
FileID getFileID() const {
assert(PP &&
"PreprocessorLexer::getFileID() should only be used with a Preprocessor");
return FID;
}
/// Number of SLocEntries before lexing the file.
unsigned getInitialNumSLocEntries() const {
return InitialNumSLocEntries;
}
/// getFileEntry - Return the FileEntry corresponding to this FileID. Like
/// getFileID(), this only works for lexers with attached preprocessors.
const FileEntry *getFileEntry() const;
/// Iterator that traverses the current stack of preprocessor
/// conditional directives (\#if/\#ifdef/\#ifndef).
using conditional_iterator =
SmallVectorImpl<PPConditionalInfo>::const_iterator;
conditional_iterator conditional_begin() const {
return ConditionalStack.begin();
}
conditional_iterator conditional_end() const {
return ConditionalStack.end();
}
void setConditionalLevels(ArrayRef<PPConditionalInfo> CL) {
ConditionalStack.clear();
ConditionalStack.append(CL.begin(), CL.end());
}
void addInclude(StringRef Filename, const FileEntry &File,
SourceLocation Location) {
IncludeHistory.insert({Filename, {&File, Location}});
}
const llvm::StringMap<IncludeInfo> &getIncludeHistory() const {
return IncludeHistory;
}
};
} // namespace clang
#endif // LLVM_CLANG_LEX_PREPROCESSORLEXER_H
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
|