clang-tools  3.9.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 
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) {}
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.
236 
237  clang::Preprocessor &PP;
238 };
239 
240 #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
const std::string Name
Definition: USRFinder.cpp:140
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.
std::vector< Argument > Arguments
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
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
void MacroUndefined(const clang::Token &MacroNameTok, const clang::MacroDefinition &MD) override