//===--- PPLexerChange.cpp - Handle changing lexers in the preprocessor ---===// // // 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 // //===----------------------------------------------------------------------===// // // This file implements pieces of the Preprocessor interface that manage the // current lexer stack. // //===----------------------------------------------------------------------===// #include "clang/Basic/FileManager.h" #include "clang/Basic/SourceManager.h" #include "clang/Lex/HeaderSearch.h" #include "clang/Lex/LexDiagnostic.h" #include "clang/Lex/MacroInfo.h" #include "clang/Lex/Preprocessor.h" #include "clang/Lex/PreprocessorOptions.h" #include "llvm/ADT/StringSwitch.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/MemoryBufferRef.h" #include "llvm/Support/Path.h" #include <optional> usingnamespaceclang; //===----------------------------------------------------------------------===// // Miscellaneous Methods. //===----------------------------------------------------------------------===// /// isInPrimaryFile - Return true if we're in the top-level file, not in a /// \#include. This looks through macro expansions and active _Pragma lexers. bool Preprocessor::isInPrimaryFile() const { … } /// getCurrentLexer - Return the current file lexer being lexed from. Note /// that this ignores any potentially active macro expansions and _Pragma /// expansions going on at the time. PreprocessorLexer *Preprocessor::getCurrentFileLexer() const { … } //===----------------------------------------------------------------------===// // Methods for Entering and Callbacks for leaving various contexts //===----------------------------------------------------------------------===// /// EnterSourceFile - Add a source file to the top of the include stack and /// start lexing tokens from it instead of the current buffer. bool Preprocessor::EnterSourceFile(FileID FID, ConstSearchDirIterator CurDir, SourceLocation Loc, bool IsFirstIncludeOfFile) { … } /// EnterSourceFileWithLexer - Add a source file to the top of the include stack /// and start lexing tokens from it instead of the current buffer. void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer, ConstSearchDirIterator CurDir) { … } /// EnterMacro - Add a Macro to the top of the include stack and start lexing /// tokens from it instead of the current buffer. void Preprocessor::EnterMacro(Token &Tok, SourceLocation ILEnd, MacroInfo *Macro, MacroArgs *Args) { … } /// EnterTokenStream - Add a "macro" context to the top of the include stack, /// which will cause the lexer to start returning the specified tokens. /// /// If DisableMacroExpansion is true, tokens lexed from the token stream will /// not be subject to further macro expansion. Otherwise, these tokens will /// be re-macro-expanded when/if expansion is enabled. /// /// If OwnsTokens is false, this method assumes that the specified stream of /// tokens has a permanent owner somewhere, so they do not need to be copied. /// If it is true, it assumes the array of tokens is allocated with new[] and /// must be freed. /// void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks, bool DisableMacroExpansion, bool OwnsTokens, bool IsReinject) { … } /// Compute the relative path that names the given file relative to /// the given directory. static void computeRelativePath(FileManager &FM, const DirectoryEntry *Dir, FileEntryRef File, SmallString<128> &Result) { … } void Preprocessor::PropagateLineStartLeadingSpaceInfo(Token &Result) { … } /// Determine the location to use as the end of the buffer for a lexer. /// /// If the file ends with a newline, form the EOF token on the newline itself, /// rather than "on the line following it", which doesn't exist. This makes /// diagnostics relating to the end of file include the last file that the user /// actually typed, which is goodness. const char *Preprocessor::getCurLexerEndPos() { … } static void collectAllSubModulesWithUmbrellaHeader( const Module &Mod, SmallVectorImpl<const Module *> &SubMods) { … } void Preprocessor::diagnoseMissingHeaderInUmbrellaDir(const Module &Mod) { … } /// HandleEndOfFile - This callback is invoked when the lexer hits the end of /// the current file. This either returns the EOF token or pops a level off /// the include stack and keeps going. bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) { … } /// HandleEndOfTokenLexer - This callback is invoked when the current TokenLexer /// hits the end of its token stream. bool Preprocessor::HandleEndOfTokenLexer(Token &Result) { … } /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the /// lexer stack. This should only be used in situations where the current /// state of the top-of-stack lexer is unknown. void Preprocessor::RemoveTopOfLexerStack() { … } /// HandleMicrosoftCommentPaste - When the macro expander pastes together a /// comment (/##/) in microsoft mode, this method handles updating the current /// state, returning the token on the next source line. void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) { … } void Preprocessor::EnterSubmodule(Module *M, SourceLocation ImportLoc, bool ForPragma) { … } bool Preprocessor::needModuleMacros() const { … } Module *Preprocessor::LeaveSubmodule(bool ForPragma) { … }