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 const FileEntry *XFE = SM.getFileEntryForID(XL.getSpellingLoc().getFileID()); 340 const FileEntry *YFE = SM.getFileEntryForID(YL.getSpellingLoc().getFileID()); 341 if (!XFE || !YFE) 342 return XFE && !YFE; 343 int NameCmp = XFE->getName().compare(YFE->getName()); 344 if (NameCmp != 0) 345 return NameCmp < 0; 346 // Last resort: Compare raw file IDs that are possibly expansions. 347 return XL.getFileID() < YL.getFileID(); 348 } 349 350 static bool compare(const PathDiagnostic &X, const PathDiagnostic &Y) { 351 FullSourceLoc XL = X.getLocation().asLocation(); 352 FullSourceLoc YL = Y.getLocation().asLocation(); 353 if (XL != YL) 354 return compareCrossTUSourceLocs(XL, YL); 355 FullSourceLoc XUL = X.getUniqueingLoc().asLocation(); 356 FullSourceLoc YUL = Y.getUniqueingLoc().asLocation(); 357 if (XUL != YUL) 358 return compareCrossTUSourceLocs(XUL, YUL); 359 if (X.getBugType() != Y.getBugType()) 360 return X.getBugType() < Y.getBugType(); 361 if (X.getCategory() != Y.getCategory()) 362 return X.getCategory() < Y.getCategory(); 363 if (X.getVerboseDescription() != Y.getVerboseDescription()) 364 return X.getVerboseDescription() < Y.getVerboseDescription(); 365 if (X.getShortDescription() != Y.getShortDescription()) 366 return X.getShortDescription() < Y.getShortDescription(); 367 auto CompareDecls = [&XL](const Decl *D1, 368 const Decl *D2) -> std::optional<bool> { 369 if (D1 == D2) 370 return std::nullopt; 371 if (!D1) 372 return true; 373 if (!D2) 374 return false; 375 SourceLocation D1L = D1->getLocation(); 376 SourceLocation D2L = D2->getLocation(); 377 if (D1L != D2L) { 378 const SourceManager &SM = XL.getManager(); 379 return compareCrossTUSourceLocs(FullSourceLoc(D1L, SM), 380 FullSourceLoc(D2L, SM)); 381 } 382 return std::nullopt; 383 }; 384 if (auto Result = CompareDecls(X.getDeclWithIssue(), Y.getDeclWithIssue())) 385 return *Result; 386 if (XUL.isValid()) { 387 if (auto Result = CompareDecls(X.getUniqueingDecl(), Y.getUniqueingDecl())) 388 return *Result; 389 } 390 PathDiagnostic::meta_iterator XI = X.meta_begin(), XE = X.meta_end(); 391 PathDiagnostic::meta_iterator YI = Y.meta_begin(), YE = Y.meta_end(); 392 if (XE - XI != YE - YI) 393 return (XE - XI) < (YE - YI); 394 for ( ; XI != XE ; ++XI, ++YI) { 395 if (*XI != *YI) 396 return (*XI) < (*YI); 397 } 398 return *comparePath(X.path, Y.path); 399 } 400 401 void PathDiagnosticConsumer::FlushDiagnostics( 402 PathDiagnosticConsumer::FilesMade *Files) { 403 if (flushed) 404 return; 405 406 flushed = true; 407 408 std::vector<const PathDiagnostic *> BatchDiags; 409 for (const auto &D : Diags) 410 BatchDiags.push_back(&D); 411 412 // Sort the diagnostics so that they are always emitted in a deterministic 413 // order. 414 int (*Comp)(const PathDiagnostic *const *, const PathDiagnostic *const *) = 415 [](const PathDiagnostic *const *X, const PathDiagnostic *const *Y) { 416 assert(*X != *Y && "PathDiagnostics not uniqued!"); 417 if (compare(**X, **Y)) 418 return -1; 419 assert(compare(**Y, **X) && "Not a total order!"); 420 return 1; 421 }; 422 array_pod_sort(BatchDiags.begin(), BatchDiags.end(), Comp); 423 424 FlushDiagnosticsImpl(BatchDiags, Files); 425 426 // Delete the flushed diagnostics. 427 for (const auto D : BatchDiags) 428 delete D; 429 430 // Clear out the FoldingSet. 431 Diags.clear(); 432 } 433 434 PathDiagnosticConsumer::FilesMade::~FilesMade() { 435 for (auto It = Set.begin(); It != Set.end();) 436 (It++)->~PDFileEntry(); 437 } 438 439 void PathDiagnosticConsumer::FilesMade::addDiagnostic(const PathDiagnostic &PD, 440 StringRef ConsumerName, 441 StringRef FileName) { 442 llvm::FoldingSetNodeID NodeID; 443 NodeID.Add(PD); 444 void *InsertPos; 445 PDFileEntry *Entry = Set.FindNodeOrInsertPos(NodeID, InsertPos); 446 if (!Entry) { 447 Entry = Alloc.Allocate<PDFileEntry>(); 448 Entry = new (Entry) PDFileEntry(NodeID); 449 Set.InsertNode(Entry, InsertPos); 450 } 451 452 // Allocate persistent storage for the file name. 453 char *FileName_cstr = (char*) Alloc.Allocate(FileName.size(), 1); 454 memcpy(FileName_cstr, FileName.data(), FileName.size()); 455 456 Entry->files.push_back(std::make_pair(ConsumerName, 457 StringRef(FileName_cstr, 458 FileName.size()))); 459 } 460 461 PathDiagnosticConsumer::PDFileEntry::ConsumerFiles * 462 PathDiagnosticConsumer::FilesMade::getFiles(const PathDiagnostic &PD) { 463 llvm::FoldingSetNodeID NodeID; 464 NodeID.Add(PD); 465 void *InsertPos; 466 PDFileEntry *Entry = Set.FindNodeOrInsertPos(NodeID, InsertPos); 467 if (!Entry) 468 return nullptr; 469 return &Entry->files; 470 } 471 472 //===----------------------------------------------------------------------===// 473 // PathDiagnosticLocation methods. 474 //===----------------------------------------------------------------------===// 475 476 SourceLocation PathDiagnosticLocation::getValidSourceLocation( 477 const Stmt *S, LocationOrAnalysisDeclContext LAC, bool UseEndOfStatement) { 478 SourceLocation L = UseEndOfStatement ? S->getEndLoc() : S->getBeginLoc(); 479 assert(!LAC.isNull() && 480 "A valid LocationContext or AnalysisDeclContext should be passed to " 481 "PathDiagnosticLocation upon creation."); 482 483 // S might be a temporary statement that does not have a location in the 484 // source code, so find an enclosing statement and use its location. 485 if (!L.isValid()) { 486 AnalysisDeclContext *ADC; 487 if (LAC.is<const LocationContext*>()) 488 ADC = LAC.get<const LocationContext*>()->getAnalysisDeclContext(); 489 else 490 ADC = LAC.get<AnalysisDeclContext*>(); 491 492 ParentMap &PM = ADC->getParentMap(); 493 494 const Stmt *Parent = S; 495 do { 496 Parent = PM.getParent(Parent); 497 498 // In rare cases, we have implicit top-level expressions, 499 // such as arguments for implicit member initializers. 500 // In this case, fall back to the start of the body (even if we were 501 // asked for the statement end location). 502 if (!Parent) { 503 const Stmt *Body = ADC->getBody(); 504 if (Body) 505 L = Body->getBeginLoc(); 506 else 507 L = ADC->getDecl()->getEndLoc(); 508 break; 509 } 510 511 L = UseEndOfStatement ? Parent->getEndLoc() : Parent->getBeginLoc(); 512 } while (!L.isValid()); 513 } 514 515 // FIXME: Ironically, this assert actually fails in some cases. 516 //assert(L.isValid()); 517 return L; 518 } 519 520 static PathDiagnosticLocation 521 getLocationForCaller(const StackFrameContext *SFC, 522 const LocationContext *CallerCtx, 523 const SourceManager &SM) { 524 const CFGBlock &Block = *SFC->getCallSiteBlock(); 525 CFGElement Source = Block[SFC->getIndex()]; 526 527 switch (Source.getKind()) { 528 case CFGElement::Statement: 529 case CFGElement::Constructor: 530 case CFGElement::CXXRecordTypedCall: 531 return PathDiagnosticLocation(Source.castAs<CFGStmt>().getStmt(), 532 SM, CallerCtx); 533 case CFGElement::Initializer: { 534 const CFGInitializer &Init = Source.castAs<CFGInitializer>(); 535 return PathDiagnosticLocation(Init.getInitializer()->getInit(), 536 SM, CallerCtx); 537 } 538 case CFGElement::AutomaticObjectDtor: { 539 const CFGAutomaticObjDtor &Dtor = Source.castAs<CFGAutomaticObjDtor>(); 540 return PathDiagnosticLocation::createEnd(Dtor.getTriggerStmt(), 541 SM, CallerCtx); 542 } 543 case CFGElement::DeleteDtor: { 544 const CFGDeleteDtor &Dtor = Source.castAs<CFGDeleteDtor>(); 545 return PathDiagnosticLocation(Dtor.getDeleteExpr(), SM, CallerCtx); 546 } 547 case CFGElement::BaseDtor: 548 case CFGElement::MemberDtor: { 549 const AnalysisDeclContext *CallerInfo = CallerCtx->getAnalysisDeclContext(); 550 if (const Stmt *CallerBody = CallerInfo->getBody()) 551 return PathDiagnosticLocation::createEnd(CallerBody, SM, CallerCtx); 552 return PathDiagnosticLocation::create(CallerInfo->getDecl(), SM); 553 } 554 case CFGElement::NewAllocator: { 555 const CFGNewAllocator &Alloc = Source.castAs<CFGNewAllocator>(); 556 return PathDiagnosticLocation(Alloc.getAllocatorExpr(), SM, CallerCtx); 557 } 558 case CFGElement::TemporaryDtor: { 559 // Temporary destructors are for temporaries. They die immediately at around 560 // the location of CXXBindTemporaryExpr. If they are lifetime-extended, 561 // they'd be dealt with via an AutomaticObjectDtor instead. 562 const auto &Dtor = Source.castAs<CFGTemporaryDtor>(); 563 return PathDiagnosticLocation::createEnd(Dtor.getBindTemporaryExpr(), SM, 564 CallerCtx); 565 } 566 case CFGElement::ScopeBegin: 567 case CFGElement::ScopeEnd: 568 llvm_unreachable("not yet implemented!"); 569 case CFGElement::LifetimeEnds: 570 case CFGElement::LoopExit: 571 llvm_unreachable("CFGElement kind should not be on callsite!"); 572 } 573 574 llvm_unreachable("Unknown CFGElement kind"); 575 } 576 577 PathDiagnosticLocation 578 PathDiagnosticLocation::createBegin(const Decl *D, 579 const SourceManager &SM) { 580 return PathDiagnosticLocation(D->getBeginLoc(), SM, SingleLocK); 581 } 582 583 PathDiagnosticLocation 584 PathDiagnosticLocation::createBegin(const Stmt *S, 585 const SourceManager &SM, 586 LocationOrAnalysisDeclContext LAC) { 587 assert(S && "Statement cannot be null"); 588 return PathDiagnosticLocation(getValidSourceLocation(S, LAC), 589 SM, SingleLocK); 590 } 591 592 PathDiagnosticLocation 593 PathDiagnosticLocation::createEnd(const Stmt *S, 594 const SourceManager &SM, 595 LocationOrAnalysisDeclContext LAC) { 596 if (const auto *CS = dyn_cast<CompoundStmt>(S)) 597 return createEndBrace(CS, SM); 598 return PathDiagnosticLocation(getValidSourceLocation(S, LAC, /*End=*/true), 599 SM, SingleLocK); 600 } 601 602 PathDiagnosticLocation 603 PathDiagnosticLocation::createOperatorLoc(const BinaryOperator *BO, 604 const SourceManager &SM) { 605 return PathDiagnosticLocation(BO->getOperatorLoc(), SM, SingleLocK); 606 } 607 608 PathDiagnosticLocation 609 PathDiagnosticLocation::createConditionalColonLoc( 610 const ConditionalOperator *CO, 611 const SourceManager &SM) { 612 return PathDiagnosticLocation(CO->getColonLoc(), SM, SingleLocK); 613 } 614 615 PathDiagnosticLocation 616 PathDiagnosticLocation::createMemberLoc(const MemberExpr *ME, 617 const SourceManager &SM) { 618 619 assert(ME->getMemberLoc().isValid() || ME->getBeginLoc().isValid()); 620 621 // In some cases, getMemberLoc isn't valid -- in this case we'll return with 622 // some other related valid SourceLocation. 623 if (ME->getMemberLoc().isValid()) 624 return PathDiagnosticLocation(ME->getMemberLoc(), SM, SingleLocK); 625 626 return PathDiagnosticLocation(ME->getBeginLoc(), SM, SingleLocK); 627 } 628 629 PathDiagnosticLocation 630 PathDiagnosticLocation::createBeginBrace(const CompoundStmt *CS, 631 const SourceManager &SM) { 632 SourceLocation L = CS->getLBracLoc(); 633 return PathDiagnosticLocation(L, SM, SingleLocK); 634 } 635 636 PathDiagnosticLocation 637 PathDiagnosticLocation::createEndBrace(const CompoundStmt *CS, 638 const SourceManager &SM) { 639 SourceLocation L = CS->getRBracLoc(); 640 return PathDiagnosticLocation(L, SM, SingleLocK); 641 } 642 643 PathDiagnosticLocation 644 PathDiagnosticLocation::createDeclBegin(const LocationContext *LC, 645 const SourceManager &SM) { 646 // FIXME: Should handle CXXTryStmt if analyser starts supporting C++. 647 if (const auto *CS = dyn_cast_or_null<CompoundStmt>(LC->getDecl()->getBody())) 648 if (!CS->body_empty()) { 649 SourceLocation Loc = (*CS->body_begin())->getBeginLoc(); 650 return PathDiagnosticLocation(Loc, SM, SingleLocK); 651 } 652 653 return PathDiagnosticLocation(); 654 } 655 656 PathDiagnosticLocation 657 PathDiagnosticLocation::createDeclEnd(const LocationContext *LC, 658 const SourceManager &SM) { 659 SourceLocation L = LC->getDecl()->getBodyRBrace(); 660 return PathDiagnosticLocation(L, SM, SingleLocK); 661 } 662 663 PathDiagnosticLocation 664 PathDiagnosticLocation::create(const ProgramPoint& P, 665 const SourceManager &SMng) { 666 const Stmt* S = nullptr; 667 if (std::optional<BlockEdge> BE = P.getAs<BlockEdge>()) { 668 const CFGBlock *BSrc = BE->getSrc(); 669 if (BSrc->getTerminator().isVirtualBaseBranch()) { 670 // TODO: VirtualBaseBranches should also appear for destructors. 671 // In this case we should put the diagnostic at the end of decl. 672 return PathDiagnosticLocation::createBegin( 673 P.getLocationContext()->getDecl(), SMng); 674 675 } else { 676 S = BSrc->getTerminatorCondition(); 677 if (!S) { 678 // If the BlockEdge has no terminator condition statement but its 679 // source is the entry of the CFG (e.g. a checker crated the branch at 680 // the beginning of a function), use the function's declaration instead. 681 assert(BSrc == &BSrc->getParent()->getEntry() && "CFGBlock has no " 682 "TerminatorCondition and is not the enrty block of the CFG"); 683 return PathDiagnosticLocation::createBegin( 684 P.getLocationContext()->getDecl(), SMng); 685 } 686 } 687 } else if (std::optional<StmtPoint> SP = P.getAs<StmtPoint>()) { 688 S = SP->getStmt(); 689 if (P.getAs<PostStmtPurgeDeadSymbols>()) 690 return PathDiagnosticLocation::createEnd(S, SMng, P.getLocationContext()); 691 } else if (std::optional<PostInitializer> PIP = P.getAs<PostInitializer>()) { 692 return PathDiagnosticLocation(PIP->getInitializer()->getSourceLocation(), 693 SMng); 694 } else if (std::optional<PreImplicitCall> PIC = P.getAs<PreImplicitCall>()) { 695 return PathDiagnosticLocation(PIC->getLocation(), SMng); 696 } else if (std::optional<PostImplicitCall> PIE = 697 P.getAs<PostImplicitCall>()) { 698 return PathDiagnosticLocation(PIE->getLocation(), SMng); 699 } else if (std::optional<CallEnter> CE = P.getAs<CallEnter>()) { 700 return getLocationForCaller(CE->getCalleeContext(), 701 CE->getLocationContext(), 702 SMng); 703 } else if (std::optional<CallExitEnd> CEE = P.getAs<CallExitEnd>()) { 704 return getLocationForCaller(CEE->getCalleeContext(), 705 CEE->getLocationContext(), 706 SMng); 707 } else if (auto CEB = P.getAs<CallExitBegin>()) { 708 if (const ReturnStmt *RS = CEB->getReturnStmt()) 709 return PathDiagnosticLocation::createBegin(RS, SMng, 710 CEB->getLocationContext()); 711 return PathDiagnosticLocation( 712 CEB->getLocationContext()->getDecl()->getSourceRange().getEnd(), SMng); 713 } else if (std::optional<BlockEntrance> BE = P.getAs<BlockEntrance>()) { 714 if (std::optional<CFGElement> BlockFront = BE->getFirstElement()) { 715 if (auto StmtElt = BlockFront->getAs<CFGStmt>()) { 716 return PathDiagnosticLocation(StmtElt->getStmt()->getBeginLoc(), SMng); 717 } else if (auto NewAllocElt = BlockFront->getAs<CFGNewAllocator>()) { 718 return PathDiagnosticLocation( 719 NewAllocElt->getAllocatorExpr()->getBeginLoc(), SMng); 720 } 721 llvm_unreachable("Unexpected CFG element at front of block"); 722 } 723 724 return PathDiagnosticLocation( 725 BE->getBlock()->getTerminatorStmt()->getBeginLoc(), SMng); 726 } else if (std::optional<FunctionExitPoint> FE = 727 P.getAs<FunctionExitPoint>()) { 728 return PathDiagnosticLocation(FE->getStmt(), SMng, 729 FE->getLocationContext()); 730 } else { 731 llvm_unreachable("Unexpected ProgramPoint"); 732 } 733 734 return PathDiagnosticLocation(S, SMng, P.getLocationContext()); 735 } 736 737 PathDiagnosticLocation PathDiagnosticLocation::createSingleLocation( 738 const PathDiagnosticLocation &PDL) { 739 FullSourceLoc L = PDL.asLocation(); 740 return PathDiagnosticLocation(L, L.getManager(), SingleLocK); 741 } 742 743 FullSourceLoc 744 PathDiagnosticLocation::genLocation(SourceLocation L, 745 LocationOrAnalysisDeclContext LAC) const { 746 assert(isValid()); 747 // Note that we want a 'switch' here so that the compiler can warn us in 748 // case we add more cases. 749 switch (K) { 750 case SingleLocK: 751 case RangeK: 752 break; 753 case StmtK: 754 // Defensive checking. 755 if (!S) 756 break; 757 return FullSourceLoc(getValidSourceLocation(S, LAC), 758 const_cast<SourceManager&>(*SM)); 759 case DeclK: 760 // Defensive checking. 761 if (!D) 762 break; 763 return FullSourceLoc(D->getLocation(), const_cast<SourceManager&>(*SM)); 764 } 765 766 return FullSourceLoc(L, const_cast<SourceManager&>(*SM)); 767 } 768 769 PathDiagnosticRange 770 PathDiagnosticLocation::genRange(LocationOrAnalysisDeclContext LAC) const { 771 assert(isValid()); 772 // Note that we want a 'switch' here so that the compiler can warn us in 773 // case we add more cases. 774 switch (K) { 775 case SingleLocK: 776 return PathDiagnosticRange(SourceRange(Loc,Loc), true); 777 case RangeK: 778 break; 779 case StmtK: { 780 const Stmt *S = asStmt(); 781 switch (S->getStmtClass()) { 782 default: 783 break; 784 case Stmt::DeclStmtClass: { 785 const auto *DS = cast<DeclStmt>(S); 786 if (DS->isSingleDecl()) { 787 // Should always be the case, but we'll be defensive. 788 return SourceRange(DS->getBeginLoc(), 789 DS->getSingleDecl()->getLocation()); 790 } 791 break; 792 } 793 // FIXME: Provide better range information for different 794 // terminators. 795 case Stmt::IfStmtClass: 796 case Stmt::WhileStmtClass: 797 case Stmt::DoStmtClass: 798 case Stmt::ForStmtClass: 799 case Stmt::ChooseExprClass: 800 case Stmt::IndirectGotoStmtClass: 801 case Stmt::SwitchStmtClass: 802 case Stmt::BinaryConditionalOperatorClass: 803 case Stmt::ConditionalOperatorClass: 804 case Stmt::ObjCForCollectionStmtClass: { 805 SourceLocation L = getValidSourceLocation(S, LAC); 806 return SourceRange(L, L); 807 } 808 } 809 SourceRange R = S->getSourceRange(); 810 if (R.isValid()) 811 return R; 812 break; 813 } 814 case DeclK: 815 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) 816 return MD->getSourceRange(); 817 if (const auto *FD = dyn_cast<FunctionDecl>(D)) { 818 if (Stmt *Body = FD->getBody()) 819 return Body->getSourceRange(); 820 } 821 else { 822 SourceLocation L = D->getLocation(); 823 return PathDiagnosticRange(SourceRange(L, L), true); 824 } 825 } 826 827 return SourceRange(Loc, Loc); 828 } 829 830 void PathDiagnosticLocation::flatten() { 831 if (K == StmtK) { 832 K = RangeK; 833 S = nullptr; 834 D = nullptr; 835 } 836 else if (K == DeclK) { 837 K = SingleLocK; 838 S = nullptr; 839 D = nullptr; 840 } 841 } 842 843 //===----------------------------------------------------------------------===// 844 // Manipulation of PathDiagnosticCallPieces. 845 //===----------------------------------------------------------------------===// 846 847 std::shared_ptr<PathDiagnosticCallPiece> 848 PathDiagnosticCallPiece::construct(const CallExitEnd &CE, 849 const SourceManager &SM) { 850 const Decl *caller = CE.getLocationContext()->getDecl(); 851 PathDiagnosticLocation pos = getLocationForCaller(CE.getCalleeContext(), 852 CE.getLocationContext(), 853 SM); 854 return std::shared_ptr<PathDiagnosticCallPiece>( 855 new PathDiagnosticCallPiece(caller, pos)); 856 } 857 858 PathDiagnosticCallPiece * 859 PathDiagnosticCallPiece::construct(PathPieces &path, 860 const Decl *caller) { 861 std::shared_ptr<PathDiagnosticCallPiece> C( 862 new PathDiagnosticCallPiece(path, caller)); 863 path.clear(); 864 auto *R = C.get(); 865 path.push_front(std::move(C)); 866 return R; 867 } 868 869 void PathDiagnosticCallPiece::setCallee(const CallEnter &CE, 870 const SourceManager &SM) { 871 const StackFrameContext *CalleeCtx = CE.getCalleeContext(); 872 Callee = CalleeCtx->getDecl(); 873 874 callEnterWithin = PathDiagnosticLocation::createBegin(Callee, SM); 875 callEnter = getLocationForCaller(CalleeCtx, CE.getLocationContext(), SM); 876 877 // Autosynthesized property accessors are special because we'd never 878 // pop back up to non-autosynthesized code until we leave them. 879 // This is not generally true for autosynthesized callees, which may call 880 // non-autosynthesized callbacks. 881 // Unless set here, the IsCalleeAnAutosynthesizedPropertyAccessor flag 882 // defaults to false. 883 if (const auto *MD = dyn_cast<ObjCMethodDecl>(Callee)) 884 IsCalleeAnAutosynthesizedPropertyAccessor = ( 885 MD->isPropertyAccessor() && 886 CalleeCtx->getAnalysisDeclContext()->isBodyAutosynthesized()); 887 } 888 889 static void describeTemplateParameters(raw_ostream &Out, 890 const ArrayRef<TemplateArgument> TAList, 891 const LangOptions &LO, 892 StringRef Prefix = StringRef(), 893 StringRef Postfix = StringRef()); 894 895 static void describeTemplateParameter(raw_ostream &Out, 896 const TemplateArgument &TArg, 897 const LangOptions &LO) { 898 899 if (TArg.getKind() == TemplateArgument::ArgKind::Pack) { 900 describeTemplateParameters(Out, TArg.getPackAsArray(), LO); 901 } else { 902 TArg.print(PrintingPolicy(LO), Out, /*IncludeType*/ true); 903 } 904 } 905 906 static void describeTemplateParameters(raw_ostream &Out, 907 const ArrayRef<TemplateArgument> TAList, 908 const LangOptions &LO, 909 StringRef Prefix, StringRef Postfix) { 910 if (TAList.empty()) 911 return; 912 913 Out << Prefix; 914 for (int I = 0, Last = TAList.size() - 1; I != Last; ++I) { 915 describeTemplateParameter(Out, TAList[I], LO); 916 Out << ", "; 917 } 918 describeTemplateParameter(Out, TAList[TAList.size() - 1], LO); 919 Out << Postfix; 920 } 921 922 static void describeClass(raw_ostream &Out, const CXXRecordDecl *D, 923 StringRef Prefix = StringRef()) { 924 if (!D->getIdentifier()) 925 return; 926 Out << Prefix << '\'' << *D; 927 if (const auto T = dyn_cast<ClassTemplateSpecializationDecl>(D)) 928 describeTemplateParameters(Out, T->getTemplateArgs().asArray(), 929 D->getLangOpts(), "<", ">"); 930 931 Out << '\''; 932 } 933 934 static bool describeCodeDecl(raw_ostream &Out, const Decl *D, 935 bool ExtendedDescription, 936 StringRef Prefix = StringRef()) { 937 if (!D) 938 return false; 939 940 if (isa<BlockDecl>(D)) { 941 if (ExtendedDescription) 942 Out << Prefix << "anonymous block"; 943 return ExtendedDescription; 944 } 945 946 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) { 947 Out << Prefix; 948 if (ExtendedDescription && !MD->isUserProvided()) { 949 if (MD->isExplicitlyDefaulted()) 950 Out << "defaulted "; 951 else 952 Out << "implicit "; 953 } 954 955 if (const auto *CD = dyn_cast<CXXConstructorDecl>(MD)) { 956 if (CD->isDefaultConstructor()) 957 Out << "default "; 958 else if (CD->isCopyConstructor()) 959 Out << "copy "; 960 else if (CD->isMoveConstructor()) 961 Out << "move "; 962 963 Out << "constructor"; 964 describeClass(Out, MD->getParent(), " for "); 965 } else if (isa<CXXDestructorDecl>(MD)) { 966 if (!MD->isUserProvided()) { 967 Out << "destructor"; 968 describeClass(Out, MD->getParent(), " for "); 969 } else { 970 // Use ~Foo for explicitly-written destructors. 971 Out << "'" << *MD << "'"; 972 } 973 } else if (MD->isCopyAssignmentOperator()) { 974 Out << "copy assignment operator"; 975 describeClass(Out, MD->getParent(), " for "); 976 } else if (MD->isMoveAssignmentOperator()) { 977 Out << "move assignment operator"; 978 describeClass(Out, MD->getParent(), " for "); 979 } else { 980 if (MD->getParent()->getIdentifier()) 981 Out << "'" << *MD->getParent() << "::" << *MD << "'"; 982 else 983 Out << "'" << *MD << "'"; 984 } 985 986 return true; 987 } 988 989 Out << Prefix << '\'' << cast<NamedDecl>(*D); 990 991 // Adding template parameters. 992 if (const auto FD = dyn_cast<FunctionDecl>(D)) 993 if (const TemplateArgumentList *TAList = 994 FD->getTemplateSpecializationArgs()) 995 describeTemplateParameters(Out, TAList->asArray(), FD->getLangOpts(), "<", 996 ">"); 997 998 Out << '\''; 999 return true; 1000 } 1001 1002 std::shared_ptr<PathDiagnosticEventPiece> 1003 PathDiagnosticCallPiece::getCallEnterEvent() const { 1004 // We do not produce call enters and call exits for autosynthesized property 1005 // accessors. We do generally produce them for other functions coming from 1006 // the body farm because they may call callbacks that bring us back into 1007 // visible code. 1008 if (!Callee || IsCalleeAnAutosynthesizedPropertyAccessor) 1009 return nullptr; 1010 1011 SmallString<256> buf; 1012 llvm::raw_svector_ostream Out(buf); 1013 1014 Out << "Calling "; 1015 describeCodeDecl(Out, Callee, /*ExtendedDescription=*/true); 1016 1017 assert(callEnter.asLocation().isValid()); 1018 return std::make_shared<PathDiagnosticEventPiece>(callEnter, Out.str()); 1019 } 1020 1021 std::shared_ptr<PathDiagnosticEventPiece> 1022 PathDiagnosticCallPiece::getCallEnterWithinCallerEvent() const { 1023 if (!callEnterWithin.asLocation().isValid()) 1024 return nullptr; 1025 if (Callee->isImplicit() || !Callee->hasBody()) 1026 return nullptr; 1027 if (const auto *MD = dyn_cast<CXXMethodDecl>(Callee)) 1028 if (MD->isDefaulted()) 1029 return nullptr; 1030 1031 SmallString<256> buf; 1032 llvm::raw_svector_ostream Out(buf); 1033 1034 Out << "Entered call"; 1035 describeCodeDecl(Out, Caller, /*ExtendedDescription=*/false, " from "); 1036 1037 return std::make_shared<PathDiagnosticEventPiece>(callEnterWithin, Out.str()); 1038 } 1039 1040 std::shared_ptr<PathDiagnosticEventPiece> 1041 PathDiagnosticCallPiece::getCallExitEvent() const { 1042 // We do not produce call enters and call exits for autosynthesized property 1043 // accessors. We do generally produce them for other functions coming from 1044 // the body farm because they may call callbacks that bring us back into 1045 // visible code. 1046 if (NoExit || IsCalleeAnAutosynthesizedPropertyAccessor) 1047 return nullptr; 1048 1049 SmallString<256> buf; 1050 llvm::raw_svector_ostream Out(buf); 1051 1052 if (!CallStackMessage.empty()) { 1053 Out << CallStackMessage; 1054 } else { 1055 bool DidDescribe = describeCodeDecl(Out, Callee, 1056 /*ExtendedDescription=*/false, 1057 "Returning from "); 1058 if (!DidDescribe) 1059 Out << "Returning to caller"; 1060 } 1061 1062 assert(callReturn.asLocation().isValid()); 1063 return std::make_shared<PathDiagnosticEventPiece>(callReturn, Out.str()); 1064 } 1065 1066 static void compute_path_size(const PathPieces &pieces, unsigned &size) { 1067 for (const auto &I : pieces) { 1068 const PathDiagnosticPiece *piece = I.get(); 1069 if (const auto *cp = dyn_cast<PathDiagnosticCallPiece>(piece)) 1070 compute_path_size(cp->path, size); 1071 else 1072 ++size; 1073 } 1074 } 1075 1076 unsigned PathDiagnostic::full_size() { 1077 unsigned size = 0; 1078 compute_path_size(path, size); 1079 return size; 1080 } 1081 1082 //===----------------------------------------------------------------------===// 1083 // FoldingSet profiling methods. 1084 //===----------------------------------------------------------------------===// 1085 1086 void PathDiagnosticLocation::Profile(llvm::FoldingSetNodeID &ID) const { 1087 ID.Add(Range.getBegin()); 1088 ID.Add(Range.getEnd()); 1089 ID.Add(static_cast<const SourceLocation &>(Loc)); 1090 } 1091 1092 void PathDiagnosticPiece::Profile(llvm::FoldingSetNodeID &ID) const { 1093 ID.AddInteger((unsigned) getKind()); 1094 ID.AddString(str); 1095 // FIXME: Add profiling support for code hints. 1096 ID.AddInteger((unsigned) getDisplayHint()); 1097 ArrayRef<SourceRange> Ranges = getRanges(); 1098 for (const auto &I : Ranges) { 1099 ID.Add(I.getBegin()); 1100 ID.Add(I.getEnd()); 1101 } 1102 } 1103 1104 void PathDiagnosticCallPiece::Profile(llvm::FoldingSetNodeID &ID) const { 1105 PathDiagnosticPiece::Profile(ID); 1106 for (const auto &I : path) 1107 ID.Add(*I); 1108 } 1109 1110 void PathDiagnosticSpotPiece::Profile(llvm::FoldingSetNodeID &ID) const { 1111 PathDiagnosticPiece::Profile(ID); 1112 ID.Add(Pos); 1113 } 1114 1115 void PathDiagnosticControlFlowPiece::Profile(llvm::FoldingSetNodeID &ID) const { 1116 PathDiagnosticPiece::Profile(ID); 1117 for (const auto &I : *this) 1118 ID.Add(I); 1119 } 1120 1121 void PathDiagnosticMacroPiece::Profile(llvm::FoldingSetNodeID &ID) const { 1122 PathDiagnosticSpotPiece::Profile(ID); 1123 for (const auto &I : subPieces) 1124 ID.Add(*I); 1125 } 1126 1127 void PathDiagnosticNotePiece::Profile(llvm::FoldingSetNodeID &ID) const { 1128 PathDiagnosticSpotPiece::Profile(ID); 1129 } 1130 1131 void PathDiagnosticPopUpPiece::Profile(llvm::FoldingSetNodeID &ID) const { 1132 PathDiagnosticSpotPiece::Profile(ID); 1133 } 1134 1135 void PathDiagnostic::Profile(llvm::FoldingSetNodeID &ID) const { 1136 ID.Add(getLocation()); 1137 ID.Add(getUniqueingLoc()); 1138 ID.AddString(BugType); 1139 ID.AddString(VerboseDesc); 1140 ID.AddString(Category); 1141 } 1142 1143 void PathDiagnostic::FullProfile(llvm::FoldingSetNodeID &ID) const { 1144 Profile(ID); 1145 for (const auto &I : path) 1146 ID.Add(*I); 1147 for (meta_iterator I = meta_begin(), E = meta_end(); I != E; ++I) 1148 ID.AddString(*I); 1149 } 1150 1151 LLVM_DUMP_METHOD void PathPieces::dump() const { 1152 unsigned index = 0; 1153 for (PathPieces::const_iterator I = begin(), E = end(); I != E; ++I) { 1154 llvm::errs() << "[" << index++ << "] "; 1155 (*I)->dump(); 1156 llvm::errs() << "\n"; 1157 } 1158 } 1159 1160 LLVM_DUMP_METHOD void PathDiagnosticCallPiece::dump() const { 1161 llvm::errs() << "CALL\n--------------\n"; 1162 1163 if (const Stmt *SLoc = getLocation().getStmtOrNull()) 1164 SLoc->dump(); 1165 else if (const auto *ND = dyn_cast_or_null<NamedDecl>(getCallee())) 1166 llvm::errs() << *ND << "\n"; 1167 else 1168 getLocation().dump(); 1169 } 1170 1171 LLVM_DUMP_METHOD void PathDiagnosticEventPiece::dump() const { 1172 llvm::errs() << "EVENT\n--------------\n"; 1173 llvm::errs() << getString() << "\n"; 1174 llvm::errs() << " ---- at ----\n"; 1175 getLocation().dump(); 1176 } 1177 1178 LLVM_DUMP_METHOD void PathDiagnosticControlFlowPiece::dump() const { 1179 llvm::errs() << "CONTROL\n--------------\n"; 1180 getStartLocation().dump(); 1181 llvm::errs() << " ---- to ----\n"; 1182 getEndLocation().dump(); 1183 } 1184 1185 LLVM_DUMP_METHOD void PathDiagnosticMacroPiece::dump() const { 1186 llvm::errs() << "MACRO\n--------------\n"; 1187 // FIXME: Print which macro is being invoked. 1188 } 1189 1190 LLVM_DUMP_METHOD void PathDiagnosticNotePiece::dump() const { 1191 llvm::errs() << "NOTE\n--------------\n"; 1192 llvm::errs() << getString() << "\n"; 1193 llvm::errs() << " ---- at ----\n"; 1194 getLocation().dump(); 1195 } 1196 1197 LLVM_DUMP_METHOD void PathDiagnosticPopUpPiece::dump() const { 1198 llvm::errs() << "POP-UP\n--------------\n"; 1199 llvm::errs() << getString() << "\n"; 1200 llvm::errs() << " ---- at ----\n"; 1201 getLocation().dump(); 1202 } 1203 1204 LLVM_DUMP_METHOD void PathDiagnosticLocation::dump() const { 1205 if (!isValid()) { 1206 llvm::errs() << "<INVALID>\n"; 1207 return; 1208 } 1209 1210 switch (K) { 1211 case RangeK: 1212 // FIXME: actually print the range. 1213 llvm::errs() << "<range>\n"; 1214 break; 1215 case SingleLocK: 1216 asLocation().dump(); 1217 llvm::errs() << "\n"; 1218 break; 1219 case StmtK: 1220 if (S) 1221 S->dump(); 1222 else 1223 llvm::errs() << "<NULL STMT>\n"; 1224 break; 1225 case DeclK: 1226 if (const auto *ND = dyn_cast_or_null<NamedDecl>(D)) 1227 llvm::errs() << *ND << "\n"; 1228 else if (isa<BlockDecl>(D)) 1229 // FIXME: Make this nicer. 1230 llvm::errs() << "<block>\n"; 1231 else if (D) 1232 llvm::errs() << "<unknown decl>\n"; 1233 else 1234 llvm::errs() << "<NULL DECL>\n"; 1235 break; 1236 } 1237 } 1238