Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:58

0001 //===- PreprocessorLexer.h - C Language Family Lexer ------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 /// \file
0010 /// Defines the PreprocessorLexer interface.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_CLANG_LEX_PREPROCESSORLEXER_H
0015 #define LLVM_CLANG_LEX_PREPROCESSORLEXER_H
0016 
0017 #include "clang/Basic/FileEntry.h"
0018 #include "clang/Basic/SourceLocation.h"
0019 #include "clang/Lex/MultipleIncludeOpt.h"
0020 #include "clang/Lex/Token.h"
0021 #include "llvm/ADT/ArrayRef.h"
0022 #include "llvm/ADT/SmallVector.h"
0023 #include <cassert>
0024 
0025 namespace clang {
0026 
0027 class FileEntry;
0028 class Preprocessor;
0029 
0030 class PreprocessorLexer {
0031   virtual void anchor();
0032 
0033 protected:
0034   friend class Preprocessor;
0035 
0036   // Preprocessor object controlling lexing.
0037   Preprocessor *PP = nullptr;
0038 
0039   /// The SourceManager FileID corresponding to the file being lexed.
0040   const FileID FID;
0041 
0042   /// Number of SLocEntries before lexing the file.
0043   unsigned InitialNumSLocEntries = 0;
0044 
0045   //===--------------------------------------------------------------------===//
0046   // Context-specific lexing flags set by the preprocessor.
0047   //===--------------------------------------------------------------------===//
0048 
0049   /// True when parsing \#XXX; turns '\\n' into a tok::eod token.
0050   bool ParsingPreprocessorDirective = false;
0051 
0052   /// True after \#include; turns \<xx> or "xxx" into a tok::header_name token.
0053   bool ParsingFilename = false;
0054 
0055   /// True if in raw mode.
0056   ///
0057   /// Raw mode disables interpretation of tokens and is a far faster mode to
0058   /// lex in than non-raw-mode.  This flag:
0059   ///  1. If EOF of the current lexer is found, the include stack isn't popped.
0060   ///  2. Identifier information is not looked up for identifier tokens.  As an
0061   ///     effect of this, implicit macro expansion is naturally disabled.
0062   ///  3. "#" tokens at the start of a line are treated as normal tokens, not
0063   ///     implicitly transformed by the lexer.
0064   ///  4. All diagnostic messages are disabled.
0065   ///  5. No callbacks are made into the preprocessor.
0066   ///
0067   /// Note that in raw mode that the PP pointer may be null.
0068   bool LexingRawMode = false;
0069 
0070   /// A state machine that detects the \#ifndef-wrapping a file
0071   /// idiom for the multiple-include optimization.
0072   MultipleIncludeOpt MIOpt;
0073 
0074   /// Information about the set of \#if/\#ifdef/\#ifndef blocks
0075   /// we are currently in.
0076   SmallVector<PPConditionalInfo, 4> ConditionalStack;
0077 
0078   PreprocessorLexer() : FID() {}
0079   PreprocessorLexer(Preprocessor *pp, FileID fid);
0080   virtual ~PreprocessorLexer() = default;
0081 
0082   virtual void IndirectLex(Token& Result) = 0;
0083 
0084   /// Return the source location for the next observable location.
0085   virtual SourceLocation getSourceLocation() = 0;
0086 
0087   //===--------------------------------------------------------------------===//
0088   // #if directive handling.
0089 
0090   /// pushConditionalLevel - When we enter a \#if directive, this keeps track of
0091   /// what we are currently in for diagnostic emission (e.g. \#if with missing
0092   /// \#endif).
0093   void pushConditionalLevel(SourceLocation DirectiveStart, bool WasSkipping,
0094                             bool FoundNonSkip, bool FoundElse) {
0095     PPConditionalInfo CI;
0096     CI.IfLoc = DirectiveStart;
0097     CI.WasSkipping = WasSkipping;
0098     CI.FoundNonSkip = FoundNonSkip;
0099     CI.FoundElse = FoundElse;
0100     ConditionalStack.push_back(CI);
0101   }
0102   void pushConditionalLevel(const PPConditionalInfo &CI) {
0103     ConditionalStack.push_back(CI);
0104   }
0105 
0106   /// popConditionalLevel - Remove an entry off the top of the conditional
0107   /// stack, returning information about it.  If the conditional stack is empty,
0108   /// this returns true and does not fill in the arguments.
0109   bool popConditionalLevel(PPConditionalInfo &CI) {
0110     if (ConditionalStack.empty())
0111       return true;
0112     CI = ConditionalStack.pop_back_val();
0113     return false;
0114   }
0115 
0116   /// Return the top of the conditional stack.
0117   /// \pre This requires that there be a conditional active.
0118   PPConditionalInfo &peekConditionalLevel() {
0119     assert(!ConditionalStack.empty() && "No conditionals active!");
0120     return ConditionalStack.back();
0121   }
0122 
0123   unsigned getConditionalStackDepth() const { return ConditionalStack.size(); }
0124 
0125 public:
0126   PreprocessorLexer(const PreprocessorLexer &) = delete;
0127   PreprocessorLexer &operator=(const PreprocessorLexer &) = delete;
0128 
0129   //===--------------------------------------------------------------------===//
0130   // Misc. lexing methods.
0131 
0132   /// Lex a token, producing a header-name token if possible.
0133   void LexIncludeFilename(Token &FilenameTok);
0134 
0135   /// Inform the lexer whether or not we are currently lexing a
0136   /// preprocessor directive.
0137   void setParsingPreprocessorDirective(bool f) {
0138     ParsingPreprocessorDirective = f;
0139   }
0140 
0141   /// Return true if this lexer is in raw mode or not.
0142   bool isLexingRawMode() const { return LexingRawMode; }
0143 
0144   /// Return the preprocessor object for this lexer.
0145   Preprocessor *getPP() const { return PP; }
0146 
0147   FileID getFileID() const {
0148     assert(PP &&
0149       "PreprocessorLexer::getFileID() should only be used with a Preprocessor");
0150     return FID;
0151   }
0152 
0153   /// Number of SLocEntries before lexing the file.
0154   unsigned getInitialNumSLocEntries() const {
0155     return InitialNumSLocEntries;
0156   }
0157 
0158   /// getFileEntry - Return the FileEntry corresponding to this FileID.  Like
0159   /// getFileID(), this only works for lexers with attached preprocessors.
0160   OptionalFileEntryRef getFileEntry() const;
0161 
0162   /// Iterator that traverses the current stack of preprocessor
0163   /// conditional directives (\#if/\#ifdef/\#ifndef).
0164   using conditional_iterator =
0165       SmallVectorImpl<PPConditionalInfo>::const_iterator;
0166 
0167   conditional_iterator conditional_begin() const {
0168     return ConditionalStack.begin();
0169   }
0170 
0171   conditional_iterator conditional_end() const {
0172     return ConditionalStack.end();
0173   }
0174 
0175   void setConditionalLevels(ArrayRef<PPConditionalInfo> CL) {
0176     ConditionalStack.clear();
0177     ConditionalStack.append(CL.begin(), CL.end());
0178   }
0179 };
0180 
0181 } // namespace clang
0182 
0183 #endif // LLVM_CLANG_LEX_PREPROCESSORLEXER_H