1 //===- PreprocessorLexer.h - C Language Family Lexer ------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// Defines the PreprocessorLexer interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LEX_PREPROCESSORLEXER_H
15 #define LLVM_CLANG_LEX_PREPROCESSORLEXER_H
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"
29 class PreprocessorLexer {
30 virtual void anchor();
33 friend class Preprocessor;
35 // Preprocessor object controlling lexing.
36 Preprocessor *PP = nullptr;
38 /// The SourceManager FileID corresponding to the file being lexed.
41 /// Number of SLocEntries before lexing the file.
42 unsigned InitialNumSLocEntries = 0;
44 //===--------------------------------------------------------------------===//
45 // Context-specific lexing flags set by the preprocessor.
46 //===--------------------------------------------------------------------===//
48 /// True when parsing \#XXX; turns '\\n' into a tok::eod token.
49 bool ParsingPreprocessorDirective = false;
51 /// True after \#include; turns \<xx> or "xxx" into a tok::header_name token.
52 bool ParsingFilename = false;
54 /// True if in raw mode.
56 /// Raw mode disables interpretation of tokens and is a far faster mode to
57 /// lex in than non-raw-mode. This flag:
58 /// 1. If EOF of the current lexer is found, the include stack isn't popped.
59 /// 2. Identifier information is not looked up for identifier tokens. As an
60 /// effect of this, implicit macro expansion is naturally disabled.
61 /// 3. "#" tokens at the start of a line are treated as normal tokens, not
62 /// implicitly transformed by the lexer.
63 /// 4. All diagnostic messages are disabled.
64 /// 5. No callbacks are made into the preprocessor.
66 /// Note that in raw mode that the PP pointer may be null.
67 bool LexingRawMode = false;
69 /// A state machine that detects the \#ifndef-wrapping a file
70 /// idiom for the multiple-include optimization.
71 MultipleIncludeOpt MIOpt;
73 /// Information about the set of \#if/\#ifdef/\#ifndef blocks
74 /// we are currently in.
75 SmallVector<PPConditionalInfo, 4> ConditionalStack;
77 PreprocessorLexer() : FID() {}
78 PreprocessorLexer(Preprocessor *pp, FileID fid);
79 virtual ~PreprocessorLexer() = default;
81 virtual void IndirectLex(Token& Result) = 0;
83 /// Return the source location for the next observable location.
84 virtual SourceLocation getSourceLocation() = 0;
86 //===--------------------------------------------------------------------===//
87 // #if directive handling.
89 /// pushConditionalLevel - When we enter a \#if directive, this keeps track of
90 /// what we are currently in for diagnostic emission (e.g. \#if with missing
92 void pushConditionalLevel(SourceLocation DirectiveStart, bool WasSkipping,
93 bool FoundNonSkip, bool FoundElse) {
95 CI.IfLoc = DirectiveStart;
96 CI.WasSkipping = WasSkipping;
97 CI.FoundNonSkip = FoundNonSkip;
98 CI.FoundElse = FoundElse;
99 ConditionalStack.push_back(CI);
101 void pushConditionalLevel(const PPConditionalInfo &CI) {
102 ConditionalStack.push_back(CI);
105 /// popConditionalLevel - Remove an entry off the top of the conditional
106 /// stack, returning information about it. If the conditional stack is empty,
107 /// this returns true and does not fill in the arguments.
108 bool popConditionalLevel(PPConditionalInfo &CI) {
109 if (ConditionalStack.empty())
111 CI = ConditionalStack.pop_back_val();
115 /// Return the top of the conditional stack.
116 /// \pre This requires that there be a conditional active.
117 PPConditionalInfo &peekConditionalLevel() {
118 assert(!ConditionalStack.empty() && "No conditionals active!");
119 return ConditionalStack.back();
122 unsigned getConditionalStackDepth() const { return ConditionalStack.size(); }
125 PreprocessorLexer(const PreprocessorLexer &) = delete;
126 PreprocessorLexer &operator=(const PreprocessorLexer &) = delete;
128 //===--------------------------------------------------------------------===//
129 // Misc. lexing methods.
131 /// Lex a token, producing a header-name token if possible.
132 void LexIncludeFilename(Token &FilenameTok);
134 /// Inform the lexer whether or not we are currently lexing a
135 /// preprocessor directive.
136 void setParsingPreprocessorDirective(bool f) {
137 ParsingPreprocessorDirective = f;
140 /// Return true if this lexer is in raw mode or not.
141 bool isLexingRawMode() const { return LexingRawMode; }
143 /// Return the preprocessor object for this lexer.
144 Preprocessor *getPP() const { return PP; }
146 FileID getFileID() const {
148 "PreprocessorLexer::getFileID() should only be used with a Preprocessor");
152 /// Number of SLocEntries before lexing the file.
153 unsigned getInitialNumSLocEntries() const {
154 return InitialNumSLocEntries;
157 /// getFileEntry - Return the FileEntry corresponding to this FileID. Like
158 /// getFileID(), this only works for lexers with attached preprocessors.
159 const FileEntry *getFileEntry() const;
161 /// Iterator that traverses the current stack of preprocessor
162 /// conditional directives (\#if/\#ifdef/\#ifndef).
163 using conditional_iterator =
164 SmallVectorImpl<PPConditionalInfo>::const_iterator;
166 conditional_iterator conditional_begin() const {
167 return ConditionalStack.begin();
170 conditional_iterator conditional_end() const {
171 return ConditionalStack.end();
174 void setConditionalLevels(ArrayRef<PPConditionalInfo> CL) {
175 ConditionalStack.clear();
176 ConditionalStack.append(CL.begin(), CL.end());
182 #endif // LLVM_CLANG_LEX_PREPROCESSORLEXER_H