blob: b46210ef25a8c52ffd8f4db759c870e2336c0ce2 [file] [log] [blame]
Eugene Zelenko86150472016-11-29 18:24:01 +00001//===--- PPCallbacksTracker.h - Preprocessor tracking -----------*- C++ -*-===//
John Thompson99794542013-10-31 12:23:32 +00002//
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//
Eugene Zelenko86150472016-11-29 18:24:01 +00008//===----------------------------------------------------------------------===//
John Thompson99794542013-10-31 12:23:32 +00009///
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///
Eugene Zelenko86150472016-11-29 18:24:01 +000020//===----------------------------------------------------------------------===//
John Thompson99794542013-10-31 12:23:32 +000021
22#ifndef PPTRACE_PPCALLBACKSTRACKER_H
23#define PPTRACE_PPCALLBACKSTRACKER_H
24
25#include "clang/Lex/PPCallbacks.h"
26#include "clang/Lex/Preprocessor.h"
Eugene Zelenko86150472016-11-29 18:24:01 +000027#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>
John Thompson99794542013-10-31 12:23:32 +000033
34/// \brief This class represents one callback function argument by name
35/// and value.
36class Argument {
37public:
38 Argument(llvm::StringRef Name, llvm::StringRef Value)
39 : Name(Name), Value(Value) {}
Eugene Zelenko86150472016-11-29 18:24:01 +000040 Argument() = default;
John Thompson99794542013-10-31 12:23:32 +000041
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.
48class CallbackCall {
49public:
50 CallbackCall(llvm::StringRef Name) : Name(Name) {}
Eugene Zelenko86150472016-11-29 18:24:01 +000051 CallbackCall() = default;
John Thompson99794542013-10-31 12:23:32 +000052
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.
74class PPCallbacksTracker : public clang::PPCallbacks {
75public:
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
Alexander Kornienko87638f62015-04-11 07:59:33 +000085 ~PPCallbacksTracker() override;
John Thompson99794542013-10-31 12:23:32 +000086
87 // Overidden callback functions.
88
89 void FileChanged(clang::SourceLocation Loc,
90 clang::PPCallbacks::FileChangeReason Reason,
91 clang::SrcMgr::CharacteristicKind FileType,
Craig Toppera3dbe842014-03-02 10:20:11 +000092 clang::FileID PrevFID = clang::FileID()) override;
Nikola Smiljanic90476a52015-05-12 11:48:21 +000093 void FileSkipped(const clang::FileEntry &SkippedFile,
John Thompson99794542013-10-31 12:23:32 +000094 const clang::Token &FilenameTok,
Craig Toppera3dbe842014-03-02 10:20:11 +000095 clang::SrcMgr::CharacteristicKind FileType) override;
John Thompson99794542013-10-31 12:23:32 +000096 bool FileNotFound(llvm::StringRef FileName,
Craig Toppera3dbe842014-03-02 10:20:11 +000097 llvm::SmallVectorImpl<char> &RecoveryPath) override;
John Thompson99794542013-10-31 12:23:32 +000098 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,
Julie Hockett546943f2018-05-10 19:13:14 +0000105 const clang::Module *Imported,
106 clang::SrcMgr::CharacteristicKind FileType) override;
John Thompson99794542013-10-31 12:23:32 +0000107 void moduleImport(clang::SourceLocation ImportLoc, clang::ModuleIdPath Path,
Craig Toppera3dbe842014-03-02 10:20:11 +0000108 const clang::Module *Imported) override;
109 void EndOfMainFile() override;
Rafael Espindolad2185652015-06-01 20:00:20 +0000110 void Ident(clang::SourceLocation Loc, llvm::StringRef str) override;
John Thompson99794542013-10-31 12:23:32 +0000111 void PragmaDirective(clang::SourceLocation Loc,
Craig Toppera3dbe842014-03-02 10:20:11 +0000112 clang::PragmaIntroducerKind Introducer) override;
John Thompson99794542013-10-31 12:23:32 +0000113 void PragmaComment(clang::SourceLocation Loc,
114 const clang::IdentifierInfo *Kind,
Rafael Espindolad2185652015-06-01 20:00:20 +0000115 llvm::StringRef Str) override;
116 void PragmaDetectMismatch(clang::SourceLocation Loc, llvm::StringRef Name,
117 llvm::StringRef Value) override;
John Thompson99794542013-10-31 12:23:32 +0000118 void PragmaDebug(clang::SourceLocation Loc,
Craig Toppera3dbe842014-03-02 10:20:11 +0000119 llvm::StringRef DebugType) override;
John Thompson99794542013-10-31 12:23:32 +0000120 void PragmaMessage(clang::SourceLocation Loc, llvm::StringRef Namespace,
121 clang::PPCallbacks::PragmaMessageKind Kind,
Craig Toppera3dbe842014-03-02 10:20:11 +0000122 llvm::StringRef Str) override;
John Thompson99794542013-10-31 12:23:32 +0000123 void PragmaDiagnosticPush(clang::SourceLocation Loc,
Craig Toppera3dbe842014-03-02 10:20:11 +0000124 llvm::StringRef Namespace) override;
John Thompson99794542013-10-31 12:23:32 +0000125 void PragmaDiagnosticPop(clang::SourceLocation Loc,
Craig Toppera3dbe842014-03-02 10:20:11 +0000126 llvm::StringRef Namespace) override;
John Thompson99794542013-10-31 12:23:32 +0000127 void PragmaDiagnostic(clang::SourceLocation Loc, llvm::StringRef Namespace,
Alp Toker9d63b5e2014-06-10 09:58:45 +0000128 clang::diag::Severity mapping,
Craig Toppera3dbe842014-03-02 10:20:11 +0000129 llvm::StringRef Str) override;
John Thompson99794542013-10-31 12:23:32 +0000130 void PragmaOpenCLExtension(clang::SourceLocation NameLoc,
131 const clang::IdentifierInfo *Name,
132 clang::SourceLocation StateLoc,
Craig Toppera3dbe842014-03-02 10:20:11 +0000133 unsigned State) override;
John Thompson99794542013-10-31 12:23:32 +0000134 void PragmaWarning(clang::SourceLocation Loc, llvm::StringRef WarningSpec,
Craig Toppera3dbe842014-03-02 10:20:11 +0000135 llvm::ArrayRef<int> Ids) override;
136 void PragmaWarningPush(clang::SourceLocation Loc, int Level) override;
137 void PragmaWarningPop(clang::SourceLocation Loc) override;
John Thompson99794542013-10-31 12:23:32 +0000138 void MacroExpands(const clang::Token &MacroNameTok,
Richard Smith33de8562015-05-04 03:15:55 +0000139 const clang::MacroDefinition &MD, clang::SourceRange Range,
Craig Toppera3dbe842014-03-02 10:20:11 +0000140 const clang::MacroArgs *Args) override;
John Thompson99794542013-10-31 12:23:32 +0000141 void MacroDefined(const clang::Token &MacroNameTok,
Craig Toppera3dbe842014-03-02 10:20:11 +0000142 const clang::MacroDirective *MD) override;
John Thompson99794542013-10-31 12:23:32 +0000143 void MacroUndefined(const clang::Token &MacroNameTok,
David Blaikie5c4ec7c2017-04-26 20:58:03 +0000144 const clang::MacroDefinition &MD,
145 const clang::MacroDirective *Undef) override;
John Thompson99794542013-10-31 12:23:32 +0000146 void Defined(const clang::Token &MacroNameTok,
Richard Smith33de8562015-05-04 03:15:55 +0000147 const clang::MacroDefinition &MD,
Craig Toppera3dbe842014-03-02 10:20:11 +0000148 clang::SourceRange Range) override;
Vedant Kumar5490afa2017-09-11 20:47:45 +0000149 void SourceRangeSkipped(clang::SourceRange Range,
150 clang::SourceLocation EndifLoc) override;
John Thompson99794542013-10-31 12:23:32 +0000151 void If(clang::SourceLocation Loc, clang::SourceRange ConditionRange,
Craig Toppera3dbe842014-03-02 10:20:11 +0000152 ConditionValueKind ConditionValue) override;
John Thompson99794542013-10-31 12:23:32 +0000153 void Elif(clang::SourceLocation Loc, clang::SourceRange ConditionRange,
Craig Toppera3dbe842014-03-02 10:20:11 +0000154 ConditionValueKind ConditionValue, clang::SourceLocation IfLoc) override;
John Thompson99794542013-10-31 12:23:32 +0000155 void Ifdef(clang::SourceLocation Loc, const clang::Token &MacroNameTok,
Richard Smith33de8562015-05-04 03:15:55 +0000156 const clang::MacroDefinition &MD) override;
John Thompson99794542013-10-31 12:23:32 +0000157 void Ifndef(clang::SourceLocation Loc, const clang::Token &MacroNameTok,
Richard Smith33de8562015-05-04 03:15:55 +0000158 const clang::MacroDefinition &MD) override;
John Thompson99794542013-10-31 12:23:32 +0000159 void Else(clang::SourceLocation Loc,
Craig Toppera3dbe842014-03-02 10:20:11 +0000160 clang::SourceLocation IfLoc) override;
John Thompson99794542013-10-31 12:23:32 +0000161 void Endif(clang::SourceLocation Loc,
Craig Toppera3dbe842014-03-02 10:20:11 +0000162 clang::SourceLocation IfLoc) override;
John Thompson99794542013-10-31 12:23:32 +0000163
164 // Helper functions.
165
166 /// \brief Start a new callback.
167 void beginCallback(const char *Name);
168
169 /// \brief Append a string to the top trace item.
170 void append(const char *Str);
171
John Thompson99794542013-10-31 12:23:32 +0000172 /// \brief Append a bool argument to the top trace item.
173 void appendArgument(const char *Name, bool Value);
174
175 /// \brief Append an int argument to the top trace item.
176 void appendArgument(const char *Name, int Value);
177
178 /// \brief Append a string argument to the top trace item.
179 void appendArgument(const char *Name, const char *Value);
180
181 /// \brief Append a string reference object argument to the top trace item.
182 void appendArgument(const char *Name, llvm::StringRef Value);
183
184 /// \brief Append a string object argument to the top trace item.
185 void appendArgument(const char *Name, const std::string &Value);
186
187 /// \brief Append a token argument to the top trace item.
188 void appendArgument(const char *Name, const clang::Token &Value);
189
190 /// \brief Append an enum argument to the top trace item.
Craig Topper45857d42015-10-18 05:14:41 +0000191 void appendArgument(const char *Name, int Value, const char *const Strings[]);
John Thompson99794542013-10-31 12:23:32 +0000192
193 /// \brief Append a FileID argument to the top trace item.
194 void appendArgument(const char *Name, clang::FileID Value);
195
196 /// \brief Append a FileEntry argument to the top trace item.
197 void appendArgument(const char *Name, const clang::FileEntry *Value);
198
199 /// \brief Append a SourceLocation argument to the top trace item.
200 void appendArgument(const char *Name, clang::SourceLocation Value);
201
202 /// \brief Append a SourceRange argument to the top trace item.
203 void appendArgument(const char *Name, clang::SourceRange Value);
204
205 /// \brief Append a CharSourceRange argument to the top trace item.
206 void appendArgument(const char *Name, clang::CharSourceRange Value);
207
208 /// \brief Append a ModuleIdPath argument to the top trace item.
209 void appendArgument(const char *Name, clang::ModuleIdPath Value);
210
211 /// \brief Append an IdentifierInfo argument to the top trace item.
212 void appendArgument(const char *Name, const clang::IdentifierInfo *Value);
213
214 /// \brief Append a MacroDirective argument to the top trace item.
215 void appendArgument(const char *Name, const clang::MacroDirective *Value);
216
Richard Smith33de8562015-05-04 03:15:55 +0000217 /// \brief Append a MacroDefinition argument to the top trace item.
218 void appendArgument(const char *Name, const clang::MacroDefinition &Value);
219
John Thompson99794542013-10-31 12:23:32 +0000220 /// \brief Append a MacroArgs argument to the top trace item.
221 void appendArgument(const char *Name, const clang::MacroArgs *Value);
222
223 /// \brief Append a Module argument to the top trace item.
224 void appendArgument(const char *Name, const clang::Module *Value);
225
226 /// \brief Append a double-quoted argument to the top trace item.
Yaron Keren40178c32015-07-03 09:30:33 +0000227 void appendQuotedArgument(const char *Name, const std::string &Value);
John Thompson99794542013-10-31 12:23:32 +0000228
229 /// \brief Append a double-quoted file path argument to the top trace item.
230 void appendFilePathArgument(const char *Name, llvm::StringRef Value);
231
232 /// \brief Get the raw source string of the range.
233 llvm::StringRef getSourceString(clang::CharSourceRange Range);
234
235 /// \brief Callback trace information.
236 /// We use a reference so the trace will be preserved for the caller
237 /// after this object is destructed.
238 std::vector<CallbackCall> &CallbackCalls;
239
240 /// \brief Names of callbacks to ignore.
241 llvm::SmallSet<std::string, 4> &Ignore;
242
243 /// \brief Inhibit trace while this is set.
244 bool DisableTrace;
245
246 clang::Preprocessor &PP;
247};
248
249#endif // PPTRACE_PPCALLBACKSTRACKER_H