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