PPCallbacks.h revision a7ff62f9443efa3b13a28a1e566d4625b15b8553
1//===--- PPCallbacks.h - Callbacks for Preprocessor actions -----*- 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 Defines the PPCallbacks interface.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_LEX_PPCALLBACKS_H
16#define LLVM_CLANG_LEX_PPCALLBACKS_H
17
18#include "clang/Basic/DiagnosticIDs.h"
19#include "clang/Basic/SourceLocation.h"
20#include "clang/Lex/DirectoryLookup.h"
21#include "clang/Lex/ModuleLoader.h"
22#include "llvm/ADT/StringRef.h"
23#include <string>
24
25namespace clang {
26  class SourceLocation;
27  class Token;
28  class IdentifierInfo;
29  class MacroDirective;
30  class MacroArgs;
31
32/// \brief This interface provides a way to observe the actions of the
33/// preprocessor as it does its thing.
34///
35/// Clients can define their hooks here to implement preprocessor level tools.
36class PPCallbacks {
37public:
38  virtual ~PPCallbacks();
39
40  enum FileChangeReason {
41    EnterFile, ExitFile, SystemHeaderPragma, RenameFile
42  };
43
44  /// \brief Callback invoked whenever a source file is entered or exited.
45  ///
46  /// \param Loc Indicates the new location.
47  /// \param PrevFID the file that was exited if \p Reason is ExitFile.
48  virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
49                           SrcMgr::CharacteristicKind FileType,
50                           FileID PrevFID = FileID()) {
51  }
52
53  /// \brief Callback invoked whenever a source file is skipped as the result
54  /// of header guard optimization.
55  ///
56  /// \param ParentFile The file that \#included the skipped file.
57  ///
58  /// \param FilenameTok The token in ParentFile that indicates the
59  /// skipped file.
60  virtual void FileSkipped(const FileEntry &ParentFile,
61                           const Token &FilenameTok,
62                           SrcMgr::CharacteristicKind FileType) {
63  }
64
65  /// \brief Callback invoked whenever an inclusion directive results in a
66  /// file-not-found error.
67  ///
68  /// \param FileName The name of the file being included, as written in the
69  /// source code.
70  ///
71  /// \param RecoveryPath If this client indicates that it can recover from
72  /// this missing file, the client should set this as an additional header
73  /// search patch.
74  ///
75  /// \returns true to indicate that the preprocessor should attempt to recover
76  /// by adding \p RecoveryPath as a header search path.
77  virtual bool FileNotFound(StringRef FileName,
78                            SmallVectorImpl<char> &RecoveryPath) {
79    return false;
80  }
81
82  /// \brief Callback invoked whenever an inclusion directive of
83  /// any kind (\c \#include, \c \#import, etc.) has been processed, regardless
84  /// of whether the inclusion will actually result in an inclusion.
85  ///
86  /// \param HashLoc The location of the '#' that starts the inclusion
87  /// directive.
88  ///
89  /// \param IncludeTok The token that indicates the kind of inclusion
90  /// directive, e.g., 'include' or 'import'.
91  ///
92  /// \param FileName The name of the file being included, as written in the
93  /// source code.
94  ///
95  /// \param IsAngled Whether the file name was enclosed in angle brackets;
96  /// otherwise, it was enclosed in quotes.
97  ///
98  /// \param FilenameRange The character range of the quotes or angle brackets
99  /// for the written file name.
100  ///
101  /// \param File The actual file that may be included by this inclusion
102  /// directive.
103  ///
104  /// \param SearchPath Contains the search path which was used to find the file
105  /// in the file system. If the file was found via an absolute include path,
106  /// SearchPath will be empty. For framework includes, the SearchPath and
107  /// RelativePath will be split up. For example, if an include of "Some/Some.h"
108  /// is found via the framework path
109  /// "path/to/Frameworks/Some.framework/Headers/Some.h", SearchPath will be
110  /// "path/to/Frameworks/Some.framework/Headers" and RelativePath will be
111  /// "Some.h".
112  ///
113  /// \param RelativePath The path relative to SearchPath, at which the include
114  /// file was found. This is equal to FileName except for framework includes.
115  ///
116  /// \param Imported The module, whenever an inclusion directive was
117  /// automatically turned into a module import or null otherwise.
118  ///
119  virtual void InclusionDirective(SourceLocation HashLoc,
120                                  const Token &IncludeTok,
121                                  StringRef FileName,
122                                  bool IsAngled,
123                                  CharSourceRange FilenameRange,
124                                  const FileEntry *File,
125                                  StringRef SearchPath,
126                                  StringRef RelativePath,
127                                  const Module *Imported) {
128  }
129
130  /// \brief Callback invoked whenever there was an explicit module-import
131  /// syntax.
132  ///
133  /// \param ImportLoc The location of import directive token.
134  ///
135  /// \param Path The identifiers (and their locations) of the module
136  /// "path", e.g., "std.vector" would be split into "std" and "vector".
137  ///
138  /// \param Imported The imported module; can be null if importing failed.
139  ///
140  virtual void moduleImport(SourceLocation ImportLoc,
141                            ModuleIdPath Path,
142                            const Module *Imported) {
143  }
144
145  /// \brief Callback invoked when the end of the main file is reached.
146  ///
147  /// No subsequent callbacks will be made.
148  virtual void EndOfMainFile() {
149  }
150
151  /// \brief Callback invoked when a \#ident or \#sccs directive is read.
152  /// \param Loc The location of the directive.
153  /// \param str The text of the directive.
154  ///
155  virtual void Ident(SourceLocation Loc, const std::string &str) {
156  }
157
158  /// \brief Callback invoked when a \#pragma comment directive is read.
159  virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
160                             const std::string &Str) {
161  }
162
163  /// \brief Callback invoked when a \#pragma detect_mismatch directive is
164  /// read.
165  virtual void PragmaDetectMismatch(SourceLocation Loc,
166                                    const std::string &Name,
167                                    const std::string &Value) {
168  }
169
170  /// \brief Callback invoked when a \#pragma clang __debug directive is read.
171  /// \param Loc The location of the debug directive.
172  /// \param DebugType The identifier following __debug.
173  virtual void PragmaDebug(SourceLocation Loc, StringRef DebugType) {
174  }
175
176  /// \brief Determines the kind of \#pragma invoking a call to PragmaMessage.
177  enum PragmaMessageKind {
178    /// \brief \#pragma message has been invoked.
179    PMK_Message,
180
181    /// \brief \#pragma GCC warning has been invoked.
182    PMK_Warning,
183
184    /// \brief \#pragma GCC error has been invoked.
185    PMK_Error
186  };
187
188  /// \brief Callback invoked when a \#pragma message directive is read.
189  /// \param Loc The location of the message directive.
190  /// \param Namespace The namespace of the message directive.
191  /// \param Kind The type of the message directive.
192  /// \param Str The text of the message directive.
193  virtual void PragmaMessage(SourceLocation Loc, StringRef Namespace,
194                             PragmaMessageKind Kind, StringRef Str) {
195  }
196
197  /// \brief Callback invoked when a \#pragma gcc dianostic push directive
198  /// is read.
199  virtual void PragmaDiagnosticPush(SourceLocation Loc,
200                                    StringRef Namespace) {
201  }
202
203  /// \brief Callback invoked when a \#pragma gcc dianostic pop directive
204  /// is read.
205  virtual void PragmaDiagnosticPop(SourceLocation Loc,
206                                   StringRef Namespace) {
207  }
208
209  /// \brief Callback invoked when a \#pragma gcc dianostic directive is read.
210  virtual void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
211                                diag::Mapping mapping, StringRef Str) {
212  }
213
214  /// \brief Called by Preprocessor::HandleMacroExpandedIdentifier when a
215  /// macro invocation is found.
216  virtual void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
217                            SourceRange Range, const MacroArgs *Args) {
218  }
219
220  /// \brief Hook called whenever a macro definition is seen.
221  virtual void MacroDefined(const Token &MacroNameTok,
222                            const MacroDirective *MD) {
223  }
224
225  /// \brief Hook called whenever a macro \#undef is seen.
226  ///
227  /// MD is released immediately following this callback.
228  virtual void MacroUndefined(const Token &MacroNameTok,
229                              const MacroDirective *MD) {
230  }
231
232  /// \brief Hook called whenever the 'defined' operator is seen.
233  /// \param MD The MacroDirective if the name was a macro, null otherwise.
234  virtual void Defined(const Token &MacroNameTok, const MacroDirective *MD) {
235  }
236
237  /// \brief Hook called when a source range is skipped.
238  /// \param Range The SourceRange that was skipped. The range begins at the
239  /// \#if/\#else directive and ends after the \#endif/\#else directive.
240  virtual void SourceRangeSkipped(SourceRange Range) {
241  }
242
243  /// \brief Hook called whenever an \#if is seen.
244  /// \param Loc the source location of the directive.
245  /// \param ConditionRange The SourceRange of the expression being tested.
246  ///
247  // FIXME: better to pass in a list (or tree!) of Tokens.
248  virtual void If(SourceLocation Loc, SourceRange ConditionRange) {
249  }
250
251  /// \brief Hook called whenever an \#elif is seen.
252  /// \param Loc the source location of the directive.
253  /// \param ConditionRange The SourceRange of the expression being tested.
254  /// \param IfLoc the source location of the \#if/\#ifdef/\#ifndef directive.
255  // FIXME: better to pass in a list (or tree!) of Tokens.
256  virtual void Elif(SourceLocation Loc, SourceRange ConditionRange,
257                    SourceLocation IfLoc) {
258  }
259
260  /// \brief Hook called whenever an \#ifdef is seen.
261  /// \param Loc the source location of the directive.
262  /// \param MacroNameTok Information on the token being tested.
263  /// \param MD The MacroDirective if the name was a macro, null otherwise.
264  virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
265                     const MacroDirective *MD) {
266  }
267
268  /// \brief Hook called whenever an \#ifndef is seen.
269  /// \param Loc the source location of the directive.
270  /// \param MacroNameTok Information on the token being tested.
271  /// \param MD The MacroDirective if the name was a macro, null otherwise.
272  virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
273                      const MacroDirective *MD) {
274  }
275
276  /// \brief Hook called whenever an \#else is seen.
277  /// \param Loc the source location of the directive.
278  /// \param IfLoc the source location of the \#if/\#ifdef/\#ifndef directive.
279  virtual void Else(SourceLocation Loc, SourceLocation IfLoc) {
280  }
281
282  /// \brief Hook called whenever an \#endif is seen.
283  /// \param Loc the source location of the directive.
284  /// \param IfLoc the source location of the \#if/\#ifdef/\#ifndef directive.
285  virtual void Endif(SourceLocation Loc, SourceLocation IfLoc) {
286  }
287};
288
289/// \brief Simple wrapper class for chaining callbacks.
290class PPChainedCallbacks : public PPCallbacks {
291  virtual void anchor();
292  PPCallbacks *First, *Second;
293
294public:
295  PPChainedCallbacks(PPCallbacks *_First, PPCallbacks *_Second)
296    : First(_First), Second(_Second) {}
297  ~PPChainedCallbacks() {
298    delete Second;
299    delete First;
300  }
301
302  virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
303                           SrcMgr::CharacteristicKind FileType,
304                           FileID PrevFID) {
305    First->FileChanged(Loc, Reason, FileType, PrevFID);
306    Second->FileChanged(Loc, Reason, FileType, PrevFID);
307  }
308
309  virtual void FileSkipped(const FileEntry &ParentFile,
310                           const Token &FilenameTok,
311                           SrcMgr::CharacteristicKind FileType) {
312    First->FileSkipped(ParentFile, FilenameTok, FileType);
313    Second->FileSkipped(ParentFile, FilenameTok, FileType);
314  }
315
316  virtual bool FileNotFound(StringRef FileName,
317                            SmallVectorImpl<char> &RecoveryPath) {
318    return First->FileNotFound(FileName, RecoveryPath) ||
319           Second->FileNotFound(FileName, RecoveryPath);
320  }
321
322  virtual void InclusionDirective(SourceLocation HashLoc,
323                                  const Token &IncludeTok,
324                                  StringRef FileName,
325                                  bool IsAngled,
326                                  CharSourceRange FilenameRange,
327                                  const FileEntry *File,
328                                  StringRef SearchPath,
329                                  StringRef RelativePath,
330                                  const Module *Imported) {
331    First->InclusionDirective(HashLoc, IncludeTok, FileName, IsAngled,
332                              FilenameRange, File, SearchPath, RelativePath,
333                              Imported);
334    Second->InclusionDirective(HashLoc, IncludeTok, FileName, IsAngled,
335                               FilenameRange, File, SearchPath, RelativePath,
336                               Imported);
337  }
338
339  virtual void moduleImport(SourceLocation ImportLoc,
340                            ModuleIdPath Path,
341                            const Module *Imported) {
342    First->moduleImport(ImportLoc, Path, Imported);
343    Second->moduleImport(ImportLoc, Path, Imported);
344  }
345
346  virtual void EndOfMainFile() {
347    First->EndOfMainFile();
348    Second->EndOfMainFile();
349  }
350
351  virtual void Ident(SourceLocation Loc, const std::string &str) {
352    First->Ident(Loc, str);
353    Second->Ident(Loc, str);
354  }
355
356  virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
357                             const std::string &Str) {
358    First->PragmaComment(Loc, Kind, Str);
359    Second->PragmaComment(Loc, Kind, Str);
360  }
361
362  virtual void PragmaDetectMismatch(SourceLocation Loc,
363                                    const std::string &Name,
364                                    const std::string &Value) {
365    First->PragmaDetectMismatch(Loc, Name, Value);
366    Second->PragmaDetectMismatch(Loc, Name, Value);
367  }
368
369  virtual void PragmaMessage(SourceLocation Loc, StringRef Namespace,
370                             PragmaMessageKind Kind, StringRef Str) {
371    First->PragmaMessage(Loc, Namespace, Kind, Str);
372    Second->PragmaMessage(Loc, Namespace, Kind, Str);
373  }
374
375  virtual void PragmaDiagnosticPush(SourceLocation Loc,
376                                    StringRef Namespace) {
377    First->PragmaDiagnosticPush(Loc, Namespace);
378    Second->PragmaDiagnosticPush(Loc, Namespace);
379  }
380
381  virtual void PragmaDiagnosticPop(SourceLocation Loc,
382                                    StringRef Namespace) {
383    First->PragmaDiagnosticPop(Loc, Namespace);
384    Second->PragmaDiagnosticPop(Loc, Namespace);
385  }
386
387  virtual void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
388                                diag::Mapping mapping, StringRef Str) {
389    First->PragmaDiagnostic(Loc, Namespace, mapping, Str);
390    Second->PragmaDiagnostic(Loc, Namespace, mapping, Str);
391  }
392
393  virtual void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
394                            SourceRange Range, const MacroArgs *Args) {
395    First->MacroExpands(MacroNameTok, MD, Range, Args);
396    Second->MacroExpands(MacroNameTok, MD, Range, Args);
397  }
398
399  virtual void MacroDefined(const Token &MacroNameTok, const MacroDirective *MD) {
400    First->MacroDefined(MacroNameTok, MD);
401    Second->MacroDefined(MacroNameTok, MD);
402  }
403
404  virtual void MacroUndefined(const Token &MacroNameTok,
405                              const MacroDirective *MD) {
406    First->MacroUndefined(MacroNameTok, MD);
407    Second->MacroUndefined(MacroNameTok, MD);
408  }
409
410  virtual void Defined(const Token &MacroNameTok, const MacroDirective *MD) {
411    First->Defined(MacroNameTok, MD);
412    Second->Defined(MacroNameTok, MD);
413  }
414
415  virtual void SourceRangeSkipped(SourceRange Range) {
416    First->SourceRangeSkipped(Range);
417    Second->SourceRangeSkipped(Range);
418  }
419
420  /// \brief Hook called whenever an \#if is seen.
421  virtual void If(SourceLocation Loc, SourceRange ConditionRange) {
422    First->If(Loc, ConditionRange);
423    Second->If(Loc, ConditionRange);
424  }
425
426  /// \brief Hook called whenever an \#if is seen.
427  virtual void Elif(SourceLocation Loc, SourceRange ConditionRange,
428                    SourceLocation IfLoc) {
429    First->Elif(Loc, ConditionRange, IfLoc);
430    Second->Elif(Loc, ConditionRange, IfLoc);
431  }
432
433  /// \brief Hook called whenever an \#ifdef is seen.
434  virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
435                     const MacroDirective *MD) {
436    First->Ifdef(Loc, MacroNameTok, MD);
437    Second->Ifdef(Loc, MacroNameTok, MD);
438  }
439
440  /// \brief Hook called whenever an \#ifndef is seen.
441  virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
442                      const MacroDirective *MD) {
443    First->Ifndef(Loc, MacroNameTok, MD);
444    Second->Ifndef(Loc, MacroNameTok, MD);
445  }
446
447  /// \brief Hook called whenever an \#else is seen.
448  virtual void Else(SourceLocation Loc, SourceLocation IfLoc) {
449    First->Else(Loc, IfLoc);
450    Second->Else(Loc, IfLoc);
451  }
452
453  /// \brief Hook called whenever an \#endif is seen.
454  virtual void Endif(SourceLocation Loc, SourceLocation IfLoc) {
455    First->Endif(Loc, IfLoc);
456    Second->Endif(Loc, IfLoc);
457  }
458};
459
460}  // end namespace clang
461
462#endif
463