PPCallbacks.h revision 2ee042dee9f7693665e28463955401905474a284
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/// \file 11/// \brief Defines the PPCallbacks interface. 12/// 13//===----------------------------------------------------------------------===// 14 15#ifndef LLVM_CLANG_LEX_PPCALLBACKS_H 16#define LLVM_CLANG_LEX_PPCALLBACKS_H 17 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" 24#include <string> 25 26namespace clang { 27 class SourceLocation; 28 class Token; 29 class IdentifierInfo; 30 class MacroDirective; 31 class MacroArgs; 32 33/// \brief This interface provides a way to observe the actions of the 34/// preprocessor as it does its thing. 35/// 36/// Clients can define their hooks here to implement preprocessor level tools. 37class PPCallbacks { 38public: 39 virtual ~PPCallbacks(); 40 41 enum FileChangeReason { 42 EnterFile, ExitFile, SystemHeaderPragma, RenameFile 43 }; 44 45 /// \brief Callback invoked whenever a source file is entered or exited. 46 /// 47 /// \param Loc Indicates the new location. 48 /// \param PrevFID the file that was exited if \p Reason is ExitFile. 49 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, 50 SrcMgr::CharacteristicKind FileType, 51 FileID PrevFID = FileID()) { 52 } 53 54 /// \brief Callback invoked whenever a source file is skipped as the result 55 /// of header guard optimization. 56 /// 57 /// \param ParentFile The file that \#included the skipped file. 58 /// 59 /// \param FilenameTok The token in ParentFile that indicates the 60 /// skipped file. 61 virtual void FileSkipped(const FileEntry &ParentFile, 62 const Token &FilenameTok, 63 SrcMgr::CharacteristicKind FileType) { 64 } 65 66 /// \brief Callback invoked whenever an inclusion directive results in a 67 /// file-not-found error. 68 /// 69 /// \param FileName The name of the file being included, as written in the 70 /// source code. 71 /// 72 /// \param RecoveryPath If this client indicates that it can recover from 73 /// this missing file, the client should set this as an additional header 74 /// search patch. 75 /// 76 /// \returns true to indicate that the preprocessor should attempt to recover 77 /// by adding \p RecoveryPath as a header search path. 78 virtual bool FileNotFound(StringRef FileName, 79 SmallVectorImpl<char> &RecoveryPath) { 80 return false; 81 } 82 83 /// \brief Callback invoked whenever an inclusion directive of 84 /// any kind (\c \#include, \c \#import, etc.) has been processed, regardless 85 /// of whether the inclusion will actually result in an inclusion. 86 /// 87 /// \param HashLoc The location of the '#' that starts the inclusion 88 /// directive. 89 /// 90 /// \param IncludeTok The token that indicates the kind of inclusion 91 /// directive, e.g., 'include' or 'import'. 92 /// 93 /// \param FileName The name of the file being included, as written in the 94 /// source code. 95 /// 96 /// \param IsAngled Whether the file name was enclosed in angle brackets; 97 /// otherwise, it was enclosed in quotes. 98 /// 99 /// \param FilenameRange The character range of the quotes or angle brackets 100 /// for the written file name. 101 /// 102 /// \param File The actual file that may be included by this inclusion 103 /// directive. 104 /// 105 /// \param SearchPath Contains the search path which was used to find the file 106 /// in the file system. If the file was found via an absolute include path, 107 /// SearchPath will be empty. For framework includes, the SearchPath and 108 /// RelativePath will be split up. For example, if an include of "Some/Some.h" 109 /// is found via the framework path 110 /// "path/to/Frameworks/Some.framework/Headers/Some.h", SearchPath will be 111 /// "path/to/Frameworks/Some.framework/Headers" and RelativePath will be 112 /// "Some.h". 113 /// 114 /// \param RelativePath The path relative to SearchPath, at which the include 115 /// file was found. This is equal to FileName except for framework includes. 116 /// 117 /// \param Imported The module, whenever an inclusion directive was 118 /// automatically turned into a module import or null otherwise. 119 /// 120 virtual void InclusionDirective(SourceLocation HashLoc, 121 const Token &IncludeTok, 122 StringRef FileName, 123 bool IsAngled, 124 CharSourceRange FilenameRange, 125 const FileEntry *File, 126 StringRef SearchPath, 127 StringRef RelativePath, 128 const Module *Imported) { 129 } 130 131 /// \brief Callback invoked whenever there was an explicit module-import 132 /// syntax. 133 /// 134 /// \param ImportLoc The location of import directive token. 135 /// 136 /// \param Path The identifiers (and their locations) of the module 137 /// "path", e.g., "std.vector" would be split into "std" and "vector". 138 /// 139 /// \param Imported The imported module; can be null if importing failed. 140 /// 141 virtual void moduleImport(SourceLocation ImportLoc, 142 ModuleIdPath Path, 143 const Module *Imported) { 144 } 145 146 /// \brief Callback invoked when the end of the main file is reached. 147 /// 148 /// No subsequent callbacks will be made. 149 virtual void EndOfMainFile() { 150 } 151 152 /// \brief Callback invoked when a \#ident or \#sccs directive is read. 153 /// \param Loc The location of the directive. 154 /// \param str The text of the directive. 155 /// 156 virtual void Ident(SourceLocation Loc, const std::string &str) { 157 } 158 159 /// \brief Callback invoked when start reading any pragma directive. 160 virtual void PragmaDirective(SourceLocation Loc, 161 PragmaIntroducerKind Introducer) { 162 } 163 164 /// \brief Callback invoked when a \#pragma comment directive is read. 165 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind, 166 const std::string &Str) { 167 } 168 169 /// \brief Callback invoked when a \#pragma detect_mismatch directive is 170 /// read. 171 virtual void PragmaDetectMismatch(SourceLocation Loc, 172 const std::string &Name, 173 const std::string &Value) { 174 } 175 176 /// \brief Callback invoked when a \#pragma clang __debug directive is read. 177 /// \param Loc The location of the debug directive. 178 /// \param DebugType The identifier following __debug. 179 virtual void PragmaDebug(SourceLocation Loc, StringRef DebugType) { 180 } 181 182 /// \brief Determines the kind of \#pragma invoking a call to PragmaMessage. 183 enum PragmaMessageKind { 184 /// \brief \#pragma message has been invoked. 185 PMK_Message, 186 187 /// \brief \#pragma GCC warning has been invoked. 188 PMK_Warning, 189 190 /// \brief \#pragma GCC error has been invoked. 191 PMK_Error 192 }; 193 194 /// \brief Callback invoked when a \#pragma message directive is read. 195 /// \param Loc The location of the message directive. 196 /// \param Namespace The namespace of the message directive. 197 /// \param Kind The type of the message directive. 198 /// \param Str The text of the message directive. 199 virtual void PragmaMessage(SourceLocation Loc, StringRef Namespace, 200 PragmaMessageKind Kind, StringRef Str) { 201 } 202 203 /// \brief Callback invoked when a \#pragma gcc dianostic push directive 204 /// is read. 205 virtual void PragmaDiagnosticPush(SourceLocation Loc, 206 StringRef Namespace) { 207 } 208 209 /// \brief Callback invoked when a \#pragma gcc dianostic pop directive 210 /// is read. 211 virtual void PragmaDiagnosticPop(SourceLocation Loc, 212 StringRef Namespace) { 213 } 214 215 /// \brief Callback invoked when a \#pragma gcc dianostic directive is read. 216 virtual void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace, 217 diag::Mapping mapping, StringRef Str) { 218 } 219 220 /// \brief Callback invoked when a \#pragma warning directive is read. 221 virtual void PragmaWarning(SourceLocation Loc, StringRef WarningSpec, 222 ArrayRef<int> Ids) { 223 } 224 225 /// \brief Callback invoked when a \#pragma warning(push) directive is read. 226 virtual void PragmaWarningPush(SourceLocation Loc, int Level) { 227 } 228 229 /// \brief Callback invoked when a \#pragma warning(pop) directive is read. 230 virtual void PragmaWarningPop(SourceLocation Loc) { 231 } 232 233 /// \brief Called by Preprocessor::HandleMacroExpandedIdentifier when a 234 /// macro invocation is found. 235 virtual void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD, 236 SourceRange Range, const MacroArgs *Args) { 237 } 238 239 /// \brief Hook called whenever a macro definition is seen. 240 virtual void MacroDefined(const Token &MacroNameTok, 241 const MacroDirective *MD) { 242 } 243 244 /// \brief Hook called whenever a macro \#undef is seen. 245 /// 246 /// MD is released immediately following this callback. 247 virtual void MacroUndefined(const Token &MacroNameTok, 248 const MacroDirective *MD) { 249 } 250 251 /// \brief Hook called whenever the 'defined' operator is seen. 252 /// \param MD The MacroDirective if the name was a macro, null otherwise. 253 virtual void Defined(const Token &MacroNameTok, const MacroDirective *MD, 254 SourceRange Range) { 255 } 256 257 /// \brief Hook called when a source range is skipped. 258 /// \param Range The SourceRange that was skipped. The range begins at the 259 /// \#if/\#else directive and ends after the \#endif/\#else directive. 260 virtual void SourceRangeSkipped(SourceRange Range) { 261 } 262 263 /// \brief Hook called whenever an \#if is seen. 264 /// \param Loc the source location of the directive. 265 /// \param ConditionRange The SourceRange of the expression being tested. 266 /// \param ConditionValue The evaluated value of the condition. 267 /// 268 // FIXME: better to pass in a list (or tree!) of Tokens. 269 virtual void If(SourceLocation Loc, SourceRange ConditionRange, 270 bool ConditionValue) { 271 } 272 273 /// \brief Hook called whenever an \#elif is seen. 274 /// \param Loc the source location of the directive. 275 /// \param ConditionRange The SourceRange of the expression being tested. 276 /// \param ConditionValue The evaluated value of the condition. 277 /// \param IfLoc the source location of the \#if/\#ifdef/\#ifndef directive. 278 // FIXME: better to pass in a list (or tree!) of Tokens. 279 virtual void Elif(SourceLocation Loc, SourceRange ConditionRange, 280 bool ConditionValue, SourceLocation IfLoc) { 281 } 282 283 /// \brief Hook called whenever an \#ifdef is seen. 284 /// \param Loc the source location of the directive. 285 /// \param MacroNameTok Information on the token being tested. 286 /// \param MD The MacroDirective if the name was a macro, null otherwise. 287 virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok, 288 const MacroDirective *MD) { 289 } 290 291 /// \brief Hook called whenever an \#ifndef is seen. 292 /// \param Loc the source location of the directive. 293 /// \param MacroNameTok Information on the token being tested. 294 /// \param MD The MacroDirective if the name was a macro, null otherwise. 295 virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok, 296 const MacroDirective *MD) { 297 } 298 299 /// \brief Hook called whenever an \#else is seen. 300 /// \param Loc the source location of the directive. 301 /// \param IfLoc the source location of the \#if/\#ifdef/\#ifndef directive. 302 virtual void Else(SourceLocation Loc, SourceLocation IfLoc) { 303 } 304 305 /// \brief Hook called whenever an \#endif is seen. 306 /// \param Loc the source location of the directive. 307 /// \param IfLoc the source location of the \#if/\#ifdef/\#ifndef directive. 308 virtual void Endif(SourceLocation Loc, SourceLocation IfLoc) { 309 } 310}; 311 312/// \brief Simple wrapper class for chaining callbacks. 313class PPChainedCallbacks : public PPCallbacks { 314 virtual void anchor(); 315 PPCallbacks *First, *Second; 316 317public: 318 PPChainedCallbacks(PPCallbacks *_First, PPCallbacks *_Second) 319 : First(_First), Second(_Second) {} 320 ~PPChainedCallbacks() { 321 delete Second; 322 delete First; 323 } 324 325 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, 326 SrcMgr::CharacteristicKind FileType, 327 FileID PrevFID) { 328 First->FileChanged(Loc, Reason, FileType, PrevFID); 329 Second->FileChanged(Loc, Reason, FileType, PrevFID); 330 } 331 332 virtual void FileSkipped(const FileEntry &ParentFile, 333 const Token &FilenameTok, 334 SrcMgr::CharacteristicKind FileType) { 335 First->FileSkipped(ParentFile, FilenameTok, FileType); 336 Second->FileSkipped(ParentFile, FilenameTok, FileType); 337 } 338 339 virtual bool FileNotFound(StringRef FileName, 340 SmallVectorImpl<char> &RecoveryPath) { 341 return First->FileNotFound(FileName, RecoveryPath) || 342 Second->FileNotFound(FileName, RecoveryPath); 343 } 344 345 virtual void InclusionDirective(SourceLocation HashLoc, 346 const Token &IncludeTok, 347 StringRef FileName, 348 bool IsAngled, 349 CharSourceRange FilenameRange, 350 const FileEntry *File, 351 StringRef SearchPath, 352 StringRef RelativePath, 353 const Module *Imported) { 354 First->InclusionDirective(HashLoc, IncludeTok, FileName, IsAngled, 355 FilenameRange, File, SearchPath, RelativePath, 356 Imported); 357 Second->InclusionDirective(HashLoc, IncludeTok, FileName, IsAngled, 358 FilenameRange, File, SearchPath, RelativePath, 359 Imported); 360 } 361 362 virtual void moduleImport(SourceLocation ImportLoc, 363 ModuleIdPath Path, 364 const Module *Imported) { 365 First->moduleImport(ImportLoc, Path, Imported); 366 Second->moduleImport(ImportLoc, Path, Imported); 367 } 368 369 virtual void EndOfMainFile() { 370 First->EndOfMainFile(); 371 Second->EndOfMainFile(); 372 } 373 374 virtual void Ident(SourceLocation Loc, const std::string &str) { 375 First->Ident(Loc, str); 376 Second->Ident(Loc, str); 377 } 378 379 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind, 380 const std::string &Str) { 381 First->PragmaComment(Loc, Kind, Str); 382 Second->PragmaComment(Loc, Kind, Str); 383 } 384 385 virtual void PragmaDetectMismatch(SourceLocation Loc, 386 const std::string &Name, 387 const std::string &Value) { 388 First->PragmaDetectMismatch(Loc, Name, Value); 389 Second->PragmaDetectMismatch(Loc, Name, Value); 390 } 391 392 virtual void PragmaMessage(SourceLocation Loc, StringRef Namespace, 393 PragmaMessageKind Kind, StringRef Str) { 394 First->PragmaMessage(Loc, Namespace, Kind, Str); 395 Second->PragmaMessage(Loc, Namespace, Kind, Str); 396 } 397 398 virtual void PragmaDiagnosticPush(SourceLocation Loc, 399 StringRef Namespace) { 400 First->PragmaDiagnosticPush(Loc, Namespace); 401 Second->PragmaDiagnosticPush(Loc, Namespace); 402 } 403 404 virtual void PragmaDiagnosticPop(SourceLocation Loc, 405 StringRef Namespace) { 406 First->PragmaDiagnosticPop(Loc, Namespace); 407 Second->PragmaDiagnosticPop(Loc, Namespace); 408 } 409 410 virtual void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace, 411 diag::Mapping mapping, StringRef Str) { 412 First->PragmaDiagnostic(Loc, Namespace, mapping, Str); 413 Second->PragmaDiagnostic(Loc, Namespace, mapping, Str); 414 } 415 416 virtual void PragmaWarning(SourceLocation Loc, StringRef WarningSpec, 417 ArrayRef<int> Ids) { 418 First->PragmaWarning(Loc, WarningSpec, Ids); 419 Second->PragmaWarning(Loc, WarningSpec, Ids); 420 } 421 422 virtual void PragmaWarningPush(SourceLocation Loc, int Level) { 423 First->PragmaWarningPush(Loc, Level); 424 Second->PragmaWarningPush(Loc, Level); 425 } 426 427 virtual void PragmaWarningPop(SourceLocation Loc) { 428 First->PragmaWarningPop(Loc); 429 Second->PragmaWarningPop(Loc); 430 } 431 432 virtual void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD, 433 SourceRange Range, const MacroArgs *Args) { 434 First->MacroExpands(MacroNameTok, MD, Range, Args); 435 Second->MacroExpands(MacroNameTok, MD, Range, Args); 436 } 437 438 virtual void MacroDefined(const Token &MacroNameTok, const MacroDirective *MD) { 439 First->MacroDefined(MacroNameTok, MD); 440 Second->MacroDefined(MacroNameTok, MD); 441 } 442 443 virtual void MacroUndefined(const Token &MacroNameTok, 444 const MacroDirective *MD) { 445 First->MacroUndefined(MacroNameTok, MD); 446 Second->MacroUndefined(MacroNameTok, MD); 447 } 448 449 virtual void Defined(const Token &MacroNameTok, const MacroDirective *MD, 450 SourceRange Range) { 451 First->Defined(MacroNameTok, MD, Range); 452 Second->Defined(MacroNameTok, MD, Range); 453 } 454 455 virtual void SourceRangeSkipped(SourceRange Range) { 456 First->SourceRangeSkipped(Range); 457 Second->SourceRangeSkipped(Range); 458 } 459 460 /// \brief Hook called whenever an \#if is seen. 461 virtual void If(SourceLocation Loc, SourceRange ConditionRange, 462 bool ConditionValue) { 463 First->If(Loc, ConditionRange, ConditionValue); 464 Second->If(Loc, ConditionRange, ConditionValue); 465 } 466 467 /// \brief Hook called whenever an \#elif is seen. 468 virtual void Elif(SourceLocation Loc, SourceRange ConditionRange, 469 bool ConditionValue, SourceLocation IfLoc) { 470 First->Elif(Loc, ConditionRange, ConditionValue, IfLoc); 471 Second->Elif(Loc, ConditionRange, ConditionValue, IfLoc); 472 } 473 474 /// \brief Hook called whenever an \#ifdef is seen. 475 virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok, 476 const MacroDirective *MD) { 477 First->Ifdef(Loc, MacroNameTok, MD); 478 Second->Ifdef(Loc, MacroNameTok, MD); 479 } 480 481 /// \brief Hook called whenever an \#ifndef is seen. 482 virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok, 483 const MacroDirective *MD) { 484 First->Ifndef(Loc, MacroNameTok, MD); 485 Second->Ifndef(Loc, MacroNameTok, MD); 486 } 487 488 /// \brief Hook called whenever an \#else is seen. 489 virtual void Else(SourceLocation Loc, SourceLocation IfLoc) { 490 First->Else(Loc, IfLoc); 491 Second->Else(Loc, IfLoc); 492 } 493 494 /// \brief Hook called whenever an \#endif is seen. 495 virtual void Endif(SourceLocation Loc, SourceLocation IfLoc) { 496 First->Endif(Loc, IfLoc); 497 Second->Endif(Loc, IfLoc); 498 } 499}; 500 501} // end namespace clang 502 503#endif 504