clang-tools  14.0.0git
PPCallbacksTracker.h
Go to the documentation of this file.
1 //===--- PPCallbacksTracker.h - Preprocessor tracking -----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// Classes and definitions for preprocessor tracking.
11 ///
12 /// The core definition is the PPCallbacksTracker class, derived from Clang's
13 /// PPCallbacks class from the Lex library, which overrides all the callbacks
14 /// and collects information about each callback call, saving it in a
15 /// data structure built up of CallbackCall and Argument objects, which
16 /// record the preprocessor callback name and arguments in high-level string
17 /// form for later inspection.
18 ///
19 //===----------------------------------------------------------------------===//
20 
21 #ifndef PPTRACE_PPCALLBACKSTRACKER_H
22 #define PPTRACE_PPCALLBACKSTRACKER_H
23 
24 #include "clang/Lex/PPCallbacks.h"
25 #include "clang/Lex/Preprocessor.h"
26 #include "clang/Basic/SourceManager.h"
27 #include "llvm/ADT/ArrayRef.h"
28 #include "llvm/ADT/SmallSet.h"
29 #include "llvm/ADT/StringMap.h"
30 #include "llvm/ADT/StringRef.h"
31 #include "llvm/Support/GlobPattern.h"
32 #include <string>
33 #include <vector>
34 
35 namespace clang {
36 namespace pp_trace {
37 
38 // This struct represents one callback function argument by name and value.
39 struct Argument {
40  std::string Name;
41  std::string Value;
42 };
43 
44 /// This class represents one callback call by name and an array
45 /// of arguments.
46 class CallbackCall {
47 public:
48  CallbackCall(llvm::StringRef Name) : Name(Name) {}
49  CallbackCall() = default;
50 
51  std::string Name;
52  std::vector<Argument> Arguments;
53 };
54 
55 using FilterType = std::vector<std::pair<llvm::GlobPattern, bool>>;
56 
57 /// 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 /// overridden 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.
75 public:
76  /// Note that all of the arguments are references, and owned
77  /// by the caller.
78  /// \param Filters - List of (Glob,Enabled) pairs used to filter callbacks.
79  /// \param CallbackCalls - Trace buffer.
80  /// \param PP - The preprocessor. Needed for getting some argument strings.
82  std::vector<CallbackCall> &CallbackCalls,
83  Preprocessor &PP);
84 
85  ~PPCallbacksTracker() override;
86 
87  // Overridden callback functions.
88 
89  void FileChanged(SourceLocation Loc, PPCallbacks::FileChangeReason Reason,
90  SrcMgr::CharacteristicKind FileType,
91  FileID PrevFID = FileID()) override;
92  void FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok,
93  SrcMgr::CharacteristicKind FileType) override;
94  bool FileNotFound(llvm::StringRef FileName,
95  llvm::SmallVectorImpl<char> &RecoveryPath) override;
96  void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
97  llvm::StringRef FileName, bool IsAngled,
98  CharSourceRange FilenameRange, const FileEntry *File,
99  llvm::StringRef SearchPath,
100  llvm::StringRef RelativePath, const Module *Imported,
101  SrcMgr::CharacteristicKind FileType) override;
102  void moduleImport(SourceLocation ImportLoc, ModuleIdPath Path,
103  const Module *Imported) override;
104  void EndOfMainFile() override;
105  void Ident(SourceLocation Loc, llvm::StringRef str) override;
106  void PragmaDirective(SourceLocation Loc,
107  PragmaIntroducerKind Introducer) override;
108  void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
109  llvm::StringRef Str) override;
110  void PragmaDetectMismatch(SourceLocation Loc, llvm::StringRef Name,
111  llvm::StringRef Value) override;
112  void PragmaDebug(SourceLocation Loc, llvm::StringRef DebugType) override;
113  void PragmaMessage(SourceLocation Loc, llvm::StringRef Namespace,
114  PPCallbacks::PragmaMessageKind Kind,
115  llvm::StringRef Str) override;
116  void PragmaDiagnosticPush(SourceLocation Loc,
117  llvm::StringRef Namespace) override;
118  void PragmaDiagnosticPop(SourceLocation Loc,
119  llvm::StringRef Namespace) override;
120  void PragmaDiagnostic(SourceLocation Loc, llvm::StringRef Namespace,
121  diag::Severity mapping, llvm::StringRef Str) override;
122  void PragmaOpenCLExtension(SourceLocation NameLoc, const IdentifierInfo *Name,
123  SourceLocation StateLoc, unsigned State) override;
124  void PragmaWarning(SourceLocation Loc, PragmaWarningSpecifier WarningSpec,
125  llvm::ArrayRef<int> Ids) override;
126  void PragmaWarningPush(SourceLocation Loc, int Level) override;
127  void PragmaWarningPop(SourceLocation Loc) override;
128  void PragmaExecCharsetPush(SourceLocation Loc, StringRef Str) override;
129  void PragmaExecCharsetPop(SourceLocation Loc) override;
130  void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
131  SourceRange Range, const MacroArgs *Args) override;
132  void MacroDefined(const Token &MacroNameTok,
133  const MacroDirective *MD) override;
134  void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD,
135  const MacroDirective *Undef) override;
136  void Defined(const Token &MacroNameTok, const MacroDefinition &MD,
137  SourceRange Range) override;
138  void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override;
139  void If(SourceLocation Loc, SourceRange ConditionRange,
140  ConditionValueKind ConditionValue) override;
141  void Elif(SourceLocation Loc, SourceRange ConditionRange,
142  ConditionValueKind ConditionValue, SourceLocation IfLoc) override;
143  void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
144  const MacroDefinition &MD) override;
145  void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
146  const MacroDefinition &MD) override;
147  void Else(SourceLocation Loc, SourceLocation IfLoc) override;
148  void Endif(SourceLocation Loc, SourceLocation IfLoc) override;
149 
150  // Helper functions.
151 
152  /// Start a new callback.
153  void beginCallback(const char *Name);
154 
155  /// Append a string to the top trace item.
156  void append(const char *Str);
157 
158  /// Append a bool argument to the top trace item.
159  void appendArgument(const char *Name, bool Value);
160 
161  /// Append an int argument to the top trace item.
162  void appendArgument(const char *Name, int Value);
163 
164  /// Append a string argument to the top trace item.
165  void appendArgument(const char *Name, const char *Value);
166 
167  /// Append a string reference object argument to the top trace item.
168  void appendArgument(const char *Name, llvm::StringRef Value);
169 
170  /// Append a string object argument to the top trace item.
171  void appendArgument(const char *Name, const std::string &Value);
172 
173  /// Append a token argument to the top trace item.
174  void appendArgument(const char *Name, const Token &Value);
175 
176  /// Append an enum argument to the top trace item.
177  void appendArgument(const char *Name, int Value, const char *const Strings[]);
178 
179  /// Append a FileID argument to the top trace item.
180  void appendArgument(const char *Name, FileID Value);
181 
182  /// Append a FileEntry argument to the top trace item.
183  void appendArgument(const char *Name, const FileEntry *Value);
184 
185  /// Append a SourceLocation argument to the top trace item.
186  void appendArgument(const char *Name, SourceLocation Value);
187 
188  /// Append a SourceRange argument to the top trace item.
189  void appendArgument(const char *Name, SourceRange Value);
190 
191  /// Append a CharSourceRange argument to the top trace item.
192  void appendArgument(const char *Name, CharSourceRange Value);
193 
194  /// Append a ModuleIdPath argument to the top trace item.
195  void appendArgument(const char *Name, ModuleIdPath Value);
196 
197  /// Append an IdentifierInfo argument to the top trace item.
198  void appendArgument(const char *Name, const IdentifierInfo *Value);
199 
200  /// Append a MacroDirective argument to the top trace item.
201  void appendArgument(const char *Name, const MacroDirective *Value);
202 
203  /// Append a MacroDefinition argument to the top trace item.
204  void appendArgument(const char *Name, const MacroDefinition &Value);
205 
206  /// Append a MacroArgs argument to the top trace item.
207  void appendArgument(const char *Name, const MacroArgs *Value);
208 
209  /// Append a Module argument to the top trace item.
210  void appendArgument(const char *Name, const Module *Value);
211 
212  /// Append a double-quoted argument to the top trace item.
213  void appendQuotedArgument(const char *Name, const std::string &Value);
214 
215  /// Append a double-quoted file path argument to the top trace item.
216  void appendFilePathArgument(const char *Name, llvm::StringRef Value);
217 
218  /// Get the raw source string of the range.
219  llvm::StringRef getSourceString(CharSourceRange Range);
220 
221  /// Callback trace information.
222  /// We use a reference so the trace will be preserved for the caller
223  /// after this object is destructed.
224  std::vector<CallbackCall> &CallbackCalls;
225 
226  // List of (Glob,Enabled) pairs used to filter callbacks.
228 
229  // Whether a callback should be printed.
230  llvm::StringMap<bool> CallbackIsEnabled;
231 
232  /// Inhibit trace while this is set.
234 
235  Preprocessor &PP;
236 };
237 
238 } // namespace pp_trace
239 } // namespace clang
240 
241 #endif // PPTRACE_PPCALLBACKSTRACKER_H
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:38
Loc
SourceLocation Loc
Definition: KernelNameRestrictionCheck.cpp:45
clang::pp_trace::PPCallbacksTracker::PragmaExecCharsetPop
void PragmaExecCharsetPop(SourceLocation Loc) override
Definition: PPCallbacksTracker.cpp:318
clang::pp_trace::PPCallbacksTracker::PragmaComment
void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind, llvm::StringRef Str) override
Definition: PPCallbacksTracker.cpp:191
clang::pp_trace::PPCallbacksTracker::append
void append(const char *Str)
Append a string to the top trace item.
clang::pp_trace::PPCallbacksTracker::PragmaOpenCLExtension
void PragmaOpenCLExtension(SourceLocation NameLoc, const IdentifierInfo *Name, SourceLocation StateLoc, unsigned State) override
Definition: PPCallbacksTracker.cpp:263
clang::pp_trace::Argument
Definition: PPCallbacksTracker.h:39
clang::doc::MD
static GeneratorRegistry::Add< MDGenerator > MD(MDGenerator::Format, "Generator for MD output.")
clang::pp_trace::PPCallbacksTracker::PPCallbacksTracker
PPCallbacksTracker(const FilterType &Filters, std::vector< CallbackCall > &CallbackCalls, Preprocessor &PP)
Note that all of the arguments are references, and owned by the caller.
Definition: PPCallbacksTracker.cpp:99
clang::pp_trace::PPCallbacksTracker::If
void If(SourceLocation Loc, SourceRange ConditionRange, ConditionValueKind ConditionValue) override
Definition: PPCallbacksTracker.cpp:371
clang::pp_trace::FilterType
std::vector< std::pair< llvm::GlobPattern, bool > > FilterType
Definition: PPCallbacksTracker.h:55
clang::pp_trace::PPCallbacksTracker::Endif
void Endif(SourceLocation Loc, SourceLocation IfLoc) override
Definition: PPCallbacksTracker.cpp:416
clang::pp_trace::PPCallbacksTracker::DisableTrace
bool DisableTrace
Inhibit trace while this is set.
Definition: PPCallbacksTracker.h:233
ConditionValue
clang::PPCallbacks::ConditionValueKind ConditionValue
Definition: PreprocessorTracker.cpp:660
clang::pp_trace::PPCallbacksTracker::PragmaWarning
void PragmaWarning(SourceLocation Loc, PragmaWarningSpecifier WarningSpec, llvm::ArrayRef< int > Ids) override
Definition: PPCallbacksTracker.cpp:275
Kind
BindArgumentKind Kind
Definition: AvoidBindCheck.cpp:59
clang::pp_trace::PPCallbacksTracker::PragmaWarningPop
void PragmaWarningPop(SourceLocation Loc) override
Definition: PPCallbacksTracker.cpp:302
clang::pp_trace::PPCallbacksTracker::Defined
void Defined(const Token &MacroNameTok, const MacroDefinition &MD, SourceRange Range) override
Definition: PPCallbacksTracker.cpp:354
clang::pp_trace::PPCallbacksTracker::~PPCallbacksTracker
~PPCallbacksTracker() override
Definition: PPCallbacksTracker.cpp:104
clang::pp_trace::PPCallbacksTracker::FileChanged
void FileChanged(SourceLocation Loc, PPCallbacks::FileChangeReason Reason, SrcMgr::CharacteristicKind FileType, FileID PrevFID=FileID()) override
Definition: PPCallbacksTracker.cpp:109
clang::pp_trace::PPCallbacksTracker::beginCallback
void beginCallback(const char *Name)
Start a new callback.
Definition: PPCallbacksTracker.cpp:425
clang::pp_trace::Argument::Value
std::string Value
Definition: PPCallbacksTracker.h:41
clang::pp_trace::CallbackCall::Arguments
std::vector< Argument > Arguments
Definition: PPCallbacksTracker.h:52
clang::pp_trace::CallbackCall::CallbackCall
CallbackCall()=default
clang::pp_trace::PPCallbacksTracker::moduleImport
void moduleImport(SourceLocation ImportLoc, ModuleIdPath Path, const Module *Imported) override
Definition: PPCallbacksTracker.cpp:162
clang::pp_trace::PPCallbacksTracker::PragmaDirective
void PragmaDirective(SourceLocation Loc, PragmaIntroducerKind Introducer) override
Definition: PPCallbacksTracker.cpp:183
clang::pp_trace::PPCallbacksTracker::PragmaWarningPush
void PragmaWarningPush(SourceLocation Loc, int Level) override
Definition: PPCallbacksTracker.cpp:295
clang::pp_trace::PPCallbacksTracker
This class overrides the PPCallbacks class for tracking preprocessor activity by means of its callbac...
Definition: PPCallbacksTracker.h:74
clang::pp_trace::PPCallbacksTracker::PP
Preprocessor & PP
Definition: PPCallbacksTracker.h:235
clang::pp_trace::PPCallbacksTracker::CallbackIsEnabled
llvm::StringMap< bool > CallbackIsEnabled
Definition: PPCallbacksTracker.h:230
clang::pp_trace::PPCallbacksTracker::Ifdef
void Ifdef(SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD) override
Definition: PPCallbacksTracker.cpp:391
clang::pp_trace::PPCallbacksTracker::CallbackCalls
std::vector< CallbackCall > & CallbackCalls
Callback trace information.
Definition: PPCallbacksTracker.h:224
clang::pp_trace::Argument::Name
std::string Name
Definition: PPCallbacksTracker.h:40
clang::pp_trace::CallbackCall::Name
std::string Name
Definition: PPCallbacksTracker.h:51
clang::pp_trace::PPCallbacksTracker::InclusionDirective
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, llvm::StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File, llvm::StringRef SearchPath, llvm::StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override
Definition: PPCallbacksTracker.cpp:144
clang::pp_trace::PPCallbacksTracker::appendArgument
void appendArgument(const char *Name, bool Value)
Append a bool argument to the top trace item.
Definition: PPCallbacksTracker.cpp:440
clang::pp_trace::PPCallbacksTracker::FileNotFound
bool FileNotFound(llvm::StringRef FileName, llvm::SmallVectorImpl< char > &RecoveryPath) override
Definition: PPCallbacksTracker.cpp:134
clang::pp_trace::PPCallbacksTracker::Ident
void Ident(SourceLocation Loc, llvm::StringRef str) override
Definition: PPCallbacksTracker.cpp:176
clang::pp_trace::CallbackCall::CallbackCall
CallbackCall(llvm::StringRef Name)
Definition: PPCallbacksTracker.h:48
clang::pp_trace::PPCallbacksTracker::FileSkipped
void FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok, SrcMgr::CharacteristicKind FileType) override
Definition: PPCallbacksTracker.cpp:122
Args
llvm::json::Object Args
Definition: Trace.cpp:139
clang::pp_trace::CallbackCall
This class represents one callback call by name and an array of arguments.
Definition: PPCallbacksTracker.h:46
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::pp_trace::PPCallbacksTracker::EndOfMainFile
void EndOfMainFile() override
Definition: PPCallbacksTracker.cpp:173
clang::pp_trace::PPCallbacksTracker::SourceRangeSkipped
void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override
Definition: PPCallbacksTracker.cpp:364
clang::pp_trace::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:654
clang::pp_trace::PPCallbacksTracker::PragmaDetectMismatch
void PragmaDetectMismatch(SourceLocation Loc, llvm::StringRef Name, llvm::StringRef Value) override
Definition: PPCallbacksTracker.cpp:202
FileName
StringRef FileName
Definition: KernelNameRestrictionCheck.cpp:46
clang::pp_trace::PPCallbacksTracker::Ifndef
void Ifndef(SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD) override
Definition: PPCallbacksTracker.cpp:400
clang::pp_trace::PPCallbacksTracker::getSourceString
llvm::StringRef getSourceString(CharSourceRange Range)
Get the raw source string of the range.
Definition: PPCallbacksTracker.cpp:663
clang::pp_trace::PPCallbacksTracker::PragmaDiagnosticPush
void PragmaDiagnosticPush(SourceLocation Loc, llvm::StringRef Namespace) override
Definition: PPCallbacksTracker.cpp:233
IsAngled
bool IsAngled
true if this was an include with angle brackets
Definition: IncludeOrderCheck.cpp:40
clang::pp_trace::PPCallbacksTracker::Filters
const FilterType & Filters
Definition: PPCallbacksTracker.h:227
clang::pp_trace::PPCallbacksTracker::PragmaDiagnosticPop
void PragmaDiagnosticPop(SourceLocation Loc, llvm::StringRef Namespace) override
Definition: PPCallbacksTracker.cpp:242
Strings
std::vector< llvm::StringRef > Strings
Definition: Serialization.cpp:212
clang::pp_trace::PPCallbacksTracker::PragmaMessage
void PragmaMessage(SourceLocation Loc, llvm::StringRef Namespace, PPCallbacks::PragmaMessageKind Kind, llvm::StringRef Str) override
Definition: PPCallbacksTracker.cpp:220
clang::pp_trace::PPCallbacksTracker::MacroExpands
void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD, SourceRange Range, const MacroArgs *Args) override
Definition: PPCallbacksTracker.cpp:325
clang::pp_trace::PPCallbacksTracker::MacroDefined
void MacroDefined(const Token &MacroNameTok, const MacroDirective *MD) override
Definition: PPCallbacksTracker.cpp:337
clang::pp_trace::PPCallbacksTracker::Elif
void Elif(SourceLocation Loc, SourceRange ConditionRange, ConditionValueKind ConditionValue, SourceLocation IfLoc) override
Definition: PPCallbacksTracker.cpp:380
PPCallbacks
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::pp_trace::PPCallbacksTracker::Else
void Else(SourceLocation Loc, SourceLocation IfLoc) override
Definition: PPCallbacksTracker.cpp:409
clang::pp_trace::PPCallbacksTracker::appendQuotedArgument
void appendQuotedArgument(const char *Name, const std::string &Value)
Append a double-quoted argument to the top trace item.
Definition: PPCallbacksTracker.cpp:645
clang::pp_trace::PPCallbacksTracker::MacroUndefined
void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD, const MacroDirective *Undef) override
Definition: PPCallbacksTracker.cpp:345
clang::pp_trace::PPCallbacksTracker::PragmaExecCharsetPush
void PragmaExecCharsetPush(SourceLocation Loc, StringRef Str) override
Definition: PPCallbacksTracker.cpp:309
clang::pp_trace::PPCallbacksTracker::PragmaDebug
void PragmaDebug(SourceLocation Loc, llvm::StringRef DebugType) override
Definition: PPCallbacksTracker.cpp:212
clang::pp_trace::PPCallbacksTracker::PragmaDiagnostic
void PragmaDiagnostic(SourceLocation Loc, llvm::StringRef Namespace, diag::Severity mapping, llvm::StringRef Str) override
Definition: PPCallbacksTracker.cpp:250
Path
std::vector< HeaderHandle > Path
Definition: PreprocessorTracker.cpp:525