summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/pp-trace/PPCallbacksTracker.h
blob: da5d1b68889f3a2d1645b75038019f8e89f4e3e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//===--- PPCallbacksTracker.h - Preprocessor tracking -----------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// Classes and definitions for preprocessor tracking.
///
/// The core definition is the PPCallbacksTracker class, derived from Clang's
/// PPCallbacks class from the Lex library, which overrides all the callbacks
/// and collects information about each callback call, saving it in a
/// data structure built up of CallbackCall and Argument objects, which
/// record the preprocessor callback name and arguments in high-level string
/// form for later inspection.
///
//===----------------------------------------------------------------------===//

#ifndef PPTRACE_PPCALLBACKSTRACKER_H
#define PPTRACE_PPCALLBACKSTRACKER_H

#include "clang/Lex/PPCallbacks.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Basic/SourceManager.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/GlobPattern.h"
#include <string>
#include <vector>

namespace clang {
namespace pp_trace {

// This struct represents one callback function argument by name and value.
struct Argument {
  std::string Name;
  std::string Value;
};

/// This class represents one callback call by name and an array
///   of arguments.
class CallbackCall {
public:
  CallbackCall(llvm::StringRef Name) : Name(Name) {}
  CallbackCall() = default;

  std::string Name;
  std::vector<Argument> Arguments;
};

using FilterType = std::vector<std::pair<llvm::GlobPattern, bool>>;

/// This class overrides the PPCallbacks class for tracking preprocessor
///   activity by means of its callback functions.
///
/// This object is given a vector for storing the trace information, built up
/// of CallbackCall and subordinate Argument objects for representing the
/// callback calls and their arguments.  It's a reference so the vector can
/// exist beyond the lifetime of this object, because it's deleted by the
/// preprocessor automatically in its destructor.
///
/// This class supports a mechanism for inhibiting trace output for
/// specific callbacks by name, for the purpose of eliminating output for
/// callbacks of no interest that might clutter the output.
///
/// Following the constructor and destructor function declarations, the
/// overidden callback functions are defined.  The remaining functions are
/// helpers for recording the trace data, to reduce the coupling between it
/// and the recorded data structure.
class PPCallbacksTracker : public PPCallbacks {
public:
  /// Note that all of the arguments are references, and owned
  /// by the caller.
  /// \param Filters - List of (Glob,Enabled) pairs used to filter callbacks.
  /// \param CallbackCalls - Trace buffer.
  /// \param PP - The preprocessor.  Needed for getting some argument strings.
  PPCallbacksTracker(const FilterType &Filters,
                     std::vector<CallbackCall> &CallbackCalls,
                     Preprocessor &PP);

  ~PPCallbacksTracker() override;

  // Overidden callback functions.

  void FileChanged(SourceLocation Loc, PPCallbacks::FileChangeReason Reason,
                   SrcMgr::CharacteristicKind FileType,
                   FileID PrevFID = FileID()) override;
  void FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok,
                   SrcMgr::CharacteristicKind FileType) override;
  bool FileNotFound(llvm::StringRef FileName,
                    llvm::SmallVectorImpl<char> &RecoveryPath) override;
  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;
  void moduleImport(SourceLocation ImportLoc, ModuleIdPath Path,
                    const Module *Imported) override;
  void EndOfMainFile() override;
  void Ident(SourceLocation Loc, llvm::StringRef str) override;
  void PragmaDirective(SourceLocation Loc,
                       PragmaIntroducerKind Introducer) override;
  void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
                     llvm::StringRef Str) override;
  void PragmaDetectMismatch(SourceLocation Loc, llvm::StringRef Name,
                            llvm::StringRef Value) override;
  void PragmaDebug(SourceLocation Loc, llvm::StringRef DebugType) override;
  void PragmaMessage(SourceLocation Loc, llvm::StringRef Namespace,
                     PPCallbacks::PragmaMessageKind Kind,
                     llvm::StringRef Str) override;
  void PragmaDiagnosticPush(SourceLocation Loc,
                            llvm::StringRef Namespace) override;
  void PragmaDiagnosticPop(SourceLocation Loc,
                           llvm::StringRef Namespace) override;
  void PragmaDiagnostic(SourceLocation Loc, llvm::StringRef Namespace,
                        diag::Severity mapping, llvm::StringRef Str) override;
  void PragmaOpenCLExtension(SourceLocation NameLoc, const IdentifierInfo *Name,
                             SourceLocation StateLoc, unsigned State) override;
  void PragmaWarning(SourceLocation Loc, llvm::StringRef WarningSpec,
                     llvm::ArrayRef<int> Ids) override;
  void PragmaWarningPush(SourceLocation Loc, int Level) override;
  void PragmaWarningPop(SourceLocation Loc) override;
  void PragmaExecCharsetPush(SourceLocation Loc, StringRef Str) override;
  void PragmaExecCharsetPop(SourceLocation Loc) override;
  void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
                    SourceRange Range, const MacroArgs *Args) override;
  void MacroDefined(const Token &MacroNameTok,
                    const MacroDirective *MD) override;
  void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD,
                      const MacroDirective *Undef) override;
  void Defined(const Token &MacroNameTok, const MacroDefinition &MD,
               SourceRange Range) override;
  void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override;
  void If(SourceLocation Loc, SourceRange ConditionRange,
          ConditionValueKind ConditionValue) override;
  void Elif(SourceLocation Loc, SourceRange ConditionRange,
            ConditionValueKind ConditionValue, SourceLocation IfLoc) override;
  void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
             const MacroDefinition &MD) override;
  void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
              const MacroDefinition &MD) override;
  void Else(SourceLocation Loc, SourceLocation IfLoc) override;
  void Endif(SourceLocation Loc, SourceLocation IfLoc) override;

  // Helper functions.

  /// Start a new callback.
  void beginCallback(const char *Name);

  /// Append a string to the top trace item.
  void append(const char *Str);

  /// Append a bool argument to the top trace item.
  void appendArgument(const char *Name, bool Value);

  /// Append an int argument to the top trace item.
  void appendArgument(const char *Name, int Value);

  /// Append a string argument to the top trace item.
  void appendArgument(const char *Name, const char *Value);

  /// Append a string reference object argument to the top trace item.
  void appendArgument(const char *Name, llvm::StringRef Value);

  /// Append a string object argument to the top trace item.
  void appendArgument(const char *Name, const std::string &Value);

  /// Append a token argument to the top trace item.
  void appendArgument(const char *Name, const Token &Value);

  /// Append an enum argument to the top trace item.
  void appendArgument(const char *Name, int Value, const char *const Strings[]);

  /// Append a FileID argument to the top trace item.
  void appendArgument(const char *Name, FileID Value);

  /// Append a FileEntry argument to the top trace item.
  void appendArgument(const char *Name, const FileEntry *Value);

  /// Append a SourceLocation argument to the top trace item.
  void appendArgument(const char *Name, SourceLocation Value);

  /// Append a SourceRange argument to the top trace item.
  void appendArgument(const char *Name, SourceRange Value);

  /// Append a CharSourceRange argument to the top trace item.
  void appendArgument(const char *Name, CharSourceRange Value);

  /// Append a ModuleIdPath argument to the top trace item.
  void appendArgument(const char *Name, ModuleIdPath Value);

  /// Append an IdentifierInfo argument to the top trace item.
  void appendArgument(const char *Name, const IdentifierInfo *Value);

  /// Append a MacroDirective argument to the top trace item.
  void appendArgument(const char *Name, const MacroDirective *Value);

  /// Append a MacroDefinition argument to the top trace item.
  void appendArgument(const char *Name, const MacroDefinition &Value);

  /// Append a MacroArgs argument to the top trace item.
  void appendArgument(const char *Name, const MacroArgs *Value);

  /// Append a Module argument to the top trace item.
  void appendArgument(const char *Name, const Module *Value);

  /// Append a double-quoted argument to the top trace item.
  void appendQuotedArgument(const char *Name, const std::string &Value);

  /// Append a double-quoted file path argument to the top trace item.
  void appendFilePathArgument(const char *Name, llvm::StringRef Value);

  /// Get the raw source string of the range.
  llvm::StringRef getSourceString(CharSourceRange Range);

  /// Callback trace information.
  /// We use a reference so the trace will be preserved for the caller
  /// after this object is destructed.
  std::vector<CallbackCall> &CallbackCalls;

  // List of (Glob,Enabled) pairs used to filter callbacks.
  const FilterType &Filters;

  // Whether a callback should be printed.
  llvm::StringMap<bool> CallbackIsEnabled;

  /// Inhibit trace while this is set.
  bool DisableTrace;

  Preprocessor &PP;
};

} // namespace pp_trace
} // namespace clang

#endif // PPTRACE_PPCALLBACKSTRACKER_H
OpenPOWER on IntegriCloud