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