clang-tools  5.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) override;
149  void If(clang::SourceLocation Loc, clang::SourceRange ConditionRange,
150  ConditionValueKind ConditionValue) override;
151  void Elif(clang::SourceLocation Loc, clang::SourceRange ConditionRange,
152  ConditionValueKind ConditionValue, clang::SourceLocation IfLoc) override;
153  void Ifdef(clang::SourceLocation Loc, const clang::Token &MacroNameTok,
154  const clang::MacroDefinition &MD) override;
155  void Ifndef(clang::SourceLocation Loc, const clang::Token &MacroNameTok,
156  const clang::MacroDefinition &MD) override;
157  void Else(clang::SourceLocation Loc,
158  clang::SourceLocation IfLoc) override;
159  void Endif(clang::SourceLocation Loc,
160  clang::SourceLocation IfLoc) override;
161 
162  // Helper functions.
163 
164  /// \brief Start a new callback.
165  void beginCallback(const char *Name);
166 
167  /// \brief Append a string to the top trace item.
168  void append(const char *Str);
169 
170  /// \brief Append a bool argument to the top trace item.
171  void appendArgument(const char *Name, bool Value);
172 
173  /// \brief Append an int argument to the top trace item.
174  void appendArgument(const char *Name, int Value);
175 
176  /// \brief Append a string argument to the top trace item.
177  void appendArgument(const char *Name, const char *Value);
178 
179  /// \brief Append a string reference object argument to the top trace item.
180  void appendArgument(const char *Name, llvm::StringRef Value);
181 
182  /// \brief Append a string object argument to the top trace item.
183  void appendArgument(const char *Name, const std::string &Value);
184 
185  /// \brief Append a token argument to the top trace item.
186  void appendArgument(const char *Name, const clang::Token &Value);
187 
188  /// \brief Append an enum argument to the top trace item.
189  void appendArgument(const char *Name, int Value, const char *const Strings[]);
190 
191  /// \brief Append a FileID argument to the top trace item.
192  void appendArgument(const char *Name, clang::FileID Value);
193 
194  /// \brief Append a FileEntry argument to the top trace item.
195  void appendArgument(const char *Name, const clang::FileEntry *Value);
196 
197  /// \brief Append a SourceLocation argument to the top trace item.
198  void appendArgument(const char *Name, clang::SourceLocation Value);
199 
200  /// \brief Append a SourceRange argument to the top trace item.
201  void appendArgument(const char *Name, clang::SourceRange Value);
202 
203  /// \brief Append a CharSourceRange argument to the top trace item.
204  void appendArgument(const char *Name, clang::CharSourceRange Value);
205 
206  /// \brief Append a ModuleIdPath argument to the top trace item.
207  void appendArgument(const char *Name, clang::ModuleIdPath Value);
208 
209  /// \brief Append an IdentifierInfo argument to the top trace item.
210  void appendArgument(const char *Name, const clang::IdentifierInfo *Value);
211 
212  /// \brief Append a MacroDirective argument to the top trace item.
213  void appendArgument(const char *Name, const clang::MacroDirective *Value);
214 
215  /// \brief Append a MacroDefinition argument to the top trace item.
216  void appendArgument(const char *Name, const clang::MacroDefinition &Value);
217 
218  /// \brief Append a MacroArgs argument to the top trace item.
219  void appendArgument(const char *Name, const clang::MacroArgs *Value);
220 
221  /// \brief Append a Module argument to the top trace item.
222  void appendArgument(const char *Name, const clang::Module *Value);
223 
224  /// \brief Append a double-quoted argument to the top trace item.
225  void appendQuotedArgument(const char *Name, const std::string &Value);
226 
227  /// \brief Append a double-quoted file path argument to the top trace item.
228  void appendFilePathArgument(const char *Name, llvm::StringRef Value);
229 
230  /// \brief Get the raw source string of the range.
231  llvm::StringRef getSourceString(clang::CharSourceRange Range);
232 
233  /// \brief Callback trace information.
234  /// We use a reference so the trace will be preserved for the caller
235  /// after this object is destructed.
236  std::vector<CallbackCall> &CallbackCalls;
237 
238  /// \brief Names of callbacks to ignore.
239  llvm::SmallSet<std::string, 4> &Ignore;
240 
241  /// \brief Inhibit trace while this is set.
243 
244  clang::Preprocessor &PP;
245 };
246 
247 #endif // PPTRACE_PPCALLBACKSTRACKER_H
SourceLocation Loc
'#' location in the include directive
llvm::StringRef getSourceString(clang::CharSourceRange Range)
Get the raw source string of the range.
This class represents one callback function argument by name and value.
void beginCallback(const char *Name)
Start a new callback.
void PragmaDebug(clang::SourceLocation Loc, llvm::StringRef DebugType) override
void EndOfMainFile() override
void Ifndef(clang::SourceLocation Loc, const clang::Token &MacroNameTok, const clang::MacroDefinition &MD) override
void appendFilePathArgument(const char *Name, llvm::StringRef Value)
Append a double-quoted file path argument to the top trace item.
StringHandle Name
std::vector< Argument > Arguments
CallbackCall()=default
void append(const char *Str)
Append a string to the top trace item.
CallbackCall(llvm::StringRef Name)
HeaderHandle File
This class overrides the PPCallbacks class for tracking preprocessor activity by means of its callbac...
std::vector< CallbackCall > & CallbackCalls
Callback trace information.
void PragmaDiagnosticPop(clang::SourceLocation Loc, llvm::StringRef Namespace) override
void PragmaOpenCLExtension(clang::SourceLocation NameLoc, const clang::IdentifierInfo *Name, clang::SourceLocation StateLoc, unsigned State) override
std::string Name
void PragmaDetectMismatch(clang::SourceLocation Loc, llvm::StringRef Name, llvm::StringRef Value) override
void FileChanged(clang::SourceLocation Loc, clang::PPCallbacks::FileChangeReason Reason, clang::SrcMgr::CharacteristicKind FileType, clang::FileID PrevFID=clang::FileID()) override
void Else(clang::SourceLocation Loc, clang::SourceLocation IfLoc) override
void MacroExpands(const clang::Token &MacroNameTok, const clang::MacroDefinition &MD, clang::SourceRange Range, const clang::MacroArgs *Args) override
std::vector< HeaderHandle > Path
void appendQuotedArgument(const char *Name, const std::string &Value)
Append a double-quoted argument to the top trace item.
void FileSkipped(const clang::FileEntry &SkippedFile, const clang::Token &FilenameTok, clang::SrcMgr::CharacteristicKind FileType) override
void SourceRangeSkipped(clang::SourceRange Range) override
llvm::SmallSet< std::string, 4 > & Ignore
Names of callbacks to ignore.
BindArgumentKind Kind
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.
void PragmaMessage(clang::SourceLocation Loc, llvm::StringRef Namespace, clang::PPCallbacks::PragmaMessageKind Kind, llvm::StringRef Str) override
void MacroDefined(const clang::Token &MacroNameTok, const clang::MacroDirective *MD) override
Argument()=default
void MacroUndefined(const clang::Token &MacroNameTok, const clang::MacroDefinition &MD, const clang::MacroDirective *Undef) override
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
void PragmaWarningPush(clang::SourceLocation Loc, int Level) override
void moduleImport(clang::SourceLocation ImportLoc, clang::ModuleIdPath Path, const clang::Module *Imported) override
bool IsAngled
true if this was an include with angle brackets
void Defined(const clang::Token &MacroNameTok, const clang::MacroDefinition &MD, clang::SourceRange Range) override
This class represents one callback call by name and an array of arguments.
void PragmaComment(clang::SourceLocation Loc, const clang::IdentifierInfo *Kind, llvm::StringRef Str) override
void If(clang::SourceLocation Loc, clang::SourceRange ConditionRange, ConditionValueKind ConditionValue) override
Argument(llvm::StringRef Name, llvm::StringRef Value)
bool FileNotFound(llvm::StringRef FileName, llvm::SmallVectorImpl< char > &RecoveryPath) override
clang::PPCallbacks::ConditionValueKind ConditionValue
void PragmaWarningPop(clang::SourceLocation Loc) override
void PragmaDiagnostic(clang::SourceLocation Loc, llvm::StringRef Namespace, clang::diag::Severity mapping, llvm::StringRef Str) override
void Elif(clang::SourceLocation Loc, clang::SourceRange ConditionRange, ConditionValueKind ConditionValue, clang::SourceLocation IfLoc) override
CharSourceRange Range
SourceRange for the file name.
std::string Value
void Endif(clang::SourceLocation Loc, clang::SourceLocation IfLoc) override
void PragmaDirective(clang::SourceLocation Loc, clang::PragmaIntroducerKind Introducer) override
llvm::StringPool Strings
std::string Name
void Ifdef(clang::SourceLocation Loc, const clang::Token &MacroNameTok, const clang::MacroDefinition &MD) override
void appendArgument(const char *Name, bool Value)
Append a bool argument to the top trace item.
bool DisableTrace
Inhibit trace while this is set.
clang::Preprocessor & PP
void PragmaDiagnosticPush(clang::SourceLocation Loc, llvm::StringRef Namespace) override
void PragmaWarning(clang::SourceLocation Loc, llvm::StringRef WarningSpec, llvm::ArrayRef< int > Ids) override
void Ident(clang::SourceLocation Loc, llvm::StringRef str) override