xref: /openbsd-src/gnu/llvm/clang/lib/CodeGen/MacroPPCallbacks.h (revision 12c855180aad702bbcca06e0398d774beeafb155)
1e5dd7070Spatrick //===--- MacroPPCallbacks.h -------------------------------------*- C++ -*-===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick //  This file defines implementation for the macro preprocessors callbacks.
10e5dd7070Spatrick //
11e5dd7070Spatrick //===----------------------------------------------------------------------===//
12e5dd7070Spatrick 
13e5dd7070Spatrick #ifndef LLVM_CLANG_LIB_CODEGEN_MACROPPCALLBACKS_H
14e5dd7070Spatrick #define LLVM_CLANG_LIB_CODEGEN_MACROPPCALLBACKS_H
15e5dd7070Spatrick 
16e5dd7070Spatrick #include "clang/Lex/PPCallbacks.h"
17e5dd7070Spatrick 
18e5dd7070Spatrick namespace llvm {
19e5dd7070Spatrick class DIMacroFile;
20e5dd7070Spatrick }
21e5dd7070Spatrick namespace clang {
22e5dd7070Spatrick class Preprocessor;
23e5dd7070Spatrick class MacroInfo;
24e5dd7070Spatrick class CodeGenerator;
25e5dd7070Spatrick 
26e5dd7070Spatrick class MacroPPCallbacks : public PPCallbacks {
27e5dd7070Spatrick   /// A pointer to code generator, where debug info generator can be found.
28e5dd7070Spatrick   CodeGenerator *Gen;
29e5dd7070Spatrick 
30e5dd7070Spatrick   /// Preprocessor.
31e5dd7070Spatrick   Preprocessor &PP;
32e5dd7070Spatrick 
33e5dd7070Spatrick   /// Location of recent included file, used for line number.
34e5dd7070Spatrick   SourceLocation LastHashLoc;
35e5dd7070Spatrick 
36e5dd7070Spatrick   /// Counts current number of command line included files, which were entered
37e5dd7070Spatrick   /// and were not exited yet.
38e5dd7070Spatrick   int EnteredCommandLineIncludeFiles = 0;
39e5dd7070Spatrick 
40e5dd7070Spatrick   enum FileScopeStatus {
41e5dd7070Spatrick     NoScope = 0,              // Scope is not initialized yet.
42e5dd7070Spatrick     InitializedScope,         // Main file scope is initialized but not set yet.
43e5dd7070Spatrick     BuiltinScope,             // <built-in> and <command line> file scopes.
44e5dd7070Spatrick     CommandLineIncludeScope,  // Included file, from <command line> file, scope.
45e5dd7070Spatrick     MainFileScope             // Main file scope.
46e5dd7070Spatrick   };
47e5dd7070Spatrick   FileScopeStatus Status;
48e5dd7070Spatrick 
49e5dd7070Spatrick   /// Parent contains all entered files that were not exited yet according to
50e5dd7070Spatrick   /// the inclusion order.
51e5dd7070Spatrick   llvm::SmallVector<llvm::DIMacroFile *, 4> Scopes;
52e5dd7070Spatrick 
53e5dd7070Spatrick   /// Get current DIMacroFile scope.
54e5dd7070Spatrick   /// \return current DIMacroFile scope or nullptr if there is no such scope.
55e5dd7070Spatrick   llvm::DIMacroFile *getCurrentScope();
56e5dd7070Spatrick 
57e5dd7070Spatrick   /// Get current line location or invalid location.
58e5dd7070Spatrick   /// \param Loc current line location.
59e5dd7070Spatrick   /// \return current line location \p `Loc`, or invalid location if it's in a
60e5dd7070Spatrick   ///         skipped file scope.
61e5dd7070Spatrick   SourceLocation getCorrectLocation(SourceLocation Loc);
62e5dd7070Spatrick 
63e5dd7070Spatrick   /// Use the passed preprocessor to write the macro name and value from the
64e5dd7070Spatrick   /// given macro info and identifier info into the given \p `Name` and \p
65e5dd7070Spatrick   /// `Value` output streams.
66e5dd7070Spatrick   ///
67e5dd7070Spatrick   /// \param II Identifier info, used to get the Macro name.
68e5dd7070Spatrick   /// \param MI Macro info, used to get the Macro argumets and values.
69e5dd7070Spatrick   /// \param PP Preprocessor.
70e5dd7070Spatrick   /// \param [out] Name Place holder for returned macro name and arguments.
71e5dd7070Spatrick   /// \param [out] Value Place holder for returned macro value.
72e5dd7070Spatrick   static void writeMacroDefinition(const IdentifierInfo &II,
73e5dd7070Spatrick                                    const MacroInfo &MI, Preprocessor &PP,
74e5dd7070Spatrick                                    raw_ostream &Name, raw_ostream &Value);
75e5dd7070Spatrick 
76e5dd7070Spatrick   /// Update current file scope status to next file scope.
77e5dd7070Spatrick   void updateStatusToNextScope();
78e5dd7070Spatrick 
79e5dd7070Spatrick   /// Handle the case when entering a file.
80e5dd7070Spatrick   ///
81e5dd7070Spatrick   /// \param Loc Indicates the new location.
82e5dd7070Spatrick   void FileEntered(SourceLocation Loc);
83e5dd7070Spatrick 
84e5dd7070Spatrick   /// Handle the case when exiting a file.
85e5dd7070Spatrick   ///
86e5dd7070Spatrick   /// \param Loc Indicates the new location.
87e5dd7070Spatrick   void FileExited(SourceLocation Loc);
88e5dd7070Spatrick 
89e5dd7070Spatrick public:
90e5dd7070Spatrick   MacroPPCallbacks(CodeGenerator *Gen, Preprocessor &PP);
91e5dd7070Spatrick 
92e5dd7070Spatrick   /// Callback invoked whenever a source file is entered or exited.
93e5dd7070Spatrick   ///
94e5dd7070Spatrick   /// \param Loc Indicates the new location.
95e5dd7070Spatrick   /// \param PrevFID the file that was exited if \p Reason is ExitFile.
96e5dd7070Spatrick   void FileChanged(SourceLocation Loc, FileChangeReason Reason,
97e5dd7070Spatrick                    SrcMgr::CharacteristicKind FileType,
98e5dd7070Spatrick                    FileID PrevFID = FileID()) override;
99e5dd7070Spatrick 
100e5dd7070Spatrick   /// Callback invoked whenever a directive (#xxx) is processed.
101e5dd7070Spatrick   void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
102e5dd7070Spatrick                           StringRef FileName, bool IsAngled,
103*12c85518Srobert                           CharSourceRange FilenameRange,
104*12c85518Srobert                           OptionalFileEntryRef File, StringRef SearchPath,
105*12c85518Srobert                           StringRef RelativePath, const Module *Imported,
106e5dd7070Spatrick                           SrcMgr::CharacteristicKind FileType) override;
107e5dd7070Spatrick 
108e5dd7070Spatrick   /// Hook called whenever a macro definition is seen.
109e5dd7070Spatrick   void MacroDefined(const Token &MacroNameTok,
110e5dd7070Spatrick                     const MacroDirective *MD) override;
111e5dd7070Spatrick 
112e5dd7070Spatrick   /// Hook called whenever a macro \#undef is seen.
113e5dd7070Spatrick   ///
114e5dd7070Spatrick   /// MD is released immediately following this callback.
115e5dd7070Spatrick   void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD,
116e5dd7070Spatrick                       const MacroDirective *Undef) override;
117e5dd7070Spatrick };
118e5dd7070Spatrick 
119e5dd7070Spatrick } // end namespace clang
120e5dd7070Spatrick 
121e5dd7070Spatrick #endif
122