clang-tools 6.0.0

PPCallbacksTracker.h
Go to the documentation of this file.
1 //===--- PPCallbacksTracker.h - Preprocessor tracking -----------*- 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 /// \file
11 /// \brief Classes and definitions for preprocessor tracking.
12 ///
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.
19 ///
20 //===----------------------------------------------------------------------===//
21
22 #ifndef PPTRACE_PPCALLBACKSTRACKER_H
23 #define PPTRACE_PPCALLBACKSTRACKER_H
24
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"
31 #include <string>
32 #include <vector>
33
34 /// \brief This class represents one callback function argument by name
35 /// and value.
36 class Argument {
37 public:
38 Argument(llvm::StringRef Name, llvm::StringRef Value)
39 : Name(Name), Value(Value) {}
40 Argument() = default;
41
42 std::string Name;
43 std::string Value;
44 };
45
46 /// \brief This class represents one callback call by name and an array
47 /// of arguments.
48 class CallbackCall {
49 public:
50 CallbackCall(llvm::StringRef Name) : Name(Name) {}
51 CallbackCall() = default;
52
53 std::string Name;
54 std::vector<Argument> Arguments;
55 };
56
57 /// \brief 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 /// 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 {
75 public:
76 /// \brief Note that all of the arguments are references, and owned
77 /// by the caller.
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);
84
85 ~PPCallbacksTracker() override;
86
87 // Overidden callback functions.
88
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;
162
163 // Helper functions.
164
165 /// \brief Start a new callback.
166 void beginCallback(const char *Name);
167
168 /// \brief Append a string to the top trace item.
169 void append(const char *Str);
170
171 /// \brief Append a bool argument to the top trace item.
172 void appendArgument(const char *Name, bool Value);
173
174 /// \brief Append an int argument to the top trace item.
175 void appendArgument(const char *Name, int Value);
176
177 /// \brief Append a string argument to the top trace item.
178 void appendArgument(const char *Name, const char *Value);
179
180 /// \brief Append a string reference object argument to the top trace item.
181 void appendArgument(const char *Name, llvm::StringRef Value);
182
183 /// \brief Append a string object argument to the top trace item.
184 void appendArgument(const char *Name, const std::string &Value);
185
186 /// \brief Append a token argument to the top trace item.
187 void appendArgument(const char *Name, const clang::Token &Value);
188
189 /// \brief Append an enum argument to the top trace item.
190 void appendArgument(const char *Name, int Value, const char *const Strings[]);
191
192 /// \brief Append a FileID argument to the top trace item.
193 void appendArgument(const char *Name, clang::FileID Value);
194
195 /// \brief Append a FileEntry argument to the top trace item.
196 void appendArgument(const char *Name, const clang::FileEntry *Value);
197
198 /// \brief Append a SourceLocation argument to the top trace item.
199 void appendArgument(const char *Name, clang::SourceLocation Value);
200
201 /// \brief Append a SourceRange argument to the top trace item.
202 void appendArgument(const char *Name, clang::SourceRange Value);
203
204 /// \brief Append a CharSourceRange argument to the top trace item.
205 void appendArgument(const char *Name, clang::CharSourceRange Value);
206
207 /// \brief Append a ModuleIdPath argument to the top trace item.
208 void appendArgument(const char *Name, clang::ModuleIdPath Value);
209
210 /// \brief Append an IdentifierInfo argument to the top trace item.
211 void appendArgument(const char *Name, const clang::IdentifierInfo *Value);
212
213 /// \brief Append a MacroDirective argument to the top trace item.
214 void appendArgument(const char *Name, const clang::MacroDirective *Value);
215
216 /// \brief Append a MacroDefinition argument to the top trace item.
217 void appendArgument(const char *Name, const clang::MacroDefinition &Value);
218
219 /// \brief Append a MacroArgs argument to the top trace item.
220 void appendArgument(const char *Name, const clang::MacroArgs *Value);
221
222 /// \brief Append a Module argument to the top trace item.
223 void appendArgument(const char *Name, const clang::Module *Value);
224
225 /// \brief Append a double-quoted argument to the top trace item.
226 void appendQuotedArgument(const char *Name, const std::string &Value);
227
228 /// \brief Append a double-quoted file path argument to the top trace item.
229 void appendFilePathArgument(const char *Name, llvm::StringRef Value);
230
231 /// \brief Get the raw source string of the range.
232 llvm::StringRef getSourceString(clang::CharSourceRange Range);
233
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;
238
239 /// \brief Names of callbacks to ignore.
240 llvm::SmallSet<std::string, 4> &Ignore;
241
242 /// \brief Inhibit trace while this is set.
243 bool DisableTrace;
244
245 clang::Preprocessor &PP;
246 };
247
248 #endif // PPTRACE_PPCALLBACKSTRACKER_H
Loc
SourceLocation Loc
&#39;#&#39; location in the include directive
Definition: IncludeOrderCheck.cpp:36
Argument
This class represents one callback function argument by name and value.
Definition: PPCallbacksTracker.h:36
Modularize::getSourceString
static std::string getSourceString(clang::Preprocessor &PP, clang::SourceRange Range)
Definition: PreprocessorTracker.cpp:303
CallbackCall::Arguments
std::vector< Argument > Arguments
Definition: PPCallbacksTracker.h:54
CallbackCall::CallbackCall
CallbackCall(llvm::StringRef Name)
Definition: PPCallbacksTracker.h:50
File
HeaderHandle File
Definition: PreprocessorTracker.cpp:526
PPCallbacksTracker
This class overrides the PPCallbacks class for tracking preprocessor activity by means of its callbac...
Definition: PPCallbacksTracker.h:74
PPCallbacksTracker::CallbackCalls
std::vector< CallbackCall > & CallbackCalls
Callback trace information.
Definition: PPCallbacksTracker.h:237
CallbackCall::Name
std::string Name
Definition: PPCallbacksTracker.h:53
Path
std::vector< HeaderHandle > Path
Definition: PreprocessorTracker.cpp:538
PPCallbacksTracker::Ignore
llvm::SmallSet< std::string, 4 > & Ignore
Names of callbacks to ignore.
Definition: PPCallbacksTracker.h:240
Kind
BindArgumentKind Kind
Definition: AvoidBindCheck.cpp:41
Argument::Argument
Argument()=default
IsAngled
bool IsAngled
true if this was an include with angle brackets
Definition: IncludeOrderCheck.cpp:39
CallbackCall
This class represents one callback call by name and an array of arguments.
Definition: PPCallbacksTracker.h:48
Argument::Argument
Argument(llvm::StringRef Name, llvm::StringRef Value)
Definition: PPCallbacksTracker.h:38
ConditionValue
clang::PPCallbacks::ConditionValueKind ConditionValue
Definition: PreprocessorTracker.cpp:673
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:37
Argument::Value
std::string Value
Definition: PPCallbacksTracker.h:43
Argument::Name
std::string Name
Definition: PPCallbacksTracker.h:42
PPCallbacksTracker::DisableTrace
bool DisableTrace
Inhibit trace while this is set.
Definition: PPCallbacksTracker.h:243
PPCallbacksTracker::PP
clang::Preprocessor & PP
Definition: PPCallbacksTracker.h:245