1 //===--- PPCallbacksTracker.h - Preprocessor tracking -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Classes and definitions for preprocessor tracking.
13 /// The core definition is the PPCallbacksTracker class, derived from Clang's
14 /// PPCallbacks class from the Lex library, which overrides all the callbacks
15 /// and collects information about each callback call, saving it in a
16 /// data structure built up of CallbackCall and Argument objects, which
17 /// record the preprocessor callback name and arguments in high-level string
18 /// form for later inspection.
20 //===----------------------------------------------------------------------===//
22 #ifndef PPTRACE_PPCALLBACKSTRACKER_H
23 #define PPTRACE_PPCALLBACKSTRACKER_H
25 #include "clang/Lex/PPCallbacks.h"
26 #include "clang/Lex/Preprocessor.h"
27 #include "clang/Basic/SourceManager.h"
28 #include "llvm/ADT/ArrayRef.h"
29 #include "llvm/ADT/SmallSet.h"
30 #include "llvm/ADT/StringRef.h"
34 /// \brief This class represents one callback function argument by name
38 Argument(llvm::StringRef Name, llvm::StringRef Value)
39 : Name(Name), Value(Value) {}
46 /// \brief This class represents one callback call by name and an array
50 CallbackCall(llvm::StringRef Name) : Name(Name) {}
51 CallbackCall() = default;
54 std::vector<Argument> Arguments;
57 /// \brief This class overrides the PPCallbacks class for tracking preprocessor
58 /// activity by means of its callback functions.
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.
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.
70 /// Following the constructor and destructor function declarations, the
71 /// overidden 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 clang::PPCallbacks {
76 /// \brief Note that all of the arguments are references, and owned
78 /// \param Ignore - Set of names of callbacks to ignore.
79 /// \param CallbackCalls - Trace buffer.
80 /// \param PP - The preprocessor. Needed for getting some argument strings.
81 PPCallbacksTracker(llvm::SmallSet<std::string, 4> &Ignore,
82 std::vector<CallbackCall> &CallbackCalls,
83 clang::Preprocessor &PP);
85 ~PPCallbacksTracker() override;
87 // Overidden callback functions.
89 void FileChanged(clang::SourceLocation Loc,
90 clang::PPCallbacks::FileChangeReason Reason,
91 clang::SrcMgr::CharacteristicKind FileType,
92 clang::FileID PrevFID = clang::FileID()) override;
93 void FileSkipped(const clang::FileEntry &SkippedFile,
94 const clang::Token &FilenameTok,
95 clang::SrcMgr::CharacteristicKind FileType) override;
96 bool FileNotFound(llvm::StringRef FileName,
97 llvm::SmallVectorImpl<char> &RecoveryPath) override;
98 void InclusionDirective(clang::SourceLocation HashLoc,
99 const clang::Token &IncludeTok,
100 llvm::StringRef FileName, bool IsAngled,
101 clang::CharSourceRange FilenameRange,
102 const clang::FileEntry *File,
103 llvm::StringRef SearchPath,
104 llvm::StringRef RelativePath,
105 const clang::Module *Imported) override;
106 void moduleImport(clang::SourceLocation ImportLoc, clang::ModuleIdPath Path,
107 const clang::Module *Imported) override;
108 void EndOfMainFile() override;
109 void Ident(clang::SourceLocation Loc, llvm::StringRef str) override;
110 void PragmaDirective(clang::SourceLocation Loc,
111 clang::PragmaIntroducerKind Introducer) override;
112 void PragmaComment(clang::SourceLocation Loc,
113 const clang::IdentifierInfo *Kind,
114 llvm::StringRef Str) override;
115 void PragmaDetectMismatch(clang::SourceLocation Loc, llvm::StringRef Name,
116 llvm::StringRef Value) override;
117 void PragmaDebug(clang::SourceLocation Loc,
118 llvm::StringRef DebugType) override;
119 void PragmaMessage(clang::SourceLocation Loc, llvm::StringRef Namespace,
120 clang::PPCallbacks::PragmaMessageKind Kind,
121 llvm::StringRef Str) override;
122 void PragmaDiagnosticPush(clang::SourceLocation Loc,
123 llvm::StringRef Namespace) override;
124 void PragmaDiagnosticPop(clang::SourceLocation Loc,
125 llvm::StringRef Namespace) override;
126 void PragmaDiagnostic(clang::SourceLocation Loc, llvm::StringRef Namespace,
127 clang::diag::Severity mapping,
128 llvm::StringRef Str) override;
129 void PragmaOpenCLExtension(clang::SourceLocation NameLoc,
130 const clang::IdentifierInfo *Name,
131 clang::SourceLocation StateLoc,
132 unsigned State) override;
133 void PragmaWarning(clang::SourceLocation Loc, llvm::StringRef WarningSpec,
134 llvm::ArrayRef<int> Ids) override;
135 void PragmaWarningPush(clang::SourceLocation Loc, int Level) override;
136 void PragmaWarningPop(clang::SourceLocation Loc) override;
137 void MacroExpands(const clang::Token &MacroNameTok,
138 const clang::MacroDefinition &MD, clang::SourceRange Range,
139 const clang::MacroArgs *Args) override;
140 void MacroDefined(const clang::Token &MacroNameTok,
141 const clang::MacroDirective *MD) override;
142 void MacroUndefined(const clang::Token &MacroNameTok,
143 const clang::MacroDefinition &MD,
144 const clang::MacroDirective *Undef) override;
145 void Defined(const clang::Token &MacroNameTok,
146 const clang::MacroDefinition &MD,
147 clang::SourceRange Range) override;
148 void SourceRangeSkipped(clang::SourceRange Range,
149 clang::SourceLocation EndifLoc) override;
150 void If(clang::SourceLocation Loc, clang::SourceRange ConditionRange,
151 ConditionValueKind ConditionValue) override;
152 void Elif(clang::SourceLocation Loc, clang::SourceRange ConditionRange,
153 ConditionValueKind ConditionValue, clang::SourceLocation IfLoc) override;
154 void Ifdef(clang::SourceLocation Loc, const clang::Token &MacroNameTok,
155 const clang::MacroDefinition &MD) override;
156 void Ifndef(clang::SourceLocation Loc, const clang::Token &MacroNameTok,
157 const clang::MacroDefinition &MD) override;
158 void Else(clang::SourceLocation Loc,
159 clang::SourceLocation IfLoc) override;
160 void Endif(clang::SourceLocation Loc,
161 clang::SourceLocation IfLoc) override;
165 /// \brief Start a new callback.
166 void beginCallback(const char *Name);
168 /// \brief Append a string to the top trace item.
169 void append(const char *Str);
171 /// \brief Append a bool argument to the top trace item.
172 void appendArgument(const char *Name, bool Value);
174 /// \brief Append an int argument to the top trace item.
175 void appendArgument(const char *Name, int Value);
177 /// \brief Append a string argument to the top trace item.
178 void appendArgument(const char *Name, const char *Value);
180 /// \brief Append a string reference object argument to the top trace item.
181 void appendArgument(const char *Name, llvm::StringRef Value);
183 /// \brief Append a string object argument to the top trace item.
184 void appendArgument(const char *Name, const std::string &Value);
186 /// \brief Append a token argument to the top trace item.
187 void appendArgument(const char *Name, const clang::Token &Value);
189 /// \brief Append an enum argument to the top trace item.
190 void appendArgument(const char *Name, int Value, const char *const Strings[]);
192 /// \brief Append a FileID argument to the top trace item.
193 void appendArgument(const char *Name, clang::FileID Value);
195 /// \brief Append a FileEntry argument to the top trace item.
196 void appendArgument(const char *Name, const clang::FileEntry *Value);
198 /// \brief Append a SourceLocation argument to the top trace item.
199 void appendArgument(const char *Name, clang::SourceLocation Value);
201 /// \brief Append a SourceRange argument to the top trace item.
202 void appendArgument(const char *Name, clang::SourceRange Value);
204 /// \brief Append a CharSourceRange argument to the top trace item.
205 void appendArgument(const char *Name, clang::CharSourceRange Value);
207 /// \brief Append a ModuleIdPath argument to the top trace item.
208 void appendArgument(const char *Name, clang::ModuleIdPath Value);
210 /// \brief Append an IdentifierInfo argument to the top trace item.
211 void appendArgument(const char *Name, const clang::IdentifierInfo *Value);
213 /// \brief Append a MacroDirective argument to the top trace item.
214 void appendArgument(const char *Name, const clang::MacroDirective *Value);
216 /// \brief Append a MacroDefinition argument to the top trace item.
217 void appendArgument(const char *Name, const clang::MacroDefinition &Value);
219 /// \brief Append a MacroArgs argument to the top trace item.
220 void appendArgument(const char *Name, const clang::MacroArgs *Value);
222 /// \brief Append a Module argument to the top trace item.
223 void appendArgument(const char *Name, const clang::Module *Value);
225 /// \brief Append a double-quoted argument to the top trace item.
226 void appendQuotedArgument(const char *Name, const std::string &Value);
228 /// \brief Append a double-quoted file path argument to the top trace item.
229 void appendFilePathArgument(const char *Name, llvm::StringRef Value);
231 /// \brief Get the raw source string of the range.
232 llvm::StringRef getSourceString(clang::CharSourceRange Range);
234 /// \brief Callback trace information.
235 /// We use a reference so the trace will be preserved for the caller
236 /// after this object is destructed.
237 std::vector<CallbackCall> &CallbackCalls;
239 /// \brief Names of callbacks to ignore.
240 llvm::SmallSet<std::string, 4> &Ignore;
242 /// \brief Inhibit trace while this is set.
245 clang::Preprocessor &PP;
248 #endif // PPTRACE_PPCALLBACKSTRACKER_H