1 //===- PathDiagnostic.cpp - Path-Specific Diagnostic Handling -------------===// 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 // This file defines the PathDiagnostic-related interfaces. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/Analysis/PathDiagnostic.h" 14 #include "clang/AST/Decl.h" 15 #include "clang/AST/DeclBase.h" 16 #include "clang/AST/DeclCXX.h" 17 #include "clang/AST/DeclObjC.h" 18 #include "clang/AST/DeclTemplate.h" 19 #include "clang/AST/Expr.h" 20 #include "clang/AST/ExprCXX.h" 21 #include "clang/AST/OperationKinds.h" 22 #include "clang/AST/ParentMap.h" 23 #include "clang/AST/PrettyPrinter.h" 24 #include "clang/AST/Stmt.h" 25 #include "clang/AST/Type.h" 26 #include "clang/Analysis/AnalysisDeclContext.h" 27 #include "clang/Analysis/CFG.h" 28 #include "clang/Analysis/ProgramPoint.h" 29 #include "clang/Basic/FileManager.h" 30 #include "clang/Basic/LLVM.h" 31 #include "clang/Basic/SourceLocation.h" 32 #include "clang/Basic/SourceManager.h" 33 #include "llvm/ADT/ArrayRef.h" 34 #include "llvm/ADT/FoldingSet.h" 35 #include "llvm/ADT/STLExtras.h" 36 #include "llvm/ADT/SmallString.h" 37 #include "llvm/ADT/SmallVector.h" 38 #include "llvm/ADT/StringExtras.h" 39 #include "llvm/ADT/StringRef.h" 40 #include "llvm/Support/Casting.h" 41 #include "llvm/Support/ErrorHandling.h" 42 #include "llvm/Support/raw_ostream.h" 43 #include <cassert> 44 #include <cstring> 45 #include <memory> 46 #include <optional> 47 #include <utility> 48 #include <vector> 49 50 using namespace clang; 51 using namespace ento; 52 53 static StringRef StripTrailingDots(StringRef s) { 54 for (StringRef::size_type i = s.size(); i != 0; --i) 55 if (s[i - 1] != '.') 56 return s.substr(0, i); 57 return {}; 58 } 59 60 PathDiagnosticPiece::PathDiagnosticPiece(StringRef s, 61 Kind k, DisplayHint hint) 62 : str(StripTrailingDots(s)), kind(k), Hint(hint) {} 63 64 PathDiagnosticPiece::PathDiagnosticPiece(Kind k, DisplayHint hint) 65 : kind(k), Hint(hint) {} 66 67 PathDiagnosticPiece::~PathDiagnosticPiece() = default; 68 69 PathDiagnosticEventPiece::~PathDiagnosticEventPiece() = default; 70 71 PathDiagnosticCallPiece::~PathDiagnosticCallPiece() = default; 72 73 PathDiagnosticControlFlowPiece::~PathDiagnosticControlFlowPiece() = default; 74 75 PathDiagnosticMacroPiece::~PathDiagnosticMacroPiece() = default; 76 77 PathDiagnosticNotePiece::~PathDiagnosticNotePiece() = default; 78 79 PathDiagnosticPopUpPiece::~PathDiagnosticPopUpPiece() = default; 80 81 void PathPieces::flattenTo(PathPieces &Primary, PathPieces &Current, 82 bool ShouldFlattenMacros) const { 83 for (auto &Piece : *this) { 84 switch (Piece->getKind()) { 85 case PathDiagnosticPiece::Call: { 86 auto &Call = cast<PathDiagnosticCallPiece>(*Piece); 87 if (auto CallEnter = Call.getCallEnterEvent()) 88 Current.push_back(std::move(CallEnter)); 89 Call.path.flattenTo(Primary, Primary, ShouldFlattenMacros); 90 if (auto callExit = Call.getCallExitEvent()) 91 Current.push_back(std::move(callExit)); 92 break; 93 } 94 case PathDiagnosticPiece::Macro: { 95 auto &Macro = cast<PathDiagnosticMacroPiece>(*Piece); 96 if (ShouldFlattenMacros) { 97 Macro.subPieces.flattenTo(Primary, Primary, ShouldFlattenMacros); 98 } else { 99 Current.push_back(Piece); 100 PathPieces NewPath; 101 Macro.subPieces.flattenTo(Primary, NewPath, ShouldFlattenMacros); 102 // FIXME: This probably shouldn't mutate the original path piece. 103 Macro.subPieces = NewPath; 104 } 105 break; 106 } 107 case PathDiagnosticPiece::Event: 108 case PathDiagnosticPiece::ControlFlow: 109 case PathDiagnosticPiece::Note: 110 case PathDiagnosticPiece::PopUp: 111 Current.push_back(Piece); 112 break; 113 } 114 } 115 } 116 117 PathDiagnostic::~PathDiagnostic() = default; 118 119 PathDiagnostic::PathDiagnostic( 120 StringRef CheckerName, const Decl *declWithIssue, StringRef bugtype, 121 StringRef verboseDesc, StringRef shortDesc, StringRef category, 122 PathDiagnosticLocation LocationToUnique, const Decl *DeclToUnique, 123 std::unique_ptr<FilesToLineNumsMap> ExecutedLines) 124 : CheckerName(CheckerName), DeclWithIssue(declWithIssue), 125 BugType(StripTrailingDots(bugtype)), 126 VerboseDesc(StripTrailingDots(verboseDesc)), 127 ShortDesc(StripTrailingDots(shortDesc)), 128 Category(StripTrailingDots(category)), UniqueingLoc(LocationToUnique), 129 UniqueingDecl(DeclToUnique), ExecutedLines(std::move(ExecutedLines)), 130 path(pathImpl) {} 131 132 void PathDiagnosticConsumer::anchor() {} 133 134 PathDiagnosticConsumer::~PathDiagnosticConsumer() { 135 // Delete the contents of the FoldingSet if it isn't empty already. 136 for (auto &Diag : Diags) 137 delete &Diag; 138 } 139 140 void PathDiagnosticConsumer::HandlePathDiagnostic( 141 std::unique_ptr<PathDiagnostic> D) { 142 if (!D || D->path.empty()) 143 return; 144 145 // We need to flatten the locations (convert Stmt* to locations) because 146 // the referenced statements may be freed by the time the diagnostics 147 // are emitted. 148 D->flattenLocations(); 149 150 // If the PathDiagnosticConsumer does not support diagnostics that 151 // cross file boundaries, prune out such diagnostics now. 152 if (!supportsCrossFileDiagnostics()) { 153 // Verify that the entire path is from the same FileID. 154 FileID FID; 155 const SourceManager &SMgr = D->path.front()->getLocation().getManager(); 156 SmallVector<const PathPieces *, 5> WorkList; 157 WorkList.push_back(&D->path); 158 SmallString<128> buf; 159 llvm::raw_svector_ostream warning(buf); 160 warning << "warning: Path diagnostic report is not generated. Current " 161 << "output format does not support diagnostics that cross file " 162 << "boundaries. Refer to --analyzer-output for valid output " 163 << "formats\n"; 164 165 while (!WorkList.empty()) { 166 const PathPieces &path = *WorkList.pop_back_val(); 167 168 for (const auto &I : path) { 169 const PathDiagnosticPiece *piece = I.get(); 170 FullSourceLoc L = piece->getLocation().asLocation().getExpansionLoc(); 171 172 if (FID.isInvalid()) { 173 FID = SMgr.getFileID(L); 174 } else if (SMgr.getFileID(L) != FID) { 175 llvm::errs() << warning.str(); 176 return; 177 } 178 179 // Check the source ranges. 180 ArrayRef<SourceRange> Ranges = piece->getRanges(); 181 for (const auto &I : Ranges) { 182 SourceLocation L = SMgr.getExpansionLoc(I.getBegin()); 183 if (!L.isFileID() || SMgr.getFileID(L) != FID) { 184 llvm::errs() << warning.str(); 185 return; 186 } 187 L = SMgr.getExpansionLoc(I.getEnd()); 188 if (!L.isFileID() || SMgr.getFileID(L) != FID) { 189 llvm::errs() << warning.str(); 190 return; 191 } 192 } 193 194 if (const auto *call = dyn_cast<PathDiagnosticCallPiece>(piece)) 195 WorkList.push_back(&call->path); 196 else if (const auto *macro = dyn_cast<PathDiagnosticMacroPiece>(piece)) 197 WorkList.push_back(¯o->subPieces); 198 } 199 } 200 201 if (FID.isInvalid()) 202 return; // FIXME: Emit a warning? 203 } 204 205 // Profile the node to see if we already have something matching it 206 llvm::FoldingSetNodeID profile; 207 D->Profile(profile); 208 void *InsertPos = nullptr; 209 210 if (PathDiagnostic *orig = Diags.FindNodeOrInsertPos(profile, InsertPos)) { 211 // Keep the PathDiagnostic with the shorter path. 212 // Note, the enclosing routine is called in deterministic order, so the 213 // results will be consistent between runs (no reason to break ties if the 214 // size is the same). 215 const unsigned orig_size = orig->full_size(); 216 const unsigned new_size = D->full_size(); 217 if (orig_size <= new_size) 218 return; 219 220 assert(orig != D.get()); 221 Diags.RemoveNode(orig); 222 delete orig; 223 } 224 225 Diags.InsertNode(D.release()); 226 } 227 228 static std::optional<bool> comparePath(const PathPieces &X, 229 const PathPieces &Y); 230 231 static std::optional<bool> 232 compareControlFlow(const PathDiagnosticControlFlowPiece &X, 233 const PathDiagnosticControlFlowPiece &Y) { 234 FullSourceLoc XSL = X.getStartLocation().asLocation(); 235 FullSourceLoc YSL = Y.getStartLocation().asLocation(); 236 if (XSL != YSL) 237 return XSL.isBeforeInTranslationUnitThan(YSL); 238 FullSourceLoc XEL = X.getEndLocation().asLocation(); 239 FullSourceLoc YEL = Y.getEndLocation().asLocation(); 240 if (XEL != YEL) 241 return XEL.isBeforeInTranslationUnitThan(YEL); 242 return std::nullopt; 243 } 244 245 static std::optional<bool> compareMacro(const PathDiagnosticMacroPiece &X, 246 const PathDiagnosticMacroPiece &Y) { 247 return comparePath(X.subPieces, Y.subPieces); 248 } 249 250 static std::optional<bool> compareCall(const PathDiagnosticCallPiece &X, 251 const PathDiagnosticCallPiece &Y) { 252 FullSourceLoc X_CEL = X.callEnter.asLocation(); 253 FullSourceLoc Y_CEL = Y.callEnter.asLocation(); 254 if (X_CEL != Y_CEL) 255 return X_CEL.isBeforeInTranslationUnitThan(Y_CEL); 256 FullSourceLoc X_CEWL = X.callEnterWithin.asLocation(); 257 FullSourceLoc Y_CEWL = Y.callEnterWithin.asLocation(); 258 if (X_CEWL != Y_CEWL) 259 return X_CEWL.isBeforeInTranslationUnitThan(Y_CEWL); 260 FullSourceLoc X_CRL = X.callReturn.asLocation(); 261 FullSourceLoc Y_CRL = Y.callReturn.asLocation(); 262 if (X_CRL != Y_CRL) 263 return X_CRL.isBeforeInTranslationUnitThan(Y_CRL); 264 return comparePath(X.path, Y.path); 265 } 266 267 static std::optional<bool> comparePiece(const PathDiagnosticPiece &X, 268 const PathDiagnosticPiece &Y) { 269 if (X.getKind() != Y.getKind()) 270 return X.getKind() < Y.getKind(); 271 272 FullSourceLoc XL = X.getLocation().asLocation(); 273 FullSourceLoc YL = Y.getLocation().asLocation(); 274 if (XL != YL) 275 return XL.isBeforeInTranslationUnitThan(YL); 276 277 if (X.getString() != Y.getString()) 278 return X.getString() < Y.getString(); 279 280 if (X.getRanges().size() != Y.getRanges().size()) 281 return X.getRanges().size() < Y.getRanges().size(); 282 283 const SourceManager &SM = XL.getManager(); 284 285 for (unsigned i = 0, n = X.getRanges().size(); i < n; ++i) { 286 SourceRange XR = X.getRanges()[i]; 287 SourceRange YR = Y.getRanges()[i]; 288 if (XR != YR) { 289 if (XR.getBegin() != YR.getBegin()) 290 return SM.isBeforeInTranslationUnit(XR.getBegin(), YR.getBegin()); 291 return SM.isBeforeInTranslationUnit(XR.getEnd(), YR.getEnd()); 292 } 293 } 294 295 switch (X.getKind()) { 296 case PathDiagnosticPiece::ControlFlow: 297 return compareControlFlow(cast<PathDiagnosticControlFlowPiece>(X), 298 cast<PathDiagnosticControlFlowPiece>(Y)); 299 case PathDiagnosticPiece::Macro: 300 return compareMacro(cast<PathDiagnosticMacroPiece>(X), 301 cast<PathDiagnosticMacroPiece>(Y)); 302 case PathDiagnosticPiece::Call: 303 return compareCall(cast<PathDiagnosticCallPiece>(X), 304 cast<PathDiagnosticCallPiece>(Y)); 305 case PathDiagnosticPiece::Event: 306 case PathDiagnosticPiece::Note: 307 case PathDiagnosticPiece::PopUp: 308 return std::nullopt; 309 } 310 llvm_unreachable("all cases handled"); 311 } 312 313 static std::optional<bool> comparePath(const PathPieces &X, 314 const PathPieces &Y) { 315 if (X.size() != Y.size()) 316 return X.size() < Y.size(); 317 318 PathPieces::const_iterator X_I = X.begin(), X_end = X.end(); 319 PathPieces::const_iterator Y_I = Y.begin(), Y_end = Y.end(); 320 321 for (; X_I != X_end && Y_I != Y_end; ++X_I, ++Y_I) 322 if (std::optional<bool> b = comparePiece(**X_I, **Y_I)) 323 return *b; 324 325 return std::nullopt; 326 } 327 328 static bool compareCrossTUSourceLocs(FullSourceLoc XL, FullSourceLoc YL) { 329 if (XL.isInvalid() && YL.isValid()) 330 return true; 331 if (XL.isValid() && YL.isInvalid()) 332 return false; 333 std::pair<FileID, unsigned> XOffs = XL.getDecomposedLoc(); 334 std::pair<FileID, unsigned> YOffs = YL.getDecomposedLoc(); 335 const SourceManager &SM = XL.getManager(); 336 std::pair<bool, bool> InSameTU = SM.isInTheSameTranslationUnit(XOffs, YOffs); 337 if (InSameTU.first) 338 return XL.isBeforeInTranslationUnitThan(YL); 339 OptionalFileEntryRef XFE = 340 SM.getFileEntryRefForID(XL.getSpellingLoc().getFileID()); 341 OptionalFileEntryRef YFE = SM.getFileEntryRefForID(YL.getSpellingLoc().getFileID()); 342 if (!XFE || !YFE) 343 return XFE && !YFE; 344 int NameCmp = XFE->getName().compare(YFE->getName()); 345 if (NameCmp != 0) 346 return NameCmp < 0; 347 // Last resort: Compare raw file IDs that are possibly expansions. 348 return XL.getFileID() < YL.getFileID(); 349 } 350 351 static bool compare(const PathDiagnostic &X, const PathDiagnostic &Y) { 352 FullSourceLoc XL = X.getLocation().asLocation(); 353 FullSourceLoc YL = Y.getLocation().asLocation(); 354 if (XL != YL) 355 return compareCrossTUSourceLocs(XL, YL); 356 FullSourceLoc XUL = X.getUniqueingLoc().asLocation(); 357 FullSourceLoc YUL = Y.getUniqueingLoc().asLocation(); 358 if (XUL != YUL) 359 return compareCrossTUSourceLocs(XUL, YUL); 360 if (X.getBugType() != Y.getBugType()) 361 return X.getBugType() < Y.getBugType(); 362 if (X.getCategory() != Y.getCategory()) 363 return X.getCategory() < Y.getCategory(); 364 if (X.getVerboseDescription() != Y.getVerboseDescription()) 365 return X.getVerboseDescription() < Y.getVerboseDescription(); 366 if (X.getShortDescription() != Y.getShortDescription()) 367 return X.getShortDescription() < Y.getShortDescription(); 368 auto CompareDecls = [&XL](const Decl *D1, 369 const Decl *D2) -> std::optional<bool> { 370 if (D1 == D2) 371 return std::nullopt; 372 if (!D1) 373 return true; 374 if (!D2) 375 return false; 376 SourceLocation D1L = D1->getLocation(); 377 SourceLocation D2L = D2->getLocation(); 378 if (D1L != D2L) { 379 const SourceManager &SM = XL.getManager(); 380 return compareCrossTUSourceLocs(FullSourceLoc(D1L, SM), 381 FullSourceLoc(D2L, SM)); 382 } 383 return std::nullopt; 384 }; 385 if (auto Result = CompareDecls(X.getDeclWithIssue(), Y.getDeclWithIssue())) 386 return *Result; 387 if (XUL.isValid()) { 388 if (auto Result = CompareDecls(X.getUniqueingDecl(), Y.getUniqueingDecl())) 389 return *Result; 390 } 391 PathDiagnostic::meta_iterator XI = X.meta_begin(), XE = X.meta_end(); 392 PathDiagnostic::meta_iterator YI = Y.meta_begin(), YE = Y.meta_end(); 393 if (XE - XI != YE - YI) 394 return (XE - XI) < (YE - YI); 395 for ( ; XI != XE ; ++XI, ++YI) { 396 if (*XI != *YI) 397 return (*XI) < (*YI); 398 } 399 return *comparePath(X.path, Y.path); 400 } 401 402 void PathDiagnosticConsumer::FlushDiagnostics( 403 PathDiagnosticConsumer::FilesMade *Files) { 404 if (flushed) 405 return; 406 407 flushed = true; 408 409 std::vector<const PathDiagnostic *> BatchDiags; 410 for (const auto &D : Diags) 411 BatchDiags.push_back(&D); 412 413 // Sort the diagnostics so that they are always emitted in a deterministic 414 // order. 415 int (*Comp)(const PathDiagnostic *const *, const PathDiagnostic *const *) = 416 [](const PathDiagnostic *const *X, const PathDiagnostic *const *Y) { 417 assert(*X != *Y && "PathDiagnostics not uniqued!"); 418 if (compare(**X, **Y)) 419 return -1; 420 assert(compare(**Y, **X) && "Not a total order!"); 421 return 1; 422 }; 423 array_pod_sort(BatchDiags.begin(), BatchDiags.end(), Comp); 424 425 FlushDiagnosticsImpl(BatchDiags, Files); 426 427 // Delete the flushed diagnostics. 428 for (const auto D : BatchDiags) 429 delete D; 430 431 // Clear out the FoldingSet. 432 Diags.clear(); 433 } 434 435 PathDiagnosticConsumer::FilesMade::~FilesMade() { 436 for (auto It = Set.begin(); It != Set.end();) 437 (It++)->~PDFileEntry(); 438 } 439 440 void PathDiagnosticConsumer::FilesMade::addDiagnostic(const PathDiagnostic &PD, 441 StringRef ConsumerName, 442 StringRef FileName) { 443 llvm::FoldingSetNodeID NodeID; 444 NodeID.Add(PD); 445 void *InsertPos; 446 PDFileEntry *Entry = Set.FindNodeOrInsertPos(NodeID, InsertPos); 447 if (!Entry) { 448 Entry = Alloc.Allocate<PDFileEntry>(); 449 Entry = new (Entry) PDFileEntry(NodeID); 450 Set.InsertNode(Entry, InsertPos); 451 } 452 453 // Allocate persistent storage for the file name. 454 char *FileName_cstr = (char*) Alloc.Allocate(FileName.size(), 1); 455 memcpy(FileName_cstr, FileName.data(), FileName.size()); 456 457 Entry->files.push_back(std::make_pair(ConsumerName, 458 StringRef(FileName_cstr, 459 FileName.size()))); 460 } 461 462 PathDiagnosticConsumer::PDFileEntry::ConsumerFiles * 463 PathDiagnosticConsumer::FilesMade::getFiles(const PathDiagnostic &PD) { 464 llvm::FoldingSetNodeID NodeID; 465 NodeID.Add(PD); 466 void *InsertPos; 467 PDFileEntry *Entry = Set.FindNodeOrInsertPos(NodeID, InsertPos); 468 if (!Entry) 469 return nullptr; 470 return &Entry->files; 471 } 472 473 //===----------------------------------------------------------------------===// 474 // PathDiagnosticLocation methods. 475 //===----------------------------------------------------------------------===// 476 477 SourceLocation PathDiagnosticLocation::getValidSourceLocation( 478 const Stmt *S, LocationOrAnalysisDeclContext LAC, bool UseEndOfStatement) { 479 SourceLocation L = UseEndOfStatement ? S->getEndLoc() : S->getBeginLoc(); 480 assert(!LAC.isNull() && 481 "A valid LocationContext or AnalysisDeclContext should be passed to " 482 "PathDiagnosticLocation upon creation."); 483 484 // S might be a temporary statement that does not have a location in the 485 // source code, so find an enclosing statement and use its location. 486 if (!L.isValid()) { 487 AnalysisDeclContext *ADC; 488 if (LAC.is<const LocationContext*>()) 489 ADC = LAC.get<const LocationContext*>()->getAnalysisDeclContext(); 490 else 491 ADC = LAC.get<AnalysisDeclContext*>(); 492 493 ParentMap &PM = ADC->getParentMap(); 494 495 const Stmt *Parent = S; 496 do { 497 Parent = PM.getParent(Parent); 498 499 // In rare cases, we have implicit top-level expressions, 500 // such as arguments for implicit member initializers. 501 // In this case, fall back to the start of the body (even if we were 502 // asked for the statement end location). 503 if (!Parent) { 504 const Stmt *Body = ADC->getBody(); 505 if (Body) 506 L = Body->getBeginLoc(); 507 else 508 L = ADC->getDecl()->getEndLoc(); 509 break; 510 } 511 512 L = UseEndOfStatement ? Parent->getEndLoc() : Parent->getBeginLoc(); 513 } while (!L.isValid()); 514 } 515 516 // FIXME: Ironically, this assert actually fails in some cases. 517 //assert(L.isValid()); 518 return L; 519 } 520 521 static PathDiagnosticLocation 522 getLocationForCaller(const StackFrameContext *SFC, 523 const LocationContext *CallerCtx, 524 const SourceManager &SM) { 525 const CFGBlock &Block = *SFC->getCallSiteBlock(); 526 CFGElement Source = Block[SFC->getIndex()]; 527 528 switch (Source.getKind()) { 529 case CFGElement::Statement: 530 case CFGElement::Constructor: 531 case CFGElement::CXXRecordTypedCall: 532 return PathDiagnosticLocation(Source.castAs<CFGStmt>().getStmt(), 533 SM, CallerCtx); 534 case CFGElement::Initializer: { 535 const CFGInitializer &Init = Source.castAs<CFGInitializer>(); 536 return PathDiagnosticLocation(Init.getInitializer()->getInit(), 537 SM, CallerCtx); 538 } 539 case CFGElement::AutomaticObjectDtor: { 540 const CFGAutomaticObjDtor &Dtor = Source.castAs<CFGAutomaticObjDtor>(); 541 return PathDiagnosticLocation::createEnd(Dtor.getTriggerStmt(), 542 SM, CallerCtx); 543 } 544 case CFGElement::DeleteDtor: { 545 const CFGDeleteDtor &Dtor = Source.castAs<CFGDeleteDtor>(); 546 return PathDiagnosticLocation(Dtor.getDeleteExpr(), SM, CallerCtx); 547 } 548 case CFGElement::BaseDtor: 549 case CFGElement::MemberDtor: { 550 const AnalysisDeclContext *CallerInfo = CallerCtx->getAnalysisDeclContext(); 551 if (const Stmt *CallerBody = CallerInfo->getBody()) 552 return PathDiagnosticLocation::createEnd(CallerBody, SM, CallerCtx); 553 return PathDiagnosticLocation::create(CallerInfo->getDecl(), SM); 554 } 555 case CFGElement::NewAllocator: { 556 const CFGNewAllocator &Alloc = Source.castAs<CFGNewAllocator>(); 557 return PathDiagnosticLocation(Alloc.getAllocatorExpr(), SM, CallerCtx); 558 } 559 case CFGElement::TemporaryDtor: { 560 // Temporary destructors are for temporaries. They die immediately at around 561 // the location of CXXBindTemporaryExpr. If they are lifetime-extended, 562 // they'd be dealt with via an AutomaticObjectDtor instead. 563 const auto &Dtor = Source.castAs<CFGTemporaryDtor>(); 564 return PathDiagnosticLocation::createEnd(Dtor.getBindTemporaryExpr(), SM, 565 CallerCtx); 566 } 567 case CFGElement::ScopeBegin: 568 case CFGElement::ScopeEnd: 569 llvm_unreachable("not yet implemented!"); 570 case CFGElement::LifetimeEnds: 571 case CFGElement::LoopExit: 572 llvm_unreachable("CFGElement kind should not be on callsite!"); 573 } 574 575 llvm_unreachable("Unknown CFGElement kind"); 576 } 577 578 PathDiagnosticLocation 579 PathDiagnosticLocation::createBegin(const Decl *D, 580 const SourceManager &SM) { 581 return PathDiagnosticLocation(D->getBeginLoc(), SM, SingleLocK); 582 } 583 584 PathDiagnosticLocation 585 PathDiagnosticLocation::createBegin(const Stmt *S, 586 const SourceManager &SM, 587 LocationOrAnalysisDeclContext LAC) { 588 assert(S && "Statement cannot be null"); 589 return PathDiagnosticLocation(getValidSourceLocation(S, LAC), 590 SM, SingleLocK); 591 } 592 593 PathDiagnosticLocation 594 PathDiagnosticLocation::createEnd(const Stmt *S, 595 const SourceManager &SM, 596 LocationOrAnalysisDeclContext LAC) { 597 if (const auto *CS = dyn_cast<CompoundStmt>(S)) 598 return createEndBrace(CS, SM); 599 return PathDiagnosticLocation(getValidSourceLocation(S, LAC, /*End=*/true), 600 SM, SingleLocK); 601 } 602 603 PathDiagnosticLocation 604 PathDiagnosticLocation::createOperatorLoc(const BinaryOperator *BO, 605 const SourceManager &SM) { 606 return PathDiagnosticLocation(BO->getOperatorLoc(), SM, SingleLocK); 607 } 608 609 PathDiagnosticLocation 610 PathDiagnosticLocation::createConditionalColonLoc( 611 const ConditionalOperator *CO, 612 const SourceManager &SM) { 613 return PathDiagnosticLocation(CO->getColonLoc(), SM, SingleLocK); 614 } 615 616 PathDiagnosticLocation 617 PathDiagnosticLocation::createMemberLoc(const MemberExpr *ME, 618 const SourceManager &SM) { 619 620 assert(ME->getMemberLoc().isValid() || ME->getBeginLoc().isValid()); 621 622 // In some cases, getMemberLoc isn't valid -- in this case we'll return with 623 // some other related valid SourceLocation. 624 if (ME->getMemberLoc().isValid()) 625 return PathDiagnosticLocation(ME->getMemberLoc(), SM, SingleLocK); 626 627 return PathDiagnosticLocation(ME->getBeginLoc(), SM, SingleLocK); 628 } 629 630 PathDiagnosticLocation 631 PathDiagnosticLocation::createBeginBrace(const CompoundStmt *CS, 632 const SourceManager &SM) { 633 SourceLocation L = CS->getLBracLoc(); 634 return PathDiagnosticLocation(L, SM, SingleLocK); 635 } 636 637 PathDiagnosticLocation 638 PathDiagnosticLocation::createEndBrace(const CompoundStmt *CS, 639 const SourceManager &SM) { 640 SourceLocation L = CS->getRBracLoc(); 641 return PathDiagnosticLocation(L, SM, SingleLocK); 642 } 643 644 PathDiagnosticLocation 645 PathDiagnosticLocation::createDeclBegin(const LocationContext *LC, 646 const SourceManager &SM) { 647 // FIXME: Should handle CXXTryStmt if analyser starts supporting C++. 648 if (const auto *CS = dyn_cast_or_null<CompoundStmt>(LC->getDecl()->getBody())) 649 if (!CS->body_empty()) { 650 SourceLocation Loc = (*CS->body_begin())->getBeginLoc(); 651 return PathDiagnosticLocation(Loc, SM, SingleLocK); 652 } 653 654 return PathDiagnosticLocation(); 655 } 656 657 PathDiagnosticLocation 658 PathDiagnosticLocation::createDeclEnd(const LocationContext *LC, 659 const SourceManager &SM) { 660 SourceLocation L = LC->getDecl()->getBodyRBrace(); 661 return PathDiagnosticLocation(L, SM, SingleLocK); 662 } 663 664 PathDiagnosticLocation 665 PathDiagnosticLocation::create(const ProgramPoint& P, 666 const SourceManager &SMng) { 667 const Stmt* S = nullptr; 668 if (std::optional<BlockEdge> BE = P.getAs<BlockEdge>()) { 669 const CFGBlock *BSrc = BE->getSrc(); 670 if (BSrc->getTerminator().isVirtualBaseBranch()) { 671 // TODO: VirtualBaseBranches should also appear for destructors. 672 // In this case we should put the diagnostic at the end of decl. 673 return PathDiagnosticLocation::createBegin( 674 P.getLocationContext()->getDecl(), SMng); 675 676 } else { 677 S = BSrc->getTerminatorCondition(); 678 if (!S) { 679 // If the BlockEdge has no terminator condition statement but its 680 // source is the entry of the CFG (e.g. a checker crated the branch at 681 // the beginning of a function), use the function's declaration instead. 682 assert(BSrc == &BSrc->getParent()->getEntry() && "CFGBlock has no " 683 "TerminatorCondition and is not the enrty block of the CFG"); 684 return PathDiagnosticLocation::createBegin( 685 P.getLocationContext()->getDecl(), SMng); 686 } 687 } 688 } else if (std::optional<StmtPoint> SP = P.getAs<StmtPoint>()) { 689 S = SP->getStmt(); 690 if (P.getAs<PostStmtPurgeDeadSymbols>()) 691 return PathDiagnosticLocation::createEnd(S, SMng, P.getLocationContext()); 692 } else if (std::optional<PostInitializer> PIP = P.getAs<PostInitializer>()) { 693 return PathDiagnosticLocation(PIP->getInitializer()->getSourceLocation(), 694 SMng); 695 } else if (std::optional<PreImplicitCall> PIC = P.getAs<PreImplicitCall>()) { 696 return PathDiagnosticLocation(PIC->getLocation(), SMng); 697 } else if (std::optional<PostImplicitCall> PIE = 698 P.getAs<PostImplicitCall>()) { 699 return PathDiagnosticLocation(PIE->getLocation(), SMng); 700 } else if (std::optional<CallEnter> CE = P.getAs<CallEnter>()) { 701 return getLocationForCaller(CE->getCalleeContext(), 702 CE->getLocationContext(), 703 SMng); 704 } else if (std::optional<CallExitEnd> CEE = P.getAs<CallExitEnd>()) { 705 return getLocationForCaller(CEE->getCalleeContext(), 706 CEE->getLocationContext(), 707 SMng); 708 } else if (auto CEB = P.getAs<CallExitBegin>()) { 709 if (const ReturnStmt *RS = CEB->getReturnStmt()) 710 return PathDiagnosticLocation::createBegin(RS, SMng, 711 CEB->getLocationContext()); 712 return PathDiagnosticLocation( 713 CEB->getLocationContext()->getDecl()->getSourceRange().getEnd(), SMng); 714 } else if (std::optional<BlockEntrance> BE = P.getAs<BlockEntrance>()) { 715 if (std::optional<CFGElement> BlockFront = BE->getFirstElement()) { 716 if (auto StmtElt = BlockFront->getAs<CFGStmt>()) { 717 return PathDiagnosticLocation(StmtElt->getStmt()->getBeginLoc(), SMng); 718 } else if (auto NewAllocElt = BlockFront->getAs<CFGNewAllocator>()) { 719 return PathDiagnosticLocation( 720 NewAllocElt->getAllocatorExpr()->getBeginLoc(), SMng); 721 } 722 llvm_unreachable("Unexpected CFG element at front of block"); 723 } 724 725 return PathDiagnosticLocation( 726 BE->getBlock()->getTerminatorStmt()->getBeginLoc(), SMng); 727 } else if (std::optional<FunctionExitPoint> FE = 728 P.getAs<FunctionExitPoint>()) { 729 return PathDiagnosticLocation(FE->getStmt(), SMng, 730 FE->getLocationContext()); 731 } else { 732 llvm_unreachable("Unexpected ProgramPoint"); 733 } 734 735 return PathDiagnosticLocation(S, SMng, P.getLocationContext()); 736 } 737 738 PathDiagnosticLocation PathDiagnosticLocation::createSingleLocation( 739 const PathDiagnosticLocation &PDL) { 740 FullSourceLoc L = PDL.asLocation(); 741 return PathDiagnosticLocation(L, L.getManager(), SingleLocK); 742 } 743 744 FullSourceLoc 745 PathDiagnosticLocation::genLocation(SourceLocation L, 746 LocationOrAnalysisDeclContext LAC) const { 747 assert(isValid()); 748 // Note that we want a 'switch' here so that the compiler can warn us in 749 // case we add more cases. 750 switch (K) { 751 case SingleLocK: 752 case RangeK: 753 break; 754 case StmtK: 755 // Defensive checking. 756 if (!S) 757 break; 758 return FullSourceLoc(getValidSourceLocation(S, LAC), 759 const_cast<SourceManager&>(*SM)); 760 case DeclK: 761 // Defensive checking. 762 if (!D) 763 break; 764 return FullSourceLoc(D->getLocation(), const_cast<SourceManager&>(*SM)); 765 } 766 767 return FullSourceLoc(L, const_cast<SourceManager&>(*SM)); 768 } 769 770 PathDiagnosticRange 771 PathDiagnosticLocation::genRange(LocationOrAnalysisDeclContext LAC) const { 772 assert(isValid()); 773 // Note that we want a 'switch' here so that the compiler can warn us in 774 // case we add more cases. 775 switch (K) { 776 case SingleLocK: 777 return PathDiagnosticRange(SourceRange(Loc,Loc), true); 778 case RangeK: 779 break; 780 case StmtK: { 781 const Stmt *S = asStmt(); 782 switch (S->getStmtClass()) { 783 default: 784 break; 785 case Stmt::DeclStmtClass: { 786 const auto *DS = cast<DeclStmt>(S); 787 if (DS->isSingleDecl()) { 788 // Should always be the case, but we'll be defensive. 789 return SourceRange(DS->getBeginLoc(), 790 DS->getSingleDecl()->getLocation()); 791 } 792 break; 793 } 794 // FIXME: Provide better range information for different 795 // terminators. 796 case Stmt::IfStmtClass: 797 case Stmt::WhileStmtClass: 798 case Stmt::DoStmtClass: 799 case Stmt::ForStmtClass: 800 case Stmt::ChooseExprClass: 801 case Stmt::IndirectGotoStmtClass: 802 case Stmt::SwitchStmtClass: 803 case Stmt::BinaryConditionalOperatorClass: 804 case Stmt::ConditionalOperatorClass: 805 case Stmt::ObjCForCollectionStmtClass: { 806 SourceLocation L = getValidSourceLocation(S, LAC); 807 return SourceRange(L, L); 808 } 809 } 810 SourceRange R = S->getSourceRange(); 811 if (R.isValid()) 812 return R; 813 break; 814 } 815 case DeclK: 816 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) 817 return MD->getSourceRange(); 818 if (const auto *FD = dyn_cast<FunctionDecl>(D)) { 819 if (Stmt *Body = FD->getBody()) 820 return Body->getSourceRange(); 821 } 822 else { 823 SourceLocation L = D->getLocation(); 824 return PathDiagnosticRange(SourceRange(L, L), true); 825 } 826 } 827 828 return SourceRange(Loc, Loc); 829 } 830 831 void PathDiagnosticLocation::flatten() { 832 if (K == StmtK) { 833 K = RangeK; 834 S = nullptr; 835 D = nullptr; 836 } 837 else if (K == DeclK) { 838 K = SingleLocK; 839 S = nullptr; 840 D = nullptr; 841 } 842 } 843 844 //===----------------------------------------------------------------------===// 845 // Manipulation of PathDiagnosticCallPieces. 846 //===----------------------------------------------------------------------===// 847 848 std::shared_ptr<PathDiagnosticCallPiece> 849 PathDiagnosticCallPiece::construct(const CallExitEnd &CE, 850 const SourceManager &SM) { 851 const Decl *caller = CE.getLocationContext()->getDecl(); 852 PathDiagnosticLocation pos = getLocationForCaller(CE.getCalleeContext(), 853 CE.getLocationContext(), 854 SM); 855 return std::shared_ptr<PathDiagnosticCallPiece>( 856 new PathDiagnosticCallPiece(caller, pos)); 857 } 858 859 PathDiagnosticCallPiece * 860 PathDiagnosticCallPiece::construct(PathPieces &path, 861 const Decl *caller) { 862 std::shared_ptr<PathDiagnosticCallPiece> C( 863 new PathDiagnosticCallPiece(path, caller)); 864 path.clear(); 865 auto *R = C.get(); 866 path.push_front(std::move(C)); 867 return R; 868 } 869 870 void PathDiagnosticCallPiece::setCallee(const CallEnter &CE, 871 const SourceManager &SM) { 872 const StackFrameContext *CalleeCtx = CE.getCalleeContext(); 873 Callee = CalleeCtx->getDecl(); 874 875 callEnterWithin = PathDiagnosticLocation::createBegin(Callee, SM); 876 callEnter = getLocationForCaller(CalleeCtx, CE.getLocationContext(), SM); 877 878 // Autosynthesized property accessors are special because we'd never 879 // pop back up to non-autosynthesized code until we leave them. 880 // This is not generally true for autosynthesized callees, which may call 881 // non-autosynthesized callbacks. 882 // Unless set here, the IsCalleeAnAutosynthesizedPropertyAccessor flag 883 // defaults to false. 884 if (const auto *MD = dyn_cast<ObjCMethodDecl>(Callee)) 885 IsCalleeAnAutosynthesizedPropertyAccessor = ( 886 MD->isPropertyAccessor() && 887 CalleeCtx->getAnalysisDeclContext()->isBodyAutosynthesized()); 888 } 889 890 static void describeTemplateParameters(raw_ostream &Out, 891 const ArrayRef<TemplateArgument> TAList, 892 const LangOptions &LO, 893 StringRef Prefix = StringRef(), 894 StringRef Postfix = StringRef()); 895 896 static void describeTemplateParameter(raw_ostream &Out, 897 const TemplateArgument &TArg, 898 const LangOptions &LO) { 899 900 if (TArg.getKind() == TemplateArgument::ArgKind::Pack) { 901 describeTemplateParameters(Out, TArg.getPackAsArray(), LO); 902 } else { 903 TArg.print(PrintingPolicy(LO), Out, /*IncludeType*/ true); 904 } 905 } 906 907 static void describeTemplateParameters(raw_ostream &Out, 908 const ArrayRef<TemplateArgument> TAList, 909 const LangOptions &LO, 910 StringRef Prefix, StringRef Postfix) { 911 if (TAList.empty()) 912 return; 913 914 Out << Prefix; 915 for (int I = 0, Last = TAList.size() - 1; I != Last; ++I) { 916 describeTemplateParameter(Out, TAList[I], LO); 917 Out << ", "; 918 } 919 describeTemplateParameter(Out, TAList[TAList.size() - 1], LO); 920 Out << Postfix; 921 } 922 923 static void describeClass(raw_ostream &Out, const CXXRecordDecl *D, 924 StringRef Prefix = StringRef()) { 925 if (!D->getIdentifier()) 926 return; 927 Out << Prefix << '\'' << *D; 928 if (const auto T = dyn_cast<ClassTemplateSpecializationDecl>(D)) 929 describeTemplateParameters(Out, T->getTemplateArgs().asArray(), 930 D->getLangOpts(), "<", ">"); 931 932 Out << '\''; 933 } 934 935 static bool describeCodeDecl(raw_ostream &Out, const Decl *D, 936 bool ExtendedDescription, 937 StringRef Prefix = StringRef()) { 938 if (!D) 939 return false; 940 941 if (isa<BlockDecl>(D)) { 942 if (ExtendedDescription) 943 Out << Prefix << "anonymous block"; 944 return ExtendedDescription; 945 } 946 947 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) { 948 Out << Prefix; 949 if (ExtendedDescription && !MD->isUserProvided()) { 950 if (MD->isExplicitlyDefaulted()) 951 Out << "defaulted "; 952 else 953 Out << "implicit "; 954 } 955 956 if (const auto *CD = dyn_cast<CXXConstructorDecl>(MD)) { 957 if (CD->isDefaultConstructor()) 958 Out << "default "; 959 else if (CD->isCopyConstructor()) 960 Out << "copy "; 961 else if (CD->isMoveConstructor()) 962 Out << "move "; 963 964 Out << "constructor"; 965 describeClass(Out, MD->getParent(), " for "); 966 } else if (isa<CXXDestructorDecl>(MD)) { 967 if (!MD->isUserProvided()) { 968 Out << "destructor"; 969 describeClass(Out, MD->getParent(), " for "); 970 } else { 971 // Use ~Foo for explicitly-written destructors. 972 Out << "'" << *MD << "'"; 973 } 974 } else if (MD->isCopyAssignmentOperator()) { 975 Out << "copy assignment operator"; 976 describeClass(Out, MD->getParent(), " for "); 977 } else if (MD->isMoveAssignmentOperator()) { 978 Out << "move assignment operator"; 979 describeClass(Out, MD->getParent(), " for "); 980 } else { 981 if (MD->getParent()->getIdentifier()) 982 Out << "'" << *MD->getParent() << "::" << *MD << "'"; 983 else 984 Out << "'" << *MD << "'"; 985 } 986 987 return true; 988 } 989 990 Out << Prefix << '\'' << cast<NamedDecl>(*D); 991 992 // Adding template parameters. 993 if (const auto FD = dyn_cast<FunctionDecl>(D)) 994 if (const TemplateArgumentList *TAList = 995 FD->getTemplateSpecializationArgs()) 996 describeTemplateParameters(Out, TAList->asArray(), FD->getLangOpts(), "<", 997 ">"); 998 999 Out << '\''; 1000 return true; 1001 } 1002 1003 std::shared_ptr<PathDiagnosticEventPiece> 1004 PathDiagnosticCallPiece::getCallEnterEvent() const { 1005 // We do not produce call enters and call exits for autosynthesized property 1006 // accessors. We do generally produce them for other functions coming from 1007 // the body farm because they may call callbacks that bring us back into 1008 // visible code. 1009 if (!Callee || IsCalleeAnAutosynthesizedPropertyAccessor) 1010 return nullptr; 1011 1012 SmallString<256> buf; 1013 llvm::raw_svector_ostream Out(buf); 1014 1015 Out << "Calling "; 1016 describeCodeDecl(Out, Callee, /*ExtendedDescription=*/true); 1017 1018 assert(callEnter.asLocation().isValid()); 1019 return std::make_shared<PathDiagnosticEventPiece>(callEnter, Out.str()); 1020 } 1021 1022 std::shared_ptr<PathDiagnosticEventPiece> 1023 PathDiagnosticCallPiece::getCallEnterWithinCallerEvent() const { 1024 if (!callEnterWithin.asLocation().isValid()) 1025 return nullptr; 1026 if (Callee->isImplicit() || !Callee->hasBody()) 1027 return nullptr; 1028 if (const auto *MD = dyn_cast<CXXMethodDecl>(Callee)) 1029 if (MD->isDefaulted()) 1030 return nullptr; 1031 1032 SmallString<256> buf; 1033 llvm::raw_svector_ostream Out(buf); 1034 1035 Out << "Entered call"; 1036 describeCodeDecl(Out, Caller, /*ExtendedDescription=*/false, " from "); 1037 1038 return std::make_shared<PathDiagnosticEventPiece>(callEnterWithin, Out.str()); 1039 } 1040 1041 std::shared_ptr<PathDiagnosticEventPiece> 1042 PathDiagnosticCallPiece::getCallExitEvent() const { 1043 // We do not produce call enters and call exits for autosynthesized property 1044 // accessors. We do generally produce them for other functions coming from 1045 // the body farm because they may call callbacks that bring us back into 1046 // visible code. 1047 if (NoExit || IsCalleeAnAutosynthesizedPropertyAccessor) 1048 return nullptr; 1049 1050 SmallString<256> buf; 1051 llvm::raw_svector_ostream Out(buf); 1052 1053 if (!CallStackMessage.empty()) { 1054 Out << CallStackMessage; 1055 } else { 1056 bool DidDescribe = describeCodeDecl(Out, Callee, 1057 /*ExtendedDescription=*/false, 1058 "Returning from "); 1059 if (!DidDescribe) 1060 Out << "Returning to caller"; 1061 } 1062 1063 assert(callReturn.asLocation().isValid()); 1064 return std::make_shared<PathDiagnosticEventPiece>(callReturn, Out.str()); 1065 } 1066 1067 static void compute_path_size(const PathPieces &pieces, unsigned &size) { 1068 for (const auto &I : pieces) { 1069 const PathDiagnosticPiece *piece = I.get(); 1070 if (const auto *cp = dyn_cast<PathDiagnosticCallPiece>(piece)) 1071 compute_path_size(cp->path, size); 1072 else 1073 ++size; 1074 } 1075 } 1076 1077 unsigned PathDiagnostic::full_size() { 1078 unsigned size = 0; 1079 compute_path_size(path, size); 1080 return size; 1081 } 1082 1083 //===----------------------------------------------------------------------===// 1084 // FoldingSet profiling methods. 1085 //===----------------------------------------------------------------------===// 1086 1087 void PathDiagnosticLocation::Profile(llvm::FoldingSetNodeID &ID) const { 1088 ID.Add(Range.getBegin()); 1089 ID.Add(Range.getEnd()); 1090 ID.Add(static_cast<const SourceLocation &>(Loc)); 1091 } 1092 1093 void PathDiagnosticPiece::Profile(llvm::FoldingSetNodeID &ID) const { 1094 ID.AddInteger((unsigned) getKind()); 1095 ID.AddString(str); 1096 // FIXME: Add profiling support for code hints. 1097 ID.AddInteger((unsigned) getDisplayHint()); 1098 ArrayRef<SourceRange> Ranges = getRanges(); 1099 for (const auto &I : Ranges) { 1100 ID.Add(I.getBegin()); 1101 ID.Add(I.getEnd()); 1102 } 1103 } 1104 1105 void PathDiagnosticCallPiece::Profile(llvm::FoldingSetNodeID &ID) const { 1106 PathDiagnosticPiece::Profile(ID); 1107 for (const auto &I : path) 1108 ID.Add(*I); 1109 } 1110 1111 void PathDiagnosticSpotPiece::Profile(llvm::FoldingSetNodeID &ID) const { 1112 PathDiagnosticPiece::Profile(ID); 1113 ID.Add(Pos); 1114 } 1115 1116 void PathDiagnosticControlFlowPiece::Profile(llvm::FoldingSetNodeID &ID) const { 1117 PathDiagnosticPiece::Profile(ID); 1118 for (const auto &I : *this) 1119 ID.Add(I); 1120 } 1121 1122 void PathDiagnosticMacroPiece::Profile(llvm::FoldingSetNodeID &ID) const { 1123 PathDiagnosticSpotPiece::Profile(ID); 1124 for (const auto &I : subPieces) 1125 ID.Add(*I); 1126 } 1127 1128 void PathDiagnosticNotePiece::Profile(llvm::FoldingSetNodeID &ID) const { 1129 PathDiagnosticSpotPiece::Profile(ID); 1130 } 1131 1132 void PathDiagnosticPopUpPiece::Profile(llvm::FoldingSetNodeID &ID) const { 1133 PathDiagnosticSpotPiece::Profile(ID); 1134 } 1135 1136 void PathDiagnostic::Profile(llvm::FoldingSetNodeID &ID) const { 1137 ID.Add(getLocation()); 1138 ID.Add(getUniqueingLoc()); 1139 ID.AddString(BugType); 1140 ID.AddString(VerboseDesc); 1141 ID.AddString(Category); 1142 } 1143 1144 void PathDiagnostic::FullProfile(llvm::FoldingSetNodeID &ID) const { 1145 Profile(ID); 1146 for (const auto &I : path) 1147 ID.Add(*I); 1148 for (meta_iterator I = meta_begin(), E = meta_end(); I != E; ++I) 1149 ID.AddString(*I); 1150 } 1151 1152 LLVM_DUMP_METHOD void PathPieces::dump() const { 1153 unsigned index = 0; 1154 for (PathPieces::const_iterator I = begin(), E = end(); I != E; ++I) { 1155 llvm::errs() << "[" << index++ << "] "; 1156 (*I)->dump(); 1157 llvm::errs() << "\n"; 1158 } 1159 } 1160 1161 LLVM_DUMP_METHOD void PathDiagnosticCallPiece::dump() const { 1162 llvm::errs() << "CALL\n--------------\n"; 1163 1164 if (const Stmt *SLoc = getLocation().getStmtOrNull()) 1165 SLoc->dump(); 1166 else if (const auto *ND = dyn_cast_or_null<NamedDecl>(getCallee())) 1167 llvm::errs() << *ND << "\n"; 1168 else 1169 getLocation().dump(); 1170 } 1171 1172 LLVM_DUMP_METHOD void PathDiagnosticEventPiece::dump() const { 1173 llvm::errs() << "EVENT\n--------------\n"; 1174 llvm::errs() << getString() << "\n"; 1175 llvm::errs() << " ---- at ----\n"; 1176 getLocation().dump(); 1177 } 1178 1179 LLVM_DUMP_METHOD void PathDiagnosticControlFlowPiece::dump() const { 1180 llvm::errs() << "CONTROL\n--------------\n"; 1181 getStartLocation().dump(); 1182 llvm::errs() << " ---- to ----\n"; 1183 getEndLocation().dump(); 1184 } 1185 1186 LLVM_DUMP_METHOD void PathDiagnosticMacroPiece::dump() const { 1187 llvm::errs() << "MACRO\n--------------\n"; 1188 // FIXME: Print which macro is being invoked. 1189 } 1190 1191 LLVM_DUMP_METHOD void PathDiagnosticNotePiece::dump() const { 1192 llvm::errs() << "NOTE\n--------------\n"; 1193 llvm::errs() << getString() << "\n"; 1194 llvm::errs() << " ---- at ----\n"; 1195 getLocation().dump(); 1196 } 1197 1198 LLVM_DUMP_METHOD void PathDiagnosticPopUpPiece::dump() const { 1199 llvm::errs() << "POP-UP\n--------------\n"; 1200 llvm::errs() << getString() << "\n"; 1201 llvm::errs() << " ---- at ----\n"; 1202 getLocation().dump(); 1203 } 1204 1205 LLVM_DUMP_METHOD void PathDiagnosticLocation::dump() const { 1206 if (!isValid()) { 1207 llvm::errs() << "<INVALID>\n"; 1208 return; 1209 } 1210 1211 switch (K) { 1212 case RangeK: 1213 // FIXME: actually print the range. 1214 llvm::errs() << "<range>\n"; 1215 break; 1216 case SingleLocK: 1217 asLocation().dump(); 1218 llvm::errs() << "\n"; 1219 break; 1220 case StmtK: 1221 if (S) 1222 S->dump(); 1223 else 1224 llvm::errs() << "<NULL STMT>\n"; 1225 break; 1226 case DeclK: 1227 if (const auto *ND = dyn_cast_or_null<NamedDecl>(D)) 1228 llvm::errs() << *ND << "\n"; 1229 else if (isa<BlockDecl>(D)) 1230 // FIXME: Make this nicer. 1231 llvm::errs() << "<block>\n"; 1232 else if (D) 1233 llvm::errs() << "<unknown decl>\n"; 1234 else 1235 llvm::errs() << "<NULL DECL>\n"; 1236 break; 1237 } 1238 } 1239