clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
28 /// \brief This class represents one callback function argument by name
29 /// and value.
30 class Argument {
31 public:
32 Argument(llvm::StringRef Name, llvm::StringRef Value)
33 : Name(Name), Value(Value) {}
34 Argument() {}
35
36 std::string Name;
37 std::string Value;
38 };
39
40 /// \brief This class represents one callback call by name and an array
41 /// of arguments.
42 class CallbackCall {
43 public:
44 CallbackCall(llvm::StringRef Name) : Name(Name) {}
45 CallbackCall() {}
46
47 std::string Name;
48 std::vector<Argument> Arguments;
49 };
50
51 /// \brief This class overrides the PPCallbacks class for tracking preprocessor
52 /// activity by means of its callback functions.
53 ///
54 /// This object is given a vector for storing the trace information, built up
55 /// of CallbackCall and subordinate Argument objects for representing the
56 /// callback calls and their arguments. It's a reference so the vector can
57 /// exist beyond the lifetime of this object, because it's deleted by the
58 /// preprocessor automatically in its destructor.
59 ///
60 /// This class supports a mechanism for inhibiting trace output for
61 /// specific callbacks by name, for the purpose of eliminating output for
62 /// callbacks of no interest that might clutter the output.
63 ///
64 /// Following the constructor and destructor function declarations, the
65 /// overidden callback functions are defined. The remaining functions are
66 /// helpers for recording the trace data, to reduce the coupling between it
67 /// and the recorded data structure.
68 class PPCallbacksTracker : public clang::PPCallbacks {
69 public:
70 /// \brief Note that all of the arguments are references, and owned
71 /// by the caller.
72 /// \param Ignore - Set of names of callbacks to ignore.
73 /// \param CallbackCalls - Trace buffer.
74 /// \param PP - The preprocessor. Needed for getting some argument strings.
75 PPCallbacksTracker(llvm::SmallSet<std::string, 4> &Ignore,
76 std::vector<CallbackCall> &CallbackCalls,
77 clang::Preprocessor &PP);
78
79 ~PPCallbacksTracker() override;
80
81 // Overidden callback functions.
82
83 void FileChanged(clang::SourceLocation Loc,
84 clang::PPCallbacks::FileChangeReason Reason,
85 clang::SrcMgr::CharacteristicKind FileType,
86 clang::FileID PrevFID = clang::FileID()) override;
87 void FileSkipped(const clang::FileEntry &SkippedFile,
88 const clang::Token &FilenameTok,
89 clang::SrcMgr::CharacteristicKind FileType) override;
90 bool FileNotFound(llvm::StringRef FileName,
91 llvm::SmallVectorImpl<char> &RecoveryPath) override;
92 void InclusionDirective(clang::SourceLocation HashLoc,
93 const clang::Token &IncludeTok,
94 llvm::StringRef FileName, bool IsAngled,
95 clang::CharSourceRange FilenameRange,
96 const clang::FileEntry *File,
97 llvm::StringRef SearchPath,
98 llvm::StringRef RelativePath,
99 const clang::Module *Imported) override;
100 void moduleImport(clang::SourceLocation ImportLoc, clang::ModuleIdPath Path,
101 const clang::Module *Imported) override;
102 void EndOfMainFile() override;
103 void Ident(clang::SourceLocation Loc, llvm::StringRef str) override;
104 void PragmaDirective(clang::SourceLocation Loc,
105 clang::PragmaIntroducerKind Introducer) override;
106 void PragmaComment(clang::SourceLocation Loc,
107 const clang::IdentifierInfo *Kind,
108 llvm::StringRef Str) override;
109 void PragmaDetectMismatch(clang::SourceLocation Loc, llvm::StringRef Name,
110 llvm::StringRef Value) override;
111 void PragmaDebug(clang::SourceLocation Loc,
112 llvm::StringRef DebugType) override;
113 void PragmaMessage(clang::SourceLocation Loc, llvm::StringRef Namespace,
114 clang::PPCallbacks::PragmaMessageKind Kind,
115 llvm::StringRef Str) override;
116 void PragmaDiagnosticPush(clang::SourceLocation Loc,
117 llvm::StringRef Namespace) override;
118 void PragmaDiagnosticPop(clang::SourceLocation Loc,
119 llvm::StringRef Namespace) override;
120 void PragmaDiagnostic(clang::SourceLocation Loc, llvm::StringRef Namespace,
121 clang::diag::Severity mapping,
122 llvm::StringRef Str) override;
123 void PragmaOpenCLExtension(clang::SourceLocation NameLoc,
124 const clang::IdentifierInfo *Name,
125 clang::SourceLocation StateLoc,
126 unsigned State) override;
127 void PragmaWarning(clang::SourceLocation Loc, llvm::StringRef WarningSpec,
128 llvm::ArrayRef<int> Ids) override;
129 void PragmaWarningPush(clang::SourceLocation Loc, int Level) override;
130 void PragmaWarningPop(clang::SourceLocation Loc) override;
131 void MacroExpands(const clang::Token &MacroNameTok,
132 const clang::MacroDefinition &MD, clang::SourceRange Range,
133 const clang::MacroArgs *Args) override;
134 void MacroDefined(const clang::Token &MacroNameTok,
135 const clang::MacroDirective *MD) override;
136 void MacroUndefined(const clang::Token &MacroNameTok,
137 const clang::MacroDefinition &MD) override;
138 void Defined(const clang::Token &MacroNameTok,
139 const clang::MacroDefinition &MD,
140 clang::SourceRange Range) override;
141 void SourceRangeSkipped(clang::SourceRange Range) override;
142 void If(clang::SourceLocation Loc, clang::SourceRange ConditionRange,
143 ConditionValueKind ConditionValue) override;
144 void Elif(clang::SourceLocation Loc, clang::SourceRange ConditionRange,
145 ConditionValueKind ConditionValue, clang::SourceLocation IfLoc) override;
146 void Ifdef(clang::SourceLocation Loc, const clang::Token &MacroNameTok,
147 const clang::MacroDefinition &MD) override;
148 void Ifndef(clang::SourceLocation Loc, const clang::Token &MacroNameTok,
149 const clang::MacroDefinition &MD) override;
150 void Else(clang::SourceLocation Loc,
151 clang::SourceLocation IfLoc) override;
152 void Endif(clang::SourceLocation Loc,
153 clang::SourceLocation IfLoc) override;
154
155 // Helper functions.
156
157 /// \brief Start a new callback.
158 void beginCallback(const char *Name);
159
160 /// \brief Append a string to the top trace item.
161 void append(const char *Str);
162
163 /// \brief Append a bool argument to the top trace item.
164 void appendArgument(const char *Name, bool Value);
165
166 /// \brief Append an int argument to the top trace item.
167 void appendArgument(const char *Name, int Value);
168
169 /// \brief Append a string argument to the top trace item.
170 void appendArgument(const char *Name, const char *Value);
171
172 /// \brief Append a string reference object argument to the top trace item.
173 void appendArgument(const char *Name, llvm::StringRef Value);
174
175 /// \brief Append a string object argument to the top trace item.
176 void appendArgument(const char *Name, const std::string &Value);
177
178 /// \brief Append a token argument to the top trace item.
179 void appendArgument(const char *Name, const clang::Token &Value);
180
181 /// \brief Append an enum argument to the top trace item.
182 void appendArgument(const char *Name, int Value, const char *const Strings[]);
183
184 /// \brief Append a FileID argument to the top trace item.
185 void appendArgument(const char *Name, clang::FileID Value);
186
187 /// \brief Append a FileEntry argument to the top trace item.
188 void appendArgument(const char *Name, const clang::FileEntry *Value);
189
190 /// \brief Append a SourceLocation argument to the top trace item.
191 void appendArgument(const char *Name, clang::SourceLocation Value);
192
193 /// \brief Append a SourceRange argument to the top trace item.
194 void appendArgument(const char *Name, clang::SourceRange Value);
195
196 /// \brief Append a CharSourceRange argument to the top trace item.
197 void appendArgument(const char *Name, clang::CharSourceRange Value);
198
199 /// \brief Append a ModuleIdPath argument to the top trace item.
200 void appendArgument(const char *Name, clang::ModuleIdPath Value);
201
202 /// \brief Append an IdentifierInfo argument to the top trace item.
203 void appendArgument(const char *Name, const clang::IdentifierInfo *Value);
204
205 /// \brief Append a MacroDirective argument to the top trace item.
206 void appendArgument(const char *Name, const clang::MacroDirective *Value);
207
208 /// \brief Append a MacroDefinition argument to the top trace item.
209 void appendArgument(const char *Name, const clang::MacroDefinition &Value);
210
211 /// \brief Append a MacroArgs argument to the top trace item.
212 void appendArgument(const char *Name, const clang::MacroArgs *Value);
213
214 /// \brief Append a Module argument to the top trace item.
215 void appendArgument(const char *Name, const clang::Module *Value);
216
217 /// \brief Append a double-quoted argument to the top trace item.
218 void appendQuotedArgument(const char *Name, const std::string &Value);
219
220 /// \brief Append a double-quoted file path argument to the top trace item.
221 void appendFilePathArgument(const char *Name, llvm::StringRef Value);
222
223 /// \brief Get the raw source string of the range.
224 llvm::StringRef getSourceString(clang::CharSourceRange Range);
225
226 /// \brief Callback trace information.
227 /// We use a reference so the trace will be preserved for the caller
228 /// after this object is destructed.
229 std::vector<CallbackCall> &CallbackCalls;
230
231 /// \brief Names of callbacks to ignore.
232 llvm::SmallSet<std::string, 4> &Ignore;
233
234 /// \brief Inhibit trace while this is set.
235 bool DisableTrace;
236
237 clang::Preprocessor &PP;
238 };
239
240 #endif // PPTRACE_PPCALLBACKSTRACKER_H
Loc
SourceLocation Loc
'#' location in the include directive
Definition: IncludeOrderCheck.cpp:34
PPCallbacksTracker::getSourceString
llvm::StringRef getSourceString(clang::CharSourceRange Range)
Get the raw source string of the range.
Definition: PPCallbacksTracker.cpp:651
Argument
This class represents one callback function argument by name and value.
Definition: PPCallbacksTracker.h:30
PPCallbacksTracker::beginCallback
void beginCallback(const char *Name)
Start a new callback.
Definition: PPCallbacksTracker.cpp:412
PPCallbacksTracker::PragmaDebug
void PragmaDebug(clang::SourceLocation Loc, llvm::StringRef DebugType) override
Definition: PPCallbacksTracker.cpp:209
PPCallbacksTracker::EndOfMainFile
void EndOfMainFile() override
Definition: PPCallbacksTracker.cpp:169
PPCallbacksTracker::Ifndef
void Ifndef(clang::SourceLocation Loc, const clang::Token &MacroNameTok, const clang::MacroDefinition &MD) override
Definition: PPCallbacksTracker.cpp:384
PPCallbacksTracker::appendFilePathArgument
void appendFilePathArgument(const char *Name, llvm::StringRef Value)
Append a double-quoted file path argument to the top trace item.
Definition: PPCallbacksTracker.cpp:641
Name
StringHandle Name
Definition: PreprocessorTracker.cpp:525
CallbackCall::Arguments
std::vector< Argument > Arguments
Definition: PPCallbacksTracker.h:48
PPCallbacksTracker::append
void append(const char *Str)
Append a string to the top trace item.
CallbackCall::CallbackCall
CallbackCall(llvm::StringRef Name)
Definition: PPCallbacksTracker.h:44
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:68
PPCallbacksTracker::CallbackCalls
std::vector< CallbackCall > & CallbackCalls
Callback trace information.
Definition: PPCallbacksTracker.h:229
PPCallbacksTracker::PragmaDiagnosticPop
void PragmaDiagnosticPop(clang::SourceLocation Loc, llvm::StringRef Namespace) override
Definition: PPCallbacksTracker.cpp:238
PPCallbacksTracker::PragmaOpenCLExtension
void PragmaOpenCLExtension(clang::SourceLocation NameLoc, const clang::IdentifierInfo *Name, clang::SourceLocation StateLoc, unsigned State) override
Definition: PPCallbacksTracker.cpp:259
CallbackCall::Name
std::string Name
Definition: PPCallbacksTracker.h:47
PPCallbacksTracker::PragmaDetectMismatch
void PragmaDetectMismatch(clang::SourceLocation Loc, llvm::StringRef Name, llvm::StringRef Value) override
Definition: PPCallbacksTracker.cpp:199
PPCallbacksTracker::FileChanged
void FileChanged(clang::SourceLocation Loc, clang::PPCallbacks::FileChangeReason Reason, clang::SrcMgr::CharacteristicKind FileType, clang::FileID PrevFID=clang::FileID()) override
Definition: PPCallbacksTracker.cpp:104
PPCallbacksTracker::Else
void Else(clang::SourceLocation Loc, clang::SourceLocation IfLoc) override
Definition: PPCallbacksTracker.cpp:394
PPCallbacksTracker::MacroExpands
void MacroExpands(const clang::Token &MacroNameTok, const clang::MacroDefinition &MD, clang::SourceRange Range, const clang::MacroArgs *Args) override
Definition: PPCallbacksTracker.cpp:306
Path
std::vector< HeaderHandle > Path
Definition: PreprocessorTracker.cpp:538
PPCallbacksTracker::appendQuotedArgument
void appendQuotedArgument(const char *Name, const std::string &Value)
Append a double-quoted argument to the top trace item.
Definition: PPCallbacksTracker.cpp:632
PPCallbacksTracker::FileSkipped
void FileSkipped(const clang::FileEntry &SkippedFile, const clang::Token &FilenameTok, clang::SrcMgr::CharacteristicKind FileType) override
Definition: PPCallbacksTracker.cpp:117
PPCallbacksTracker::SourceRangeSkipped
void SourceRangeSkipped(clang::SourceRange Range) override
Definition: PPCallbacksTracker.cpp:346
PPCallbacksTracker::Ignore
llvm::SmallSet< std::string, 4 > & Ignore
Names of callbacks to ignore.
Definition: PPCallbacksTracker.h:232
CallbackCall::CallbackCall
CallbackCall()
Definition: PPCallbacksTracker.h:45
PPCallbacksTracker::~PPCallbacksTracker
~PPCallbacksTracker() override
Definition: PPCallbacksTracker.cpp:99
PPCallbacksTracker::PPCallbacksTracker
PPCallbacksTracker(llvm::SmallSet< std::string, 4 > &Ignore, std::vector< CallbackCall > &CallbackCalls, clang::Preprocessor &PP)
Note that all of the arguments are references, and owned by the caller.
Definition: PPCallbacksTracker.cpp:94
PPCallbacksTracker::PragmaMessage
void PragmaMessage(clang::SourceLocation Loc, llvm::StringRef Namespace, clang::PPCallbacks::PragmaMessageKind Kind, llvm::StringRef Str) override
Definition: PPCallbacksTracker.cpp:217
PPCallbacksTracker::MacroDefined
void MacroDefined(const clang::Token &MacroNameTok, const clang::MacroDirective *MD) override
Definition: PPCallbacksTracker.cpp:319
PPCallbacksTracker::InclusionDirective
void InclusionDirective(clang::SourceLocation HashLoc, const clang::Token &IncludeTok, llvm::StringRef FileName, bool IsAngled, clang::CharSourceRange FilenameRange, const clang::FileEntry *File, llvm::StringRef SearchPath, llvm::StringRef RelativePath, const clang::Module *Imported) override
Definition: PPCallbacksTracker.cpp:139
PPCallbacksTracker::PragmaWarningPush
void PragmaWarningPush(clang::SourceLocation Loc, int Level) override
Definition: PPCallbacksTracker.cpp:290
PPCallbacksTracker::moduleImport
void moduleImport(clang::SourceLocation ImportLoc, clang::ModuleIdPath Path, const clang::Module *Imported) override
Definition: PPCallbacksTracker.cpp:158
IsAngled
bool IsAngled
true if this was an include with angle brackets
Definition: IncludeOrderCheck.cpp:37
PPCallbacksTracker::Defined
void Defined(const clang::Token &MacroNameTok, const clang::MacroDefinition &MD, clang::SourceRange Range) override
Definition: PPCallbacksTracker.cpp:336
CallbackCall
This class represents one callback call by name and an array of arguments.
Definition: PPCallbacksTracker.h:42
PPCallbacksTracker::PragmaComment
void PragmaComment(clang::SourceLocation Loc, const clang::IdentifierInfo *Kind, llvm::StringRef Str) override
Definition: PPCallbacksTracker.cpp:188
PPCallbacksTracker::If
void If(clang::SourceLocation Loc, clang::SourceRange ConditionRange, ConditionValueKind ConditionValue) override
Definition: PPCallbacksTracker.cpp:352
Argument::Argument
Argument(llvm::StringRef Name, llvm::StringRef Value)
Definition: PPCallbacksTracker.h:32
PPCallbacksTracker::FileNotFound
bool FileNotFound(llvm::StringRef FileName, llvm::SmallVectorImpl< char > &RecoveryPath) override
Definition: PPCallbacksTracker.cpp:129
ConditionValue
clang::PPCallbacks::ConditionValueKind ConditionValue
Definition: PreprocessorTracker.cpp:678
PPCallbacksTracker::PragmaWarningPop
void PragmaWarningPop(clang::SourceLocation Loc) override
Definition: PPCallbacksTracker.cpp:298
PPCallbacksTracker::PragmaDiagnostic
void PragmaDiagnostic(clang::SourceLocation Loc, llvm::StringRef Namespace, clang::diag::Severity mapping, llvm::StringRef Str) override
Definition: PPCallbacksTracker.cpp:246
PPCallbacksTracker::Elif
void Elif(clang::SourceLocation Loc, clang::SourceRange ConditionRange, ConditionValueKind ConditionValue, clang::SourceLocation IfLoc) override
Definition: PPCallbacksTracker.cpp:362
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:35
Argument::Value
std::string Value
Definition: PPCallbacksTracker.h:37
PPCallbacksTracker::Endif
void Endif(clang::SourceLocation Loc, clang::SourceLocation IfLoc) override
Definition: PPCallbacksTracker.cpp:402
PPCallbacksTracker::PragmaDirective
void PragmaDirective(clang::SourceLocation Loc, clang::PragmaIntroducerKind Introducer) override
Definition: PPCallbacksTracker.cpp:180
Strings
llvm::StringPool Strings
Definition: PreprocessorTracker.cpp:1274
Argument::Name
std::string Name
Definition: PPCallbacksTracker.h:36
PPCallbacksTracker::Ifdef
void Ifdef(clang::SourceLocation Loc, const clang::Token &MacroNameTok, const clang::MacroDefinition &MD) override
Definition: PPCallbacksTracker.cpp:374
PPCallbacksTracker::appendArgument
void appendArgument(const char *Name, bool Value)
Append a bool argument to the top trace item.
Definition: PPCallbacksTracker.cpp:420
PPCallbacksTracker::DisableTrace
bool DisableTrace
Inhibit trace while this is set.
Definition: PPCallbacksTracker.h:235
PPCallbacksTracker::PP
clang::Preprocessor & PP
Definition: PPCallbacksTracker.h:237
PPCallbacksTracker::PragmaDiagnosticPush
void PragmaDiagnosticPush(clang::SourceLocation Loc, llvm::StringRef Namespace) override
Definition: PPCallbacksTracker.cpp:229
PPCallbacksTracker::PragmaWarning
void PragmaWarning(clang::SourceLocation Loc, llvm::StringRef WarningSpec, llvm::ArrayRef< int > Ids) override
Definition: PPCallbacksTracker.cpp:270
PPCallbacksTracker::Ident
void Ident(clang::SourceLocation Loc, llvm::StringRef str) override
Definition: PPCallbacksTracker.cpp:172
PPCallbacksTracker::MacroUndefined
void MacroUndefined(const clang::Token &MacroNameTok, const clang::MacroDefinition &MD) override
Definition: PPCallbacksTracker.cpp:327
Argument::Argument
Argument()
Definition: PPCallbacksTracker.h:34