clang  14.0.0git
PreprocessorLexer.h
Go to the documentation of this file.
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 
18 #include "clang/Lex/HeaderSearch.h"
20 #include "clang/Lex/Token.h"
21 #include "llvm/ADT/ArrayRef.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringMap.h"
24 #include <cassert>
25 
26 namespace clang {
27 
28 class FileEntry;
29 class Preprocessor;
30 
32  virtual void anchor();
33 
34 protected:
35  friend class Preprocessor;
36 
37  // Preprocessor object controlling lexing.
38  Preprocessor *PP = nullptr;
39 
40  /// The SourceManager FileID corresponding to the file being lexed.
41  const FileID FID;
42 
43  /// Number of SLocEntries before lexing the file.
44  unsigned InitialNumSLocEntries = 0;
45 
46  //===--------------------------------------------------------------------===//
47  // Context-specific lexing flags set by the preprocessor.
48  //===--------------------------------------------------------------------===//
49 
50  /// True when parsing \#XXX; turns '\\n' into a tok::eod token.
52 
53  /// True after \#include; turns <xx> or "xxx" into a tok::header_name token.
54  bool ParsingFilename = false;
55 
56  /// True if in raw mode.
57  ///
58  /// Raw mode disables interpretation of tokens and is a far faster mode to
59  /// lex in than non-raw-mode. This flag:
60  /// 1. If EOF of the current lexer is found, the include stack isn't popped.
61  /// 2. Identifier information is not looked up for identifier tokens. As an
62  /// effect of this, implicit macro expansion is naturally disabled.
63  /// 3. "#" tokens at the start of a line are treated as normal tokens, not
64  /// implicitly transformed by the lexer.
65  /// 4. All diagnostic messages are disabled.
66  /// 5. No callbacks are made into the preprocessor.
67  ///
68  /// Note that in raw mode that the PP pointer may be null.
69  bool LexingRawMode = false;
70 
71  /// A state machine that detects the \#ifndef-wrapping a file
72  /// idiom for the multiple-include optimization.
74 
75  /// Information about the set of \#if/\#ifdef/\#ifndef blocks
76  /// we are currently in.
78 
79  struct IncludeInfo {
80  const FileEntry *File;
82  };
83  // A complete history of all the files included by the current file.
84  llvm::StringMap<IncludeInfo> IncludeHistory;
85 
88  virtual ~PreprocessorLexer() = default;
89 
90  virtual void IndirectLex(Token& Result) = 0;
91 
92  /// Return the source location for the next observable location.
93  virtual SourceLocation getSourceLocation() = 0;
94 
95  //===--------------------------------------------------------------------===//
96  // #if directive handling.
97 
98  /// pushConditionalLevel - When we enter a \#if directive, this keeps track of
99  /// what we are currently in for diagnostic emission (e.g. \#if with missing
100  /// \#endif).
101  void pushConditionalLevel(SourceLocation DirectiveStart, bool WasSkipping,
102  bool FoundNonSkip, bool FoundElse) {
104  CI.IfLoc = DirectiveStart;
105  CI.WasSkipping = WasSkipping;
106  CI.FoundNonSkip = FoundNonSkip;
107  CI.FoundElse = FoundElse;
108  ConditionalStack.push_back(CI);
109  }
111  ConditionalStack.push_back(CI);
112  }
113 
114  /// popConditionalLevel - Remove an entry off the top of the conditional
115  /// stack, returning information about it. If the conditional stack is empty,
116  /// this returns true and does not fill in the arguments.
118  if (ConditionalStack.empty())
119  return true;
120  CI = ConditionalStack.pop_back_val();
121  return false;
122  }
123 
124  /// Return the top of the conditional stack.
125  /// \pre This requires that there be a conditional active.
127  assert(!ConditionalStack.empty() && "No conditionals active!");
128  return ConditionalStack.back();
129  }
130 
131  unsigned getConditionalStackDepth() const { return ConditionalStack.size(); }
132 
133 public:
134  PreprocessorLexer(const PreprocessorLexer &) = delete;
135  PreprocessorLexer &operator=(const PreprocessorLexer &) = delete;
136 
137  //===--------------------------------------------------------------------===//
138  // Misc. lexing methods.
139 
140  /// Lex a token, producing a header-name token if possible.
141  void LexIncludeFilename(Token &FilenameTok);
142 
143  /// Inform the lexer whether or not we are currently lexing a
144  /// preprocessor directive.
147  }
148 
149  /// Return true if this lexer is in raw mode or not.
150  bool isLexingRawMode() const { return LexingRawMode; }
151 
152  /// Return the preprocessor object for this lexer.
153  Preprocessor *getPP() const { return PP; }
154 
155  FileID getFileID() const {
156  assert(PP &&
157  "PreprocessorLexer::getFileID() should only be used with a Preprocessor");
158  return FID;
159  }
160 
161  /// Number of SLocEntries before lexing the file.
162  unsigned getInitialNumSLocEntries() const {
163  return InitialNumSLocEntries;
164  }
165 
166  /// getFileEntry - Return the FileEntry corresponding to this FileID. Like
167  /// getFileID(), this only works for lexers with attached preprocessors.
168  const FileEntry *getFileEntry() const;
169 
170  /// Iterator that traverses the current stack of preprocessor
171  /// conditional directives (\#if/\#ifdef/\#ifndef).
172  using conditional_iterator =
174 
176  return ConditionalStack.begin();
177  }
178 
180  return ConditionalStack.end();
181  }
182 
184  ConditionalStack.clear();
185  ConditionalStack.append(CL.begin(), CL.end());
186  }
187 
188  void addInclude(StringRef Filename, const FileEntry &File,
189  SourceLocation Location) {
190  IncludeHistory.insert({Filename, {&File, Location}});
191  }
192 
193  const llvm::StringMap<IncludeInfo> &getIncludeHistory() const {
194  return IncludeHistory;
195  }
196 };
197 
198 } // namespace clang
199 
200 #endif // LLVM_CLANG_LEX_PREPROCESSORLEXER_H
clang::PreprocessorLexer::getConditionalStackDepth
unsigned getConditionalStackDepth() const
Definition: PreprocessorLexer.h:131
clang::PreprocessorLexer::IncludeInfo::File
const FileEntry * File
Definition: PreprocessorLexer.h:80
clang::DeclaratorContext::File
@ File
llvm::SmallVector
Definition: LLVM.h:38
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::PreprocessorLexer
Definition: PreprocessorLexer.h:31
clang::PreprocessorLexer::pushConditionalLevel
void pushConditionalLevel(const PPConditionalInfo &CI)
Definition: PreprocessorLexer.h:110
clang::PreprocessorLexer::ConditionalStack
SmallVector< PPConditionalInfo, 4 > ConditionalStack
Information about the set of #if/#ifdef/#ifndef blocks we are currently in.
Definition: PreprocessorLexer.h:77
clang::PreprocessorLexer::setConditionalLevels
void setConditionalLevels(ArrayRef< PPConditionalInfo > CL)
Definition: PreprocessorLexer.h:183
clang::PreprocessorLexer::IndirectLex
virtual void IndirectLex(Token &Result)=0
Filename
StringRef Filename
Definition: Format.cpp:2333
clang::PreprocessorLexer::MIOpt
MultipleIncludeOpt MIOpt
A state machine that detects the #ifndef-wrapping a file idiom for the multiple-include optimization.
Definition: PreprocessorLexer.h:73
clang::PreprocessorLexer::ParsingFilename
bool ParsingFilename
True after #include; turns <xx> or "xxx" into a tok::header_name token.
Definition: PreprocessorLexer.h:54
clang::PreprocessorLexer::getInitialNumSLocEntries
unsigned getInitialNumSLocEntries() const
Number of SLocEntries before lexing the file.
Definition: PreprocessorLexer.h:162
clang::PreprocessorLexer::conditional_iterator
SmallVectorImpl< PPConditionalInfo >::const_iterator conditional_iterator
Iterator that traverses the current stack of preprocessor conditional directives (#if/#ifdef/#ifndef)...
Definition: PreprocessorLexer.h:173
clang::PreprocessorLexer::pushConditionalLevel
void pushConditionalLevel(SourceLocation DirectiveStart, bool WasSkipping, bool FoundNonSkip, bool FoundElse)
pushConditionalLevel - When we enter a #if directive, this keeps track of what we are currently in fo...
Definition: PreprocessorLexer.h:101
clang::PreprocessorLexer::conditional_begin
conditional_iterator conditional_begin() const
Definition: PreprocessorLexer.h:175
clang::PreprocessorLexer::LexingRawMode
bool LexingRawMode
True if in raw mode.
Definition: PreprocessorLexer.h:69
clang::PreprocessorLexer::getIncludeHistory
const llvm::StringMap< IncludeInfo > & getIncludeHistory() const
Definition: PreprocessorLexer.h:193
clang::Token
Token - This structure provides full information about a lexed token.
Definition: Token.h:34
clang::PPConditionalInfo::FoundElse
bool FoundElse
True if we've seen a #else in this block.
Definition: Token.h:329
clang::MultipleIncludeOpt
Implements the simple state machine that the Lexer class uses to detect files subject to the 'multipl...
Definition: MultipleIncludeOpt.h:28
clang::PreprocessorLexer::LexIncludeFilename
void LexIncludeFilename(Token &FilenameTok)
Lex a token, producing a header-name token if possible.
Definition: PreprocessorLexer.cpp:32
clang::PreprocessorLexer::getPP
Preprocessor * getPP() const
Return the preprocessor object for this lexer.
Definition: PreprocessorLexer.h:153
clang::PreprocessorLexer::~PreprocessorLexer
virtual ~PreprocessorLexer()=default
MultipleIncludeOpt.h
clang::PreprocessorLexer::IncludeInfo::Location
SourceLocation Location
Definition: PreprocessorLexer.h:81
clang::PreprocessorLexer::addInclude
void addInclude(StringRef Filename, const FileEntry &File, SourceLocation Location)
Definition: PreprocessorLexer.h:188
clang::PreprocessorLexer::peekConditionalLevel
PPConditionalInfo & peekConditionalLevel()
Return the top of the conditional stack.
Definition: PreprocessorLexer.h:126
clang::PreprocessorLexer::IncludeHistory
llvm::StringMap< IncludeInfo > IncludeHistory
Definition: PreprocessorLexer.h:84
clang::PPConditionalInfo
Information about the conditional stack (#if directives) currently active.
Definition: Token.h:315
clang::PreprocessorLexer::ParsingPreprocessorDirective
bool ParsingPreprocessorDirective
True when parsing #XXX; turns '\n' into a tok::eod token.
Definition: PreprocessorLexer.h:51
clang::PreprocessorLexer::FID
const FileID FID
The SourceManager FileID corresponding to the file being lexed.
Definition: PreprocessorLexer.h:41
clang::PreprocessorLexer::PP
Preprocessor * PP
Definition: PreprocessorLexer.h:38
clang::PreprocessorLexer::popConditionalLevel
bool popConditionalLevel(PPConditionalInfo &CI)
popConditionalLevel - Remove an entry off the top of the conditional stack, returning information abo...
Definition: PreprocessorLexer.h:117
clang::PreprocessorLexer::isLexingRawMode
bool isLexingRawMode() const
Return true if this lexer is in raw mode or not.
Definition: PreprocessorLexer.h:150
clang::FileEntry
Cached information about one file (either on disk or in the virtual file system).
Definition: FileEntry.h:331
clang::PreprocessorLexer::setParsingPreprocessorDirective
void setParsingPreprocessorDirective(bool f)
Inform the lexer whether or not we are currently lexing a preprocessor directive.
Definition: PreprocessorLexer.h:145
clang::PreprocessorLexer::operator=
PreprocessorLexer & operator=(const PreprocessorLexer &)=delete
SourceLocation.h
clang::PreprocessorLexer::getFileID
FileID getFileID() const
Definition: PreprocessorLexer.h:155
llvm::ArrayRef
Definition: LLVM.h:34
HeaderSearch.h
clang::PPConditionalInfo::WasSkipping
bool WasSkipping
True if this was contained in a skipping directive, e.g., in a "\#if 0" block.
Definition: Token.h:321
clang::PreprocessorLexer::InitialNumSLocEntries
unsigned InitialNumSLocEntries
Number of SLocEntries before lexing the file.
Definition: PreprocessorLexer.h:44
clang::PreprocessorLexer::getSourceLocation
virtual SourceLocation getSourceLocation()=0
Return the source location for the next observable location.
clang
Definition: CalledOnceCheck.h:17
clang::PPConditionalInfo::IfLoc
SourceLocation IfLoc
Location where the conditional started.
Definition: Token.h:317
clang::FileID
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Definition: SourceLocation.h:40
clang::PPConditionalInfo::FoundNonSkip
bool FoundNonSkip
True if we have emitted tokens already, and now we're in an #else block or something.
Definition: Token.h:325
clang::PreprocessorLexer::PreprocessorLexer
PreprocessorLexer()
Definition: PreprocessorLexer.h:86
llvm::SmallVectorImpl
Definition: LLVM.h:39
clang::Preprocessor
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:130
clang::PreprocessorLexer::IncludeInfo
Definition: PreprocessorLexer.h:79
Token.h
clang::PreprocessorLexer::getFileEntry
const FileEntry * getFileEntry() const
getFileEntry - Return the FileEntry corresponding to this FileID.
Definition: PreprocessorLexer.cpp:50
clang::PreprocessorLexer::conditional_end
conditional_iterator conditional_end() const
Definition: PreprocessorLexer.h:179