PPCallbacks.h revision 6fbe3ebeaef08665a37423f8425314c90b8b5bcf
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//  This file defines the PPCallbacks interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_LEX_PPCALLBACKS_H
15#define LLVM_CLANG_LEX_PPCALLBACKS_H
16
17#include "clang/Lex/DirectoryLookup.h"
18#include "clang/Basic/SourceLocation.h"
19#include <string>
20
21namespace clang {
22  class SourceLocation;
23  class Token;
24  class IdentifierInfo;
25  class MacroInfo;
26
27/// PPCallbacks - This interface provides a way to observe the actions of the
28/// preprocessor as it does its thing.  Clients can define their hooks here to
29/// implement preprocessor level tools.
30class PPCallbacks {
31public:
32  virtual ~PPCallbacks();
33
34  enum FileChangeReason {
35    EnterFile, ExitFile, SystemHeaderPragma, RenameFile
36  };
37
38  /// FileChanged - This callback is invoked whenever a source file is
39  /// entered or exited.  The SourceLocation indicates the new location, and
40  /// EnteringFile indicates whether this is because we are entering a new
41  /// #include'd file (when true) or whether we're exiting one because we ran
42  /// off the end (when false).
43  virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
44                           SrcMgr::CharacteristicKind FileType) {
45  }
46
47  /// FileSkipped - This callback is invoked whenever a source file is
48  /// skipped as the result of header guard optimization.  ParentFile
49  /// is the file that #includes the skipped file.  FilenameTok is the
50  /// token in ParentFile that indicates the skipped file.
51  virtual void FileSkipped(const FileEntry &ParentFile,
52                           const Token &FilenameTok,
53                           SrcMgr::CharacteristicKind FileType) {
54  }
55
56  /// EndOfMainFile - This callback is invoked when the end of the main file is
57  /// reach, no subsequent callbacks will be made.
58  virtual void EndOfMainFile() {
59  }
60
61  /// Ident - This callback is invoked when a #ident or #sccs directive is read.
62  ///
63  virtual void Ident(SourceLocation Loc, const std::string &str) {
64  }
65
66  /// PragmaComment - This callback is invoked when a #pragma comment directive
67  /// is read.
68  ///
69  virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
70                             const std::string &Str) {
71  }
72
73  /// MacroExpands - This is called by
74  /// Preprocessor::HandleMacroExpandedIdentifier when a macro invocation is
75  /// found.
76  virtual void MacroExpands(const Token &Id, const MacroInfo* MI) {
77  }
78
79  /// MacroDefined - This hook is called whenever a macro definition is seen.
80  virtual void MacroDefined(const IdentifierInfo *II, const MacroInfo *MI) {
81  }
82
83  /// MacroUndefined - This hook is called whenever a macro #undef is seen.
84  /// MI is released immediately following this callback.
85  virtual void MacroUndefined(const IdentifierInfo *II, const MacroInfo *MI) {
86  }
87};
88
89/// PPChainedCallbacks - Simple wrapper class for chaining callbacks.
90class PPChainedCallbacks : public PPCallbacks {
91  PPCallbacks *First, *Second;
92
93public:
94  PPChainedCallbacks(PPCallbacks *_First, PPCallbacks *_Second)
95    : First(_First), Second(_Second) {}
96  ~PPChainedCallbacks() {
97    delete Second;
98    delete First;
99  }
100
101  virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
102                           SrcMgr::CharacteristicKind FileType) {
103    First->FileChanged(Loc, Reason, FileType);
104    Second->FileChanged(Loc, Reason, FileType);
105  }
106
107  virtual void FileSkipped(const FileEntry &ParentFile,
108                           const Token &FilenameTok,
109                           SrcMgr::CharacteristicKind FileType) {
110    First->FileSkipped(ParentFile, FilenameTok, FileType);
111    Second->FileSkipped(ParentFile, FilenameTok, FileType);
112  }
113
114  virtual void EndOfMainFile() {
115    First->EndOfMainFile();
116    Second->EndOfMainFile();
117  }
118
119  virtual void Ident(SourceLocation Loc, const std::string &str) {
120    First->Ident(Loc, str);
121    Second->Ident(Loc, str);
122  }
123
124  virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
125                             const std::string &Str) {
126    First->PragmaComment(Loc, Kind, Str);
127    Second->PragmaComment(Loc, Kind, Str);
128  }
129
130  virtual void MacroExpands(const Token &Id, const MacroInfo* MI) {
131    First->MacroExpands(Id, MI);
132    Second->MacroExpands(Id, MI);
133  }
134
135  virtual void MacroDefined(const IdentifierInfo *II, const MacroInfo *MI) {
136    First->MacroDefined(II, MI);
137    Second->MacroDefined(II, MI);
138  }
139
140  virtual void MacroUndefined(const IdentifierInfo *II, const MacroInfo *MI) {
141    First->MacroUndefined(II, MI);
142    Second->MacroUndefined(II, MI);
143  }
144};
145
146}  // end namespace clang
147
148#endif
149