PPCallbacks.h revision da313592441db36cf4b06be97c4bcc238ee6fa9c
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/Lex/DirectoryLookup.h" 19#include "clang/Basic/SourceLocation.h" 20#include "clang/Basic/DiagnosticIDs.h" 21#include "llvm/ADT/StringRef.h" 22#include <string> 23 24namespace clang { 25 class SourceLocation; 26 class Token; 27 class IdentifierInfo; 28 class MacroInfo; 29 30/// \brief This interface provides a way to observe the actions of the 31/// preprocessor as it does its thing. 32/// 33/// Clients can define their hooks here to implement preprocessor level tools. 34class PPCallbacks { 35public: 36 virtual ~PPCallbacks(); 37 38 enum FileChangeReason { 39 EnterFile, ExitFile, SystemHeaderPragma, RenameFile 40 }; 41 42 /// \brief Callback invoked whenever a source file is entered or exited. 43 /// 44 /// \param Loc Indicates the new location. 45 /// \param PrevFID the file that was exited if \p Reason is ExitFile. 46 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, 47 SrcMgr::CharacteristicKind FileType, 48 FileID PrevFID = FileID()) { 49 } 50 51 /// \brief Callback invoked whenever a source file is skipped as the result 52 /// of header guard optimization. 53 /// 54 /// \param ParentFile The file that \#included the skipped file. 55 /// 56 /// \param FilenameTok The token in ParentFile that indicates the 57 /// skipped file. 58 virtual void FileSkipped(const FileEntry &ParentFile, 59 const Token &FilenameTok, 60 SrcMgr::CharacteristicKind FileType) { 61 } 62 63 /// \brief Callback invoked whenever an inclusion directive results in a 64 /// file-not-found error. 65 /// 66 /// \param FileName The name of the file being included, as written in the 67 /// source code. 68 /// 69 /// \param RecoveryPath If this client indicates that it can recover from 70 /// this missing file, the client should set this as an additional header 71 /// search patch. 72 /// 73 /// \returns true to indicate that the preprocessor should attempt to recover 74 /// by adding \p RecoveryPath as a header search path. 75 virtual bool FileNotFound(StringRef FileName, 76 SmallVectorImpl<char> &RecoveryPath) { 77 return false; 78 } 79 80 /// \brief Callback invoked whenever an inclusion directive of 81 /// any kind (\c \#include, \c \#import, etc.) has been processed, regardless 82 /// of whether the inclusion will actually result in an inclusion. 83 /// 84 /// \param HashLoc The location of the '#' that starts the inclusion 85 /// directive. 86 /// 87 /// \param IncludeTok The token that indicates the kind of inclusion 88 /// directive, e.g., 'include' or 'import'. 89 /// 90 /// \param FileName The name of the file being included, as written in the 91 /// source code. 92 /// 93 /// \param IsAngled Whether the file name was enclosed in angle brackets; 94 /// otherwise, it was enclosed in quotes. 95 /// 96 /// \param FilenameRange The character range of the quotes or angle brackets 97 /// for the written file name. 98 /// 99 /// \param File The actual file that may be included by this inclusion 100 /// directive. 101 /// 102 /// \param EndLoc The location of the last token within the 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 virtual void InclusionDirective(SourceLocation HashLoc, 117 const Token &IncludeTok, 118 StringRef FileName, 119 bool IsAngled, 120 CharSourceRange FilenameRange, 121 const FileEntry *File, 122 StringRef SearchPath, 123 StringRef RelativePath) { 124 } 125 126 /// \brief Callback invoked when the end of the main file is reached. 127 /// 128 /// No subsequent callbacks will be made. 129 virtual void EndOfMainFile() { 130 } 131 132 /// \brief Callback invoked when a \#ident or \#sccs directive is read. 133 /// \param Loc The location of the directive. 134 /// \param str The text of the directive. 135 /// 136 virtual void Ident(SourceLocation Loc, const std::string &str) { 137 } 138 139 /// \brief Callback invoked when a \#pragma comment directive is read. 140 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind, 141 const std::string &Str) { 142 } 143 144 /// \brief Callback invoked when a \#pragma message directive is read. 145 /// \param Loc The location of the message directive. 146 /// \param Str The text of the message directive. 147 virtual void PragmaMessage(SourceLocation Loc, StringRef Str) { 148 } 149 150 /// \brief Callback invoked when a \#pragma gcc dianostic push directive 151 /// is read. 152 virtual void PragmaDiagnosticPush(SourceLocation Loc, 153 StringRef Namespace) { 154 } 155 156 /// \brief Callback invoked when a \#pragma gcc dianostic pop directive 157 /// is read. 158 virtual void PragmaDiagnosticPop(SourceLocation Loc, 159 StringRef Namespace) { 160 } 161 162 /// \brief Callback invoked when a \#pragma gcc dianostic directive is read. 163 virtual void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace, 164 diag::Mapping mapping, StringRef Str) { 165 } 166 167 /// \brief Called by Preprocessor::HandleMacroExpandedIdentifier when a 168 /// macro invocation is found. 169 virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo* MI, 170 SourceRange Range) { 171 } 172 173 /// \brief Hook called whenever a macro definition is seen. 174 virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) { 175 } 176 177 /// \brief Hook called whenever a macro \#undef is seen. 178 /// 179 /// MI is released immediately following this callback. 180 virtual void MacroUndefined(const Token &MacroNameTok, const MacroInfo *MI) { 181 } 182 183 /// \brief Hook called whenever the 'defined' operator is seen. 184 virtual void Defined(const Token &MacroNameTok) { 185 } 186 187 /// \brief Hook called when a source range is skipped. 188 /// \param Range The SourceRange that was skipped. The range begins at the 189 /// \#if/\#else directive and ends after the \#endif/\#else directive. 190 virtual void SourceRangeSkipped(SourceRange Range) { 191 } 192 193 /// \brief Hook called whenever an \#if is seen. 194 /// \param Loc the source location of the directive. 195 /// \param ConditionRange The SourceRange of the expression being tested. 196 /// 197 // FIXME: better to pass in a list (or tree!) of Tokens. 198 virtual void If(SourceLocation Loc, SourceRange ConditionRange) { 199 } 200 201 /// \brief Hook called whenever an \#elif is seen. 202 /// \param Loc the source location of the directive. 203 /// \param ConditionRange The SourceRange of the expression being tested. 204 /// \param IfLoc the source location of the \#if/\#ifdef/\#ifndef directive. 205 // FIXME: better to pass in a list (or tree!) of Tokens. 206 virtual void Elif(SourceLocation Loc, SourceRange ConditionRange, 207 SourceLocation IfLoc) { 208 } 209 210 /// \brief Hook called whenever an \#ifdef is seen. 211 /// \param Loc the source location of the directive. 212 /// \param MacroNameTok Information on the token being tested. 213 virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok) { 214 } 215 216 /// \brief Hook called whenever an \#ifndef is seen. 217 /// \param Loc the source location of the directive. 218 /// \param MacroNameTok Information on the token being tested. 219 virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok) { 220 } 221 222 /// \brief Hook called whenever an \#else is seen. 223 /// \param Loc the source location of the directive. 224 /// \param IfLoc the source location of the \#if/\#ifdef/\#ifndef directive. 225 virtual void Else(SourceLocation Loc, SourceLocation IfLoc) { 226 } 227 228 /// \brief Hook called whenever an \#endif is seen. 229 /// \param Loc the source location of the directive. 230 /// \param IfLoc the source location of the \#if/\#ifdef/\#ifndef directive. 231 virtual void Endif(SourceLocation Loc, SourceLocation IfLoc) { 232 } 233}; 234 235/// \brief Simple wrapper class for chaining callbacks. 236class PPChainedCallbacks : public PPCallbacks { 237 virtual void anchor(); 238 PPCallbacks *First, *Second; 239 240public: 241 PPChainedCallbacks(PPCallbacks *_First, PPCallbacks *_Second) 242 : First(_First), Second(_Second) {} 243 ~PPChainedCallbacks() { 244 delete Second; 245 delete First; 246 } 247 248 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, 249 SrcMgr::CharacteristicKind FileType, 250 FileID PrevFID) { 251 First->FileChanged(Loc, Reason, FileType, PrevFID); 252 Second->FileChanged(Loc, Reason, FileType, PrevFID); 253 } 254 255 virtual void FileSkipped(const FileEntry &ParentFile, 256 const Token &FilenameTok, 257 SrcMgr::CharacteristicKind FileType) { 258 First->FileSkipped(ParentFile, FilenameTok, FileType); 259 Second->FileSkipped(ParentFile, FilenameTok, FileType); 260 } 261 262 virtual bool FileNotFound(StringRef FileName, 263 SmallVectorImpl<char> &RecoveryPath) { 264 return First->FileNotFound(FileName, RecoveryPath) || 265 Second->FileNotFound(FileName, RecoveryPath); 266 } 267 268 virtual void InclusionDirective(SourceLocation HashLoc, 269 const Token &IncludeTok, 270 StringRef FileName, 271 bool IsAngled, 272 CharSourceRange FilenameRange, 273 const FileEntry *File, 274 StringRef SearchPath, 275 StringRef RelativePath) { 276 First->InclusionDirective(HashLoc, IncludeTok, FileName, IsAngled, 277 FilenameRange, File, SearchPath, RelativePath); 278 Second->InclusionDirective(HashLoc, IncludeTok, FileName, IsAngled, 279 FilenameRange, File, SearchPath, RelativePath); 280 } 281 282 virtual void EndOfMainFile() { 283 First->EndOfMainFile(); 284 Second->EndOfMainFile(); 285 } 286 287 virtual void Ident(SourceLocation Loc, const std::string &str) { 288 First->Ident(Loc, str); 289 Second->Ident(Loc, str); 290 } 291 292 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind, 293 const std::string &Str) { 294 First->PragmaComment(Loc, Kind, Str); 295 Second->PragmaComment(Loc, Kind, Str); 296 } 297 298 virtual void PragmaMessage(SourceLocation Loc, StringRef Str) { 299 First->PragmaMessage(Loc, Str); 300 Second->PragmaMessage(Loc, Str); 301 } 302 303 virtual void PragmaDiagnosticPush(SourceLocation Loc, 304 StringRef Namespace) { 305 First->PragmaDiagnosticPush(Loc, Namespace); 306 Second->PragmaDiagnosticPush(Loc, Namespace); 307 } 308 309 virtual void PragmaDiagnosticPop(SourceLocation Loc, 310 StringRef Namespace) { 311 First->PragmaDiagnosticPop(Loc, Namespace); 312 Second->PragmaDiagnosticPop(Loc, Namespace); 313 } 314 315 virtual void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace, 316 diag::Mapping mapping, StringRef Str) { 317 First->PragmaDiagnostic(Loc, Namespace, mapping, Str); 318 Second->PragmaDiagnostic(Loc, Namespace, mapping, Str); 319 } 320 321 virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo* MI, 322 SourceRange Range) { 323 First->MacroExpands(MacroNameTok, MI, Range); 324 Second->MacroExpands(MacroNameTok, MI, Range); 325 } 326 327 virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) { 328 First->MacroDefined(MacroNameTok, MI); 329 Second->MacroDefined(MacroNameTok, MI); 330 } 331 332 virtual void MacroUndefined(const Token &MacroNameTok, const MacroInfo *MI) { 333 First->MacroUndefined(MacroNameTok, MI); 334 Second->MacroUndefined(MacroNameTok, MI); 335 } 336 337 virtual void Defined(const Token &MacroNameTok) { 338 First->Defined(MacroNameTok); 339 Second->Defined(MacroNameTok); 340 } 341 342 virtual void SourceRangeSkipped(SourceRange Range) { 343 First->SourceRangeSkipped(Range); 344 Second->SourceRangeSkipped(Range); 345 } 346 347 /// \brief Hook called whenever an \#if is seen. 348 virtual void If(SourceLocation Loc, SourceRange ConditionRange) { 349 First->If(Loc, ConditionRange); 350 Second->If(Loc, ConditionRange); 351 } 352 353 /// \brief Hook called whenever an \#if is seen. 354 virtual void Elif(SourceLocation Loc, SourceRange ConditionRange, 355 SourceLocation IfLoc) { 356 First->Elif(Loc, ConditionRange, IfLoc); 357 Second->Elif(Loc, ConditionRange, IfLoc); 358 } 359 360 /// \brief Hook called whenever an \#ifdef is seen. 361 virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok) { 362 First->Ifdef(Loc, MacroNameTok); 363 Second->Ifdef(Loc, MacroNameTok); 364 } 365 366 /// \brief Hook called whenever an \#ifndef is seen. 367 virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok) { 368 First->Ifndef(Loc, MacroNameTok); 369 Second->Ifndef(Loc, MacroNameTok); 370 } 371 372 /// \brief Hook called whenever an \#else is seen. 373 virtual void Else(SourceLocation Loc, SourceLocation IfLoc) { 374 First->Else(Loc, IfLoc); 375 Second->Else(Loc, IfLoc); 376 } 377 378 /// \brief Hook called whenever an \#endif is seen. 379 virtual void Endif(SourceLocation Loc, SourceLocation IfLoc) { 380 First->Endif(Loc, IfLoc); 381 Second->Endif(Loc, IfLoc); 382 } 383}; 384 385} // end namespace clang 386 387#endif 388