| 1 | //===--- PPCallbacksTracker.cpp - Preprocessor tracker -*--*---------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
| 10 | /// Implementations for preprocessor tracking. |
| 11 | /// |
| 12 | /// See the header for details. |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "PPCallbacksTracker.h" |
| 17 | #include "clang/Basic/FileManager.h" |
| 18 | #include "clang/Lex/MacroArgs.h" |
| 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | |
| 21 | namespace clang { |
| 22 | namespace pp_trace { |
| 23 | |
| 24 | // Get a "file:line:column" source location string. |
| 25 | static std::string getSourceLocationString(Preprocessor &PP, |
| 26 | SourceLocation Loc) { |
| 27 | if (Loc.isInvalid()) |
| 28 | return std::string("(none)" ); |
| 29 | |
| 30 | if (Loc.isFileID()) { |
| 31 | PresumedLoc PLoc = PP.getSourceManager().getPresumedLoc(Loc); |
| 32 | |
| 33 | if (PLoc.isInvalid()) { |
| 34 | return std::string("(invalid)" ); |
| 35 | } |
| 36 | |
| 37 | std::string Str; |
| 38 | llvm::raw_string_ostream SS(Str); |
| 39 | |
| 40 | // The macro expansion and spelling pos is identical for file locs. |
| 41 | SS << "\"" << PLoc.getFilename() << ':' << PLoc.getLine() << ':' |
| 42 | << PLoc.getColumn() << "\"" ; |
| 43 | |
| 44 | std::string Result = SS.str(); |
| 45 | |
| 46 | // YAML treats backslash as escape, so use forward slashes. |
| 47 | llvm::replace(Range&: Result, OldValue: '\\', NewValue: '/'); |
| 48 | |
| 49 | return Result; |
| 50 | } |
| 51 | |
| 52 | return std::string("(nonfile)" ); |
| 53 | } |
| 54 | |
| 55 | // Enum string tables. |
| 56 | |
| 57 | // FileChangeReason strings. |
| 58 | static const char *const FileChangeReasonStrings[] = { |
| 59 | "EnterFile" , "ExitFile" , "SystemHeaderPragma" , "RenameFile" |
| 60 | }; |
| 61 | |
| 62 | // CharacteristicKind strings. |
| 63 | static const char *const CharacteristicKindStrings[] = { "C_User" , "C_System" , |
| 64 | "C_ExternCSystem" }; |
| 65 | |
| 66 | // MacroDirective::Kind strings. |
| 67 | static const char *const MacroDirectiveKindStrings[] = { |
| 68 | "MD_Define" ,"MD_Undefine" , "MD_Visibility" |
| 69 | }; |
| 70 | |
| 71 | // PragmaIntroducerKind strings. |
| 72 | static const char *const PragmaIntroducerKindStrings[] = { "PIK_HashPragma" , |
| 73 | "PIK__Pragma" , |
| 74 | "PIK___pragma" }; |
| 75 | |
| 76 | // PragmaMessageKind strings. |
| 77 | static const char *const PragmaMessageKindStrings[] = { |
| 78 | "PMK_Message" , "PMK_Warning" , "PMK_Error" |
| 79 | }; |
| 80 | |
| 81 | // PragmaWarningSpecifier strings. |
| 82 | static const char *const PragmaWarningSpecifierStrings[] = { |
| 83 | "PWS_Default" , "PWS_Disable" , "PWS_Error" , "PWS_Once" , "PWS_Suppress" , |
| 84 | "PWS_Level1" , "PWS_Level2" , "PWS_Level3" , "PWS_Level4" , |
| 85 | }; |
| 86 | |
| 87 | // ConditionValueKind strings. |
| 88 | static const char *const ConditionValueKindStrings[] = { |
| 89 | "CVK_NotEvaluated" , "CVK_False" , "CVK_True" |
| 90 | }; |
| 91 | |
| 92 | // Mapping strings. |
| 93 | static const char *const MappingStrings[] = { "0" , "MAP_IGNORE" , |
| 94 | "MAP_REMARK" , "MAP_WARNING" , |
| 95 | "MAP_ERROR" , "MAP_FATAL" }; |
| 96 | |
| 97 | // PPCallbacksTracker functions. |
| 98 | |
| 99 | PPCallbacksTracker::PPCallbacksTracker(const FilterType &Filters, |
| 100 | std::vector<CallbackCall> &CallbackCalls, |
| 101 | Preprocessor &PP) |
| 102 | : CallbackCalls(CallbackCalls), Filters(Filters), PP(PP) {} |
| 103 | |
| 104 | PPCallbacksTracker::~PPCallbacksTracker() {} |
| 105 | |
| 106 | // Callback functions. |
| 107 | |
| 108 | // Callback invoked whenever a source file is entered or exited. |
| 109 | void PPCallbacksTracker::FileChanged(SourceLocation Loc, |
| 110 | PPCallbacks::FileChangeReason Reason, |
| 111 | SrcMgr::CharacteristicKind FileType, |
| 112 | FileID PrevFID) { |
| 113 | beginCallback(Name: "FileChanged" ); |
| 114 | appendArgument(Name: "Loc" , Value: Loc); |
| 115 | appendArgument(Name: "Reason" , Value: Reason, Strings: FileChangeReasonStrings); |
| 116 | appendArgument(Name: "FileType" , Value: FileType, Strings: CharacteristicKindStrings); |
| 117 | appendArgument(Name: "PrevFID" , Value: PrevFID); |
| 118 | } |
| 119 | |
| 120 | // Callback invoked whenever a source file is skipped as the result |
| 121 | // of header guard optimization. |
| 122 | void PPCallbacksTracker::FileSkipped(const FileEntryRef &SkippedFile, |
| 123 | const Token &FilenameTok, |
| 124 | SrcMgr::CharacteristicKind FileType) { |
| 125 | beginCallback(Name: "FileSkipped" ); |
| 126 | appendArgument(Name: "ParentFile" , Value: SkippedFile); |
| 127 | appendArgument(Name: "FilenameTok" , Value: FilenameTok); |
| 128 | appendArgument(Name: "FileType" , Value: FileType, Strings: CharacteristicKindStrings); |
| 129 | } |
| 130 | |
| 131 | // Callback invoked whenever an inclusion directive of |
| 132 | // any kind (#include, #import, etc.) has been processed, regardless |
| 133 | // of whether the inclusion will actually result in an inclusion. |
| 134 | void PPCallbacksTracker::InclusionDirective( |
| 135 | SourceLocation HashLoc, const Token &IncludeTok, llvm::StringRef FileName, |
| 136 | bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File, |
| 137 | llvm::StringRef SearchPath, llvm::StringRef RelativePath, |
| 138 | const Module *SuggestedModule, bool ModuleImported, |
| 139 | SrcMgr::CharacteristicKind FileType) { |
| 140 | beginCallback(Name: "InclusionDirective" ); |
| 141 | appendArgument(Name: "HashLoc" , Value: HashLoc); |
| 142 | appendArgument(Name: "IncludeTok" , Value: IncludeTok); |
| 143 | appendFilePathArgument(Name: "FileName" , Value: FileName); |
| 144 | appendArgument(Name: "IsAngled" , Value: IsAngled); |
| 145 | appendArgument(Name: "FilenameRange" , Value: FilenameRange); |
| 146 | appendArgument(Name: "File" , Value: File); |
| 147 | appendFilePathArgument(Name: "SearchPath" , Value: SearchPath); |
| 148 | appendFilePathArgument(Name: "RelativePath" , Value: RelativePath); |
| 149 | appendArgument(Name: "SuggestedModule" , Value: SuggestedModule); |
| 150 | appendArgument(Name: "ModuleImported" , Value: ModuleImported); |
| 151 | } |
| 152 | |
| 153 | // Callback invoked whenever there was an explicit module-import |
| 154 | // syntax. |
| 155 | void PPCallbacksTracker::moduleImport(SourceLocation ImportLoc, |
| 156 | ModuleIdPath Path, |
| 157 | const Module *Imported) { |
| 158 | beginCallback(Name: "moduleImport" ); |
| 159 | appendArgument(Name: "ImportLoc" , Value: ImportLoc); |
| 160 | appendArgument(Name: "Path" , Value: Path); |
| 161 | appendArgument(Name: "Imported" , Value: Imported); |
| 162 | } |
| 163 | |
| 164 | // Callback invoked when the end of the main file is reached. |
| 165 | // No subsequent callbacks will be made. |
| 166 | void PPCallbacksTracker::EndOfMainFile() { beginCallback(Name: "EndOfMainFile" ); } |
| 167 | |
| 168 | // Callback invoked when a #ident or #sccs directive is read. |
| 169 | void PPCallbacksTracker::Ident(SourceLocation Loc, llvm::StringRef Str) { |
| 170 | beginCallback(Name: "Ident" ); |
| 171 | appendArgument(Name: "Loc" , Value: Loc); |
| 172 | appendArgument(Name: "Str" , Value: Str); |
| 173 | } |
| 174 | |
| 175 | // Callback invoked when start reading any pragma directive. |
| 176 | void PPCallbacksTracker::PragmaDirective(SourceLocation Loc, |
| 177 | PragmaIntroducerKind Introducer) { |
| 178 | beginCallback(Name: "PragmaDirective" ); |
| 179 | appendArgument(Name: "Loc" , Value: Loc); |
| 180 | appendArgument(Name: "Introducer" , Value: Introducer, Strings: PragmaIntroducerKindStrings); |
| 181 | } |
| 182 | |
| 183 | // Callback invoked when a #pragma comment directive is read. |
| 184 | void PPCallbacksTracker::(SourceLocation Loc, |
| 185 | const IdentifierInfo *Kind, |
| 186 | llvm::StringRef Str) { |
| 187 | beginCallback(Name: "PragmaComment" ); |
| 188 | appendArgument(Name: "Loc" , Value: Loc); |
| 189 | appendArgument(Name: "Kind" , Value: Kind); |
| 190 | appendArgument(Name: "Str" , Value: Str); |
| 191 | } |
| 192 | |
| 193 | // Callback invoked when a #pragma detect_mismatch directive is |
| 194 | // read. |
| 195 | void PPCallbacksTracker::PragmaDetectMismatch(SourceLocation Loc, |
| 196 | llvm::StringRef Name, |
| 197 | llvm::StringRef Value) { |
| 198 | beginCallback(Name: "PragmaDetectMismatch" ); |
| 199 | appendArgument(Name: "Loc" , Value: Loc); |
| 200 | appendArgument(Name: "Name" , Value: Name); |
| 201 | appendArgument(Name: "Value" , Value); |
| 202 | } |
| 203 | |
| 204 | // Callback invoked when a #pragma clang __debug directive is read. |
| 205 | void PPCallbacksTracker::PragmaDebug(SourceLocation Loc, |
| 206 | llvm::StringRef DebugType) { |
| 207 | beginCallback(Name: "PragmaDebug" ); |
| 208 | appendArgument(Name: "Loc" , Value: Loc); |
| 209 | appendArgument(Name: "DebugType" , Value: DebugType); |
| 210 | } |
| 211 | |
| 212 | // Callback invoked when a #pragma message directive is read. |
| 213 | void PPCallbacksTracker::PragmaMessage(SourceLocation Loc, |
| 214 | llvm::StringRef Namespace, |
| 215 | PPCallbacks::PragmaMessageKind Kind, |
| 216 | llvm::StringRef Str) { |
| 217 | beginCallback(Name: "PragmaMessage" ); |
| 218 | appendArgument(Name: "Loc" , Value: Loc); |
| 219 | appendArgument(Name: "Namespace" , Value: Namespace); |
| 220 | appendArgument(Name: "Kind" , Value: Kind, Strings: PragmaMessageKindStrings); |
| 221 | appendArgument(Name: "Str" , Value: Str); |
| 222 | } |
| 223 | |
| 224 | // Callback invoked when a #pragma gcc diagnostic push directive |
| 225 | // is read. |
| 226 | void PPCallbacksTracker::PragmaDiagnosticPush(SourceLocation Loc, |
| 227 | llvm::StringRef Namespace) { |
| 228 | beginCallback(Name: "PragmaDiagnosticPush" ); |
| 229 | appendArgument(Name: "Loc" , Value: Loc); |
| 230 | appendArgument(Name: "Namespace" , Value: Namespace); |
| 231 | } |
| 232 | |
| 233 | // Callback invoked when a #pragma gcc diagnostic pop directive |
| 234 | // is read. |
| 235 | void PPCallbacksTracker::PragmaDiagnosticPop(SourceLocation Loc, |
| 236 | llvm::StringRef Namespace) { |
| 237 | beginCallback(Name: "PragmaDiagnosticPop" ); |
| 238 | appendArgument(Name: "Loc" , Value: Loc); |
| 239 | appendArgument(Name: "Namespace" , Value: Namespace); |
| 240 | } |
| 241 | |
| 242 | // Callback invoked when a #pragma gcc diagnostic directive is read. |
| 243 | void PPCallbacksTracker::PragmaDiagnostic(SourceLocation Loc, |
| 244 | llvm::StringRef Namespace, |
| 245 | diag::Severity Mapping, |
| 246 | llvm::StringRef Str) { |
| 247 | beginCallback(Name: "PragmaDiagnostic" ); |
| 248 | appendArgument(Name: "Loc" , Value: Loc); |
| 249 | appendArgument(Name: "Namespace" , Value: Namespace); |
| 250 | appendArgument(Name: "Mapping" , Value: (unsigned)Mapping, Strings: MappingStrings); |
| 251 | appendArgument(Name: "Str" , Value: Str); |
| 252 | } |
| 253 | |
| 254 | // Called when an OpenCL extension is either disabled or |
| 255 | // enabled with a pragma. |
| 256 | void PPCallbacksTracker::PragmaOpenCLExtension(SourceLocation NameLoc, |
| 257 | const IdentifierInfo *Name, |
| 258 | SourceLocation StateLoc, |
| 259 | unsigned State) { |
| 260 | beginCallback(Name: "PragmaOpenCLExtension" ); |
| 261 | appendArgument(Name: "NameLoc" , Value: NameLoc); |
| 262 | appendArgument(Name: "Name" , Value: Name); |
| 263 | appendArgument(Name: "StateLoc" , Value: StateLoc); |
| 264 | appendArgument(Name: "State" , Value: (int)State); |
| 265 | } |
| 266 | |
| 267 | // Callback invoked when a #pragma warning directive is read. |
| 268 | void PPCallbacksTracker::PragmaWarning(SourceLocation Loc, |
| 269 | PragmaWarningSpecifier WarningSpec, |
| 270 | llvm::ArrayRef<int> Ids) { |
| 271 | beginCallback(Name: "PragmaWarning" ); |
| 272 | appendArgument(Name: "Loc" , Value: Loc); |
| 273 | appendArgument(Name: "WarningSpec" , Value: WarningSpec, Strings: PragmaWarningSpecifierStrings); |
| 274 | |
| 275 | std::string Str; |
| 276 | llvm::raw_string_ostream SS(Str); |
| 277 | SS << "[" ; |
| 278 | for (int i = 0, e = Ids.size(); i != e; ++i) { |
| 279 | if (i) |
| 280 | SS << ", " ; |
| 281 | SS << Ids[i]; |
| 282 | } |
| 283 | SS << "]" ; |
| 284 | appendArgument(Name: "Ids" , Value: SS.str()); |
| 285 | } |
| 286 | |
| 287 | // Callback invoked when a #pragma warning(push) directive is read. |
| 288 | void PPCallbacksTracker::PragmaWarningPush(SourceLocation Loc, int Level) { |
| 289 | beginCallback(Name: "PragmaWarningPush" ); |
| 290 | appendArgument(Name: "Loc" , Value: Loc); |
| 291 | appendArgument(Name: "Level" , Value: Level); |
| 292 | } |
| 293 | |
| 294 | // Callback invoked when a #pragma warning(pop) directive is read. |
| 295 | void PPCallbacksTracker::PragmaWarningPop(SourceLocation Loc) { |
| 296 | beginCallback(Name: "PragmaWarningPop" ); |
| 297 | appendArgument(Name: "Loc" , Value: Loc); |
| 298 | } |
| 299 | |
| 300 | // Callback invoked when a #pragma execution_character_set(push) directive |
| 301 | // is read. |
| 302 | void PPCallbacksTracker::PragmaExecCharsetPush(SourceLocation Loc, |
| 303 | StringRef Str) { |
| 304 | beginCallback(Name: "PragmaExecCharsetPush" ); |
| 305 | appendArgument(Name: "Loc" , Value: Loc); |
| 306 | appendArgument(Name: "Charset" , Value: Str); |
| 307 | } |
| 308 | |
| 309 | // Callback invoked when a #pragma execution_character_set(pop) directive |
| 310 | // is read. |
| 311 | void PPCallbacksTracker::PragmaExecCharsetPop(SourceLocation Loc) { |
| 312 | beginCallback(Name: "PragmaExecCharsetPop" ); |
| 313 | appendArgument(Name: "Loc" , Value: Loc); |
| 314 | } |
| 315 | |
| 316 | // Called by Preprocessor::HandleMacroExpandedIdentifier when a |
| 317 | // macro invocation is found. |
| 318 | void PPCallbacksTracker::MacroExpands(const Token &MacroNameTok, |
| 319 | const MacroDefinition &MacroDefinition, |
| 320 | SourceRange Range, |
| 321 | const MacroArgs *Args) { |
| 322 | beginCallback(Name: "MacroExpands" ); |
| 323 | appendArgument(Name: "MacroNameTok" , Value: MacroNameTok); |
| 324 | appendArgument(Name: "MacroDefinition" , Value: MacroDefinition); |
| 325 | appendArgument(Name: "Range" , Value: Range); |
| 326 | appendArgument(Name: "Args" , Value: Args); |
| 327 | } |
| 328 | |
| 329 | // Hook called whenever a macro definition is seen. |
| 330 | void PPCallbacksTracker::MacroDefined(const Token &MacroNameTok, |
| 331 | const MacroDirective *MacroDirective) { |
| 332 | beginCallback(Name: "MacroDefined" ); |
| 333 | appendArgument(Name: "MacroNameTok" , Value: MacroNameTok); |
| 334 | appendArgument(Name: "MacroDirective" , Value: MacroDirective); |
| 335 | } |
| 336 | |
| 337 | // Hook called whenever a macro #undef is seen. |
| 338 | void PPCallbacksTracker::MacroUndefined(const Token &MacroNameTok, |
| 339 | const MacroDefinition &MacroDefinition, |
| 340 | const MacroDirective *Undef) { |
| 341 | beginCallback(Name: "MacroUndefined" ); |
| 342 | appendArgument(Name: "MacroNameTok" , Value: MacroNameTok); |
| 343 | appendArgument(Name: "MacroDefinition" , Value: MacroDefinition); |
| 344 | } |
| 345 | |
| 346 | // Hook called whenever the 'defined' operator is seen. |
| 347 | void PPCallbacksTracker::Defined(const Token &MacroNameTok, |
| 348 | const MacroDefinition &MacroDefinition, |
| 349 | SourceRange Range) { |
| 350 | beginCallback(Name: "Defined" ); |
| 351 | appendArgument(Name: "MacroNameTok" , Value: MacroNameTok); |
| 352 | appendArgument(Name: "MacroDefinition" , Value: MacroDefinition); |
| 353 | appendArgument(Name: "Range" , Value: Range); |
| 354 | } |
| 355 | |
| 356 | // Hook called when a source range is skipped. |
| 357 | void PPCallbacksTracker::SourceRangeSkipped(SourceRange Range, |
| 358 | SourceLocation EndifLoc) { |
| 359 | beginCallback(Name: "SourceRangeSkipped" ); |
| 360 | appendArgument(Name: "Range" , Value: SourceRange(Range.getBegin(), EndifLoc)); |
| 361 | } |
| 362 | |
| 363 | // Hook called whenever an #if is seen. |
| 364 | void PPCallbacksTracker::If(SourceLocation Loc, SourceRange ConditionRange, |
| 365 | ConditionValueKind ConditionValue) { |
| 366 | beginCallback(Name: "If" ); |
| 367 | appendArgument(Name: "Loc" , Value: Loc); |
| 368 | appendArgument(Name: "ConditionRange" , Value: ConditionRange); |
| 369 | appendArgument(Name: "ConditionValue" , Value: ConditionValue, Strings: ConditionValueKindStrings); |
| 370 | } |
| 371 | |
| 372 | // Hook called whenever an #elif is seen. |
| 373 | void PPCallbacksTracker::Elif(SourceLocation Loc, SourceRange ConditionRange, |
| 374 | ConditionValueKind ConditionValue, |
| 375 | SourceLocation IfLoc) { |
| 376 | beginCallback(Name: "Elif" ); |
| 377 | appendArgument(Name: "Loc" , Value: Loc); |
| 378 | appendArgument(Name: "ConditionRange" , Value: ConditionRange); |
| 379 | appendArgument(Name: "ConditionValue" , Value: ConditionValue, Strings: ConditionValueKindStrings); |
| 380 | appendArgument(Name: "IfLoc" , Value: IfLoc); |
| 381 | } |
| 382 | |
| 383 | // Hook called whenever an #ifdef is seen. |
| 384 | void PPCallbacksTracker::Ifdef(SourceLocation Loc, const Token &MacroNameTok, |
| 385 | const MacroDefinition &MacroDefinition) { |
| 386 | beginCallback(Name: "Ifdef" ); |
| 387 | appendArgument(Name: "Loc" , Value: Loc); |
| 388 | appendArgument(Name: "MacroNameTok" , Value: MacroNameTok); |
| 389 | appendArgument(Name: "MacroDefinition" , Value: MacroDefinition); |
| 390 | } |
| 391 | |
| 392 | // Hook called whenever an #ifndef is seen. |
| 393 | void PPCallbacksTracker::Ifndef(SourceLocation Loc, const Token &MacroNameTok, |
| 394 | const MacroDefinition &MacroDefinition) { |
| 395 | beginCallback(Name: "Ifndef" ); |
| 396 | appendArgument(Name: "Loc" , Value: Loc); |
| 397 | appendArgument(Name: "MacroNameTok" , Value: MacroNameTok); |
| 398 | appendArgument(Name: "MacroDefinition" , Value: MacroDefinition); |
| 399 | } |
| 400 | |
| 401 | // Hook called whenever an #else is seen. |
| 402 | void PPCallbacksTracker::Else(SourceLocation Loc, SourceLocation IfLoc) { |
| 403 | beginCallback(Name: "Else" ); |
| 404 | appendArgument(Name: "Loc" , Value: Loc); |
| 405 | appendArgument(Name: "IfLoc" , Value: IfLoc); |
| 406 | } |
| 407 | |
| 408 | // Hook called whenever an #endif is seen. |
| 409 | void PPCallbacksTracker::Endif(SourceLocation Loc, SourceLocation IfLoc) { |
| 410 | beginCallback(Name: "Endif" ); |
| 411 | appendArgument(Name: "Loc" , Value: Loc); |
| 412 | appendArgument(Name: "IfLoc" , Value: IfLoc); |
| 413 | } |
| 414 | |
| 415 | // Helper functions. |
| 416 | |
| 417 | // Start a new callback. |
| 418 | void PPCallbacksTracker::beginCallback(const char *Name) { |
| 419 | auto R = CallbackIsEnabled.try_emplace(Key: Name, Args: false); |
| 420 | if (R.second) { |
| 421 | llvm::StringRef N(Name); |
| 422 | for (const std::pair<llvm::GlobPattern, bool> &Filter : Filters) |
| 423 | if (Filter.first.match(S: N)) |
| 424 | R.first->second = Filter.second; |
| 425 | } |
| 426 | DisableTrace = !R.first->second; |
| 427 | if (DisableTrace) |
| 428 | return; |
| 429 | CallbackCalls.push_back(x: CallbackCall(Name)); |
| 430 | } |
| 431 | |
| 432 | // Append a bool argument to the top trace item. |
| 433 | void PPCallbacksTracker::appendArgument(const char *Name, bool Value) { |
| 434 | appendArgument(Name, Value: (Value ? "true" : "false" )); |
| 435 | } |
| 436 | |
| 437 | // Append an int argument to the top trace item. |
| 438 | void PPCallbacksTracker::appendArgument(const char *Name, int Value) { |
| 439 | std::string Str; |
| 440 | llvm::raw_string_ostream SS(Str); |
| 441 | SS << Value; |
| 442 | appendArgument(Name, Value: SS.str()); |
| 443 | } |
| 444 | |
| 445 | // Append a string argument to the top trace item. |
| 446 | void PPCallbacksTracker::appendArgument(const char *Name, const char *Value) { |
| 447 | if (DisableTrace) |
| 448 | return; |
| 449 | CallbackCalls.back().Arguments.push_back(x: Argument{.Name: Name, .Value: Value}); |
| 450 | } |
| 451 | |
| 452 | // Append a string object argument to the top trace item. |
| 453 | void PPCallbacksTracker::appendArgument(const char *Name, |
| 454 | llvm::StringRef Value) { |
| 455 | appendArgument(Name, Value: Value.str()); |
| 456 | } |
| 457 | |
| 458 | // Append a string object argument to the top trace item. |
| 459 | void PPCallbacksTracker::appendArgument(const char *Name, |
| 460 | const std::string &Value) { |
| 461 | appendArgument(Name, Value: Value.c_str()); |
| 462 | } |
| 463 | |
| 464 | // Append a token argument to the top trace item. |
| 465 | void PPCallbacksTracker::appendArgument(const char *Name, const Token &Value) { |
| 466 | appendArgument(Name, Value: PP.getSpelling(Tok: Value)); |
| 467 | } |
| 468 | |
| 469 | // Append an enum argument to the top trace item. |
| 470 | void PPCallbacksTracker::appendArgument(const char *Name, int Value, |
| 471 | const char *const Strings[]) { |
| 472 | appendArgument(Name, Value: Strings[Value]); |
| 473 | } |
| 474 | |
| 475 | // Append a FileID argument to the top trace item. |
| 476 | void PPCallbacksTracker::appendArgument(const char *Name, FileID Value) { |
| 477 | if (Value.isInvalid()) { |
| 478 | appendArgument(Name, Value: "(invalid)" ); |
| 479 | return; |
| 480 | } |
| 481 | OptionalFileEntryRef FileEntry = |
| 482 | PP.getSourceManager().getFileEntryRefForID(FID: Value); |
| 483 | if (!FileEntry) { |
| 484 | appendArgument(Name, Value: "(getFileEntryForID failed)" ); |
| 485 | return; |
| 486 | } |
| 487 | appendFilePathArgument(Name, Value: FileEntry->getName()); |
| 488 | } |
| 489 | |
| 490 | // Append a FileEntry argument to the top trace item. |
| 491 | void PPCallbacksTracker::appendArgument(const char *Name, |
| 492 | OptionalFileEntryRef Value) { |
| 493 | if (!Value) { |
| 494 | appendArgument(Name, Value: "(null)" ); |
| 495 | return; |
| 496 | } |
| 497 | appendArgument(Name, Value: *Value); |
| 498 | } |
| 499 | |
| 500 | void PPCallbacksTracker::appendArgument(const char *Name, FileEntryRef Value) { |
| 501 | appendFilePathArgument(Name, Value: Value.getName()); |
| 502 | } |
| 503 | |
| 504 | // Append a SourceLocation argument to the top trace item. |
| 505 | void PPCallbacksTracker::appendArgument(const char *Name, |
| 506 | SourceLocation Value) { |
| 507 | if (Value.isInvalid()) { |
| 508 | appendArgument(Name, Value: "(invalid)" ); |
| 509 | return; |
| 510 | } |
| 511 | appendArgument(Name, Value: getSourceLocationString(PP, Loc: Value).c_str()); |
| 512 | } |
| 513 | |
| 514 | // Append a SourceRange argument to the top trace item. |
| 515 | void PPCallbacksTracker::appendArgument(const char *Name, SourceRange Value) { |
| 516 | if (DisableTrace) |
| 517 | return; |
| 518 | if (Value.isInvalid()) { |
| 519 | appendArgument(Name, Value: "(invalid)" ); |
| 520 | return; |
| 521 | } |
| 522 | std::string Str; |
| 523 | llvm::raw_string_ostream SS(Str); |
| 524 | SS << "[" << getSourceLocationString(PP, Loc: Value.getBegin()) << ", " |
| 525 | << getSourceLocationString(PP, Loc: Value.getEnd()) << "]" ; |
| 526 | appendArgument(Name, Value: SS.str()); |
| 527 | } |
| 528 | |
| 529 | // Append a CharSourceRange argument to the top trace item. |
| 530 | void PPCallbacksTracker::appendArgument(const char *Name, |
| 531 | CharSourceRange Value) { |
| 532 | if (Value.isInvalid()) { |
| 533 | appendArgument(Name, Value: "(invalid)" ); |
| 534 | return; |
| 535 | } |
| 536 | appendArgument(Name, Value: getSourceString(Range: Value).str().c_str()); |
| 537 | } |
| 538 | |
| 539 | // Append a SourceLocation argument to the top trace item. |
| 540 | void PPCallbacksTracker::appendArgument(const char *Name, ModuleIdPath Value) { |
| 541 | if (DisableTrace) |
| 542 | return; |
| 543 | std::string Str; |
| 544 | llvm::raw_string_ostream SS(Str); |
| 545 | SS << "[" ; |
| 546 | for (int I = 0, E = Value.size(); I != E; ++I) { |
| 547 | if (I) |
| 548 | SS << ", " ; |
| 549 | SS << "{" |
| 550 | << "Name: " << Value[I].getIdentifierInfo()->getName() << ", " |
| 551 | << "Loc: " << getSourceLocationString(PP, Loc: Value[I].getLoc()) << "}" ; |
| 552 | } |
| 553 | SS << "]" ; |
| 554 | appendArgument(Name, Value: SS.str()); |
| 555 | } |
| 556 | |
| 557 | // Append an IdentifierInfo argument to the top trace item. |
| 558 | void PPCallbacksTracker::appendArgument(const char *Name, |
| 559 | const IdentifierInfo *Value) { |
| 560 | if (!Value) { |
| 561 | appendArgument(Name, Value: "(null)" ); |
| 562 | return; |
| 563 | } |
| 564 | appendArgument(Name, Value: Value->getName().str().c_str()); |
| 565 | } |
| 566 | |
| 567 | // Append a MacroDirective argument to the top trace item. |
| 568 | void PPCallbacksTracker::appendArgument(const char *Name, |
| 569 | const MacroDirective *Value) { |
| 570 | if (!Value) { |
| 571 | appendArgument(Name, Value: "(null)" ); |
| 572 | return; |
| 573 | } |
| 574 | appendArgument(Name, Value: MacroDirectiveKindStrings[Value->getKind()]); |
| 575 | } |
| 576 | |
| 577 | // Append a MacroDefinition argument to the top trace item. |
| 578 | void PPCallbacksTracker::appendArgument(const char *Name, |
| 579 | const MacroDefinition &Value) { |
| 580 | std::string Str; |
| 581 | llvm::raw_string_ostream SS(Str); |
| 582 | SS << "[" ; |
| 583 | bool Any = false; |
| 584 | if (Value.getLocalDirective()) { |
| 585 | SS << "(local)" ; |
| 586 | Any = true; |
| 587 | } |
| 588 | for (auto *MM : Value.getModuleMacros()) { |
| 589 | if (Any) SS << ", " ; |
| 590 | SS << MM->getOwningModule()->getFullModuleName(); |
| 591 | } |
| 592 | SS << "]" ; |
| 593 | appendArgument(Name, Value: SS.str()); |
| 594 | } |
| 595 | |
| 596 | // Append a MacroArgs argument to the top trace item. |
| 597 | void PPCallbacksTracker::appendArgument(const char *Name, |
| 598 | const MacroArgs *Value) { |
| 599 | if (!Value) { |
| 600 | appendArgument(Name, Value: "(null)" ); |
| 601 | return; |
| 602 | } |
| 603 | std::string Str; |
| 604 | llvm::raw_string_ostream SS(Str); |
| 605 | SS << "[" ; |
| 606 | |
| 607 | // Each argument is a series of contiguous Tokens, terminated by a eof. |
| 608 | // Go through each argument printing tokens until we reach eof. |
| 609 | for (unsigned I = 0; I < Value->getNumMacroArguments(); ++I) { |
| 610 | const Token *Current = Value->getUnexpArgument(Arg: I); |
| 611 | if (I) |
| 612 | SS << ", " ; |
| 613 | bool First = true; |
| 614 | while (Current->isNot(K: tok::eof)) { |
| 615 | if (!First) |
| 616 | SS << " " ; |
| 617 | // We need to be careful here because the arguments might not be legal in |
| 618 | // YAML, so we use the token name for anything but identifiers and |
| 619 | // numeric literals. |
| 620 | if (Current->isAnyIdentifier() || Current->is(K: tok::numeric_constant)) { |
| 621 | SS << PP.getSpelling(Tok: *Current); |
| 622 | } else { |
| 623 | SS << "<" << Current->getName() << ">" ; |
| 624 | } |
| 625 | ++Current; |
| 626 | First = false; |
| 627 | } |
| 628 | } |
| 629 | SS << "]" ; |
| 630 | appendArgument(Name, Value: SS.str()); |
| 631 | } |
| 632 | |
| 633 | // Append a Module argument to the top trace item. |
| 634 | void PPCallbacksTracker::appendArgument(const char *Name, const Module *Value) { |
| 635 | if (!Value) { |
| 636 | appendArgument(Name, Value: "(null)" ); |
| 637 | return; |
| 638 | } |
| 639 | appendArgument(Name, Value: Value->Name.c_str()); |
| 640 | } |
| 641 | |
| 642 | // Append a double-quoted argument to the top trace item. |
| 643 | void PPCallbacksTracker::appendQuotedArgument(const char *Name, |
| 644 | const std::string &Value) { |
| 645 | std::string Str; |
| 646 | llvm::raw_string_ostream SS(Str); |
| 647 | SS << "\"" << Value << "\"" ; |
| 648 | appendArgument(Name, Value: SS.str()); |
| 649 | } |
| 650 | |
| 651 | // Append a double-quoted file path argument to the top trace item. |
| 652 | void PPCallbacksTracker::appendFilePathArgument(const char *Name, |
| 653 | llvm::StringRef Value) { |
| 654 | std::string Path(Value); |
| 655 | // YAML treats backslash as escape, so use forward slashes. |
| 656 | llvm::replace(Range&: Path, OldValue: '\\', NewValue: '/'); |
| 657 | appendQuotedArgument(Name, Value: Path); |
| 658 | } |
| 659 | |
| 660 | // Get the raw source string of the range. |
| 661 | llvm::StringRef PPCallbacksTracker::getSourceString(CharSourceRange Range) { |
| 662 | const char *B = PP.getSourceManager().getCharacterData(SL: Range.getBegin()); |
| 663 | const char *E = PP.getSourceManager().getCharacterData(SL: Range.getEnd()); |
| 664 | return llvm::StringRef(B, E - B); |
| 665 | } |
| 666 | |
| 667 | } // namespace pp_trace |
| 668 | } // namespace clang |
| 669 | |