10b57cec5SDimitry Andric //===--- MacroPPCallbacks.h -------------------------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file defines implementation for the macro preprocessors callbacks. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #ifndef LLVM_CLANG_LIB_CODEGEN_MACROPPCALLBACKS_H 140b57cec5SDimitry Andric #define LLVM_CLANG_LIB_CODEGEN_MACROPPCALLBACKS_H 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric #include "clang/Lex/PPCallbacks.h" 170b57cec5SDimitry Andric 180b57cec5SDimitry Andric namespace llvm { 190b57cec5SDimitry Andric class DIMacroFile; 200b57cec5SDimitry Andric } 210b57cec5SDimitry Andric namespace clang { 220b57cec5SDimitry Andric class Preprocessor; 230b57cec5SDimitry Andric class MacroInfo; 240b57cec5SDimitry Andric class CodeGenerator; 250b57cec5SDimitry Andric 260b57cec5SDimitry Andric class MacroPPCallbacks : public PPCallbacks { 270b57cec5SDimitry Andric /// A pointer to code generator, where debug info generator can be found. 280b57cec5SDimitry Andric CodeGenerator *Gen; 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric /// Preprocessor. 310b57cec5SDimitry Andric Preprocessor &PP; 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric /// Location of recent included file, used for line number. 340b57cec5SDimitry Andric SourceLocation LastHashLoc; 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric /// Counts current number of command line included files, which were entered 370b57cec5SDimitry Andric /// and were not exited yet. 380b57cec5SDimitry Andric int EnteredCommandLineIncludeFiles = 0; 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric enum FileScopeStatus { 410b57cec5SDimitry Andric NoScope = 0, // Scope is not initialized yet. 420b57cec5SDimitry Andric InitializedScope, // Main file scope is initialized but not set yet. 430b57cec5SDimitry Andric BuiltinScope, // <built-in> and <command line> file scopes. 440b57cec5SDimitry Andric CommandLineIncludeScope, // Included file, from <command line> file, scope. 450b57cec5SDimitry Andric MainFileScope // Main file scope. 460b57cec5SDimitry Andric }; 470b57cec5SDimitry Andric FileScopeStatus Status; 480b57cec5SDimitry Andric 490b57cec5SDimitry Andric /// Parent contains all entered files that were not exited yet according to 500b57cec5SDimitry Andric /// the inclusion order. 510b57cec5SDimitry Andric llvm::SmallVector<llvm::DIMacroFile *, 4> Scopes; 520b57cec5SDimitry Andric 530b57cec5SDimitry Andric /// Get current DIMacroFile scope. 540b57cec5SDimitry Andric /// \return current DIMacroFile scope or nullptr if there is no such scope. 550b57cec5SDimitry Andric llvm::DIMacroFile *getCurrentScope(); 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric /// Get current line location or invalid location. 580b57cec5SDimitry Andric /// \param Loc current line location. 590b57cec5SDimitry Andric /// \return current line location \p `Loc`, or invalid location if it's in a 600b57cec5SDimitry Andric /// skipped file scope. 610b57cec5SDimitry Andric SourceLocation getCorrectLocation(SourceLocation Loc); 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric /// Use the passed preprocessor to write the macro name and value from the 640b57cec5SDimitry Andric /// given macro info and identifier info into the given \p `Name` and \p 650b57cec5SDimitry Andric /// `Value` output streams. 660b57cec5SDimitry Andric /// 670b57cec5SDimitry Andric /// \param II Identifier info, used to get the Macro name. 680b57cec5SDimitry Andric /// \param MI Macro info, used to get the Macro argumets and values. 690b57cec5SDimitry Andric /// \param PP Preprocessor. 700b57cec5SDimitry Andric /// \param [out] Name Place holder for returned macro name and arguments. 710b57cec5SDimitry Andric /// \param [out] Value Place holder for returned macro value. 720b57cec5SDimitry Andric static void writeMacroDefinition(const IdentifierInfo &II, 730b57cec5SDimitry Andric const MacroInfo &MI, Preprocessor &PP, 740b57cec5SDimitry Andric raw_ostream &Name, raw_ostream &Value); 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric /// Update current file scope status to next file scope. 770b57cec5SDimitry Andric void updateStatusToNextScope(); 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric /// Handle the case when entering a file. 800b57cec5SDimitry Andric /// 810b57cec5SDimitry Andric /// \param Loc Indicates the new location. 820b57cec5SDimitry Andric void FileEntered(SourceLocation Loc); 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric /// Handle the case when exiting a file. 850b57cec5SDimitry Andric /// 860b57cec5SDimitry Andric /// \param Loc Indicates the new location. 870b57cec5SDimitry Andric void FileExited(SourceLocation Loc); 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric public: 900b57cec5SDimitry Andric MacroPPCallbacks(CodeGenerator *Gen, Preprocessor &PP); 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric /// Callback invoked whenever a source file is entered or exited. 930b57cec5SDimitry Andric /// 940b57cec5SDimitry Andric /// \param Loc Indicates the new location. 950b57cec5SDimitry Andric /// \param PrevFID the file that was exited if \p Reason is ExitFile. 960b57cec5SDimitry Andric void FileChanged(SourceLocation Loc, FileChangeReason Reason, 970b57cec5SDimitry Andric SrcMgr::CharacteristicKind FileType, 980b57cec5SDimitry Andric FileID PrevFID = FileID()) override; 990b57cec5SDimitry Andric 1000b57cec5SDimitry Andric /// Callback invoked whenever a directive (#xxx) is processed. 1010b57cec5SDimitry Andric void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, 1020b57cec5SDimitry Andric StringRef FileName, bool IsAngled, 10381ad6265SDimitry Andric CharSourceRange FilenameRange, 104bdd1243dSDimitry Andric OptionalFileEntryRef File, StringRef SearchPath, 105*0fca6ea1SDimitry Andric StringRef RelativePath, const Module *SuggestedModule, 106*0fca6ea1SDimitry Andric bool ModuleImported, 1070b57cec5SDimitry Andric SrcMgr::CharacteristicKind FileType) override; 1080b57cec5SDimitry Andric 1090b57cec5SDimitry Andric /// Hook called whenever a macro definition is seen. 1100b57cec5SDimitry Andric void MacroDefined(const Token &MacroNameTok, 1110b57cec5SDimitry Andric const MacroDirective *MD) override; 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric /// Hook called whenever a macro \#undef is seen. 1140b57cec5SDimitry Andric /// 1150b57cec5SDimitry Andric /// MD is released immediately following this callback. 1160b57cec5SDimitry Andric void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD, 1170b57cec5SDimitry Andric const MacroDirective *Undef) override; 1180b57cec5SDimitry Andric }; 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric } // end namespace clang 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric #endif 123