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