PPCallbacks.h revision 6fb63ab0c74bb777772a3c3ecec09799e275599a
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                       SourceRange Range) {
236  }
237
238  /// \brief Hook called when a source range is skipped.
239  /// \param Range The SourceRange that was skipped. The range begins at the
240  /// \#if/\#else directive and ends after the \#endif/\#else directive.
241  virtual void SourceRangeSkipped(SourceRange Range) {
242  }
243
244  /// \brief Hook called whenever an \#if is seen.
245  /// \param Loc the source location of the directive.
246  /// \param ConditionRange The SourceRange of the expression being tested.
247  /// \param ConditionValue The evaluated value of the condition.
248  ///
249  // FIXME: better to pass in a list (or tree!) of Tokens.
250  virtual void If(SourceLocation Loc, SourceRange ConditionRange,
251                  bool ConditionValue) {
252  }
253
254  /// \brief Hook called whenever an \#elif is seen.
255  /// \param Loc the source location of the directive.
256  /// \param ConditionRange The SourceRange of the expression being tested.
257  /// \param ConditionValue The evaluated value of the condition.
258  /// \param IfLoc the source location of the \#if/\#ifdef/\#ifndef directive.
259  // FIXME: better to pass in a list (or tree!) of Tokens.
260  virtual void Elif(SourceLocation Loc, SourceRange ConditionRange,
261                    bool ConditionValue, SourceLocation IfLoc) {
262  }
263
264  /// \brief Hook called whenever an \#ifdef is seen.
265  /// \param Loc the source location of the directive.
266  /// \param MacroNameTok Information on the token being tested.
267  /// \param MD The MacroDirective if the name was a macro, null otherwise.
268  virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
269                     const MacroDirective *MD) {
270  }
271
272  /// \brief Hook called whenever an \#ifndef is seen.
273  /// \param Loc the source location of the directive.
274  /// \param MacroNameTok Information on the token being tested.
275  /// \param MD The MacroDirective if the name was a macro, null otherwise.
276  virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
277                      const MacroDirective *MD) {
278  }
279
280  /// \brief Hook called whenever an \#else is seen.
281  /// \param Loc the source location of the directive.
282  /// \param IfLoc the source location of the \#if/\#ifdef/\#ifndef directive.
283  virtual void Else(SourceLocation Loc, SourceLocation IfLoc) {
284  }
285
286  /// \brief Hook called whenever an \#endif is seen.
287  /// \param Loc the source location of the directive.
288  /// \param IfLoc the source location of the \#if/\#ifdef/\#ifndef directive.
289  virtual void Endif(SourceLocation Loc, SourceLocation IfLoc) {
290  }
291};
292
293/// \brief Simple wrapper class for chaining callbacks.
294class PPChainedCallbacks : public PPCallbacks {
295  virtual void anchor();
296  PPCallbacks *First, *Second;
297
298public:
299  PPChainedCallbacks(PPCallbacks *_First, PPCallbacks *_Second)
300    : First(_First), Second(_Second) {}
301  ~PPChainedCallbacks() {
302    delete Second;
303    delete First;
304  }
305
306  virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
307                           SrcMgr::CharacteristicKind FileType,
308                           FileID PrevFID) {
309    First->FileChanged(Loc, Reason, FileType, PrevFID);
310    Second->FileChanged(Loc, Reason, FileType, PrevFID);
311  }
312
313  virtual void FileSkipped(const FileEntry &ParentFile,
314                           const Token &FilenameTok,
315                           SrcMgr::CharacteristicKind FileType) {
316    First->FileSkipped(ParentFile, FilenameTok, FileType);
317    Second->FileSkipped(ParentFile, FilenameTok, FileType);
318  }
319
320  virtual bool FileNotFound(StringRef FileName,
321                            SmallVectorImpl<char> &RecoveryPath) {
322    return First->FileNotFound(FileName, RecoveryPath) ||
323           Second->FileNotFound(FileName, RecoveryPath);
324  }
325
326  virtual void InclusionDirective(SourceLocation HashLoc,
327                                  const Token &IncludeTok,
328                                  StringRef FileName,
329                                  bool IsAngled,
330                                  CharSourceRange FilenameRange,
331                                  const FileEntry *File,
332                                  StringRef SearchPath,
333                                  StringRef RelativePath,
334                                  const Module *Imported) {
335    First->InclusionDirective(HashLoc, IncludeTok, FileName, IsAngled,
336                              FilenameRange, File, SearchPath, RelativePath,
337                              Imported);
338    Second->InclusionDirective(HashLoc, IncludeTok, FileName, IsAngled,
339                               FilenameRange, File, SearchPath, RelativePath,
340                               Imported);
341  }
342
343  virtual void moduleImport(SourceLocation ImportLoc,
344                            ModuleIdPath Path,
345                            const Module *Imported) {
346    First->moduleImport(ImportLoc, Path, Imported);
347    Second->moduleImport(ImportLoc, Path, Imported);
348  }
349
350  virtual void EndOfMainFile() {
351    First->EndOfMainFile();
352    Second->EndOfMainFile();
353  }
354
355  virtual void Ident(SourceLocation Loc, const std::string &str) {
356    First->Ident(Loc, str);
357    Second->Ident(Loc, str);
358  }
359
360  virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
361                             const std::string &Str) {
362    First->PragmaComment(Loc, Kind, Str);
363    Second->PragmaComment(Loc, Kind, Str);
364  }
365
366  virtual void PragmaDetectMismatch(SourceLocation Loc,
367                                    const std::string &Name,
368                                    const std::string &Value) {
369    First->PragmaDetectMismatch(Loc, Name, Value);
370    Second->PragmaDetectMismatch(Loc, Name, Value);
371  }
372
373  virtual void PragmaMessage(SourceLocation Loc, StringRef Namespace,
374                             PragmaMessageKind Kind, StringRef Str) {
375    First->PragmaMessage(Loc, Namespace, Kind, Str);
376    Second->PragmaMessage(Loc, Namespace, Kind, Str);
377  }
378
379  virtual void PragmaDiagnosticPush(SourceLocation Loc,
380                                    StringRef Namespace) {
381    First->PragmaDiagnosticPush(Loc, Namespace);
382    Second->PragmaDiagnosticPush(Loc, Namespace);
383  }
384
385  virtual void PragmaDiagnosticPop(SourceLocation Loc,
386                                    StringRef Namespace) {
387    First->PragmaDiagnosticPop(Loc, Namespace);
388    Second->PragmaDiagnosticPop(Loc, Namespace);
389  }
390
391  virtual void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
392                                diag::Mapping mapping, StringRef Str) {
393    First->PragmaDiagnostic(Loc, Namespace, mapping, Str);
394    Second->PragmaDiagnostic(Loc, Namespace, mapping, Str);
395  }
396
397  virtual void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
398                            SourceRange Range, const MacroArgs *Args) {
399    First->MacroExpands(MacroNameTok, MD, Range, Args);
400    Second->MacroExpands(MacroNameTok, MD, Range, Args);
401  }
402
403  virtual void MacroDefined(const Token &MacroNameTok, const MacroDirective *MD) {
404    First->MacroDefined(MacroNameTok, MD);
405    Second->MacroDefined(MacroNameTok, MD);
406  }
407
408  virtual void MacroUndefined(const Token &MacroNameTok,
409                              const MacroDirective *MD) {
410    First->MacroUndefined(MacroNameTok, MD);
411    Second->MacroUndefined(MacroNameTok, MD);
412  }
413
414  virtual void Defined(const Token &MacroNameTok, const MacroDirective *MD,
415                       SourceRange Range) {
416    First->Defined(MacroNameTok, MD, Range);
417    Second->Defined(MacroNameTok, MD, Range);
418  }
419
420  virtual void SourceRangeSkipped(SourceRange Range) {
421    First->SourceRangeSkipped(Range);
422    Second->SourceRangeSkipped(Range);
423  }
424
425  /// \brief Hook called whenever an \#if is seen.
426  virtual void If(SourceLocation Loc, SourceRange ConditionRange,
427                  bool ConditionValue) {
428    First->If(Loc, ConditionRange, ConditionValue);
429    Second->If(Loc, ConditionRange, ConditionValue);
430  }
431
432  /// \brief Hook called whenever an \#elif is seen.
433  virtual void Elif(SourceLocation Loc, SourceRange ConditionRange,
434                    bool ConditionValue, SourceLocation IfLoc) {
435    First->Elif(Loc, ConditionRange, ConditionValue, IfLoc);
436    Second->Elif(Loc, ConditionRange, ConditionValue, IfLoc);
437  }
438
439  /// \brief Hook called whenever an \#ifdef is seen.
440  virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
441                     const MacroDirective *MD) {
442    First->Ifdef(Loc, MacroNameTok, MD);
443    Second->Ifdef(Loc, MacroNameTok, MD);
444  }
445
446  /// \brief Hook called whenever an \#ifndef is seen.
447  virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
448                      const MacroDirective *MD) {
449    First->Ifndef(Loc, MacroNameTok, MD);
450    Second->Ifndef(Loc, MacroNameTok, MD);
451  }
452
453  /// \brief Hook called whenever an \#else is seen.
454  virtual void Else(SourceLocation Loc, SourceLocation IfLoc) {
455    First->Else(Loc, IfLoc);
456    Second->Else(Loc, IfLoc);
457  }
458
459  /// \brief Hook called whenever an \#endif is seen.
460  virtual void Endif(SourceLocation Loc, SourceLocation IfLoc) {
461    First->Endif(Loc, IfLoc);
462    Second->Endif(Loc, IfLoc);
463  }
464};
465
466}  // end namespace clang
467
468#endif
469