PPCallbacks.h revision 218893
1193326Sed//===--- PPCallbacks.h - Callbacks for Preprocessor actions -----*- C++ -*-===//
2193326Sed//
3193326Sed//                     The LLVM Compiler Infrastructure
4193326Sed//
5193326Sed// This file is distributed under the University of Illinois Open Source
6193326Sed// License. See LICENSE.TXT for details.
7193326Sed//
8193326Sed//===----------------------------------------------------------------------===//
9193326Sed//
10193326Sed//  This file defines the PPCallbacks interface.
11193326Sed//
12193326Sed//===----------------------------------------------------------------------===//
13193326Sed
14193326Sed#ifndef LLVM_CLANG_LEX_PPCALLBACKS_H
15193326Sed#define LLVM_CLANG_LEX_PPCALLBACKS_H
16193326Sed
17193326Sed#include "clang/Lex/DirectoryLookup.h"
18193326Sed#include "clang/Basic/SourceLocation.h"
19210299Sed#include "llvm/ADT/StringRef.h"
20193326Sed#include <string>
21193326Sed
22193326Sednamespace clang {
23193326Sed  class SourceLocation;
24193326Sed  class Token;
25193326Sed  class IdentifierInfo;
26193326Sed  class MacroInfo;
27198092Srdivacky
28193326Sed/// PPCallbacks - This interface provides a way to observe the actions of the
29193326Sed/// preprocessor as it does its thing.  Clients can define their hooks here to
30193326Sed/// implement preprocessor level tools.
31193326Sedclass PPCallbacks {
32193326Sedpublic:
33193326Sed  virtual ~PPCallbacks();
34198092Srdivacky
35193326Sed  enum FileChangeReason {
36193326Sed    EnterFile, ExitFile, SystemHeaderPragma, RenameFile
37193326Sed  };
38198092Srdivacky
39193326Sed  /// FileChanged - This callback is invoked whenever a source file is
40193326Sed  /// entered or exited.  The SourceLocation indicates the new location, and
41193326Sed  /// EnteringFile indicates whether this is because we are entering a new
42193326Sed  /// #include'd file (when true) or whether we're exiting one because we ran
43193326Sed  /// off the end (when false).
44193326Sed  virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
45193326Sed                           SrcMgr::CharacteristicKind FileType) {
46193326Sed  }
47198092Srdivacky
48207619Srdivacky  /// FileSkipped - This callback is invoked whenever a source file is
49207619Srdivacky  /// skipped as the result of header guard optimization.  ParentFile
50207619Srdivacky  /// is the file that #includes the skipped file.  FilenameTok is the
51207619Srdivacky  /// token in ParentFile that indicates the skipped file.
52207619Srdivacky  virtual void FileSkipped(const FileEntry &ParentFile,
53207619Srdivacky                           const Token &FilenameTok,
54207619Srdivacky                           SrcMgr::CharacteristicKind FileType) {
55207619Srdivacky  }
56206084Srdivacky
57218893Sdim  /// \brief This callback is invoked whenever an inclusion directive of
58218893Sdim  /// any kind (\c #include, \c #import, etc.) has been processed, regardless
59218893Sdim  /// of whether the inclusion will actually result in an inclusion.
60218893Sdim  ///
61218893Sdim  /// \param HashLoc The location of the '#' that starts the inclusion
62218893Sdim  /// directive.
63218893Sdim  ///
64218893Sdim  /// \param IncludeTok The token that indicates the kind of inclusion
65218893Sdim  /// directive, e.g., 'include' or 'import'.
66218893Sdim  ///
67218893Sdim  /// \param FileName The name of the file being included, as written in the
68218893Sdim  /// source code.
69218893Sdim  ///
70218893Sdim  /// \param IsAngled Whether the file name was enclosed in angle brackets;
71218893Sdim  /// otherwise, it was enclosed in quotes.
72218893Sdim  ///
73218893Sdim  /// \param File The actual file that may be included by this inclusion
74218893Sdim  /// directive.
75218893Sdim  ///
76218893Sdim  /// \param EndLoc The location of the last token within the inclusion
77218893Sdim  /// directive.
78218893Sdim  virtual void InclusionDirective(SourceLocation HashLoc,
79218893Sdim                                  const Token &IncludeTok,
80218893Sdim                                  llvm::StringRef FileName,
81218893Sdim                                  bool IsAngled,
82218893Sdim                                  const FileEntry *File,
83218893Sdim                                  SourceLocation EndLoc) {
84218893Sdim  }
85218893Sdim
86206084Srdivacky  /// EndOfMainFile - This callback is invoked when the end of the main file is
87206084Srdivacky  /// reach, no subsequent callbacks will be made.
88206084Srdivacky  virtual void EndOfMainFile() {
89206084Srdivacky  }
90206084Srdivacky
91193326Sed  /// Ident - This callback is invoked when a #ident or #sccs directive is read.
92218893Sdim  /// \param Loc The location of the directive.
93218893Sdim  /// \param str The text of the directive.
94193326Sed  ///
95193326Sed  virtual void Ident(SourceLocation Loc, const std::string &str) {
96193326Sed  }
97198092Srdivacky
98193326Sed  /// PragmaComment - This callback is invoked when a #pragma comment directive
99193326Sed  /// is read.
100193326Sed  ///
101198092Srdivacky  virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
102193326Sed                             const std::string &Str) {
103193326Sed  }
104198092Srdivacky
105210299Sed  /// PragmaMessage - This callback is invoked when a #pragma message directive
106210299Sed  /// is read.
107218893Sdim  /// \param Loc The location of the message directive.
108218893Sdim  /// \param str The text of the message directive.
109210299Sed  ///
110210299Sed  virtual void PragmaMessage(SourceLocation Loc, llvm::StringRef Str) {
111210299Sed  }
112210299Sed
113193326Sed  /// MacroExpands - This is called by
114193326Sed  /// Preprocessor::HandleMacroExpandedIdentifier when a macro invocation is
115193326Sed  /// found.
116218893Sdim  virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo* MI) {
117193326Sed  }
118198092Srdivacky
119193326Sed  /// MacroDefined - This hook is called whenever a macro definition is seen.
120218893Sdim  virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) {
121193326Sed  }
122193326Sed
123193326Sed  /// MacroUndefined - This hook is called whenever a macro #undef is seen.
124193326Sed  /// MI is released immediately following this callback.
125218893Sdim  virtual void MacroUndefined(const Token &MacroNameTok, const MacroInfo *MI) {
126193326Sed  }
127218893Sdim
128218893Sdim  /// If -- This hook is called whenever an #if is seen.
129218893Sdim  /// \param Range The SourceRange of the expression being tested.
130218893Sdim  // FIXME: better to pass in a list (or tree!) of Tokens.
131218893Sdim  virtual void If(SourceRange Range) {
132218893Sdim  }
133218893Sdim
134218893Sdim  /// Elif -- This hook is called whenever an #elif is seen.
135218893Sdim  /// \param Range The SourceRange of the expression being tested.
136218893Sdim  // FIXME: better to pass in a list (or tree!) of Tokens.
137218893Sdim  virtual void Elif(SourceRange Range) {
138218893Sdim  }
139218893Sdim
140218893Sdim  /// Ifdef -- This hook is called whenever an #ifdef is seen.
141218893Sdim  /// \param Loc The location of the token being tested.
142218893Sdim  /// \param II Information on the token being tested.
143218893Sdim  virtual void Ifdef(const Token &MacroNameTok) {
144218893Sdim  }
145218893Sdim
146218893Sdim  /// Ifndef -- This hook is called whenever an #ifndef is seen.
147218893Sdim  /// \param Loc The location of the token being tested.
148218893Sdim  /// \param II Information on the token being tested.
149218893Sdim  virtual void Ifndef(const Token &MacroNameTok) {
150218893Sdim  }
151218893Sdim
152218893Sdim  /// Else -- This hook is called whenever an #else is seen.
153218893Sdim  virtual void Else() {
154218893Sdim  }
155218893Sdim
156218893Sdim  /// Endif -- This hook is called whenever an #endif is seen.
157218893Sdim  virtual void Endif() {
158218893Sdim  }
159193326Sed};
160193326Sed
161193326Sed/// PPChainedCallbacks - Simple wrapper class for chaining callbacks.
162193326Sedclass PPChainedCallbacks : public PPCallbacks {
163193326Sed  PPCallbacks *First, *Second;
164193326Sed
165198092Srdivackypublic:
166193326Sed  PPChainedCallbacks(PPCallbacks *_First, PPCallbacks *_Second)
167193326Sed    : First(_First), Second(_Second) {}
168193326Sed  ~PPChainedCallbacks() {
169193326Sed    delete Second;
170193326Sed    delete First;
171193326Sed  }
172193326Sed
173193326Sed  virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
174193326Sed                           SrcMgr::CharacteristicKind FileType) {
175193326Sed    First->FileChanged(Loc, Reason, FileType);
176193326Sed    Second->FileChanged(Loc, Reason, FileType);
177193326Sed  }
178198092Srdivacky
179207619Srdivacky  virtual void FileSkipped(const FileEntry &ParentFile,
180207619Srdivacky                           const Token &FilenameTok,
181207619Srdivacky                           SrcMgr::CharacteristicKind FileType) {
182207619Srdivacky    First->FileSkipped(ParentFile, FilenameTok, FileType);
183207619Srdivacky    Second->FileSkipped(ParentFile, FilenameTok, FileType);
184207619Srdivacky  }
185207619Srdivacky
186218893Sdim  virtual void InclusionDirective(SourceLocation HashLoc,
187218893Sdim                                  const Token &IncludeTok,
188218893Sdim                                  llvm::StringRef FileName,
189218893Sdim                                  bool IsAngled,
190218893Sdim                                  const FileEntry *File,
191218893Sdim                                  SourceLocation EndLoc) {
192218893Sdim    First->InclusionDirective(HashLoc, IncludeTok, FileName, IsAngled, File,
193218893Sdim                              EndLoc);
194218893Sdim    Second->InclusionDirective(HashLoc, IncludeTok, FileName, IsAngled, File,
195218893Sdim                               EndLoc);
196218893Sdim  }
197218893Sdim
198206084Srdivacky  virtual void EndOfMainFile() {
199206084Srdivacky    First->EndOfMainFile();
200206084Srdivacky    Second->EndOfMainFile();
201206084Srdivacky  }
202206084Srdivacky
203193326Sed  virtual void Ident(SourceLocation Loc, const std::string &str) {
204193326Sed    First->Ident(Loc, str);
205193326Sed    Second->Ident(Loc, str);
206193326Sed  }
207198092Srdivacky
208198092Srdivacky  virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
209193326Sed                             const std::string &Str) {
210193326Sed    First->PragmaComment(Loc, Kind, Str);
211193326Sed    Second->PragmaComment(Loc, Kind, Str);
212193326Sed  }
213198092Srdivacky
214210299Sed  virtual void PragmaMessage(SourceLocation Loc, llvm::StringRef Str) {
215210299Sed    First->PragmaMessage(Loc, Str);
216210299Sed    Second->PragmaMessage(Loc, Str);
217210299Sed  }
218210299Sed
219218893Sdim  virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo* MI) {
220218893Sdim    First->MacroExpands(MacroNameTok, MI);
221218893Sdim    Second->MacroExpands(MacroNameTok, MI);
222193326Sed  }
223198092Srdivacky
224218893Sdim  virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) {
225218893Sdim    First->MacroDefined(MacroNameTok, MI);
226218893Sdim    Second->MacroDefined(MacroNameTok, MI);
227193326Sed  }
228193326Sed
229218893Sdim  virtual void MacroUndefined(const Token &MacroNameTok, const MacroInfo *MI) {
230218893Sdim    First->MacroUndefined(MacroNameTok, MI);
231218893Sdim    Second->MacroUndefined(MacroNameTok, MI);
232193326Sed  }
233218893Sdim
234218893Sdim  /// If -- This hook is called whenever an #if is seen.
235218893Sdim  virtual void If(SourceRange Range) {
236218893Sdim    First->If(Range);
237218893Sdim    Second->If(Range);
238218893Sdim  }
239218893Sdim
240218893Sdim  /// Elif -- This hook is called whenever an #if is seen.
241218893Sdim  virtual void Elif(SourceRange Range) {
242218893Sdim    First->Elif(Range);
243218893Sdim    Second->Elif(Range);
244218893Sdim  }
245218893Sdim
246218893Sdim  /// Ifdef -- This hook is called whenever an #ifdef is seen.
247218893Sdim  virtual void Ifdef(const Token &MacroNameTok) {
248218893Sdim    First->Ifdef(MacroNameTok);
249218893Sdim    Second->Ifdef(MacroNameTok);
250218893Sdim  }
251218893Sdim
252218893Sdim  /// Ifndef -- This hook is called whenever an #ifndef is seen.
253218893Sdim  virtual void Ifndef(const Token &MacroNameTok) {
254218893Sdim    First->Ifndef(MacroNameTok);
255218893Sdim    Second->Ifndef(MacroNameTok);
256218893Sdim  }
257218893Sdim
258218893Sdim  /// Else -- This hook is called whenever an #else is seen.
259218893Sdim  virtual void Else() {
260218893Sdim    First->Else();
261218893Sdim    Second->Else();
262218893Sdim  }
263218893Sdim
264218893Sdim  /// Endif -- This hook is called whenever an #endif is seen.
265218893Sdim  virtual void Endif() {
266218893Sdim    First->Endif();
267218893Sdim    Second->Endif();
268218893Sdim  }
269193326Sed};
270193326Sed
271193326Sed}  // end namespace clang
272193326Sed
273193326Sed#endif
274