1 //===--- MacroPPCallbacks.h -------------------------------------*- 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 //===----------------------------------------------------------------------===//
9 // This file defines implementation for the macro preprocessors callbacks.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_LIB_CODEGEN_MACROPPCALLBACKS_H
14 #define LLVM_CLANG_LIB_CODEGEN_MACROPPCALLBACKS_H
16 #include "clang/Lex/PPCallbacks.h"
26 class MacroPPCallbacks
: public PPCallbacks
{
27 /// A pointer to code generator, where debug info generator can be found.
33 /// Location of recent included file, used for line number.
34 SourceLocation LastHashLoc
;
36 /// Counts current number of command line included files, which were entered
37 /// and were not exited yet.
38 int EnteredCommandLineIncludeFiles
= 0;
40 enum FileScopeStatus
{
41 NoScope
= 0, // Scope is not initialized yet.
42 InitializedScope
, // Main file scope is initialized but not set yet.
43 BuiltinScope
, // <built-in> and <command line> file scopes.
44 CommandLineIncludeScope
, // Included file, from <command line> file, scope.
45 MainFileScope
// Main file scope.
47 FileScopeStatus Status
;
49 /// Parent contains all entered files that were not exited yet according to
50 /// the inclusion order.
51 llvm::SmallVector
<llvm::DIMacroFile
*, 4> Scopes
;
53 /// Get current DIMacroFile scope.
54 /// \return current DIMacroFile scope or nullptr if there is no such scope.
55 llvm::DIMacroFile
*getCurrentScope();
57 /// Get current line location or invalid location.
58 /// \param Loc current line location.
59 /// \return current line location \p `Loc`, or invalid location if it's in a
60 /// skipped file scope.
61 SourceLocation
getCorrectLocation(SourceLocation Loc
);
63 /// Use the passed preprocessor to write the macro name and value from the
64 /// given macro info and identifier info into the given \p `Name` and \p
65 /// `Value` output streams.
67 /// \param II Identifier info, used to get the Macro name.
68 /// \param MI Macro info, used to get the Macro argumets and values.
69 /// \param PP Preprocessor.
70 /// \param [out] Name Place holder for returned macro name and arguments.
71 /// \param [out] Value Place holder for returned macro value.
72 static void writeMacroDefinition(const IdentifierInfo
&II
,
73 const MacroInfo
&MI
, Preprocessor
&PP
,
74 raw_ostream
&Name
, raw_ostream
&Value
);
76 /// Update current file scope status to next file scope.
77 void updateStatusToNextScope();
79 /// Handle the case when entering a file.
81 /// \param Loc Indicates the new location.
82 void FileEntered(SourceLocation Loc
);
84 /// Handle the case when exiting a file.
86 /// \param Loc Indicates the new location.
87 void FileExited(SourceLocation Loc
);
90 MacroPPCallbacks(CodeGenerator
*Gen
, Preprocessor
&PP
);
92 /// Callback invoked whenever a source file is entered or exited.
94 /// \param Loc Indicates the new location.
95 /// \param PrevFID the file that was exited if \p Reason is ExitFile.
96 void FileChanged(SourceLocation Loc
, FileChangeReason Reason
,
97 SrcMgr::CharacteristicKind FileType
,
98 FileID PrevFID
= FileID()) override
;
100 /// Callback invoked whenever a directive (#xxx) is processed.
101 void InclusionDirective(SourceLocation HashLoc
, const Token
&IncludeTok
,
102 StringRef FileName
, bool IsAngled
,
103 CharSourceRange FilenameRange
,
104 OptionalFileEntryRef File
, StringRef SearchPath
,
105 StringRef RelativePath
, const Module
*SuggestedModule
,
107 SrcMgr::CharacteristicKind FileType
) override
;
109 /// Hook called whenever a macro definition is seen.
110 void MacroDefined(const Token
&MacroNameTok
,
111 const MacroDirective
*MD
) override
;
113 /// Hook called whenever a macro \#undef is seen.
115 /// MD is released immediately following this callback.
116 void MacroUndefined(const Token
&MacroNameTok
, const MacroDefinition
&MD
,
117 const MacroDirective
*Undef
) override
;
120 } // end namespace clang