[flang] Use object before converts in fir.dispatch (#68589)
[llvm-project.git] / clang-tools-extra / pp-trace / PPCallbacksTracker.h
blobc195a72b08c1aad90dbbd093c6e36c1d7a7a57d5
1 //===--- PPCallbacksTracker.h - Preprocessor tracking -----------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// Classes and definitions for preprocessor tracking.
11 ///
12 /// The core definition is the PPCallbacksTracker class, derived from Clang's
13 /// PPCallbacks class from the Lex library, which overrides all the callbacks
14 /// and collects information about each callback call, saving it in a
15 /// data structure built up of CallbackCall and Argument objects, which
16 /// record the preprocessor callback name and arguments in high-level string
17 /// form for later inspection.
18 ///
19 //===----------------------------------------------------------------------===//
21 #ifndef PPTRACE_PPCALLBACKSTRACKER_H
22 #define PPTRACE_PPCALLBACKSTRACKER_H
24 #include "clang/Lex/PPCallbacks.h"
25 #include "clang/Lex/Preprocessor.h"
26 #include "clang/Basic/SourceManager.h"
27 #include "llvm/ADT/ArrayRef.h"
28 #include "llvm/ADT/SmallSet.h"
29 #include "llvm/ADT/StringMap.h"
30 #include "llvm/ADT/StringRef.h"
31 #include "llvm/Support/GlobPattern.h"
32 #include <string>
33 #include <vector>
35 namespace clang {
36 namespace pp_trace {
38 // This struct represents one callback function argument by name and value.
39 struct Argument {
40 std::string Name;
41 std::string Value;
44 /// This class represents one callback call by name and an array
45 /// of arguments.
46 class CallbackCall {
47 public:
48 CallbackCall(llvm::StringRef Name) : Name(Name) {}
49 CallbackCall() = default;
51 std::string Name;
52 std::vector<Argument> Arguments;
55 using FilterType = std::vector<std::pair<llvm::GlobPattern, bool>>;
57 /// This class overrides the PPCallbacks class for tracking preprocessor
58 /// activity by means of its callback functions.
59 ///
60 /// This object is given a vector for storing the trace information, built up
61 /// of CallbackCall and subordinate Argument objects for representing the
62 /// callback calls and their arguments. It's a reference so the vector can
63 /// exist beyond the lifetime of this object, because it's deleted by the
64 /// preprocessor automatically in its destructor.
65 ///
66 /// This class supports a mechanism for inhibiting trace output for
67 /// specific callbacks by name, for the purpose of eliminating output for
68 /// callbacks of no interest that might clutter the output.
69 ///
70 /// Following the constructor and destructor function declarations, the
71 /// overridden callback functions are defined. The remaining functions are
72 /// helpers for recording the trace data, to reduce the coupling between it
73 /// and the recorded data structure.
74 class PPCallbacksTracker : public PPCallbacks {
75 public:
76 /// Note that all of the arguments are references, and owned
77 /// by the caller.
78 /// \param Filters - List of (Glob,Enabled) pairs used to filter callbacks.
79 /// \param CallbackCalls - Trace buffer.
80 /// \param PP - The preprocessor. Needed for getting some argument strings.
81 PPCallbacksTracker(const FilterType &Filters,
82 std::vector<CallbackCall> &CallbackCalls,
83 Preprocessor &PP);
85 ~PPCallbacksTracker() override;
87 // Overridden callback functions.
89 void FileChanged(SourceLocation Loc, PPCallbacks::FileChangeReason Reason,
90 SrcMgr::CharacteristicKind FileType,
91 FileID PrevFID = FileID()) override;
92 void FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok,
93 SrcMgr::CharacteristicKind FileType) override;
94 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
95 llvm::StringRef FileName, bool IsAngled,
96 CharSourceRange FilenameRange,
97 OptionalFileEntryRef File, llvm::StringRef SearchPath,
98 llvm::StringRef RelativePath, const Module *Imported,
99 SrcMgr::CharacteristicKind FileType) override;
100 void moduleImport(SourceLocation ImportLoc, ModuleIdPath Path,
101 const Module *Imported) override;
102 void EndOfMainFile() override;
103 void Ident(SourceLocation Loc, llvm::StringRef str) override;
104 void PragmaDirective(SourceLocation Loc,
105 PragmaIntroducerKind Introducer) override;
106 void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
107 llvm::StringRef Str) override;
108 void PragmaDetectMismatch(SourceLocation Loc, llvm::StringRef Name,
109 llvm::StringRef Value) override;
110 void PragmaDebug(SourceLocation Loc, llvm::StringRef DebugType) override;
111 void PragmaMessage(SourceLocation Loc, llvm::StringRef Namespace,
112 PPCallbacks::PragmaMessageKind Kind,
113 llvm::StringRef Str) override;
114 void PragmaDiagnosticPush(SourceLocation Loc,
115 llvm::StringRef Namespace) override;
116 void PragmaDiagnosticPop(SourceLocation Loc,
117 llvm::StringRef Namespace) override;
118 void PragmaDiagnostic(SourceLocation Loc, llvm::StringRef Namespace,
119 diag::Severity mapping, llvm::StringRef Str) override;
120 void PragmaOpenCLExtension(SourceLocation NameLoc, const IdentifierInfo *Name,
121 SourceLocation StateLoc, unsigned State) override;
122 void PragmaWarning(SourceLocation Loc, PragmaWarningSpecifier WarningSpec,
123 llvm::ArrayRef<int> Ids) override;
124 void PragmaWarningPush(SourceLocation Loc, int Level) override;
125 void PragmaWarningPop(SourceLocation Loc) override;
126 void PragmaExecCharsetPush(SourceLocation Loc, StringRef Str) override;
127 void PragmaExecCharsetPop(SourceLocation Loc) override;
128 void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
129 SourceRange Range, const MacroArgs *Args) override;
130 void MacroDefined(const Token &MacroNameTok,
131 const MacroDirective *MD) override;
132 void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD,
133 const MacroDirective *Undef) override;
134 void Defined(const Token &MacroNameTok, const MacroDefinition &MD,
135 SourceRange Range) override;
136 void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override;
137 void If(SourceLocation Loc, SourceRange ConditionRange,
138 ConditionValueKind ConditionValue) override;
139 void Elif(SourceLocation Loc, SourceRange ConditionRange,
140 ConditionValueKind ConditionValue, SourceLocation IfLoc) override;
141 void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
142 const MacroDefinition &MD) override;
143 void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
144 const MacroDefinition &MD) override;
145 void Else(SourceLocation Loc, SourceLocation IfLoc) override;
146 void Endif(SourceLocation Loc, SourceLocation IfLoc) override;
148 // Helper functions.
150 /// Start a new callback.
151 void beginCallback(const char *Name);
153 /// Append a string to the top trace item.
154 void append(const char *Str);
156 /// Append a bool argument to the top trace item.
157 void appendArgument(const char *Name, bool Value);
159 /// Append an int argument to the top trace item.
160 void appendArgument(const char *Name, int Value);
162 /// Append a string argument to the top trace item.
163 void appendArgument(const char *Name, const char *Value);
165 /// Append a string reference object argument to the top trace item.
166 void appendArgument(const char *Name, llvm::StringRef Value);
168 /// Append a string object argument to the top trace item.
169 void appendArgument(const char *Name, const std::string &Value);
171 /// Append a token argument to the top trace item.
172 void appendArgument(const char *Name, const Token &Value);
174 /// Append an enum argument to the top trace item.
175 void appendArgument(const char *Name, int Value, const char *const Strings[]);
177 /// Append a FileID argument to the top trace item.
178 void appendArgument(const char *Name, FileID Value);
180 /// Append a FileEntryRef argument to the top trace item.
181 void appendArgument(const char *Name, OptionalFileEntryRef Value);
182 void appendArgument(const char *Name, FileEntryRef Value);
184 /// Append a SourceLocation argument to the top trace item.
185 void appendArgument(const char *Name, SourceLocation Value);
187 /// Append a SourceRange argument to the top trace item.
188 void appendArgument(const char *Name, SourceRange Value);
190 /// Append a CharSourceRange argument to the top trace item.
191 void appendArgument(const char *Name, CharSourceRange Value);
193 /// Append a ModuleIdPath argument to the top trace item.
194 void appendArgument(const char *Name, ModuleIdPath Value);
196 /// Append an IdentifierInfo argument to the top trace item.
197 void appendArgument(const char *Name, const IdentifierInfo *Value);
199 /// Append a MacroDirective argument to the top trace item.
200 void appendArgument(const char *Name, const MacroDirective *Value);
202 /// Append a MacroDefinition argument to the top trace item.
203 void appendArgument(const char *Name, const MacroDefinition &Value);
205 /// Append a MacroArgs argument to the top trace item.
206 void appendArgument(const char *Name, const MacroArgs *Value);
208 /// Append a Module argument to the top trace item.
209 void appendArgument(const char *Name, const Module *Value);
211 /// Append a double-quoted argument to the top trace item.
212 void appendQuotedArgument(const char *Name, const std::string &Value);
214 /// Append a double-quoted file path argument to the top trace item.
215 void appendFilePathArgument(const char *Name, llvm::StringRef Value);
217 /// Get the raw source string of the range.
218 llvm::StringRef getSourceString(CharSourceRange Range);
220 /// Callback trace information.
221 /// We use a reference so the trace will be preserved for the caller
222 /// after this object is destructed.
223 std::vector<CallbackCall> &CallbackCalls;
225 // List of (Glob,Enabled) pairs used to filter callbacks.
226 const FilterType &Filters;
228 // Whether a callback should be printed.
229 llvm::StringMap<bool> CallbackIsEnabled;
231 /// Inhibit trace while this is set.
232 bool DisableTrace;
234 Preprocessor &PP;
237 } // namespace pp_trace
238 } // namespace clang
240 #endif // PPTRACE_PPCALLBACKSTRACKER_H