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).
46 /// \param PrevFID the file that was exited if \arg Reason is ExitFile.
47 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
48 SrcMgr::CharacteristicKind FileType,
49 FileID PrevFID = FileID()) {
52 /// FileSkipped - This callback is invoked whenever a source file is
53 /// skipped as the result of header guard optimization. ParentFile
54 /// is the file that #includes the skipped file. FilenameTok is the
55 /// token in ParentFile that indicates the skipped file.
56 virtual void FileSkipped(const FileEntry &ParentFile,
57 const Token &FilenameTok,
58 SrcMgr::CharacteristicKind FileType) {
61 /// \brief This callback is invoked whenever an inclusion directive of
62 /// any kind (\c #include, \c #import, etc.) has been processed, regardless
63 /// of whether the inclusion will actually result in an inclusion.
65 /// \param HashLoc The location of the '#' that starts the inclusion
68 /// \param IncludeTok The token that indicates the kind of inclusion
69 /// directive, e.g., 'include' or 'import'.
71 /// \param FileName The name of the file being included, as written in the
74 /// \param IsAngled Whether the file name was enclosed in angle brackets;
75 /// otherwise, it was enclosed in quotes.
77 /// \param File The actual file that may be included by this inclusion
80 /// \param EndLoc The location of the last token within the inclusion
83 /// \param SearchPath Contains the search path which was used to find the file
84 /// in the file system. If the file was found via an absolute include path,
85 /// SearchPath will be empty. For framework includes, the SearchPath and
86 /// RelativePath will be split up. For example, if an include of "Some/Some.h"
87 /// is found via the framework path
88 /// "path/to/Frameworks/Some.framework/Headers/Some.h", SearchPath will be
89 /// "path/to/Frameworks/Some.framework/Headers" and RelativePath will be
92 /// \param RelativePath The path relative to SearchPath, at which the include
93 /// file was found. This is equal to FileName except for framework includes.
94 virtual void InclusionDirective(SourceLocation HashLoc,
95 const Token &IncludeTok,
98 const FileEntry *File,
99 SourceLocation EndLoc,
100 StringRef SearchPath,
101 StringRef RelativePath) {
104 /// EndOfMainFile - This callback is invoked when the end of the main file is
105 /// reach, no subsequent callbacks will be made.
106 virtual void EndOfMainFile() {
109 /// Ident - This callback is invoked when a #ident or #sccs directive is read.
110 /// \param Loc The location of the directive.
111 /// \param str The text of the directive.
113 virtual void Ident(SourceLocation Loc, const std::string &str) {
116 /// PragmaComment - This callback is invoked when a #pragma comment directive
119 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
120 const std::string &Str) {
123 /// PragmaMessage - This callback is invoked when a #pragma message directive
125 /// \param Loc The location of the message directive.
126 /// \param str The text of the message directive.
128 virtual void PragmaMessage(SourceLocation Loc, StringRef Str) {
131 /// PragmaDiagnosticPush - This callback is invoked when a
132 /// #pragma gcc dianostic push directive is read.
133 virtual void PragmaDiagnosticPush(SourceLocation Loc,
134 StringRef Namespace) {
137 /// PragmaDiagnosticPop - This callback is invoked when a
138 /// #pragma gcc dianostic pop directive is read.
139 virtual void PragmaDiagnosticPop(SourceLocation Loc,
140 StringRef Namespace) {
143 /// PragmaDiagnostic - This callback is invoked when a
144 /// #pragma gcc dianostic directive is read.
145 virtual void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
146 diag::Mapping mapping, StringRef Str) {
149 /// MacroExpands - This is called by
150 /// Preprocessor::HandleMacroExpandedIdentifier when a macro invocation is
152 virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo* MI,
156 /// MacroDefined - This hook is called whenever a macro definition is seen.
157 virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) {
160 /// MacroUndefined - This hook is called whenever a macro #undef is seen.
161 /// MI is released immediately following this callback.
162 virtual void MacroUndefined(const Token &MacroNameTok, const MacroInfo *MI) {
165 /// Defined - This hook is called whenever the 'defined' operator is seen.
166 virtual void Defined(const Token &MacroNameTok) {
169 /// SourceRangeSkipped - This hook is called when a source range is skipped.
170 /// \param Range The SourceRange that was skipped. The range begins at the
171 /// #if/#else directive and ends after the #endif/#else directive.
172 virtual void SourceRangeSkipped(SourceRange Range) {
175 /// If -- This hook is called whenever an #if is seen.
176 /// \param Range The SourceRange of the expression being tested.
177 // FIXME: better to pass in a list (or tree!) of Tokens.
178 virtual void If(SourceRange Range) {
181 /// Elif -- This hook is called whenever an #elif is seen.
182 /// \param Range The SourceRange of the expression being tested.
183 // FIXME: better to pass in a list (or tree!) of Tokens.
184 virtual void Elif(SourceRange Range) {
187 /// Ifdef -- This hook is called whenever an #ifdef is seen.
188 /// \param Loc The location of the token being tested.
189 /// \param II Information on the token being tested.
190 virtual void Ifdef(const Token &MacroNameTok) {
193 /// Ifndef -- This hook is called whenever an #ifndef is seen.
194 /// \param Loc The location of the token being tested.
195 /// \param II Information on the token being tested.
196 virtual void Ifndef(const Token &MacroNameTok) {
199 /// Else -- This hook is called whenever an #else is seen.
200 virtual void Else() {
203 /// Endif -- This hook is called whenever an #endif is seen.
204 virtual void Endif() {
208 /// PPChainedCallbacks - Simple wrapper class for chaining callbacks.
209 class PPChainedCallbacks : public PPCallbacks {
210 PPCallbacks *First, *Second;
213 PPChainedCallbacks(PPCallbacks *_First, PPCallbacks *_Second)
214 : First(_First), Second(_Second) {}
215 ~PPChainedCallbacks() {
220 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
221 SrcMgr::CharacteristicKind FileType,
223 First->FileChanged(Loc, Reason, FileType, PrevFID);
224 Second->FileChanged(Loc, Reason, FileType, PrevFID);
227 virtual void FileSkipped(const FileEntry &ParentFile,
228 const Token &FilenameTok,
229 SrcMgr::CharacteristicKind FileType) {
230 First->FileSkipped(ParentFile, FilenameTok, FileType);
231 Second->FileSkipped(ParentFile, FilenameTok, FileType);
234 virtual void InclusionDirective(SourceLocation HashLoc,
235 const Token &IncludeTok,
238 const FileEntry *File,
239 SourceLocation EndLoc,
240 StringRef SearchPath,
241 StringRef RelativePath) {
242 First->InclusionDirective(HashLoc, IncludeTok, FileName, IsAngled, File,
243 EndLoc, SearchPath, RelativePath);
244 Second->InclusionDirective(HashLoc, IncludeTok, FileName, IsAngled, File,
245 EndLoc, SearchPath, RelativePath);
248 virtual void EndOfMainFile() {
249 First->EndOfMainFile();
250 Second->EndOfMainFile();
253 virtual void Ident(SourceLocation Loc, const std::string &str) {
254 First->Ident(Loc, str);
255 Second->Ident(Loc, str);
258 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
259 const std::string &Str) {
260 First->PragmaComment(Loc, Kind, Str);
261 Second->PragmaComment(Loc, Kind, Str);
264 virtual void PragmaMessage(SourceLocation Loc, StringRef Str) {
265 First->PragmaMessage(Loc, Str);
266 Second->PragmaMessage(Loc, Str);
269 virtual void PragmaDiagnosticPush(SourceLocation Loc,
270 StringRef Namespace) {
271 First->PragmaDiagnosticPush(Loc, Namespace);
272 Second->PragmaDiagnosticPush(Loc, Namespace);
275 virtual void PragmaDiagnosticPop(SourceLocation Loc,
276 StringRef Namespace) {
277 First->PragmaDiagnosticPop(Loc, Namespace);
278 Second->PragmaDiagnosticPop(Loc, Namespace);
281 virtual void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
282 diag::Mapping mapping, StringRef Str) {
283 First->PragmaDiagnostic(Loc, Namespace, mapping, Str);
284 Second->PragmaDiagnostic(Loc, Namespace, mapping, Str);
287 virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo* MI,
289 First->MacroExpands(MacroNameTok, MI, Range);
290 Second->MacroExpands(MacroNameTok, MI, Range);
293 virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) {
294 First->MacroDefined(MacroNameTok, MI);
295 Second->MacroDefined(MacroNameTok, MI);
298 virtual void MacroUndefined(const Token &MacroNameTok, const MacroInfo *MI) {
299 First->MacroUndefined(MacroNameTok, MI);
300 Second->MacroUndefined(MacroNameTok, MI);
303 virtual void Defined(const Token &MacroNameTok) {
304 First->Defined(MacroNameTok);
305 Second->Defined(MacroNameTok);
308 virtual void SourceRangeSkipped(SourceRange Range) {
309 First->SourceRangeSkipped(Range);
310 Second->SourceRangeSkipped(Range);
313 /// If -- This hook is called whenever an #if is seen.
314 virtual void If(SourceRange Range) {
319 /// Elif -- This hook is called whenever an #if is seen.
320 virtual void Elif(SourceRange Range) {
325 /// Ifdef -- This hook is called whenever an #ifdef is seen.
326 virtual void Ifdef(const Token &MacroNameTok) {
327 First->Ifdef(MacroNameTok);
328 Second->Ifdef(MacroNameTok);
331 /// Ifndef -- This hook is called whenever an #ifndef is seen.
332 virtual void Ifndef(const Token &MacroNameTok) {
333 First->Ifndef(MacroNameTok);
334 Second->Ifndef(MacroNameTok);
337 /// Else -- This hook is called whenever an #else is seen.
338 virtual void Else() {
343 /// Endif -- This hook is called whenever an #endif is seen.
344 virtual void Endif() {
350 } // end namespace clang