1 //===--- PPCallbacks.h - Callbacks for Preprocessor actions -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the PPCallbacks interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LEX_PPCALLBACKS_H
15 #define LLVM_CLANG_LEX_PPCALLBACKS_H
17 #include "clang/Lex/DirectoryLookup.h"
18 #include "clang/Basic/SourceLocation.h"
19 #include "clang/Basic/DiagnosticIDs.h"
20 #include "llvm/ADT/StringRef.h"
29 /// PPCallbacks - This interface provides a way to observe the actions of the
30 /// preprocessor as it does its thing. Clients can define their hooks here to
31 /// implement preprocessor level tools.
34 virtual ~PPCallbacks();
36 enum FileChangeReason {
37 EnterFile, ExitFile, SystemHeaderPragma, RenameFile
40 /// FileChanged - This callback is invoked whenever a source file is
41 /// entered or exited. The SourceLocation indicates the new location, and
42 /// EnteringFile indicates whether this is because we are entering a new
43 /// #include'd file (when true) or whether we're exiting one because we ran
44 /// off the end (when false).
45 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
46 SrcMgr::CharacteristicKind FileType) {
49 /// FileSkipped - This callback is invoked whenever a source file is
50 /// skipped as the result of header guard optimization. ParentFile
51 /// is the file that #includes the skipped file. FilenameTok is the
52 /// token in ParentFile that indicates the skipped file.
53 virtual void FileSkipped(const FileEntry &ParentFile,
54 const Token &FilenameTok,
55 SrcMgr::CharacteristicKind FileType) {
58 /// \brief This callback is invoked whenever an inclusion directive of
59 /// any kind (\c #include, \c #import, etc.) has been processed, regardless
60 /// of whether the inclusion will actually result in an inclusion.
62 /// \param HashLoc The location of the '#' that starts the inclusion
65 /// \param IncludeTok The token that indicates the kind of inclusion
66 /// directive, e.g., 'include' or 'import'.
68 /// \param FileName The name of the file being included, as written in the
71 /// \param IsAngled Whether the file name was enclosed in angle brackets;
72 /// otherwise, it was enclosed in quotes.
74 /// \param File The actual file that may be included by this inclusion
77 /// \param EndLoc The location of the last token within the inclusion
80 /// \param SearchPath Contains the search path which was used to find the file
81 /// in the file system. If the file was found via an absolute include path,
82 /// SearchPath will be empty. For framework includes, the SearchPath and
83 /// RelativePath will be split up. For example, if an include of "Some/Some.h"
84 /// is found via the framework path
85 /// "path/to/Frameworks/Some.framework/Headers/Some.h", SearchPath will be
86 /// "path/to/Frameworks/Some.framework/Headers" and RelativePath will be
89 /// \param RelativePath The path relative to SearchPath, at which the include
90 /// file was found. This is equal to FileName except for framework includes.
91 virtual void InclusionDirective(SourceLocation HashLoc,
92 const Token &IncludeTok,
93 llvm::StringRef FileName,
95 const FileEntry *File,
96 SourceLocation EndLoc,
97 llvm::StringRef SearchPath,
98 llvm::StringRef RelativePath) {
101 /// EndOfMainFile - This callback is invoked when the end of the main file is
102 /// reach, no subsequent callbacks will be made.
103 virtual void EndOfMainFile() {
106 /// Ident - This callback is invoked when a #ident or #sccs directive is read.
107 /// \param Loc The location of the directive.
108 /// \param str The text of the directive.
110 virtual void Ident(SourceLocation Loc, const std::string &str) {
113 /// PragmaComment - This callback is invoked when a #pragma comment directive
116 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
117 const std::string &Str) {
120 /// PragmaMessage - This callback is invoked when a #pragma message directive
122 /// \param Loc The location of the message directive.
123 /// \param str The text of the message directive.
125 virtual void PragmaMessage(SourceLocation Loc, llvm::StringRef Str) {
128 /// PragmaDiagnosticPush - This callback is invoked when a
129 /// #pragma gcc dianostic push directive is read.
130 virtual void PragmaDiagnosticPush(SourceLocation Loc,
131 llvm::StringRef Namespace) {
134 /// PragmaDiagnosticPop - This callback is invoked when a
135 /// #pragma gcc dianostic pop directive is read.
136 virtual void PragmaDiagnosticPop(SourceLocation Loc,
137 llvm::StringRef Namespace) {
140 /// PragmaDiagnostic - This callback is invoked when a
141 /// #pragma gcc dianostic directive is read.
142 virtual void PragmaDiagnostic(SourceLocation Loc, llvm::StringRef Namespace,
143 diag::Mapping mapping, llvm::StringRef Str) {
146 /// MacroExpands - This is called by
147 /// Preprocessor::HandleMacroExpandedIdentifier when a macro invocation is
149 virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo* MI) {
152 /// MacroDefined - This hook is called whenever a macro definition is seen.
153 virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) {
156 /// MacroUndefined - This hook is called whenever a macro #undef is seen.
157 /// MI is released immediately following this callback.
158 virtual void MacroUndefined(const Token &MacroNameTok, const MacroInfo *MI) {
161 /// If -- This hook is called whenever an #if is seen.
162 /// \param Range The SourceRange of the expression being tested.
163 // FIXME: better to pass in a list (or tree!) of Tokens.
164 virtual void If(SourceRange Range) {
167 /// Elif -- This hook is called whenever an #elif is seen.
168 /// \param Range The SourceRange of the expression being tested.
169 // FIXME: better to pass in a list (or tree!) of Tokens.
170 virtual void Elif(SourceRange Range) {
173 /// Ifdef -- This hook is called whenever an #ifdef is seen.
174 /// \param Loc The location of the token being tested.
175 /// \param II Information on the token being tested.
176 virtual void Ifdef(const Token &MacroNameTok) {
179 /// Ifndef -- This hook is called whenever an #ifndef is seen.
180 /// \param Loc The location of the token being tested.
181 /// \param II Information on the token being tested.
182 virtual void Ifndef(const Token &MacroNameTok) {
185 /// Else -- This hook is called whenever an #else is seen.
186 virtual void Else() {
189 /// Endif -- This hook is called whenever an #endif is seen.
190 virtual void Endif() {
194 /// PPChainedCallbacks - Simple wrapper class for chaining callbacks.
195 class PPChainedCallbacks : public PPCallbacks {
196 PPCallbacks *First, *Second;
199 PPChainedCallbacks(PPCallbacks *_First, PPCallbacks *_Second)
200 : First(_First), Second(_Second) {}
201 ~PPChainedCallbacks() {
206 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
207 SrcMgr::CharacteristicKind FileType) {
208 First->FileChanged(Loc, Reason, FileType);
209 Second->FileChanged(Loc, Reason, FileType);
212 virtual void FileSkipped(const FileEntry &ParentFile,
213 const Token &FilenameTok,
214 SrcMgr::CharacteristicKind FileType) {
215 First->FileSkipped(ParentFile, FilenameTok, FileType);
216 Second->FileSkipped(ParentFile, FilenameTok, FileType);
219 virtual void InclusionDirective(SourceLocation HashLoc,
220 const Token &IncludeTok,
221 llvm::StringRef FileName,
223 const FileEntry *File,
224 SourceLocation EndLoc,
225 llvm::StringRef SearchPath,
226 llvm::StringRef RelativePath) {
227 First->InclusionDirective(HashLoc, IncludeTok, FileName, IsAngled, File,
228 EndLoc, SearchPath, RelativePath);
229 Second->InclusionDirective(HashLoc, IncludeTok, FileName, IsAngled, File,
230 EndLoc, SearchPath, RelativePath);
233 virtual void EndOfMainFile() {
234 First->EndOfMainFile();
235 Second->EndOfMainFile();
238 virtual void Ident(SourceLocation Loc, const std::string &str) {
239 First->Ident(Loc, str);
240 Second->Ident(Loc, str);
243 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
244 const std::string &Str) {
245 First->PragmaComment(Loc, Kind, Str);
246 Second->PragmaComment(Loc, Kind, Str);
249 virtual void PragmaMessage(SourceLocation Loc, llvm::StringRef Str) {
250 First->PragmaMessage(Loc, Str);
251 Second->PragmaMessage(Loc, Str);
254 virtual void PragmaDiagnosticPush(SourceLocation Loc,
255 llvm::StringRef Namespace) {
256 First->PragmaDiagnosticPush(Loc, Namespace);
257 Second->PragmaDiagnosticPush(Loc, Namespace);
260 virtual void PragmaDiagnosticPop(SourceLocation Loc,
261 llvm::StringRef Namespace) {
262 First->PragmaDiagnosticPop(Loc, Namespace);
263 Second->PragmaDiagnosticPop(Loc, Namespace);
266 virtual void PragmaDiagnostic(SourceLocation Loc, llvm::StringRef Namespace,
267 diag::Mapping mapping, llvm::StringRef Str) {
268 First->PragmaDiagnostic(Loc, Namespace, mapping, Str);
269 Second->PragmaDiagnostic(Loc, Namespace, mapping, Str);
272 virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo* MI) {
273 First->MacroExpands(MacroNameTok, MI);
274 Second->MacroExpands(MacroNameTok, MI);
277 virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) {
278 First->MacroDefined(MacroNameTok, MI);
279 Second->MacroDefined(MacroNameTok, MI);
282 virtual void MacroUndefined(const Token &MacroNameTok, const MacroInfo *MI) {
283 First->MacroUndefined(MacroNameTok, MI);
284 Second->MacroUndefined(MacroNameTok, MI);
287 /// If -- This hook is called whenever an #if is seen.
288 virtual void If(SourceRange Range) {
293 /// Elif -- This hook is called whenever an #if is seen.
294 virtual void Elif(SourceRange Range) {
299 /// Ifdef -- This hook is called whenever an #ifdef is seen.
300 virtual void Ifdef(const Token &MacroNameTok) {
301 First->Ifdef(MacroNameTok);
302 Second->Ifdef(MacroNameTok);
305 /// Ifndef -- This hook is called whenever an #ifndef is seen.
306 virtual void Ifndef(const Token &MacroNameTok) {
307 First->Ifndef(MacroNameTok);
308 Second->Ifndef(MacroNameTok);
311 /// Else -- This hook is called whenever an #else is seen.
312 virtual void Else() {
317 /// Endif -- This hook is called whenever an #endif is seen.
318 virtual void Endif() {
324 } // end namespace clang