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 //===----------------------------------------------------------------------===//
11 /// \brief Defines the PPCallbacks interface.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_LEX_PPCALLBACKS_H
16 #define LLVM_CLANG_LEX_PPCALLBACKS_H
18 #include "clang/Basic/DiagnosticIDs.h"
19 #include "clang/Basic/SourceLocation.h"
20 #include "clang/Lex/DirectoryLookup.h"
21 #include "clang/Lex/ModuleLoader.h"
22 #include "clang/Lex/Pragma.h"
23 #include "llvm/ADT/StringRef.h"
30 class MacroDefinition;
34 /// \brief This interface provides a way to observe the actions of the
35 /// preprocessor as it does its thing.
37 /// Clients can define their hooks here to implement preprocessor level tools.
40 virtual ~PPCallbacks();
42 enum FileChangeReason {
43 EnterFile, ExitFile, SystemHeaderPragma, RenameFile
46 /// \brief Callback invoked whenever a source file is entered or exited.
48 /// \param Loc Indicates the new location.
49 /// \param PrevFID the file that was exited if \p Reason is ExitFile.
50 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
51 SrcMgr::CharacteristicKind FileType,
52 FileID PrevFID = FileID()) {
55 /// \brief Callback invoked whenever a source file is skipped as the result
56 /// of header guard optimization.
58 /// \param SkippedFile The file that is skipped instead of entering \#include
60 /// \param FilenameTok The file name token in \#include "FileName" directive
61 /// or macro expanded file name token from \#include MACRO(PARAMS) directive.
62 /// Note that FilenameTok contains corresponding quotes/angles symbols.
63 virtual void FileSkipped(const FileEntry &SkippedFile,
64 const Token &FilenameTok,
65 SrcMgr::CharacteristicKind FileType) {
68 /// \brief Callback invoked whenever an inclusion directive results in a
69 /// file-not-found error.
71 /// \param FileName The name of the file being included, as written in the
74 /// \param RecoveryPath If this client indicates that it can recover from
75 /// this missing file, the client should set this as an additional header
78 /// \returns true to indicate that the preprocessor should attempt to recover
79 /// by adding \p RecoveryPath as a header search path.
80 virtual bool FileNotFound(StringRef FileName,
81 SmallVectorImpl<char> &RecoveryPath) {
85 /// \brief Callback invoked whenever an inclusion directive of
86 /// any kind (\c \#include, \c \#import, etc.) has been processed, regardless
87 /// of whether the inclusion will actually result in an inclusion.
89 /// \param HashLoc The location of the '#' that starts the inclusion
92 /// \param IncludeTok The token that indicates the kind of inclusion
93 /// directive, e.g., 'include' or 'import'.
95 /// \param FileName The name of the file being included, as written in the
98 /// \param IsAngled Whether the file name was enclosed in angle brackets;
99 /// otherwise, it was enclosed in quotes.
101 /// \param FilenameRange The character range of the quotes or angle brackets
102 /// for the written file name.
104 /// \param File The actual file that may be included by this inclusion
107 /// \param SearchPath Contains the search path which was used to find the file
108 /// in the file system. If the file was found via an absolute include path,
109 /// SearchPath will be empty. For framework includes, the SearchPath and
110 /// RelativePath will be split up. For example, if an include of "Some/Some.h"
111 /// is found via the framework path
112 /// "path/to/Frameworks/Some.framework/Headers/Some.h", SearchPath will be
113 /// "path/to/Frameworks/Some.framework/Headers" and RelativePath will be
116 /// \param RelativePath The path relative to SearchPath, at which the include
117 /// file was found. This is equal to FileName except for framework includes.
119 /// \param Imported The module, whenever an inclusion directive was
120 /// automatically turned into a module import or null otherwise.
122 virtual void InclusionDirective(SourceLocation HashLoc,
123 const Token &IncludeTok,
126 CharSourceRange FilenameRange,
127 const FileEntry *File,
128 StringRef SearchPath,
129 StringRef RelativePath,
130 const Module *Imported) {
133 /// \brief Callback invoked whenever there was an explicit module-import
136 /// \param ImportLoc The location of import directive token.
138 /// \param Path The identifiers (and their locations) of the module
139 /// "path", e.g., "std.vector" would be split into "std" and "vector".
141 /// \param Imported The imported module; can be null if importing failed.
143 virtual void moduleImport(SourceLocation ImportLoc,
145 const Module *Imported) {
148 /// \brief Callback invoked when the end of the main file is reached.
150 /// No subsequent callbacks will be made.
151 virtual void EndOfMainFile() {
154 /// \brief Callback invoked when a \#ident or \#sccs directive is read.
155 /// \param Loc The location of the directive.
156 /// \param str The text of the directive.
158 virtual void Ident(SourceLocation Loc, const std::string &str) {
161 /// \brief Callback invoked when start reading any pragma directive.
162 virtual void PragmaDirective(SourceLocation Loc,
163 PragmaIntroducerKind Introducer) {
166 /// \brief Callback invoked when a \#pragma comment directive is read.
167 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
168 const std::string &Str) {
171 /// \brief Callback invoked when a \#pragma detect_mismatch directive is
173 virtual void PragmaDetectMismatch(SourceLocation Loc,
174 const std::string &Name,
175 const std::string &Value) {
178 /// \brief Callback invoked when a \#pragma clang __debug directive is read.
179 /// \param Loc The location of the debug directive.
180 /// \param DebugType The identifier following __debug.
181 virtual void PragmaDebug(SourceLocation Loc, StringRef DebugType) {
184 /// \brief Determines the kind of \#pragma invoking a call to PragmaMessage.
185 enum PragmaMessageKind {
186 /// \brief \#pragma message has been invoked.
189 /// \brief \#pragma GCC warning has been invoked.
192 /// \brief \#pragma GCC error has been invoked.
196 /// \brief Callback invoked when a \#pragma message directive is read.
197 /// \param Loc The location of the message directive.
198 /// \param Namespace The namespace of the message directive.
199 /// \param Kind The type of the message directive.
200 /// \param Str The text of the message directive.
201 virtual void PragmaMessage(SourceLocation Loc, StringRef Namespace,
202 PragmaMessageKind Kind, StringRef Str) {
205 /// \brief Callback invoked when a \#pragma gcc dianostic push directive
207 virtual void PragmaDiagnosticPush(SourceLocation Loc,
208 StringRef Namespace) {
211 /// \brief Callback invoked when a \#pragma gcc dianostic pop directive
213 virtual void PragmaDiagnosticPop(SourceLocation Loc,
214 StringRef Namespace) {
217 /// \brief Callback invoked when a \#pragma gcc dianostic directive is read.
218 virtual void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
219 diag::Severity mapping, StringRef Str) {}
221 /// \brief Called when an OpenCL extension is either disabled or
222 /// enabled with a pragma.
223 virtual void PragmaOpenCLExtension(SourceLocation NameLoc,
224 const IdentifierInfo *Name,
225 SourceLocation StateLoc, unsigned State) {
228 /// \brief Callback invoked when a \#pragma warning directive is read.
229 virtual void PragmaWarning(SourceLocation Loc, StringRef WarningSpec,
233 /// \brief Callback invoked when a \#pragma warning(push) directive is read.
234 virtual void PragmaWarningPush(SourceLocation Loc, int Level) {
237 /// \brief Callback invoked when a \#pragma warning(pop) directive is read.
238 virtual void PragmaWarningPop(SourceLocation Loc) {
241 /// \brief Called by Preprocessor::HandleMacroExpandedIdentifier when a
242 /// macro invocation is found.
243 virtual void MacroExpands(const Token &MacroNameTok,
244 const MacroDefinition &MD, SourceRange Range,
245 const MacroArgs *Args) {}
247 /// \brief Hook called whenever a macro definition is seen.
248 virtual void MacroDefined(const Token &MacroNameTok,
249 const MacroDirective *MD) {
252 /// \brief Hook called whenever a macro \#undef is seen.
254 /// MD is released immediately following this callback.
255 virtual void MacroUndefined(const Token &MacroNameTok,
256 const MacroDefinition &MD) {
259 /// \brief Hook called whenever the 'defined' operator is seen.
260 /// \param MD The MacroDirective if the name was a macro, null otherwise.
261 virtual void Defined(const Token &MacroNameTok, const MacroDefinition &MD,
265 /// \brief Hook called when a source range is skipped.
266 /// \param Range The SourceRange that was skipped. The range begins at the
267 /// \#if/\#else directive and ends after the \#endif/\#else directive.
268 virtual void SourceRangeSkipped(SourceRange Range) {
271 enum ConditionValueKind {
272 CVK_NotEvaluated, CVK_False, CVK_True
275 /// \brief Hook called whenever an \#if is seen.
276 /// \param Loc the source location of the directive.
277 /// \param ConditionRange The SourceRange of the expression being tested.
278 /// \param ConditionValue The evaluated value of the condition.
280 // FIXME: better to pass in a list (or tree!) of Tokens.
281 virtual void If(SourceLocation Loc, SourceRange ConditionRange,
282 ConditionValueKind ConditionValue) {
285 /// \brief Hook called whenever an \#elif is seen.
286 /// \param Loc the source location of the directive.
287 /// \param ConditionRange The SourceRange of the expression being tested.
288 /// \param ConditionValue The evaluated value of the condition.
289 /// \param IfLoc the source location of the \#if/\#ifdef/\#ifndef directive.
290 // FIXME: better to pass in a list (or tree!) of Tokens.
291 virtual void Elif(SourceLocation Loc, SourceRange ConditionRange,
292 ConditionValueKind ConditionValue, SourceLocation IfLoc) {
295 /// \brief Hook called whenever an \#ifdef is seen.
296 /// \param Loc the source location of the directive.
297 /// \param MacroNameTok Information on the token being tested.
298 /// \param MD The MacroDefinition if the name was a macro, null otherwise.
299 virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
300 const MacroDefinition &MD) {
303 /// \brief Hook called whenever an \#ifndef is seen.
304 /// \param Loc the source location of the directive.
305 /// \param MacroNameTok Information on the token being tested.
306 /// \param MD The MacroDefiniton if the name was a macro, null otherwise.
307 virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
308 const MacroDefinition &MD) {
311 /// \brief Hook called whenever an \#else is seen.
312 /// \param Loc the source location of the directive.
313 /// \param IfLoc the source location of the \#if/\#ifdef/\#ifndef directive.
314 virtual void Else(SourceLocation Loc, SourceLocation IfLoc) {
317 /// \brief Hook called whenever an \#endif is seen.
318 /// \param Loc the source location of the directive.
319 /// \param IfLoc the source location of the \#if/\#ifdef/\#ifndef directive.
320 virtual void Endif(SourceLocation Loc, SourceLocation IfLoc) {
324 /// \brief Simple wrapper class for chaining callbacks.
325 class PPChainedCallbacks : public PPCallbacks {
326 virtual void anchor();
327 std::unique_ptr<PPCallbacks> First, Second;
330 PPChainedCallbacks(std::unique_ptr<PPCallbacks> _First,
331 std::unique_ptr<PPCallbacks> _Second)
332 : First(std::move(_First)), Second(std::move(_Second)) {}
334 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
335 SrcMgr::CharacteristicKind FileType,
336 FileID PrevFID) override {
337 First->FileChanged(Loc, Reason, FileType, PrevFID);
338 Second->FileChanged(Loc, Reason, FileType, PrevFID);
341 void FileSkipped(const FileEntry &SkippedFile,
342 const Token &FilenameTok,
343 SrcMgr::CharacteristicKind FileType) override {
344 First->FileSkipped(SkippedFile, FilenameTok, FileType);
345 Second->FileSkipped(SkippedFile, FilenameTok, FileType);
348 bool FileNotFound(StringRef FileName,
349 SmallVectorImpl<char> &RecoveryPath) override {
350 return First->FileNotFound(FileName, RecoveryPath) ||
351 Second->FileNotFound(FileName, RecoveryPath);
354 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
355 StringRef FileName, bool IsAngled,
356 CharSourceRange FilenameRange, const FileEntry *File,
357 StringRef SearchPath, StringRef RelativePath,
358 const Module *Imported) override {
359 First->InclusionDirective(HashLoc, IncludeTok, FileName, IsAngled,
360 FilenameRange, File, SearchPath, RelativePath,
362 Second->InclusionDirective(HashLoc, IncludeTok, FileName, IsAngled,
363 FilenameRange, File, SearchPath, RelativePath,
367 void moduleImport(SourceLocation ImportLoc, ModuleIdPath Path,
368 const Module *Imported) override {
369 First->moduleImport(ImportLoc, Path, Imported);
370 Second->moduleImport(ImportLoc, Path, Imported);
373 void EndOfMainFile() override {
374 First->EndOfMainFile();
375 Second->EndOfMainFile();
378 void Ident(SourceLocation Loc, const std::string &str) override {
379 First->Ident(Loc, str);
380 Second->Ident(Loc, str);
383 void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
384 const std::string &Str) override {
385 First->PragmaComment(Loc, Kind, Str);
386 Second->PragmaComment(Loc, Kind, Str);
389 void PragmaDetectMismatch(SourceLocation Loc, const std::string &Name,
390 const std::string &Value) override {
391 First->PragmaDetectMismatch(Loc, Name, Value);
392 Second->PragmaDetectMismatch(Loc, Name, Value);
395 void PragmaMessage(SourceLocation Loc, StringRef Namespace,
396 PragmaMessageKind Kind, StringRef Str) override {
397 First->PragmaMessage(Loc, Namespace, Kind, Str);
398 Second->PragmaMessage(Loc, Namespace, Kind, Str);
401 void PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) override {
402 First->PragmaDiagnosticPush(Loc, Namespace);
403 Second->PragmaDiagnosticPush(Loc, Namespace);
406 void PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) override {
407 First->PragmaDiagnosticPop(Loc, Namespace);
408 Second->PragmaDiagnosticPop(Loc, Namespace);
411 void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
412 diag::Severity mapping, StringRef Str) override {
413 First->PragmaDiagnostic(Loc, Namespace, mapping, Str);
414 Second->PragmaDiagnostic(Loc, Namespace, mapping, Str);
417 void PragmaOpenCLExtension(SourceLocation NameLoc, const IdentifierInfo *Name,
418 SourceLocation StateLoc, unsigned State) override {
419 First->PragmaOpenCLExtension(NameLoc, Name, StateLoc, State);
420 Second->PragmaOpenCLExtension(NameLoc, Name, StateLoc, State);
423 void PragmaWarning(SourceLocation Loc, StringRef WarningSpec,
424 ArrayRef<int> Ids) override {
425 First->PragmaWarning(Loc, WarningSpec, Ids);
426 Second->PragmaWarning(Loc, WarningSpec, Ids);
429 void PragmaWarningPush(SourceLocation Loc, int Level) override {
430 First->PragmaWarningPush(Loc, Level);
431 Second->PragmaWarningPush(Loc, Level);
434 void PragmaWarningPop(SourceLocation Loc) override {
435 First->PragmaWarningPop(Loc);
436 Second->PragmaWarningPop(Loc);
439 void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
440 SourceRange Range, const MacroArgs *Args) override {
441 First->MacroExpands(MacroNameTok, MD, Range, Args);
442 Second->MacroExpands(MacroNameTok, MD, Range, Args);
445 void MacroDefined(const Token &MacroNameTok, const MacroDirective *MD) override {
446 First->MacroDefined(MacroNameTok, MD);
447 Second->MacroDefined(MacroNameTok, MD);
450 void MacroUndefined(const Token &MacroNameTok,
451 const MacroDefinition &MD) override {
452 First->MacroUndefined(MacroNameTok, MD);
453 Second->MacroUndefined(MacroNameTok, MD);
456 void Defined(const Token &MacroNameTok, const MacroDefinition &MD,
457 SourceRange Range) override {
458 First->Defined(MacroNameTok, MD, Range);
459 Second->Defined(MacroNameTok, MD, Range);
462 void SourceRangeSkipped(SourceRange Range) override {
463 First->SourceRangeSkipped(Range);
464 Second->SourceRangeSkipped(Range);
467 /// \brief Hook called whenever an \#if is seen.
468 void If(SourceLocation Loc, SourceRange ConditionRange,
469 ConditionValueKind ConditionValue) override {
470 First->If(Loc, ConditionRange, ConditionValue);
471 Second->If(Loc, ConditionRange, ConditionValue);
474 /// \brief Hook called whenever an \#elif is seen.
475 void Elif(SourceLocation Loc, SourceRange ConditionRange,
476 ConditionValueKind ConditionValue, SourceLocation IfLoc) override {
477 First->Elif(Loc, ConditionRange, ConditionValue, IfLoc);
478 Second->Elif(Loc, ConditionRange, ConditionValue, IfLoc);
481 /// \brief Hook called whenever an \#ifdef is seen.
482 void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
483 const MacroDefinition &MD) override {
484 First->Ifdef(Loc, MacroNameTok, MD);
485 Second->Ifdef(Loc, MacroNameTok, MD);
488 /// \brief Hook called whenever an \#ifndef is seen.
489 void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
490 const MacroDefinition &MD) override {
491 First->Ifndef(Loc, MacroNameTok, MD);
492 Second->Ifndef(Loc, MacroNameTok, MD);
495 /// \brief Hook called whenever an \#else is seen.
496 void Else(SourceLocation Loc, SourceLocation IfLoc) override {
497 First->Else(Loc, IfLoc);
498 Second->Else(Loc, IfLoc);
501 /// \brief Hook called whenever an \#endif is seen.
502 void Endif(SourceLocation Loc, SourceLocation IfLoc) override {
503 First->Endif(Loc, IfLoc);
504 Second->Endif(Loc, IfLoc);
508 } // end namespace clang