1 //===- BugReporter.cpp - Generate PathDiagnostics for bugs ----------------===// 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 BugReporter, a utility class for generating 10 // PathDiagnostics. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" 15 #include "clang/AST/ASTTypeTraits.h" 16 #include "clang/AST/Attr.h" 17 #include "clang/AST/Decl.h" 18 #include "clang/AST/DeclBase.h" 19 #include "clang/AST/DeclObjC.h" 20 #include "clang/AST/Expr.h" 21 #include "clang/AST/ExprCXX.h" 22 #include "clang/AST/ParentMap.h" 23 #include "clang/AST/ParentMapContext.h" 24 #include "clang/AST/Stmt.h" 25 #include "clang/AST/StmtCXX.h" 26 #include "clang/AST/StmtObjC.h" 27 #include "clang/Analysis/AnalysisDeclContext.h" 28 #include "clang/Analysis/CFG.h" 29 #include "clang/Analysis/CFGStmtMap.h" 30 #include "clang/Analysis/PathDiagnostic.h" 31 #include "clang/Analysis/ProgramPoint.h" 32 #include "clang/Basic/LLVM.h" 33 #include "clang/Basic/SourceLocation.h" 34 #include "clang/Basic/SourceManager.h" 35 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h" 36 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h" 37 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" 38 #include "clang/StaticAnalyzer/Core/BugReporter/Z3CrosscheckVisitor.h" 39 #include "clang/StaticAnalyzer/Core/Checker.h" 40 #include "clang/StaticAnalyzer/Core/CheckerManager.h" 41 #include "clang/StaticAnalyzer/Core/CheckerRegistryData.h" 42 #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h" 43 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 44 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h" 45 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" 46 #include "clang/StaticAnalyzer/Core/PathSensitive/SMTConv.h" 47 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" 48 #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h" 49 #include "llvm/ADT/ArrayRef.h" 50 #include "llvm/ADT/DenseMap.h" 51 #include "llvm/ADT/DenseSet.h" 52 #include "llvm/ADT/FoldingSet.h" 53 #include "llvm/ADT/STLExtras.h" 54 #include "llvm/ADT/SmallPtrSet.h" 55 #include "llvm/ADT/SmallString.h" 56 #include "llvm/ADT/SmallVector.h" 57 #include "llvm/ADT/Statistic.h" 58 #include "llvm/ADT/StringExtras.h" 59 #include "llvm/ADT/StringRef.h" 60 #include "llvm/ADT/iterator_range.h" 61 #include "llvm/Support/Casting.h" 62 #include "llvm/Support/Compiler.h" 63 #include "llvm/Support/ErrorHandling.h" 64 #include "llvm/Support/MemoryBuffer.h" 65 #include "llvm/Support/raw_ostream.h" 66 #include <algorithm> 67 #include <cassert> 68 #include <cstddef> 69 #include <iterator> 70 #include <memory> 71 #include <optional> 72 #include <queue> 73 #include <string> 74 #include <tuple> 75 #include <utility> 76 #include <vector> 77 78 using namespace clang; 79 using namespace ento; 80 using namespace llvm; 81 82 #define DEBUG_TYPE "BugReporter" 83 84 STATISTIC(MaxBugClassSize, 85 "The maximum number of bug reports in the same equivalence class"); 86 STATISTIC(MaxValidBugClassSize, 87 "The maximum number of bug reports in the same equivalence class " 88 "where at least one report is valid (not suppressed)"); 89 90 STATISTIC(NumTimesReportPassesZ3, "Number of reports passed Z3"); 91 STATISTIC(NumTimesReportRefuted, "Number of reports refuted by Z3"); 92 STATISTIC(NumTimesReportEQClassAborted, 93 "Number of times a report equivalence class was aborted by the Z3 " 94 "oracle heuristic"); 95 STATISTIC(NumTimesReportEQClassWasExhausted, 96 "Number of times all reports of an equivalence class was refuted"); 97 98 BugReporterVisitor::~BugReporterVisitor() = default; 99 100 void BugReporterContext::anchor() {} 101 102 //===----------------------------------------------------------------------===// 103 // PathDiagnosticBuilder and its associated routines and helper objects. 104 //===----------------------------------------------------------------------===// 105 106 namespace { 107 108 /// A (CallPiece, node assiciated with its CallEnter) pair. 109 using CallWithEntry = 110 std::pair<PathDiagnosticCallPiece *, const ExplodedNode *>; 111 using CallWithEntryStack = SmallVector<CallWithEntry, 6>; 112 113 /// Map from each node to the diagnostic pieces visitors emit for them. 114 using VisitorsDiagnosticsTy = 115 llvm::DenseMap<const ExplodedNode *, std::vector<PathDiagnosticPieceRef>>; 116 117 /// A map from PathDiagnosticPiece to the LocationContext of the inlined 118 /// function call it represents. 119 using LocationContextMap = 120 llvm::DenseMap<const PathPieces *, const LocationContext *>; 121 122 /// A helper class that contains everything needed to construct a 123 /// PathDiagnostic object. It does no much more then providing convenient 124 /// getters and some well placed asserts for extra security. 125 class PathDiagnosticConstruct { 126 /// The consumer we're constructing the bug report for. 127 const PathDiagnosticConsumer *Consumer; 128 /// Our current position in the bug path, which is owned by 129 /// PathDiagnosticBuilder. 130 const ExplodedNode *CurrentNode; 131 /// A mapping from parts of the bug path (for example, a function call, which 132 /// would span backwards from a CallExit to a CallEnter with the nodes in 133 /// between them) with the location contexts it is associated with. 134 LocationContextMap LCM; 135 const SourceManager &SM; 136 137 public: 138 /// We keep stack of calls to functions as we're ascending the bug path. 139 /// TODO: PathDiagnostic has a stack doing the same thing, shouldn't we use 140 /// that instead? 141 CallWithEntryStack CallStack; 142 /// The bug report we're constructing. For ease of use, this field is kept 143 /// public, though some "shortcut" getters are provided for commonly used 144 /// methods of PathDiagnostic. 145 std::unique_ptr<PathDiagnostic> PD; 146 147 public: 148 PathDiagnosticConstruct(const PathDiagnosticConsumer *PDC, 149 const ExplodedNode *ErrorNode, 150 const PathSensitiveBugReport *R, 151 const Decl *AnalysisEntryPoint); 152 153 /// \returns the location context associated with the current position in the 154 /// bug path. 155 const LocationContext *getCurrLocationContext() const { 156 assert(CurrentNode && "Already reached the root!"); 157 return CurrentNode->getLocationContext(); 158 } 159 160 /// Same as getCurrLocationContext (they should always return the same 161 /// location context), but works after reaching the root of the bug path as 162 /// well. 163 const LocationContext *getLocationContextForActivePath() const { 164 return LCM.find(&PD->getActivePath())->getSecond(); 165 } 166 167 const ExplodedNode *getCurrentNode() const { return CurrentNode; } 168 169 /// Steps the current node to its predecessor. 170 /// \returns whether we reached the root of the bug path. 171 bool ascendToPrevNode() { 172 CurrentNode = CurrentNode->getFirstPred(); 173 return static_cast<bool>(CurrentNode); 174 } 175 176 const ParentMap &getParentMap() const { 177 return getCurrLocationContext()->getParentMap(); 178 } 179 180 const SourceManager &getSourceManager() const { return SM; } 181 182 const Stmt *getParent(const Stmt *S) const { 183 return getParentMap().getParent(S); 184 } 185 186 void updateLocCtxMap(const PathPieces *Path, const LocationContext *LC) { 187 assert(Path && LC); 188 LCM[Path] = LC; 189 } 190 191 const LocationContext *getLocationContextFor(const PathPieces *Path) const { 192 assert(LCM.count(Path) && 193 "Failed to find the context associated with these pieces!"); 194 return LCM.find(Path)->getSecond(); 195 } 196 197 bool isInLocCtxMap(const PathPieces *Path) const { return LCM.count(Path); } 198 199 PathPieces &getActivePath() { return PD->getActivePath(); } 200 PathPieces &getMutablePieces() { return PD->getMutablePieces(); } 201 202 bool shouldAddPathEdges() const { return Consumer->shouldAddPathEdges(); } 203 bool shouldAddControlNotes() const { 204 return Consumer->shouldAddControlNotes(); 205 } 206 bool shouldGenerateDiagnostics() const { 207 return Consumer->shouldGenerateDiagnostics(); 208 } 209 bool supportsLogicalOpControlFlow() const { 210 return Consumer->supportsLogicalOpControlFlow(); 211 } 212 }; 213 214 /// Contains every contextual information needed for constructing a 215 /// PathDiagnostic object for a given bug report. This class and its fields are 216 /// immutable, and passes a BugReportConstruct object around during the 217 /// construction. 218 class PathDiagnosticBuilder : public BugReporterContext { 219 /// A linear path from the error node to the root. 220 std::unique_ptr<const ExplodedGraph> BugPath; 221 /// The bug report we're describing. Visitors create their diagnostics with 222 /// them being the last entities being able to modify it (for example, 223 /// changing interestingness here would cause inconsistencies as to how this 224 /// file and visitors construct diagnostics), hence its const. 225 const PathSensitiveBugReport *R; 226 /// The leaf of the bug path. This isn't the same as the bug reports error 227 /// node, which refers to the *original* graph, not the bug path. 228 const ExplodedNode *const ErrorNode; 229 /// The diagnostic pieces visitors emitted, which is expected to be collected 230 /// by the time this builder is constructed. 231 std::unique_ptr<const VisitorsDiagnosticsTy> VisitorsDiagnostics; 232 233 public: 234 /// Find a non-invalidated report for a given equivalence class, and returns 235 /// a PathDiagnosticBuilder able to construct bug reports for different 236 /// consumers. Returns std::nullopt if no valid report is found. 237 static std::optional<PathDiagnosticBuilder> 238 findValidReport(ArrayRef<PathSensitiveBugReport *> &bugReports, 239 PathSensitiveBugReporter &Reporter); 240 241 PathDiagnosticBuilder( 242 BugReporterContext BRC, std::unique_ptr<ExplodedGraph> BugPath, 243 PathSensitiveBugReport *r, const ExplodedNode *ErrorNode, 244 std::unique_ptr<VisitorsDiagnosticsTy> VisitorsDiagnostics); 245 246 /// This function is responsible for generating diagnostic pieces that are 247 /// *not* provided by bug report visitors. 248 /// These diagnostics may differ depending on the consumer's settings, 249 /// and are therefore constructed separately for each consumer. 250 /// 251 /// There are two path diagnostics generation modes: with adding edges (used 252 /// for plists) and without (used for HTML and text). When edges are added, 253 /// the path is modified to insert artificially generated edges. 254 /// Otherwise, more detailed diagnostics is emitted for block edges, 255 /// explaining the transitions in words. 256 std::unique_ptr<PathDiagnostic> 257 generate(const PathDiagnosticConsumer *PDC) const; 258 259 private: 260 void updateStackPiecesWithMessage(PathDiagnosticPieceRef P, 261 const CallWithEntryStack &CallStack) const; 262 void generatePathDiagnosticsForNode(PathDiagnosticConstruct &C, 263 PathDiagnosticLocation &PrevLoc) const; 264 265 void generateMinimalDiagForBlockEdge(PathDiagnosticConstruct &C, 266 BlockEdge BE) const; 267 268 PathDiagnosticPieceRef 269 generateDiagForGotoOP(const PathDiagnosticConstruct &C, const Stmt *S, 270 PathDiagnosticLocation &Start) const; 271 272 PathDiagnosticPieceRef 273 generateDiagForSwitchOP(const PathDiagnosticConstruct &C, const CFGBlock *Dst, 274 PathDiagnosticLocation &Start) const; 275 276 PathDiagnosticPieceRef 277 generateDiagForBinaryOP(const PathDiagnosticConstruct &C, const Stmt *T, 278 const CFGBlock *Src, const CFGBlock *DstC) const; 279 280 PathDiagnosticLocation 281 ExecutionContinues(const PathDiagnosticConstruct &C) const; 282 283 PathDiagnosticLocation 284 ExecutionContinues(llvm::raw_string_ostream &os, 285 const PathDiagnosticConstruct &C) const; 286 287 const PathSensitiveBugReport *getBugReport() const { return R; } 288 }; 289 290 } // namespace 291 292 //===----------------------------------------------------------------------===// 293 // Base implementation of stack hint generators. 294 //===----------------------------------------------------------------------===// 295 296 StackHintGenerator::~StackHintGenerator() = default; 297 298 std::string StackHintGeneratorForSymbol::getMessage(const ExplodedNode *N){ 299 if (!N) 300 return getMessageForSymbolNotFound(); 301 302 ProgramPoint P = N->getLocation(); 303 CallExitEnd CExit = P.castAs<CallExitEnd>(); 304 305 // FIXME: Use CallEvent to abstract this over all calls. 306 const Stmt *CallSite = CExit.getCalleeContext()->getCallSite(); 307 const auto *CE = dyn_cast_or_null<CallExpr>(CallSite); 308 if (!CE) 309 return {}; 310 311 // Check if one of the parameters are set to the interesting symbol. 312 for (auto [Idx, ArgExpr] : llvm::enumerate(CE->arguments())) { 313 SVal SV = N->getSVal(ArgExpr); 314 315 // Check if the variable corresponding to the symbol is passed by value. 316 SymbolRef AS = SV.getAsLocSymbol(); 317 if (AS == Sym) { 318 return getMessageForArg(ArgExpr, Idx); 319 } 320 321 // Check if the parameter is a pointer to the symbol. 322 if (std::optional<loc::MemRegionVal> Reg = SV.getAs<loc::MemRegionVal>()) { 323 // Do not attempt to dereference void*. 324 if (ArgExpr->getType()->isVoidPointerType()) 325 continue; 326 SVal PSV = N->getState()->getSVal(Reg->getRegion()); 327 SymbolRef AS = PSV.getAsLocSymbol(); 328 if (AS == Sym) { 329 return getMessageForArg(ArgExpr, Idx); 330 } 331 } 332 } 333 334 // Check if we are returning the interesting symbol. 335 SVal SV = N->getSVal(CE); 336 SymbolRef RetSym = SV.getAsLocSymbol(); 337 if (RetSym == Sym) { 338 return getMessageForReturn(CE); 339 } 340 341 return getMessageForSymbolNotFound(); 342 } 343 344 std::string StackHintGeneratorForSymbol::getMessageForArg(const Expr *ArgE, 345 unsigned ArgIndex) { 346 // Printed parameters start at 1, not 0. 347 ++ArgIndex; 348 349 return (llvm::Twine(Msg) + " via " + std::to_string(ArgIndex) + 350 llvm::getOrdinalSuffix(ArgIndex) + " parameter").str(); 351 } 352 353 //===----------------------------------------------------------------------===// 354 // Diagnostic cleanup. 355 //===----------------------------------------------------------------------===// 356 357 static PathDiagnosticEventPiece * 358 eventsDescribeSameCondition(PathDiagnosticEventPiece *X, 359 PathDiagnosticEventPiece *Y) { 360 // Prefer diagnostics that come from ConditionBRVisitor over 361 // those that came from TrackConstraintBRVisitor, 362 // unless the one from ConditionBRVisitor is 363 // its generic fallback diagnostic. 364 const void *tagPreferred = ConditionBRVisitor::getTag(); 365 const void *tagLesser = TrackConstraintBRVisitor::getTag(); 366 367 if (X->getLocation() != Y->getLocation()) 368 return nullptr; 369 370 if (X->getTag() == tagPreferred && Y->getTag() == tagLesser) 371 return ConditionBRVisitor::isPieceMessageGeneric(X) ? Y : X; 372 373 if (Y->getTag() == tagPreferred && X->getTag() == tagLesser) 374 return ConditionBRVisitor::isPieceMessageGeneric(Y) ? X : Y; 375 376 return nullptr; 377 } 378 379 /// An optimization pass over PathPieces that removes redundant diagnostics 380 /// generated by both ConditionBRVisitor and TrackConstraintBRVisitor. Both 381 /// BugReporterVisitors use different methods to generate diagnostics, with 382 /// one capable of emitting diagnostics in some cases but not in others. This 383 /// can lead to redundant diagnostic pieces at the same point in a path. 384 static void removeRedundantMsgs(PathPieces &path) { 385 unsigned N = path.size(); 386 if (N < 2) 387 return; 388 // NOTE: this loop intentionally is not using an iterator. Instead, we 389 // are streaming the path and modifying it in place. This is done by 390 // grabbing the front, processing it, and if we decide to keep it append 391 // it to the end of the path. The entire path is processed in this way. 392 for (unsigned i = 0; i < N; ++i) { 393 auto piece = std::move(path.front()); 394 path.pop_front(); 395 396 switch (piece->getKind()) { 397 case PathDiagnosticPiece::Call: 398 removeRedundantMsgs(cast<PathDiagnosticCallPiece>(*piece).path); 399 break; 400 case PathDiagnosticPiece::Macro: 401 removeRedundantMsgs(cast<PathDiagnosticMacroPiece>(*piece).subPieces); 402 break; 403 case PathDiagnosticPiece::Event: { 404 if (i == N-1) 405 break; 406 407 if (auto *nextEvent = 408 dyn_cast<PathDiagnosticEventPiece>(path.front().get())) { 409 auto *event = cast<PathDiagnosticEventPiece>(piece.get()); 410 // Check to see if we should keep one of the two pieces. If we 411 // come up with a preference, record which piece to keep, and consume 412 // another piece from the path. 413 if (auto *pieceToKeep = 414 eventsDescribeSameCondition(event, nextEvent)) { 415 piece = std::move(pieceToKeep == event ? piece : path.front()); 416 path.pop_front(); 417 ++i; 418 } 419 } 420 break; 421 } 422 case PathDiagnosticPiece::ControlFlow: 423 case PathDiagnosticPiece::Note: 424 case PathDiagnosticPiece::PopUp: 425 break; 426 } 427 path.push_back(std::move(piece)); 428 } 429 } 430 431 /// Recursively scan through a path and prune out calls and macros pieces 432 /// that aren't needed. Return true if afterwards the path contains 433 /// "interesting stuff" which means it shouldn't be pruned from the parent path. 434 static bool removeUnneededCalls(const PathDiagnosticConstruct &C, 435 PathPieces &pieces, 436 const PathSensitiveBugReport *R, 437 bool IsInteresting = false) { 438 bool containsSomethingInteresting = IsInteresting; 439 const unsigned N = pieces.size(); 440 441 for (unsigned i = 0 ; i < N ; ++i) { 442 // Remove the front piece from the path. If it is still something we 443 // want to keep once we are done, we will push it back on the end. 444 auto piece = std::move(pieces.front()); 445 pieces.pop_front(); 446 447 switch (piece->getKind()) { 448 case PathDiagnosticPiece::Call: { 449 auto &call = cast<PathDiagnosticCallPiece>(*piece); 450 // Check if the location context is interesting. 451 if (!removeUnneededCalls( 452 C, call.path, R, 453 R->isInteresting(C.getLocationContextFor(&call.path)))) 454 continue; 455 456 containsSomethingInteresting = true; 457 break; 458 } 459 case PathDiagnosticPiece::Macro: { 460 auto ¯o = cast<PathDiagnosticMacroPiece>(*piece); 461 if (!removeUnneededCalls(C, macro.subPieces, R, IsInteresting)) 462 continue; 463 containsSomethingInteresting = true; 464 break; 465 } 466 case PathDiagnosticPiece::Event: { 467 auto &event = cast<PathDiagnosticEventPiece>(*piece); 468 469 // We never throw away an event, but we do throw it away wholesale 470 // as part of a path if we throw the entire path away. 471 containsSomethingInteresting |= !event.isPrunable(); 472 break; 473 } 474 case PathDiagnosticPiece::ControlFlow: 475 case PathDiagnosticPiece::Note: 476 case PathDiagnosticPiece::PopUp: 477 break; 478 } 479 480 pieces.push_back(std::move(piece)); 481 } 482 483 return containsSomethingInteresting; 484 } 485 486 /// Same logic as above to remove extra pieces. 487 static void removePopUpNotes(PathPieces &Path) { 488 for (unsigned int i = 0; i < Path.size(); ++i) { 489 auto Piece = std::move(Path.front()); 490 Path.pop_front(); 491 if (!isa<PathDiagnosticPopUpPiece>(*Piece)) 492 Path.push_back(std::move(Piece)); 493 } 494 } 495 496 /// Returns true if the given decl has been implicitly given a body, either by 497 /// the analyzer or by the compiler proper. 498 static bool hasImplicitBody(const Decl *D) { 499 assert(D); 500 return D->isImplicit() || !D->hasBody(); 501 } 502 503 /// Recursively scan through a path and make sure that all call pieces have 504 /// valid locations. 505 static void 506 adjustCallLocations(PathPieces &Pieces, 507 PathDiagnosticLocation *LastCallLocation = nullptr) { 508 for (const auto &I : Pieces) { 509 auto *Call = dyn_cast<PathDiagnosticCallPiece>(I.get()); 510 511 if (!Call) 512 continue; 513 514 if (LastCallLocation) { 515 bool CallerIsImplicit = hasImplicitBody(Call->getCaller()); 516 if (CallerIsImplicit || !Call->callEnter.asLocation().isValid()) 517 Call->callEnter = *LastCallLocation; 518 if (CallerIsImplicit || !Call->callReturn.asLocation().isValid()) 519 Call->callReturn = *LastCallLocation; 520 } 521 522 // Recursively clean out the subclass. Keep this call around if 523 // it contains any informative diagnostics. 524 PathDiagnosticLocation *ThisCallLocation; 525 if (Call->callEnterWithin.asLocation().isValid() && 526 !hasImplicitBody(Call->getCallee())) 527 ThisCallLocation = &Call->callEnterWithin; 528 else 529 ThisCallLocation = &Call->callEnter; 530 531 assert(ThisCallLocation && "Outermost call has an invalid location"); 532 adjustCallLocations(Call->path, ThisCallLocation); 533 } 534 } 535 536 /// Remove edges in and out of C++ default initializer expressions. These are 537 /// for fields that have in-class initializers, as opposed to being initialized 538 /// explicitly in a constructor or braced list. 539 static void removeEdgesToDefaultInitializers(PathPieces &Pieces) { 540 for (PathPieces::iterator I = Pieces.begin(), E = Pieces.end(); I != E;) { 541 if (auto *C = dyn_cast<PathDiagnosticCallPiece>(I->get())) 542 removeEdgesToDefaultInitializers(C->path); 543 544 if (auto *M = dyn_cast<PathDiagnosticMacroPiece>(I->get())) 545 removeEdgesToDefaultInitializers(M->subPieces); 546 547 if (auto *CF = dyn_cast<PathDiagnosticControlFlowPiece>(I->get())) { 548 const Stmt *Start = CF->getStartLocation().asStmt(); 549 const Stmt *End = CF->getEndLocation().asStmt(); 550 if (isa_and_nonnull<CXXDefaultInitExpr>(Start)) { 551 I = Pieces.erase(I); 552 continue; 553 } else if (isa_and_nonnull<CXXDefaultInitExpr>(End)) { 554 PathPieces::iterator Next = std::next(I); 555 if (Next != E) { 556 if (auto *NextCF = 557 dyn_cast<PathDiagnosticControlFlowPiece>(Next->get())) { 558 NextCF->setStartLocation(CF->getStartLocation()); 559 } 560 } 561 I = Pieces.erase(I); 562 continue; 563 } 564 } 565 566 I++; 567 } 568 } 569 570 /// Remove all pieces with invalid locations as these cannot be serialized. 571 /// We might have pieces with invalid locations as a result of inlining Body 572 /// Farm generated functions. 573 static void removePiecesWithInvalidLocations(PathPieces &Pieces) { 574 for (PathPieces::iterator I = Pieces.begin(), E = Pieces.end(); I != E;) { 575 if (auto *C = dyn_cast<PathDiagnosticCallPiece>(I->get())) 576 removePiecesWithInvalidLocations(C->path); 577 578 if (auto *M = dyn_cast<PathDiagnosticMacroPiece>(I->get())) 579 removePiecesWithInvalidLocations(M->subPieces); 580 581 if (!(*I)->getLocation().isValid() || 582 !(*I)->getLocation().asLocation().isValid()) { 583 I = Pieces.erase(I); 584 continue; 585 } 586 I++; 587 } 588 } 589 590 PathDiagnosticLocation PathDiagnosticBuilder::ExecutionContinues( 591 const PathDiagnosticConstruct &C) const { 592 if (const Stmt *S = C.getCurrentNode()->getNextStmtForDiagnostics()) 593 return PathDiagnosticLocation(S, getSourceManager(), 594 C.getCurrLocationContext()); 595 596 return PathDiagnosticLocation::createDeclEnd(C.getCurrLocationContext(), 597 getSourceManager()); 598 } 599 600 PathDiagnosticLocation PathDiagnosticBuilder::ExecutionContinues( 601 llvm::raw_string_ostream &os, const PathDiagnosticConstruct &C) const { 602 // Slow, but probably doesn't matter. 603 if (os.str().empty()) 604 os << ' '; 605 606 const PathDiagnosticLocation &Loc = ExecutionContinues(C); 607 608 if (Loc.asStmt()) 609 os << "Execution continues on line " 610 << getSourceManager().getExpansionLineNumber(Loc.asLocation()) 611 << '.'; 612 else { 613 os << "Execution jumps to the end of the "; 614 const Decl *D = C.getCurrLocationContext()->getDecl(); 615 if (isa<ObjCMethodDecl>(D)) 616 os << "method"; 617 else if (isa<FunctionDecl>(D)) 618 os << "function"; 619 else { 620 assert(isa<BlockDecl>(D)); 621 os << "anonymous block"; 622 } 623 os << '.'; 624 } 625 626 return Loc; 627 } 628 629 static const Stmt *getEnclosingParent(const Stmt *S, const ParentMap &PM) { 630 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S))) 631 return PM.getParentIgnoreParens(S); 632 633 const Stmt *Parent = PM.getParentIgnoreParens(S); 634 if (!Parent) 635 return nullptr; 636 637 switch (Parent->getStmtClass()) { 638 case Stmt::ForStmtClass: 639 case Stmt::DoStmtClass: 640 case Stmt::WhileStmtClass: 641 case Stmt::ObjCForCollectionStmtClass: 642 case Stmt::CXXForRangeStmtClass: 643 return Parent; 644 default: 645 break; 646 } 647 648 return nullptr; 649 } 650 651 static PathDiagnosticLocation 652 getEnclosingStmtLocation(const Stmt *S, const LocationContext *LC, 653 bool allowNestedContexts = false) { 654 if (!S) 655 return {}; 656 657 const SourceManager &SMgr = LC->getDecl()->getASTContext().getSourceManager(); 658 659 while (const Stmt *Parent = getEnclosingParent(S, LC->getParentMap())) { 660 switch (Parent->getStmtClass()) { 661 case Stmt::BinaryOperatorClass: { 662 const auto *B = cast<BinaryOperator>(Parent); 663 if (B->isLogicalOp()) 664 return PathDiagnosticLocation(allowNestedContexts ? B : S, SMgr, LC); 665 break; 666 } 667 case Stmt::CompoundStmtClass: 668 case Stmt::StmtExprClass: 669 return PathDiagnosticLocation(S, SMgr, LC); 670 case Stmt::ChooseExprClass: 671 // Similar to '?' if we are referring to condition, just have the edge 672 // point to the entire choose expression. 673 if (allowNestedContexts || cast<ChooseExpr>(Parent)->getCond() == S) 674 return PathDiagnosticLocation(Parent, SMgr, LC); 675 else 676 return PathDiagnosticLocation(S, SMgr, LC); 677 case Stmt::BinaryConditionalOperatorClass: 678 case Stmt::ConditionalOperatorClass: 679 // For '?', if we are referring to condition, just have the edge point 680 // to the entire '?' expression. 681 if (allowNestedContexts || 682 cast<AbstractConditionalOperator>(Parent)->getCond() == S) 683 return PathDiagnosticLocation(Parent, SMgr, LC); 684 else 685 return PathDiagnosticLocation(S, SMgr, LC); 686 case Stmt::CXXForRangeStmtClass: 687 if (cast<CXXForRangeStmt>(Parent)->getBody() == S) 688 return PathDiagnosticLocation(S, SMgr, LC); 689 break; 690 case Stmt::DoStmtClass: 691 return PathDiagnosticLocation(S, SMgr, LC); 692 case Stmt::ForStmtClass: 693 if (cast<ForStmt>(Parent)->getBody() == S) 694 return PathDiagnosticLocation(S, SMgr, LC); 695 break; 696 case Stmt::IfStmtClass: 697 if (cast<IfStmt>(Parent)->getCond() != S) 698 return PathDiagnosticLocation(S, SMgr, LC); 699 break; 700 case Stmt::ObjCForCollectionStmtClass: 701 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S) 702 return PathDiagnosticLocation(S, SMgr, LC); 703 break; 704 case Stmt::WhileStmtClass: 705 if (cast<WhileStmt>(Parent)->getCond() != S) 706 return PathDiagnosticLocation(S, SMgr, LC); 707 break; 708 default: 709 break; 710 } 711 712 S = Parent; 713 } 714 715 assert(S && "Cannot have null Stmt for PathDiagnosticLocation"); 716 717 return PathDiagnosticLocation(S, SMgr, LC); 718 } 719 720 //===----------------------------------------------------------------------===// 721 // "Minimal" path diagnostic generation algorithm. 722 //===----------------------------------------------------------------------===// 723 724 /// If the piece contains a special message, add it to all the call pieces on 725 /// the active stack. For example, my_malloc allocated memory, so MallocChecker 726 /// will construct an event at the call to malloc(), and add a stack hint that 727 /// an allocated memory was returned. We'll use this hint to construct a message 728 /// when returning from the call to my_malloc 729 /// 730 /// void *my_malloc() { return malloc(sizeof(int)); } 731 /// void fishy() { 732 /// void *ptr = my_malloc(); // returned allocated memory 733 /// } // leak 734 void PathDiagnosticBuilder::updateStackPiecesWithMessage( 735 PathDiagnosticPieceRef P, const CallWithEntryStack &CallStack) const { 736 if (R->hasCallStackHint(P)) 737 for (const auto &I : CallStack) { 738 PathDiagnosticCallPiece *CP = I.first; 739 const ExplodedNode *N = I.second; 740 std::string stackMsg = R->getCallStackMessage(P, N); 741 742 // The last message on the path to final bug is the most important 743 // one. Since we traverse the path backwards, do not add the message 744 // if one has been previously added. 745 if (!CP->hasCallStackMessage()) 746 CP->setCallStackMessage(stackMsg); 747 } 748 } 749 750 static void CompactMacroExpandedPieces(PathPieces &path, 751 const SourceManager& SM); 752 753 PathDiagnosticPieceRef PathDiagnosticBuilder::generateDiagForSwitchOP( 754 const PathDiagnosticConstruct &C, const CFGBlock *Dst, 755 PathDiagnosticLocation &Start) const { 756 757 const SourceManager &SM = getSourceManager(); 758 // Figure out what case arm we took. 759 std::string sbuf; 760 llvm::raw_string_ostream os(sbuf); 761 PathDiagnosticLocation End; 762 763 if (const Stmt *S = Dst->getLabel()) { 764 End = PathDiagnosticLocation(S, SM, C.getCurrLocationContext()); 765 766 switch (S->getStmtClass()) { 767 default: 768 os << "No cases match in the switch statement. " 769 "Control jumps to line " 770 << End.asLocation().getExpansionLineNumber(); 771 break; 772 case Stmt::DefaultStmtClass: 773 os << "Control jumps to the 'default' case at line " 774 << End.asLocation().getExpansionLineNumber(); 775 break; 776 777 case Stmt::CaseStmtClass: { 778 os << "Control jumps to 'case "; 779 const auto *Case = cast<CaseStmt>(S); 780 const Expr *LHS = Case->getLHS()->IgnoreParenImpCasts(); 781 782 // Determine if it is an enum. 783 bool GetRawInt = true; 784 785 if (const auto *DR = dyn_cast<DeclRefExpr>(LHS)) { 786 // FIXME: Maybe this should be an assertion. Are there cases 787 // were it is not an EnumConstantDecl? 788 const auto *D = dyn_cast<EnumConstantDecl>(DR->getDecl()); 789 790 if (D) { 791 GetRawInt = false; 792 os << *D; 793 } 794 } 795 796 if (GetRawInt) 797 os << LHS->EvaluateKnownConstInt(getASTContext()); 798 799 os << ":' at line " << End.asLocation().getExpansionLineNumber(); 800 break; 801 } 802 } 803 } else { 804 os << "'Default' branch taken. "; 805 End = ExecutionContinues(os, C); 806 } 807 return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, 808 os.str()); 809 } 810 811 PathDiagnosticPieceRef PathDiagnosticBuilder::generateDiagForGotoOP( 812 const PathDiagnosticConstruct &C, const Stmt *S, 813 PathDiagnosticLocation &Start) const { 814 std::string sbuf; 815 llvm::raw_string_ostream os(sbuf); 816 const PathDiagnosticLocation &End = 817 getEnclosingStmtLocation(S, C.getCurrLocationContext()); 818 os << "Control jumps to line " << End.asLocation().getExpansionLineNumber(); 819 return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, os.str()); 820 } 821 822 PathDiagnosticPieceRef PathDiagnosticBuilder::generateDiagForBinaryOP( 823 const PathDiagnosticConstruct &C, const Stmt *T, const CFGBlock *Src, 824 const CFGBlock *Dst) const { 825 826 const SourceManager &SM = getSourceManager(); 827 828 const auto *B = cast<BinaryOperator>(T); 829 std::string sbuf; 830 llvm::raw_string_ostream os(sbuf); 831 os << "Left side of '"; 832 PathDiagnosticLocation Start, End; 833 834 if (B->getOpcode() == BO_LAnd) { 835 os << "&&" 836 << "' is "; 837 838 if (*(Src->succ_begin() + 1) == Dst) { 839 os << "false"; 840 End = PathDiagnosticLocation(B->getLHS(), SM, C.getCurrLocationContext()); 841 Start = 842 PathDiagnosticLocation::createOperatorLoc(B, SM); 843 } else { 844 os << "true"; 845 Start = 846 PathDiagnosticLocation(B->getLHS(), SM, C.getCurrLocationContext()); 847 End = ExecutionContinues(C); 848 } 849 } else { 850 assert(B->getOpcode() == BO_LOr); 851 os << "||" 852 << "' is "; 853 854 if (*(Src->succ_begin() + 1) == Dst) { 855 os << "false"; 856 Start = 857 PathDiagnosticLocation(B->getLHS(), SM, C.getCurrLocationContext()); 858 End = ExecutionContinues(C); 859 } else { 860 os << "true"; 861 End = PathDiagnosticLocation(B->getLHS(), SM, C.getCurrLocationContext()); 862 Start = 863 PathDiagnosticLocation::createOperatorLoc(B, SM); 864 } 865 } 866 return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, 867 os.str()); 868 } 869 870 void PathDiagnosticBuilder::generateMinimalDiagForBlockEdge( 871 PathDiagnosticConstruct &C, BlockEdge BE) const { 872 const SourceManager &SM = getSourceManager(); 873 const LocationContext *LC = C.getCurrLocationContext(); 874 const CFGBlock *Src = BE.getSrc(); 875 const CFGBlock *Dst = BE.getDst(); 876 const Stmt *T = Src->getTerminatorStmt(); 877 if (!T) 878 return; 879 880 auto Start = PathDiagnosticLocation::createBegin(T, SM, LC); 881 switch (T->getStmtClass()) { 882 default: 883 break; 884 885 case Stmt::GotoStmtClass: 886 case Stmt::IndirectGotoStmtClass: { 887 if (const Stmt *S = C.getCurrentNode()->getNextStmtForDiagnostics()) 888 C.getActivePath().push_front(generateDiagForGotoOP(C, S, Start)); 889 break; 890 } 891 892 case Stmt::SwitchStmtClass: { 893 C.getActivePath().push_front(generateDiagForSwitchOP(C, Dst, Start)); 894 break; 895 } 896 897 case Stmt::BreakStmtClass: 898 case Stmt::ContinueStmtClass: { 899 std::string sbuf; 900 llvm::raw_string_ostream os(sbuf); 901 PathDiagnosticLocation End = ExecutionContinues(os, C); 902 C.getActivePath().push_front( 903 std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, os.str())); 904 break; 905 } 906 907 // Determine control-flow for ternary '?'. 908 case Stmt::BinaryConditionalOperatorClass: 909 case Stmt::ConditionalOperatorClass: { 910 std::string sbuf; 911 llvm::raw_string_ostream os(sbuf); 912 os << "'?' condition is "; 913 914 if (*(Src->succ_begin() + 1) == Dst) 915 os << "false"; 916 else 917 os << "true"; 918 919 PathDiagnosticLocation End = ExecutionContinues(C); 920 921 if (const Stmt *S = End.asStmt()) 922 End = getEnclosingStmtLocation(S, C.getCurrLocationContext()); 923 924 C.getActivePath().push_front( 925 std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, os.str())); 926 break; 927 } 928 929 // Determine control-flow for short-circuited '&&' and '||'. 930 case Stmt::BinaryOperatorClass: { 931 if (!C.supportsLogicalOpControlFlow()) 932 break; 933 934 C.getActivePath().push_front(generateDiagForBinaryOP(C, T, Src, Dst)); 935 break; 936 } 937 938 case Stmt::DoStmtClass: 939 if (*(Src->succ_begin()) == Dst) { 940 std::string sbuf; 941 llvm::raw_string_ostream os(sbuf); 942 943 os << "Loop condition is true. "; 944 PathDiagnosticLocation End = ExecutionContinues(os, C); 945 946 if (const Stmt *S = End.asStmt()) 947 End = getEnclosingStmtLocation(S, C.getCurrLocationContext()); 948 949 C.getActivePath().push_front( 950 std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, 951 os.str())); 952 } else { 953 PathDiagnosticLocation End = ExecutionContinues(C); 954 955 if (const Stmt *S = End.asStmt()) 956 End = getEnclosingStmtLocation(S, C.getCurrLocationContext()); 957 958 C.getActivePath().push_front( 959 std::make_shared<PathDiagnosticControlFlowPiece>( 960 Start, End, "Loop condition is false. Exiting loop")); 961 } 962 break; 963 964 case Stmt::WhileStmtClass: 965 case Stmt::ForStmtClass: 966 if (*(Src->succ_begin() + 1) == Dst) { 967 std::string sbuf; 968 llvm::raw_string_ostream os(sbuf); 969 970 os << "Loop condition is false. "; 971 PathDiagnosticLocation End = ExecutionContinues(os, C); 972 if (const Stmt *S = End.asStmt()) 973 End = getEnclosingStmtLocation(S, C.getCurrLocationContext()); 974 975 C.getActivePath().push_front( 976 std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, 977 os.str())); 978 } else { 979 PathDiagnosticLocation End = ExecutionContinues(C); 980 if (const Stmt *S = End.asStmt()) 981 End = getEnclosingStmtLocation(S, C.getCurrLocationContext()); 982 983 C.getActivePath().push_front( 984 std::make_shared<PathDiagnosticControlFlowPiece>( 985 Start, End, "Loop condition is true. Entering loop body")); 986 } 987 988 break; 989 990 case Stmt::IfStmtClass: { 991 PathDiagnosticLocation End = ExecutionContinues(C); 992 993 if (const Stmt *S = End.asStmt()) 994 End = getEnclosingStmtLocation(S, C.getCurrLocationContext()); 995 996 if (*(Src->succ_begin() + 1) == Dst) 997 C.getActivePath().push_front( 998 std::make_shared<PathDiagnosticControlFlowPiece>( 999 Start, End, "Taking false branch")); 1000 else 1001 C.getActivePath().push_front( 1002 std::make_shared<PathDiagnosticControlFlowPiece>( 1003 Start, End, "Taking true branch")); 1004 1005 break; 1006 } 1007 } 1008 } 1009 1010 //===----------------------------------------------------------------------===// 1011 // Functions for determining if a loop was executed 0 times. 1012 //===----------------------------------------------------------------------===// 1013 1014 static bool isLoop(const Stmt *Term) { 1015 switch (Term->getStmtClass()) { 1016 case Stmt::ForStmtClass: 1017 case Stmt::WhileStmtClass: 1018 case Stmt::ObjCForCollectionStmtClass: 1019 case Stmt::CXXForRangeStmtClass: 1020 return true; 1021 default: 1022 // Note that we intentionally do not include do..while here. 1023 return false; 1024 } 1025 } 1026 1027 static bool isJumpToFalseBranch(const BlockEdge *BE) { 1028 const CFGBlock *Src = BE->getSrc(); 1029 assert(Src->succ_size() == 2); 1030 return (*(Src->succ_begin()+1) == BE->getDst()); 1031 } 1032 1033 static bool isContainedByStmt(const ParentMap &PM, const Stmt *S, 1034 const Stmt *SubS) { 1035 while (SubS) { 1036 if (SubS == S) 1037 return true; 1038 SubS = PM.getParent(SubS); 1039 } 1040 return false; 1041 } 1042 1043 static const Stmt *getStmtBeforeCond(const ParentMap &PM, const Stmt *Term, 1044 const ExplodedNode *N) { 1045 while (N) { 1046 std::optional<StmtPoint> SP = N->getLocation().getAs<StmtPoint>(); 1047 if (SP) { 1048 const Stmt *S = SP->getStmt(); 1049 if (!isContainedByStmt(PM, Term, S)) 1050 return S; 1051 } 1052 N = N->getFirstPred(); 1053 } 1054 return nullptr; 1055 } 1056 1057 static bool isInLoopBody(const ParentMap &PM, const Stmt *S, const Stmt *Term) { 1058 const Stmt *LoopBody = nullptr; 1059 switch (Term->getStmtClass()) { 1060 case Stmt::CXXForRangeStmtClass: { 1061 const auto *FR = cast<CXXForRangeStmt>(Term); 1062 if (isContainedByStmt(PM, FR->getInc(), S)) 1063 return true; 1064 if (isContainedByStmt(PM, FR->getLoopVarStmt(), S)) 1065 return true; 1066 LoopBody = FR->getBody(); 1067 break; 1068 } 1069 case Stmt::ForStmtClass: { 1070 const auto *FS = cast<ForStmt>(Term); 1071 if (isContainedByStmt(PM, FS->getInc(), S)) 1072 return true; 1073 LoopBody = FS->getBody(); 1074 break; 1075 } 1076 case Stmt::ObjCForCollectionStmtClass: { 1077 const auto *FC = cast<ObjCForCollectionStmt>(Term); 1078 LoopBody = FC->getBody(); 1079 break; 1080 } 1081 case Stmt::WhileStmtClass: 1082 LoopBody = cast<WhileStmt>(Term)->getBody(); 1083 break; 1084 default: 1085 return false; 1086 } 1087 return isContainedByStmt(PM, LoopBody, S); 1088 } 1089 1090 /// Adds a sanitized control-flow diagnostic edge to a path. 1091 static void addEdgeToPath(PathPieces &path, 1092 PathDiagnosticLocation &PrevLoc, 1093 PathDiagnosticLocation NewLoc) { 1094 if (!NewLoc.isValid()) 1095 return; 1096 1097 SourceLocation NewLocL = NewLoc.asLocation(); 1098 if (NewLocL.isInvalid()) 1099 return; 1100 1101 if (!PrevLoc.isValid() || !PrevLoc.asLocation().isValid()) { 1102 PrevLoc = NewLoc; 1103 return; 1104 } 1105 1106 // Ignore self-edges, which occur when there are multiple nodes at the same 1107 // statement. 1108 if (NewLoc.asStmt() && NewLoc.asStmt() == PrevLoc.asStmt()) 1109 return; 1110 1111 path.push_front( 1112 std::make_shared<PathDiagnosticControlFlowPiece>(NewLoc, PrevLoc)); 1113 PrevLoc = NewLoc; 1114 } 1115 1116 /// A customized wrapper for CFGBlock::getTerminatorCondition() 1117 /// which returns the element for ObjCForCollectionStmts. 1118 static const Stmt *getTerminatorCondition(const CFGBlock *B) { 1119 const Stmt *S = B->getTerminatorCondition(); 1120 if (const auto *FS = dyn_cast_or_null<ObjCForCollectionStmt>(S)) 1121 return FS->getElement(); 1122 return S; 1123 } 1124 1125 constexpr llvm::StringLiteral StrEnteringLoop = "Entering loop body"; 1126 constexpr llvm::StringLiteral StrLoopBodyZero = "Loop body executed 0 times"; 1127 constexpr llvm::StringLiteral StrLoopRangeEmpty = 1128 "Loop body skipped when range is empty"; 1129 constexpr llvm::StringLiteral StrLoopCollectionEmpty = 1130 "Loop body skipped when collection is empty"; 1131 1132 static std::unique_ptr<FilesToLineNumsMap> 1133 findExecutedLines(const SourceManager &SM, const ExplodedNode *N); 1134 1135 void PathDiagnosticBuilder::generatePathDiagnosticsForNode( 1136 PathDiagnosticConstruct &C, PathDiagnosticLocation &PrevLoc) const { 1137 ProgramPoint P = C.getCurrentNode()->getLocation(); 1138 const SourceManager &SM = getSourceManager(); 1139 1140 // Have we encountered an entrance to a call? It may be 1141 // the case that we have not encountered a matching 1142 // call exit before this point. This means that the path 1143 // terminated within the call itself. 1144 if (auto CE = P.getAs<CallEnter>()) { 1145 1146 if (C.shouldAddPathEdges()) { 1147 // Add an edge to the start of the function. 1148 const StackFrameContext *CalleeLC = CE->getCalleeContext(); 1149 const Decl *D = CalleeLC->getDecl(); 1150 // Add the edge only when the callee has body. We jump to the beginning 1151 // of the *declaration*, however we expect it to be followed by the 1152 // body. This isn't the case for autosynthesized property accessors in 1153 // Objective-C. No need for a similar extra check for CallExit points 1154 // because the exit edge comes from a statement (i.e. return), 1155 // not from declaration. 1156 if (D->hasBody()) 1157 addEdgeToPath(C.getActivePath(), PrevLoc, 1158 PathDiagnosticLocation::createBegin(D, SM)); 1159 } 1160 1161 // Did we visit an entire call? 1162 bool VisitedEntireCall = C.PD->isWithinCall(); 1163 C.PD->popActivePath(); 1164 1165 PathDiagnosticCallPiece *Call; 1166 if (VisitedEntireCall) { 1167 Call = cast<PathDiagnosticCallPiece>(C.getActivePath().front().get()); 1168 } else { 1169 // The path terminated within a nested location context, create a new 1170 // call piece to encapsulate the rest of the path pieces. 1171 const Decl *Caller = CE->getLocationContext()->getDecl(); 1172 Call = PathDiagnosticCallPiece::construct(C.getActivePath(), Caller); 1173 assert(C.getActivePath().size() == 1 && 1174 C.getActivePath().front().get() == Call); 1175 1176 // Since we just transferred the path over to the call piece, reset the 1177 // mapping of the active path to the current location context. 1178 assert(C.isInLocCtxMap(&C.getActivePath()) && 1179 "When we ascend to a previously unvisited call, the active path's " 1180 "address shouldn't change, but rather should be compacted into " 1181 "a single CallEvent!"); 1182 C.updateLocCtxMap(&C.getActivePath(), C.getCurrLocationContext()); 1183 1184 // Record the location context mapping for the path within the call. 1185 assert(!C.isInLocCtxMap(&Call->path) && 1186 "When we ascend to a previously unvisited call, this must be the " 1187 "first time we encounter the caller context!"); 1188 C.updateLocCtxMap(&Call->path, CE->getCalleeContext()); 1189 } 1190 Call->setCallee(*CE, SM); 1191 1192 // Update the previous location in the active path. 1193 PrevLoc = Call->getLocation(); 1194 1195 if (!C.CallStack.empty()) { 1196 assert(C.CallStack.back().first == Call); 1197 C.CallStack.pop_back(); 1198 } 1199 return; 1200 } 1201 1202 assert(C.getCurrLocationContext() == C.getLocationContextForActivePath() && 1203 "The current position in the bug path is out of sync with the " 1204 "location context associated with the active path!"); 1205 1206 // Have we encountered an exit from a function call? 1207 if (std::optional<CallExitEnd> CE = P.getAs<CallExitEnd>()) { 1208 1209 // We are descending into a call (backwards). Construct 1210 // a new call piece to contain the path pieces for that call. 1211 auto Call = PathDiagnosticCallPiece::construct(*CE, SM); 1212 // Record the mapping from call piece to LocationContext. 1213 assert(!C.isInLocCtxMap(&Call->path) && 1214 "We just entered a call, this must've been the first time we " 1215 "encounter its context!"); 1216 C.updateLocCtxMap(&Call->path, CE->getCalleeContext()); 1217 1218 if (C.shouldAddPathEdges()) { 1219 // Add the edge to the return site. 1220 addEdgeToPath(C.getActivePath(), PrevLoc, Call->callReturn); 1221 PrevLoc.invalidate(); 1222 } 1223 1224 auto *P = Call.get(); 1225 C.getActivePath().push_front(std::move(Call)); 1226 1227 // Make the contents of the call the active path for now. 1228 C.PD->pushActivePath(&P->path); 1229 C.CallStack.push_back(CallWithEntry(P, C.getCurrentNode())); 1230 return; 1231 } 1232 1233 if (auto PS = P.getAs<PostStmt>()) { 1234 if (!C.shouldAddPathEdges()) 1235 return; 1236 1237 // Add an edge. If this is an ObjCForCollectionStmt do 1238 // not add an edge here as it appears in the CFG both 1239 // as a terminator and as a terminator condition. 1240 if (!isa<ObjCForCollectionStmt>(PS->getStmt())) { 1241 PathDiagnosticLocation L = 1242 PathDiagnosticLocation(PS->getStmt(), SM, C.getCurrLocationContext()); 1243 addEdgeToPath(C.getActivePath(), PrevLoc, L); 1244 } 1245 1246 } else if (auto BE = P.getAs<BlockEdge>()) { 1247 1248 if (C.shouldAddControlNotes()) { 1249 generateMinimalDiagForBlockEdge(C, *BE); 1250 } 1251 1252 if (!C.shouldAddPathEdges()) { 1253 return; 1254 } 1255 1256 // Are we jumping to the head of a loop? Add a special diagnostic. 1257 if (const Stmt *Loop = BE->getSrc()->getLoopTarget()) { 1258 PathDiagnosticLocation L(Loop, SM, C.getCurrLocationContext()); 1259 const Stmt *Body = nullptr; 1260 1261 if (const auto *FS = dyn_cast<ForStmt>(Loop)) 1262 Body = FS->getBody(); 1263 else if (const auto *WS = dyn_cast<WhileStmt>(Loop)) 1264 Body = WS->getBody(); 1265 else if (const auto *OFS = dyn_cast<ObjCForCollectionStmt>(Loop)) { 1266 Body = OFS->getBody(); 1267 } else if (const auto *FRS = dyn_cast<CXXForRangeStmt>(Loop)) { 1268 Body = FRS->getBody(); 1269 } 1270 // do-while statements are explicitly excluded here 1271 1272 auto p = std::make_shared<PathDiagnosticEventPiece>( 1273 L, "Looping back to the head of the loop"); 1274 p->setPrunable(true); 1275 1276 addEdgeToPath(C.getActivePath(), PrevLoc, p->getLocation()); 1277 // We might've added a very similar control node already 1278 if (!C.shouldAddControlNotes()) { 1279 C.getActivePath().push_front(std::move(p)); 1280 } 1281 1282 if (const auto *CS = dyn_cast_or_null<CompoundStmt>(Body)) { 1283 addEdgeToPath(C.getActivePath(), PrevLoc, 1284 PathDiagnosticLocation::createEndBrace(CS, SM)); 1285 } 1286 } 1287 1288 const CFGBlock *BSrc = BE->getSrc(); 1289 const ParentMap &PM = C.getParentMap(); 1290 1291 if (const Stmt *Term = BSrc->getTerminatorStmt()) { 1292 // Are we jumping past the loop body without ever executing the 1293 // loop (because the condition was false)? 1294 if (isLoop(Term)) { 1295 const Stmt *TermCond = getTerminatorCondition(BSrc); 1296 bool IsInLoopBody = isInLoopBody( 1297 PM, getStmtBeforeCond(PM, TermCond, C.getCurrentNode()), Term); 1298 1299 StringRef str; 1300 1301 if (isJumpToFalseBranch(&*BE)) { 1302 if (!IsInLoopBody) { 1303 if (isa<ObjCForCollectionStmt>(Term)) { 1304 str = StrLoopCollectionEmpty; 1305 } else if (isa<CXXForRangeStmt>(Term)) { 1306 str = StrLoopRangeEmpty; 1307 } else { 1308 str = StrLoopBodyZero; 1309 } 1310 } 1311 } else { 1312 str = StrEnteringLoop; 1313 } 1314 1315 if (!str.empty()) { 1316 PathDiagnosticLocation L(TermCond ? TermCond : Term, SM, 1317 C.getCurrLocationContext()); 1318 auto PE = std::make_shared<PathDiagnosticEventPiece>(L, str); 1319 PE->setPrunable(true); 1320 addEdgeToPath(C.getActivePath(), PrevLoc, PE->getLocation()); 1321 1322 // We might've added a very similar control node already 1323 if (!C.shouldAddControlNotes()) { 1324 C.getActivePath().push_front(std::move(PE)); 1325 } 1326 } 1327 } else if (isa<BreakStmt, ContinueStmt, GotoStmt>(Term)) { 1328 PathDiagnosticLocation L(Term, SM, C.getCurrLocationContext()); 1329 addEdgeToPath(C.getActivePath(), PrevLoc, L); 1330 } 1331 } 1332 } 1333 } 1334 1335 static std::unique_ptr<PathDiagnostic> 1336 generateDiagnosticForBasicReport(const BasicBugReport *R, 1337 const Decl *AnalysisEntryPoint) { 1338 const BugType &BT = R->getBugType(); 1339 return std::make_unique<PathDiagnostic>( 1340 BT.getCheckerName(), R->getDeclWithIssue(), BT.getDescription(), 1341 R->getDescription(), R->getShortDescription(/*UseFallback=*/false), 1342 BT.getCategory(), R->getUniqueingLocation(), R->getUniqueingDecl(), 1343 AnalysisEntryPoint, std::make_unique<FilesToLineNumsMap>()); 1344 } 1345 1346 static std::unique_ptr<PathDiagnostic> 1347 generateEmptyDiagnosticForReport(const PathSensitiveBugReport *R, 1348 const SourceManager &SM, 1349 const Decl *AnalysisEntryPoint) { 1350 const BugType &BT = R->getBugType(); 1351 return std::make_unique<PathDiagnostic>( 1352 BT.getCheckerName(), R->getDeclWithIssue(), BT.getDescription(), 1353 R->getDescription(), R->getShortDescription(/*UseFallback=*/false), 1354 BT.getCategory(), R->getUniqueingLocation(), R->getUniqueingDecl(), 1355 AnalysisEntryPoint, findExecutedLines(SM, R->getErrorNode())); 1356 } 1357 1358 static const Stmt *getStmtParent(const Stmt *S, const ParentMap &PM) { 1359 if (!S) 1360 return nullptr; 1361 1362 while (true) { 1363 S = PM.getParentIgnoreParens(S); 1364 1365 if (!S) 1366 break; 1367 1368 if (isa<FullExpr, CXXBindTemporaryExpr, SubstNonTypeTemplateParmExpr>(S)) 1369 continue; 1370 1371 break; 1372 } 1373 1374 return S; 1375 } 1376 1377 static bool isConditionForTerminator(const Stmt *S, const Stmt *Cond) { 1378 switch (S->getStmtClass()) { 1379 case Stmt::BinaryOperatorClass: { 1380 const auto *BO = cast<BinaryOperator>(S); 1381 if (!BO->isLogicalOp()) 1382 return false; 1383 return BO->getLHS() == Cond || BO->getRHS() == Cond; 1384 } 1385 case Stmt::IfStmtClass: 1386 return cast<IfStmt>(S)->getCond() == Cond; 1387 case Stmt::ForStmtClass: 1388 return cast<ForStmt>(S)->getCond() == Cond; 1389 case Stmt::WhileStmtClass: 1390 return cast<WhileStmt>(S)->getCond() == Cond; 1391 case Stmt::DoStmtClass: 1392 return cast<DoStmt>(S)->getCond() == Cond; 1393 case Stmt::ChooseExprClass: 1394 return cast<ChooseExpr>(S)->getCond() == Cond; 1395 case Stmt::IndirectGotoStmtClass: 1396 return cast<IndirectGotoStmt>(S)->getTarget() == Cond; 1397 case Stmt::SwitchStmtClass: 1398 return cast<SwitchStmt>(S)->getCond() == Cond; 1399 case Stmt::BinaryConditionalOperatorClass: 1400 return cast<BinaryConditionalOperator>(S)->getCond() == Cond; 1401 case Stmt::ConditionalOperatorClass: { 1402 const auto *CO = cast<ConditionalOperator>(S); 1403 return CO->getCond() == Cond || 1404 CO->getLHS() == Cond || 1405 CO->getRHS() == Cond; 1406 } 1407 case Stmt::ObjCForCollectionStmtClass: 1408 return cast<ObjCForCollectionStmt>(S)->getElement() == Cond; 1409 case Stmt::CXXForRangeStmtClass: { 1410 const auto *FRS = cast<CXXForRangeStmt>(S); 1411 return FRS->getCond() == Cond || FRS->getRangeInit() == Cond; 1412 } 1413 default: 1414 return false; 1415 } 1416 } 1417 1418 static bool isIncrementOrInitInForLoop(const Stmt *S, const Stmt *FL) { 1419 if (const auto *FS = dyn_cast<ForStmt>(FL)) 1420 return FS->getInc() == S || FS->getInit() == S; 1421 if (const auto *FRS = dyn_cast<CXXForRangeStmt>(FL)) 1422 return FRS->getInc() == S || FRS->getRangeStmt() == S || 1423 FRS->getLoopVarStmt() || FRS->getRangeInit() == S; 1424 return false; 1425 } 1426 1427 using OptimizedCallsSet = llvm::DenseSet<const PathDiagnosticCallPiece *>; 1428 1429 /// Adds synthetic edges from top-level statements to their subexpressions. 1430 /// 1431 /// This avoids a "swoosh" effect, where an edge from a top-level statement A 1432 /// points to a sub-expression B.1 that's not at the start of B. In these cases, 1433 /// we'd like to see an edge from A to B, then another one from B to B.1. 1434 static void addContextEdges(PathPieces &pieces, const LocationContext *LC) { 1435 const ParentMap &PM = LC->getParentMap(); 1436 PathPieces::iterator Prev = pieces.end(); 1437 for (PathPieces::iterator I = pieces.begin(), E = Prev; I != E; 1438 Prev = I, ++I) { 1439 auto *Piece = dyn_cast<PathDiagnosticControlFlowPiece>(I->get()); 1440 1441 if (!Piece) 1442 continue; 1443 1444 PathDiagnosticLocation SrcLoc = Piece->getStartLocation(); 1445 SmallVector<PathDiagnosticLocation, 4> SrcContexts; 1446 1447 PathDiagnosticLocation NextSrcContext = SrcLoc; 1448 const Stmt *InnerStmt = nullptr; 1449 while (NextSrcContext.isValid() && NextSrcContext.asStmt() != InnerStmt) { 1450 SrcContexts.push_back(NextSrcContext); 1451 InnerStmt = NextSrcContext.asStmt(); 1452 NextSrcContext = getEnclosingStmtLocation(InnerStmt, LC, 1453 /*allowNested=*/true); 1454 } 1455 1456 // Repeatedly split the edge as necessary. 1457 // This is important for nested logical expressions (||, &&, ?:) where we 1458 // want to show all the levels of context. 1459 while (true) { 1460 const Stmt *Dst = Piece->getEndLocation().getStmtOrNull(); 1461 1462 // We are looking at an edge. Is the destination within a larger 1463 // expression? 1464 PathDiagnosticLocation DstContext = 1465 getEnclosingStmtLocation(Dst, LC, /*allowNested=*/true); 1466 if (!DstContext.isValid() || DstContext.asStmt() == Dst) 1467 break; 1468 1469 // If the source is in the same context, we're already good. 1470 if (llvm::is_contained(SrcContexts, DstContext)) 1471 break; 1472 1473 // Update the subexpression node to point to the context edge. 1474 Piece->setStartLocation(DstContext); 1475 1476 // Try to extend the previous edge if it's at the same level as the source 1477 // context. 1478 if (Prev != E) { 1479 auto *PrevPiece = dyn_cast<PathDiagnosticControlFlowPiece>(Prev->get()); 1480 1481 if (PrevPiece) { 1482 if (const Stmt *PrevSrc = 1483 PrevPiece->getStartLocation().getStmtOrNull()) { 1484 const Stmt *PrevSrcParent = getStmtParent(PrevSrc, PM); 1485 if (PrevSrcParent == 1486 getStmtParent(DstContext.getStmtOrNull(), PM)) { 1487 PrevPiece->setEndLocation(DstContext); 1488 break; 1489 } 1490 } 1491 } 1492 } 1493 1494 // Otherwise, split the current edge into a context edge and a 1495 // subexpression edge. Note that the context statement may itself have 1496 // context. 1497 auto P = 1498 std::make_shared<PathDiagnosticControlFlowPiece>(SrcLoc, DstContext); 1499 Piece = P.get(); 1500 I = pieces.insert(I, std::move(P)); 1501 } 1502 } 1503 } 1504 1505 /// Move edges from a branch condition to a branch target 1506 /// when the condition is simple. 1507 /// 1508 /// This restructures some of the work of addContextEdges. That function 1509 /// creates edges this may destroy, but they work together to create a more 1510 /// aesthetically set of edges around branches. After the call to 1511 /// addContextEdges, we may have (1) an edge to the branch, (2) an edge from 1512 /// the branch to the branch condition, and (3) an edge from the branch 1513 /// condition to the branch target. We keep (1), but may wish to remove (2) 1514 /// and move the source of (3) to the branch if the branch condition is simple. 1515 static void simplifySimpleBranches(PathPieces &pieces) { 1516 for (PathPieces::iterator I = pieces.begin(), E = pieces.end(); I != E; ++I) { 1517 const auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get()); 1518 1519 if (!PieceI) 1520 continue; 1521 1522 const Stmt *s1Start = PieceI->getStartLocation().getStmtOrNull(); 1523 const Stmt *s1End = PieceI->getEndLocation().getStmtOrNull(); 1524 1525 if (!s1Start || !s1End) 1526 continue; 1527 1528 PathPieces::iterator NextI = I; ++NextI; 1529 if (NextI == E) 1530 break; 1531 1532 PathDiagnosticControlFlowPiece *PieceNextI = nullptr; 1533 1534 while (true) { 1535 if (NextI == E) 1536 break; 1537 1538 const auto *EV = dyn_cast<PathDiagnosticEventPiece>(NextI->get()); 1539 if (EV) { 1540 StringRef S = EV->getString(); 1541 if (S == StrEnteringLoop || S == StrLoopBodyZero || 1542 S == StrLoopCollectionEmpty || S == StrLoopRangeEmpty) { 1543 ++NextI; 1544 continue; 1545 } 1546 break; 1547 } 1548 1549 PieceNextI = dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get()); 1550 break; 1551 } 1552 1553 if (!PieceNextI) 1554 continue; 1555 1556 const Stmt *s2Start = PieceNextI->getStartLocation().getStmtOrNull(); 1557 const Stmt *s2End = PieceNextI->getEndLocation().getStmtOrNull(); 1558 1559 if (!s2Start || !s2End || s1End != s2Start) 1560 continue; 1561 1562 // We only perform this transformation for specific branch kinds. 1563 // We don't want to do this for do..while, for example. 1564 if (!isa<ForStmt, WhileStmt, IfStmt, ObjCForCollectionStmt, 1565 CXXForRangeStmt>(s1Start)) 1566 continue; 1567 1568 // Is s1End the branch condition? 1569 if (!isConditionForTerminator(s1Start, s1End)) 1570 continue; 1571 1572 // Perform the hoisting by eliminating (2) and changing the start 1573 // location of (3). 1574 PieceNextI->setStartLocation(PieceI->getStartLocation()); 1575 I = pieces.erase(I); 1576 } 1577 } 1578 1579 /// Returns the number of bytes in the given (character-based) SourceRange. 1580 /// 1581 /// If the locations in the range are not on the same line, returns 1582 /// std::nullopt. 1583 /// 1584 /// Note that this does not do a precise user-visible character or column count. 1585 static std::optional<size_t> getLengthOnSingleLine(const SourceManager &SM, 1586 SourceRange Range) { 1587 SourceRange ExpansionRange(SM.getExpansionLoc(Range.getBegin()), 1588 SM.getExpansionRange(Range.getEnd()).getEnd()); 1589 1590 FileID FID = SM.getFileID(ExpansionRange.getBegin()); 1591 if (FID != SM.getFileID(ExpansionRange.getEnd())) 1592 return std::nullopt; 1593 1594 std::optional<MemoryBufferRef> Buffer = SM.getBufferOrNone(FID); 1595 if (!Buffer) 1596 return std::nullopt; 1597 1598 unsigned BeginOffset = SM.getFileOffset(ExpansionRange.getBegin()); 1599 unsigned EndOffset = SM.getFileOffset(ExpansionRange.getEnd()); 1600 StringRef Snippet = Buffer->getBuffer().slice(BeginOffset, EndOffset); 1601 1602 // We're searching the raw bytes of the buffer here, which might include 1603 // escaped newlines and such. That's okay; we're trying to decide whether the 1604 // SourceRange is covering a large or small amount of space in the user's 1605 // editor. 1606 if (Snippet.find_first_of("\r\n") != StringRef::npos) 1607 return std::nullopt; 1608 1609 // This isn't Unicode-aware, but it doesn't need to be. 1610 return Snippet.size(); 1611 } 1612 1613 /// \sa getLengthOnSingleLine(SourceManager, SourceRange) 1614 static std::optional<size_t> getLengthOnSingleLine(const SourceManager &SM, 1615 const Stmt *S) { 1616 return getLengthOnSingleLine(SM, S->getSourceRange()); 1617 } 1618 1619 /// Eliminate two-edge cycles created by addContextEdges(). 1620 /// 1621 /// Once all the context edges are in place, there are plenty of cases where 1622 /// there's a single edge from a top-level statement to a subexpression, 1623 /// followed by a single path note, and then a reverse edge to get back out to 1624 /// the top level. If the statement is simple enough, the subexpression edges 1625 /// just add noise and make it harder to understand what's going on. 1626 /// 1627 /// This function only removes edges in pairs, because removing only one edge 1628 /// might leave other edges dangling. 1629 /// 1630 /// This will not remove edges in more complicated situations: 1631 /// - if there is more than one "hop" leading to or from a subexpression. 1632 /// - if there is an inlined call between the edges instead of a single event. 1633 /// - if the whole statement is large enough that having subexpression arrows 1634 /// might be helpful. 1635 static void removeContextCycles(PathPieces &Path, const SourceManager &SM) { 1636 for (PathPieces::iterator I = Path.begin(), E = Path.end(); I != E; ) { 1637 // Pattern match the current piece and its successor. 1638 const auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get()); 1639 1640 if (!PieceI) { 1641 ++I; 1642 continue; 1643 } 1644 1645 const Stmt *s1Start = PieceI->getStartLocation().getStmtOrNull(); 1646 const Stmt *s1End = PieceI->getEndLocation().getStmtOrNull(); 1647 1648 PathPieces::iterator NextI = I; ++NextI; 1649 if (NextI == E) 1650 break; 1651 1652 const auto *PieceNextI = 1653 dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get()); 1654 1655 if (!PieceNextI) { 1656 if (isa<PathDiagnosticEventPiece>(NextI->get())) { 1657 ++NextI; 1658 if (NextI == E) 1659 break; 1660 PieceNextI = dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get()); 1661 } 1662 1663 if (!PieceNextI) { 1664 ++I; 1665 continue; 1666 } 1667 } 1668 1669 const Stmt *s2Start = PieceNextI->getStartLocation().getStmtOrNull(); 1670 const Stmt *s2End = PieceNextI->getEndLocation().getStmtOrNull(); 1671 1672 if (s1Start && s2Start && s1Start == s2End && s2Start == s1End) { 1673 const size_t MAX_SHORT_LINE_LENGTH = 80; 1674 std::optional<size_t> s1Length = getLengthOnSingleLine(SM, s1Start); 1675 if (s1Length && *s1Length <= MAX_SHORT_LINE_LENGTH) { 1676 std::optional<size_t> s2Length = getLengthOnSingleLine(SM, s2Start); 1677 if (s2Length && *s2Length <= MAX_SHORT_LINE_LENGTH) { 1678 Path.erase(I); 1679 I = Path.erase(NextI); 1680 continue; 1681 } 1682 } 1683 } 1684 1685 ++I; 1686 } 1687 } 1688 1689 /// Return true if X is contained by Y. 1690 static bool lexicalContains(const ParentMap &PM, const Stmt *X, const Stmt *Y) { 1691 while (X) { 1692 if (X == Y) 1693 return true; 1694 X = PM.getParent(X); 1695 } 1696 return false; 1697 } 1698 1699 // Remove short edges on the same line less than 3 columns in difference. 1700 static void removePunyEdges(PathPieces &path, const SourceManager &SM, 1701 const ParentMap &PM) { 1702 bool erased = false; 1703 1704 for (PathPieces::iterator I = path.begin(), E = path.end(); I != E; 1705 erased ? I : ++I) { 1706 erased = false; 1707 1708 const auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get()); 1709 1710 if (!PieceI) 1711 continue; 1712 1713 const Stmt *start = PieceI->getStartLocation().getStmtOrNull(); 1714 const Stmt *end = PieceI->getEndLocation().getStmtOrNull(); 1715 1716 if (!start || !end) 1717 continue; 1718 1719 const Stmt *endParent = PM.getParent(end); 1720 if (!endParent) 1721 continue; 1722 1723 if (isConditionForTerminator(end, endParent)) 1724 continue; 1725 1726 SourceLocation FirstLoc = start->getBeginLoc(); 1727 SourceLocation SecondLoc = end->getBeginLoc(); 1728 1729 if (!SM.isWrittenInSameFile(FirstLoc, SecondLoc)) 1730 continue; 1731 if (SM.isBeforeInTranslationUnit(SecondLoc, FirstLoc)) 1732 std::swap(SecondLoc, FirstLoc); 1733 1734 SourceRange EdgeRange(FirstLoc, SecondLoc); 1735 std::optional<size_t> ByteWidth = getLengthOnSingleLine(SM, EdgeRange); 1736 1737 // If the statements are on different lines, continue. 1738 if (!ByteWidth) 1739 continue; 1740 1741 const size_t MAX_PUNY_EDGE_LENGTH = 2; 1742 if (*ByteWidth <= MAX_PUNY_EDGE_LENGTH) { 1743 // FIXME: There are enough /bytes/ between the endpoints of the edge, but 1744 // there might not be enough /columns/. A proper user-visible column count 1745 // is probably too expensive, though. 1746 I = path.erase(I); 1747 erased = true; 1748 continue; 1749 } 1750 } 1751 } 1752 1753 static void removeIdenticalEvents(PathPieces &path) { 1754 for (PathPieces::iterator I = path.begin(), E = path.end(); I != E; ++I) { 1755 const auto *PieceI = dyn_cast<PathDiagnosticEventPiece>(I->get()); 1756 1757 if (!PieceI) 1758 continue; 1759 1760 PathPieces::iterator NextI = I; ++NextI; 1761 if (NextI == E) 1762 return; 1763 1764 const auto *PieceNextI = dyn_cast<PathDiagnosticEventPiece>(NextI->get()); 1765 1766 if (!PieceNextI) 1767 continue; 1768 1769 // Erase the second piece if it has the same exact message text. 1770 if (PieceI->getString() == PieceNextI->getString()) { 1771 path.erase(NextI); 1772 } 1773 } 1774 } 1775 1776 static bool optimizeEdges(const PathDiagnosticConstruct &C, PathPieces &path, 1777 OptimizedCallsSet &OCS) { 1778 bool hasChanges = false; 1779 const LocationContext *LC = C.getLocationContextFor(&path); 1780 assert(LC); 1781 const ParentMap &PM = LC->getParentMap(); 1782 const SourceManager &SM = C.getSourceManager(); 1783 1784 for (PathPieces::iterator I = path.begin(), E = path.end(); I != E; ) { 1785 // Optimize subpaths. 1786 if (auto *CallI = dyn_cast<PathDiagnosticCallPiece>(I->get())) { 1787 // Record the fact that a call has been optimized so we only do the 1788 // effort once. 1789 if (!OCS.count(CallI)) { 1790 while (optimizeEdges(C, CallI->path, OCS)) { 1791 } 1792 OCS.insert(CallI); 1793 } 1794 ++I; 1795 continue; 1796 } 1797 1798 // Pattern match the current piece and its successor. 1799 auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get()); 1800 1801 if (!PieceI) { 1802 ++I; 1803 continue; 1804 } 1805 1806 const Stmt *s1Start = PieceI->getStartLocation().getStmtOrNull(); 1807 const Stmt *s1End = PieceI->getEndLocation().getStmtOrNull(); 1808 const Stmt *level1 = getStmtParent(s1Start, PM); 1809 const Stmt *level2 = getStmtParent(s1End, PM); 1810 1811 PathPieces::iterator NextI = I; ++NextI; 1812 if (NextI == E) 1813 break; 1814 1815 const auto *PieceNextI = dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get()); 1816 1817 if (!PieceNextI) { 1818 ++I; 1819 continue; 1820 } 1821 1822 const Stmt *s2Start = PieceNextI->getStartLocation().getStmtOrNull(); 1823 const Stmt *s2End = PieceNextI->getEndLocation().getStmtOrNull(); 1824 const Stmt *level3 = getStmtParent(s2Start, PM); 1825 const Stmt *level4 = getStmtParent(s2End, PM); 1826 1827 // Rule I. 1828 // 1829 // If we have two consecutive control edges whose end/begin locations 1830 // are at the same level (e.g. statements or top-level expressions within 1831 // a compound statement, or siblings share a single ancestor expression), 1832 // then merge them if they have no interesting intermediate event. 1833 // 1834 // For example: 1835 // 1836 // (1.1 -> 1.2) -> (1.2 -> 1.3) becomes (1.1 -> 1.3) because the common 1837 // parent is '1'. Here 'x.y.z' represents the hierarchy of statements. 1838 // 1839 // NOTE: this will be limited later in cases where we add barriers 1840 // to prevent this optimization. 1841 if (level1 && level1 == level2 && level1 == level3 && level1 == level4) { 1842 PieceI->setEndLocation(PieceNextI->getEndLocation()); 1843 path.erase(NextI); 1844 hasChanges = true; 1845 continue; 1846 } 1847 1848 // Rule II. 1849 // 1850 // Eliminate edges between subexpressions and parent expressions 1851 // when the subexpression is consumed. 1852 // 1853 // NOTE: this will be limited later in cases where we add barriers 1854 // to prevent this optimization. 1855 if (s1End && s1End == s2Start && level2) { 1856 bool removeEdge = false; 1857 // Remove edges into the increment or initialization of a 1858 // loop that have no interleaving event. This means that 1859 // they aren't interesting. 1860 if (isIncrementOrInitInForLoop(s1End, level2)) 1861 removeEdge = true; 1862 // Next only consider edges that are not anchored on 1863 // the condition of a terminator. This are intermediate edges 1864 // that we might want to trim. 1865 else if (!isConditionForTerminator(level2, s1End)) { 1866 // Trim edges on expressions that are consumed by 1867 // the parent expression. 1868 if (isa<Expr>(s1End) && PM.isConsumedExpr(cast<Expr>(s1End))) { 1869 removeEdge = true; 1870 } 1871 // Trim edges where a lexical containment doesn't exist. 1872 // For example: 1873 // 1874 // X -> Y -> Z 1875 // 1876 // If 'Z' lexically contains Y (it is an ancestor) and 1877 // 'X' does not lexically contain Y (it is a descendant OR 1878 // it has no lexical relationship at all) then trim. 1879 // 1880 // This can eliminate edges where we dive into a subexpression 1881 // and then pop back out, etc. 1882 else if (s1Start && s2End && 1883 lexicalContains(PM, s2Start, s2End) && 1884 !lexicalContains(PM, s1End, s1Start)) { 1885 removeEdge = true; 1886 } 1887 // Trim edges from a subexpression back to the top level if the 1888 // subexpression is on a different line. 1889 // 1890 // A.1 -> A -> B 1891 // becomes 1892 // A.1 -> B 1893 // 1894 // These edges just look ugly and don't usually add anything. 1895 else if (s1Start && s2End && 1896 lexicalContains(PM, s1Start, s1End)) { 1897 SourceRange EdgeRange(PieceI->getEndLocation().asLocation(), 1898 PieceI->getStartLocation().asLocation()); 1899 if (!getLengthOnSingleLine(SM, EdgeRange)) 1900 removeEdge = true; 1901 } 1902 } 1903 1904 if (removeEdge) { 1905 PieceI->setEndLocation(PieceNextI->getEndLocation()); 1906 path.erase(NextI); 1907 hasChanges = true; 1908 continue; 1909 } 1910 } 1911 1912 // Optimize edges for ObjC fast-enumeration loops. 1913 // 1914 // (X -> collection) -> (collection -> element) 1915 // 1916 // becomes: 1917 // 1918 // (X -> element) 1919 if (s1End == s2Start) { 1920 const auto *FS = dyn_cast_or_null<ObjCForCollectionStmt>(level3); 1921 if (FS && FS->getCollection()->IgnoreParens() == s2Start && 1922 s2End == FS->getElement()) { 1923 PieceI->setEndLocation(PieceNextI->getEndLocation()); 1924 path.erase(NextI); 1925 hasChanges = true; 1926 continue; 1927 } 1928 } 1929 1930 // No changes at this index? Move to the next one. 1931 ++I; 1932 } 1933 1934 if (!hasChanges) { 1935 // Adjust edges into subexpressions to make them more uniform 1936 // and aesthetically pleasing. 1937 addContextEdges(path, LC); 1938 // Remove "cyclical" edges that include one or more context edges. 1939 removeContextCycles(path, SM); 1940 // Hoist edges originating from branch conditions to branches 1941 // for simple branches. 1942 simplifySimpleBranches(path); 1943 // Remove any puny edges left over after primary optimization pass. 1944 removePunyEdges(path, SM, PM); 1945 // Remove identical events. 1946 removeIdenticalEvents(path); 1947 } 1948 1949 return hasChanges; 1950 } 1951 1952 /// Drop the very first edge in a path, which should be a function entry edge. 1953 /// 1954 /// If the first edge is not a function entry edge (say, because the first 1955 /// statement had an invalid source location), this function does nothing. 1956 // FIXME: We should just generate invalid edges anyway and have the optimizer 1957 // deal with them. 1958 static void dropFunctionEntryEdge(const PathDiagnosticConstruct &C, 1959 PathPieces &Path) { 1960 const auto *FirstEdge = 1961 dyn_cast<PathDiagnosticControlFlowPiece>(Path.front().get()); 1962 if (!FirstEdge) 1963 return; 1964 1965 const Decl *D = C.getLocationContextFor(&Path)->getDecl(); 1966 PathDiagnosticLocation EntryLoc = 1967 PathDiagnosticLocation::createBegin(D, C.getSourceManager()); 1968 if (FirstEdge->getStartLocation() != EntryLoc) 1969 return; 1970 1971 Path.pop_front(); 1972 } 1973 1974 /// Populate executes lines with lines containing at least one diagnostics. 1975 static void updateExecutedLinesWithDiagnosticPieces(PathDiagnostic &PD) { 1976 1977 PathPieces path = PD.path.flatten(/*ShouldFlattenMacros=*/true); 1978 FilesToLineNumsMap &ExecutedLines = PD.getExecutedLines(); 1979 1980 for (const auto &P : path) { 1981 FullSourceLoc Loc = P->getLocation().asLocation().getExpansionLoc(); 1982 FileID FID = Loc.getFileID(); 1983 unsigned LineNo = Loc.getLineNumber(); 1984 assert(FID.isValid()); 1985 ExecutedLines[FID].insert(LineNo); 1986 } 1987 } 1988 1989 PathDiagnosticConstruct::PathDiagnosticConstruct( 1990 const PathDiagnosticConsumer *PDC, const ExplodedNode *ErrorNode, 1991 const PathSensitiveBugReport *R, const Decl *AnalysisEntryPoint) 1992 : Consumer(PDC), CurrentNode(ErrorNode), 1993 SM(CurrentNode->getCodeDecl().getASTContext().getSourceManager()), 1994 PD(generateEmptyDiagnosticForReport(R, getSourceManager(), 1995 AnalysisEntryPoint)) { 1996 LCM[&PD->getActivePath()] = ErrorNode->getLocationContext(); 1997 } 1998 1999 PathDiagnosticBuilder::PathDiagnosticBuilder( 2000 BugReporterContext BRC, std::unique_ptr<ExplodedGraph> BugPath, 2001 PathSensitiveBugReport *r, const ExplodedNode *ErrorNode, 2002 std::unique_ptr<VisitorsDiagnosticsTy> VisitorsDiagnostics) 2003 : BugReporterContext(BRC), BugPath(std::move(BugPath)), R(r), 2004 ErrorNode(ErrorNode), 2005 VisitorsDiagnostics(std::move(VisitorsDiagnostics)) {} 2006 2007 std::unique_ptr<PathDiagnostic> 2008 PathDiagnosticBuilder::generate(const PathDiagnosticConsumer *PDC) const { 2009 const Decl *EntryPoint = getBugReporter().getAnalysisEntryPoint(); 2010 PathDiagnosticConstruct Construct(PDC, ErrorNode, R, EntryPoint); 2011 2012 const SourceManager &SM = getSourceManager(); 2013 const AnalyzerOptions &Opts = getAnalyzerOptions(); 2014 2015 if (!PDC->shouldGenerateDiagnostics()) 2016 return generateEmptyDiagnosticForReport(R, getSourceManager(), EntryPoint); 2017 2018 // Construct the final (warning) event for the bug report. 2019 auto EndNotes = VisitorsDiagnostics->find(ErrorNode); 2020 PathDiagnosticPieceRef LastPiece; 2021 if (EndNotes != VisitorsDiagnostics->end()) { 2022 assert(!EndNotes->second.empty()); 2023 LastPiece = EndNotes->second[0]; 2024 } else { 2025 LastPiece = BugReporterVisitor::getDefaultEndPath(*this, ErrorNode, 2026 *getBugReport()); 2027 } 2028 Construct.PD->setEndOfPath(LastPiece); 2029 2030 PathDiagnosticLocation PrevLoc = Construct.PD->getLocation(); 2031 // From the error node to the root, ascend the bug path and construct the bug 2032 // report. 2033 while (Construct.ascendToPrevNode()) { 2034 generatePathDiagnosticsForNode(Construct, PrevLoc); 2035 2036 auto VisitorNotes = VisitorsDiagnostics->find(Construct.getCurrentNode()); 2037 if (VisitorNotes == VisitorsDiagnostics->end()) 2038 continue; 2039 2040 // This is a workaround due to inability to put shared PathDiagnosticPiece 2041 // into a FoldingSet. 2042 std::set<llvm::FoldingSetNodeID> DeduplicationSet; 2043 2044 // Add pieces from custom visitors. 2045 for (const PathDiagnosticPieceRef &Note : VisitorNotes->second) { 2046 llvm::FoldingSetNodeID ID; 2047 Note->Profile(ID); 2048 if (!DeduplicationSet.insert(ID).second) 2049 continue; 2050 2051 if (PDC->shouldAddPathEdges()) 2052 addEdgeToPath(Construct.getActivePath(), PrevLoc, Note->getLocation()); 2053 updateStackPiecesWithMessage(Note, Construct.CallStack); 2054 Construct.getActivePath().push_front(Note); 2055 } 2056 } 2057 2058 if (PDC->shouldAddPathEdges()) { 2059 // Add an edge to the start of the function. 2060 // We'll prune it out later, but it helps make diagnostics more uniform. 2061 const StackFrameContext *CalleeLC = 2062 Construct.getLocationContextForActivePath()->getStackFrame(); 2063 const Decl *D = CalleeLC->getDecl(); 2064 addEdgeToPath(Construct.getActivePath(), PrevLoc, 2065 PathDiagnosticLocation::createBegin(D, SM)); 2066 } 2067 2068 2069 // Finally, prune the diagnostic path of uninteresting stuff. 2070 if (!Construct.PD->path.empty()) { 2071 if (R->shouldPrunePath() && Opts.ShouldPrunePaths) { 2072 bool stillHasNotes = 2073 removeUnneededCalls(Construct, Construct.getMutablePieces(), R); 2074 assert(stillHasNotes); 2075 (void)stillHasNotes; 2076 } 2077 2078 // Remove pop-up notes if needed. 2079 if (!Opts.ShouldAddPopUpNotes) 2080 removePopUpNotes(Construct.getMutablePieces()); 2081 2082 // Redirect all call pieces to have valid locations. 2083 adjustCallLocations(Construct.getMutablePieces()); 2084 removePiecesWithInvalidLocations(Construct.getMutablePieces()); 2085 2086 if (PDC->shouldAddPathEdges()) { 2087 2088 // Reduce the number of edges from a very conservative set 2089 // to an aesthetically pleasing subset that conveys the 2090 // necessary information. 2091 OptimizedCallsSet OCS; 2092 while (optimizeEdges(Construct, Construct.getMutablePieces(), OCS)) { 2093 } 2094 2095 // Drop the very first function-entry edge. It's not really necessary 2096 // for top-level functions. 2097 dropFunctionEntryEdge(Construct, Construct.getMutablePieces()); 2098 } 2099 2100 // Remove messages that are basically the same, and edges that may not 2101 // make sense. 2102 // We have to do this after edge optimization in the Extensive mode. 2103 removeRedundantMsgs(Construct.getMutablePieces()); 2104 removeEdgesToDefaultInitializers(Construct.getMutablePieces()); 2105 } 2106 2107 if (Opts.ShouldDisplayMacroExpansions) 2108 CompactMacroExpandedPieces(Construct.getMutablePieces(), SM); 2109 2110 return std::move(Construct.PD); 2111 } 2112 2113 //===----------------------------------------------------------------------===// 2114 // Methods for BugType and subclasses. 2115 //===----------------------------------------------------------------------===// 2116 2117 void BugType::anchor() {} 2118 2119 //===----------------------------------------------------------------------===// 2120 // Methods for BugReport and subclasses. 2121 //===----------------------------------------------------------------------===// 2122 2123 LLVM_ATTRIBUTE_USED static bool 2124 isDependency(const CheckerRegistryData &Registry, StringRef CheckerName) { 2125 for (const std::pair<StringRef, StringRef> &Pair : Registry.Dependencies) { 2126 if (Pair.second == CheckerName) 2127 return true; 2128 } 2129 return false; 2130 } 2131 2132 LLVM_ATTRIBUTE_USED static bool isHidden(const CheckerRegistryData &Registry, 2133 StringRef CheckerName) { 2134 for (const CheckerInfo &Checker : Registry.Checkers) { 2135 if (Checker.FullName == CheckerName) 2136 return Checker.IsHidden; 2137 } 2138 llvm_unreachable( 2139 "Checker name not found in CheckerRegistry -- did you retrieve it " 2140 "correctly from CheckerManager::getCurrentCheckerName?"); 2141 } 2142 2143 PathSensitiveBugReport::PathSensitiveBugReport( 2144 const BugType &bt, StringRef shortDesc, StringRef desc, 2145 const ExplodedNode *errorNode, PathDiagnosticLocation LocationToUnique, 2146 const Decl *DeclToUnique) 2147 : BugReport(Kind::PathSensitive, bt, shortDesc, desc), ErrorNode(errorNode), 2148 ErrorNodeRange(getStmt() ? getStmt()->getSourceRange() : SourceRange()), 2149 UniqueingLocation(LocationToUnique), UniqueingDecl(DeclToUnique) { 2150 assert(!isDependency(ErrorNode->getState() 2151 ->getAnalysisManager() 2152 .getCheckerManager() 2153 ->getCheckerRegistryData(), 2154 bt.getCheckerName()) && 2155 "Some checkers depend on this one! We don't allow dependency " 2156 "checkers to emit warnings, because checkers should depend on " 2157 "*modeling*, not *diagnostics*."); 2158 2159 assert((bt.getCheckerName().starts_with("debug") || 2160 !isHidden(ErrorNode->getState() 2161 ->getAnalysisManager() 2162 .getCheckerManager() 2163 ->getCheckerRegistryData(), 2164 bt.getCheckerName())) && 2165 "Hidden checkers musn't emit diagnostics as they are by definition " 2166 "non-user facing!"); 2167 } 2168 2169 void PathSensitiveBugReport::addVisitor( 2170 std::unique_ptr<BugReporterVisitor> visitor) { 2171 if (!visitor) 2172 return; 2173 2174 llvm::FoldingSetNodeID ID; 2175 visitor->Profile(ID); 2176 2177 void *InsertPos = nullptr; 2178 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) { 2179 return; 2180 } 2181 2182 Callbacks.push_back(std::move(visitor)); 2183 } 2184 2185 void PathSensitiveBugReport::clearVisitors() { 2186 Callbacks.clear(); 2187 } 2188 2189 const Decl *PathSensitiveBugReport::getDeclWithIssue() const { 2190 const ExplodedNode *N = getErrorNode(); 2191 if (!N) 2192 return nullptr; 2193 2194 const LocationContext *LC = N->getLocationContext(); 2195 return LC->getStackFrame()->getDecl(); 2196 } 2197 2198 void BasicBugReport::Profile(llvm::FoldingSetNodeID& hash) const { 2199 hash.AddInteger(static_cast<int>(getKind())); 2200 hash.AddPointer(&BT); 2201 hash.AddString(getShortDescription()); 2202 assert(Location.isValid()); 2203 Location.Profile(hash); 2204 2205 for (SourceRange range : Ranges) { 2206 if (!range.isValid()) 2207 continue; 2208 hash.Add(range.getBegin()); 2209 hash.Add(range.getEnd()); 2210 } 2211 } 2212 2213 void PathSensitiveBugReport::Profile(llvm::FoldingSetNodeID &hash) const { 2214 hash.AddInteger(static_cast<int>(getKind())); 2215 hash.AddPointer(&BT); 2216 hash.AddString(getShortDescription()); 2217 PathDiagnosticLocation UL = getUniqueingLocation(); 2218 if (UL.isValid()) { 2219 UL.Profile(hash); 2220 } else { 2221 // TODO: The statement may be null if the report was emitted before any 2222 // statements were executed. In particular, some checkers by design 2223 // occasionally emit their reports in empty functions (that have no 2224 // statements in their body). Do we profile correctly in this case? 2225 hash.AddPointer(ErrorNode->getCurrentOrPreviousStmtForDiagnostics()); 2226 } 2227 2228 for (SourceRange range : Ranges) { 2229 if (!range.isValid()) 2230 continue; 2231 hash.Add(range.getBegin()); 2232 hash.Add(range.getEnd()); 2233 } 2234 } 2235 2236 template <class T> 2237 static void insertToInterestingnessMap( 2238 llvm::DenseMap<T, bugreporter::TrackingKind> &InterestingnessMap, T Val, 2239 bugreporter::TrackingKind TKind) { 2240 auto Result = InterestingnessMap.insert({Val, TKind}); 2241 2242 if (Result.second) 2243 return; 2244 2245 // Even if this symbol/region was already marked as interesting as a 2246 // condition, if we later mark it as interesting again but with 2247 // thorough tracking, overwrite it. Entities marked with thorough 2248 // interestiness are the most important (or most interesting, if you will), 2249 // and we wouldn't like to downplay their importance. 2250 2251 switch (TKind) { 2252 case bugreporter::TrackingKind::Thorough: 2253 Result.first->getSecond() = bugreporter::TrackingKind::Thorough; 2254 return; 2255 case bugreporter::TrackingKind::Condition: 2256 return; 2257 } 2258 2259 llvm_unreachable( 2260 "BugReport::markInteresting currently can only handle 2 different " 2261 "tracking kinds! Please define what tracking kind should this entitiy" 2262 "have, if it was already marked as interesting with a different kind!"); 2263 } 2264 2265 void PathSensitiveBugReport::markInteresting(SymbolRef sym, 2266 bugreporter::TrackingKind TKind) { 2267 if (!sym) 2268 return; 2269 2270 insertToInterestingnessMap(InterestingSymbols, sym, TKind); 2271 2272 // FIXME: No tests exist for this code and it is questionable: 2273 // How to handle multiple metadata for the same region? 2274 if (const auto *meta = dyn_cast<SymbolMetadata>(sym)) 2275 markInteresting(meta->getRegion(), TKind); 2276 } 2277 2278 void PathSensitiveBugReport::markNotInteresting(SymbolRef sym) { 2279 if (!sym) 2280 return; 2281 InterestingSymbols.erase(sym); 2282 2283 // The metadata part of markInteresting is not reversed here. 2284 // Just making the same region not interesting is incorrect 2285 // in specific cases. 2286 if (const auto *meta = dyn_cast<SymbolMetadata>(sym)) 2287 markNotInteresting(meta->getRegion()); 2288 } 2289 2290 void PathSensitiveBugReport::markInteresting(const MemRegion *R, 2291 bugreporter::TrackingKind TKind) { 2292 if (!R) 2293 return; 2294 2295 R = R->getBaseRegion(); 2296 insertToInterestingnessMap(InterestingRegions, R, TKind); 2297 2298 if (const auto *SR = dyn_cast<SymbolicRegion>(R)) 2299 markInteresting(SR->getSymbol(), TKind); 2300 } 2301 2302 void PathSensitiveBugReport::markNotInteresting(const MemRegion *R) { 2303 if (!R) 2304 return; 2305 2306 R = R->getBaseRegion(); 2307 InterestingRegions.erase(R); 2308 2309 if (const auto *SR = dyn_cast<SymbolicRegion>(R)) 2310 markNotInteresting(SR->getSymbol()); 2311 } 2312 2313 void PathSensitiveBugReport::markInteresting(SVal V, 2314 bugreporter::TrackingKind TKind) { 2315 markInteresting(V.getAsRegion(), TKind); 2316 markInteresting(V.getAsSymbol(), TKind); 2317 } 2318 2319 void PathSensitiveBugReport::markInteresting(const LocationContext *LC) { 2320 if (!LC) 2321 return; 2322 InterestingLocationContexts.insert(LC); 2323 } 2324 2325 std::optional<bugreporter::TrackingKind> 2326 PathSensitiveBugReport::getInterestingnessKind(SVal V) const { 2327 auto RKind = getInterestingnessKind(V.getAsRegion()); 2328 auto SKind = getInterestingnessKind(V.getAsSymbol()); 2329 if (!RKind) 2330 return SKind; 2331 if (!SKind) 2332 return RKind; 2333 2334 // If either is marked with throrough tracking, return that, we wouldn't like 2335 // to downplay a note's importance by 'only' mentioning it as a condition. 2336 switch(*RKind) { 2337 case bugreporter::TrackingKind::Thorough: 2338 return RKind; 2339 case bugreporter::TrackingKind::Condition: 2340 return SKind; 2341 } 2342 2343 llvm_unreachable( 2344 "BugReport::getInterestingnessKind currently can only handle 2 different " 2345 "tracking kinds! Please define what tracking kind should we return here " 2346 "when the kind of getAsRegion() and getAsSymbol() is different!"); 2347 return std::nullopt; 2348 } 2349 2350 std::optional<bugreporter::TrackingKind> 2351 PathSensitiveBugReport::getInterestingnessKind(SymbolRef sym) const { 2352 if (!sym) 2353 return std::nullopt; 2354 // We don't currently consider metadata symbols to be interesting 2355 // even if we know their region is interesting. Is that correct behavior? 2356 auto It = InterestingSymbols.find(sym); 2357 if (It == InterestingSymbols.end()) 2358 return std::nullopt; 2359 return It->getSecond(); 2360 } 2361 2362 std::optional<bugreporter::TrackingKind> 2363 PathSensitiveBugReport::getInterestingnessKind(const MemRegion *R) const { 2364 if (!R) 2365 return std::nullopt; 2366 2367 R = R->getBaseRegion(); 2368 auto It = InterestingRegions.find(R); 2369 if (It != InterestingRegions.end()) 2370 return It->getSecond(); 2371 2372 if (const auto *SR = dyn_cast<SymbolicRegion>(R)) 2373 return getInterestingnessKind(SR->getSymbol()); 2374 return std::nullopt; 2375 } 2376 2377 bool PathSensitiveBugReport::isInteresting(SVal V) const { 2378 return getInterestingnessKind(V).has_value(); 2379 } 2380 2381 bool PathSensitiveBugReport::isInteresting(SymbolRef sym) const { 2382 return getInterestingnessKind(sym).has_value(); 2383 } 2384 2385 bool PathSensitiveBugReport::isInteresting(const MemRegion *R) const { 2386 return getInterestingnessKind(R).has_value(); 2387 } 2388 2389 bool PathSensitiveBugReport::isInteresting(const LocationContext *LC) const { 2390 if (!LC) 2391 return false; 2392 return InterestingLocationContexts.count(LC); 2393 } 2394 2395 const Stmt *PathSensitiveBugReport::getStmt() const { 2396 if (!ErrorNode) 2397 return nullptr; 2398 2399 ProgramPoint ProgP = ErrorNode->getLocation(); 2400 const Stmt *S = nullptr; 2401 2402 if (std::optional<BlockEntrance> BE = ProgP.getAs<BlockEntrance>()) { 2403 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit(); 2404 if (BE->getBlock() == &Exit) 2405 S = ErrorNode->getPreviousStmtForDiagnostics(); 2406 } 2407 if (!S) 2408 S = ErrorNode->getStmtForDiagnostics(); 2409 2410 return S; 2411 } 2412 2413 ArrayRef<SourceRange> 2414 PathSensitiveBugReport::getRanges() const { 2415 // If no custom ranges, add the range of the statement corresponding to 2416 // the error node. 2417 if (Ranges.empty() && isa_and_nonnull<Expr>(getStmt())) 2418 return ErrorNodeRange; 2419 2420 return Ranges; 2421 } 2422 2423 PathDiagnosticLocation 2424 PathSensitiveBugReport::getLocation() const { 2425 assert(ErrorNode && "Cannot create a location with a null node."); 2426 const Stmt *S = ErrorNode->getStmtForDiagnostics(); 2427 ProgramPoint P = ErrorNode->getLocation(); 2428 const LocationContext *LC = P.getLocationContext(); 2429 SourceManager &SM = 2430 ErrorNode->getState()->getStateManager().getContext().getSourceManager(); 2431 2432 if (!S) { 2433 // If this is an implicit call, return the implicit call point location. 2434 if (std::optional<PreImplicitCall> PIE = P.getAs<PreImplicitCall>()) 2435 return PathDiagnosticLocation(PIE->getLocation(), SM); 2436 if (auto FE = P.getAs<FunctionExitPoint>()) { 2437 if (const ReturnStmt *RS = FE->getStmt()) 2438 return PathDiagnosticLocation::createBegin(RS, SM, LC); 2439 } 2440 S = ErrorNode->getNextStmtForDiagnostics(); 2441 } 2442 2443 if (S) { 2444 // Attributed statements usually have corrupted begin locations, 2445 // it's OK to ignore attributes for our purposes and deal with 2446 // the actual annotated statement. 2447 if (const auto *AS = dyn_cast<AttributedStmt>(S)) 2448 S = AS->getSubStmt(); 2449 2450 // For member expressions, return the location of the '.' or '->'. 2451 if (const auto *ME = dyn_cast<MemberExpr>(S)) 2452 return PathDiagnosticLocation::createMemberLoc(ME, SM); 2453 2454 // For binary operators, return the location of the operator. 2455 if (const auto *B = dyn_cast<BinaryOperator>(S)) 2456 return PathDiagnosticLocation::createOperatorLoc(B, SM); 2457 2458 if (P.getAs<PostStmtPurgeDeadSymbols>()) 2459 return PathDiagnosticLocation::createEnd(S, SM, LC); 2460 2461 if (S->getBeginLoc().isValid()) 2462 return PathDiagnosticLocation(S, SM, LC); 2463 2464 return PathDiagnosticLocation( 2465 PathDiagnosticLocation::getValidSourceLocation(S, LC), SM); 2466 } 2467 2468 return PathDiagnosticLocation::createDeclEnd(ErrorNode->getLocationContext(), 2469 SM); 2470 } 2471 2472 //===----------------------------------------------------------------------===// 2473 // Methods for BugReporter and subclasses. 2474 //===----------------------------------------------------------------------===// 2475 2476 const ExplodedGraph &PathSensitiveBugReporter::getGraph() const { 2477 return Eng.getGraph(); 2478 } 2479 2480 ProgramStateManager &PathSensitiveBugReporter::getStateManager() const { 2481 return Eng.getStateManager(); 2482 } 2483 2484 BugReporter::BugReporter(BugReporterData &D) 2485 : D(D), UserSuppressions(D.getASTContext()) {} 2486 2487 BugReporter::~BugReporter() { 2488 // Make sure reports are flushed. 2489 assert(StrBugTypes.empty() && 2490 "Destroying BugReporter before diagnostics are emitted!"); 2491 2492 // Free the bug reports we are tracking. 2493 for (const auto I : EQClassesVector) 2494 delete I; 2495 } 2496 2497 void BugReporter::FlushReports() { 2498 // We need to flush reports in deterministic order to ensure the order 2499 // of the reports is consistent between runs. 2500 for (const auto EQ : EQClassesVector) 2501 FlushReport(*EQ); 2502 2503 // BugReporter owns and deletes only BugTypes created implicitly through 2504 // EmitBasicReport. 2505 // FIXME: There are leaks from checkers that assume that the BugTypes they 2506 // create will be destroyed by the BugReporter. 2507 StrBugTypes.clear(); 2508 } 2509 2510 //===----------------------------------------------------------------------===// 2511 // PathDiagnostics generation. 2512 //===----------------------------------------------------------------------===// 2513 2514 namespace { 2515 2516 /// A wrapper around an ExplodedGraph that contains a single path from the root 2517 /// to the error node. 2518 class BugPathInfo { 2519 public: 2520 std::unique_ptr<ExplodedGraph> BugPath; 2521 PathSensitiveBugReport *Report; 2522 const ExplodedNode *ErrorNode; 2523 }; 2524 2525 /// A wrapper around an ExplodedGraph whose leafs are all error nodes. Can 2526 /// conveniently retrieve bug paths from a single error node to the root. 2527 class BugPathGetter { 2528 std::unique_ptr<ExplodedGraph> TrimmedGraph; 2529 2530 using PriorityMapTy = llvm::DenseMap<const ExplodedNode *, unsigned>; 2531 2532 /// Assign each node with its distance from the root. 2533 PriorityMapTy PriorityMap; 2534 2535 /// Since the getErrorNode() or BugReport refers to the original ExplodedGraph, 2536 /// we need to pair it to the error node of the constructed trimmed graph. 2537 using ReportNewNodePair = 2538 std::pair<PathSensitiveBugReport *, const ExplodedNode *>; 2539 SmallVector<ReportNewNodePair, 32> ReportNodes; 2540 2541 BugPathInfo CurrentBugPath; 2542 2543 /// A helper class for sorting ExplodedNodes by priority. 2544 template <bool Descending> 2545 class PriorityCompare { 2546 const PriorityMapTy &PriorityMap; 2547 2548 public: 2549 PriorityCompare(const PriorityMapTy &M) : PriorityMap(M) {} 2550 2551 bool operator()(const ExplodedNode *LHS, const ExplodedNode *RHS) const { 2552 PriorityMapTy::const_iterator LI = PriorityMap.find(LHS); 2553 PriorityMapTy::const_iterator RI = PriorityMap.find(RHS); 2554 PriorityMapTy::const_iterator E = PriorityMap.end(); 2555 2556 if (LI == E) 2557 return Descending; 2558 if (RI == E) 2559 return !Descending; 2560 2561 return Descending ? LI->second > RI->second 2562 : LI->second < RI->second; 2563 } 2564 2565 bool operator()(const ReportNewNodePair &LHS, 2566 const ReportNewNodePair &RHS) const { 2567 return (*this)(LHS.second, RHS.second); 2568 } 2569 }; 2570 2571 public: 2572 BugPathGetter(const ExplodedGraph *OriginalGraph, 2573 ArrayRef<PathSensitiveBugReport *> &bugReports); 2574 2575 BugPathInfo *getNextBugPath(); 2576 }; 2577 2578 } // namespace 2579 2580 BugPathGetter::BugPathGetter(const ExplodedGraph *OriginalGraph, 2581 ArrayRef<PathSensitiveBugReport *> &bugReports) { 2582 SmallVector<const ExplodedNode *, 32> Nodes; 2583 for (const auto I : bugReports) { 2584 assert(I->isValid() && 2585 "We only allow BugReporterVisitors and BugReporter itself to " 2586 "invalidate reports!"); 2587 Nodes.emplace_back(I->getErrorNode()); 2588 } 2589 2590 // The trimmed graph is created in the body of the constructor to ensure 2591 // that the DenseMaps have been initialized already. 2592 InterExplodedGraphMap ForwardMap; 2593 TrimmedGraph = OriginalGraph->trim(Nodes, &ForwardMap); 2594 2595 // Find the (first) error node in the trimmed graph. We just need to consult 2596 // the node map which maps from nodes in the original graph to nodes 2597 // in the new graph. 2598 llvm::SmallPtrSet<const ExplodedNode *, 32> RemainingNodes; 2599 2600 for (PathSensitiveBugReport *Report : bugReports) { 2601 const ExplodedNode *NewNode = ForwardMap.lookup(Report->getErrorNode()); 2602 assert(NewNode && 2603 "Failed to construct a trimmed graph that contains this error " 2604 "node!"); 2605 ReportNodes.emplace_back(Report, NewNode); 2606 RemainingNodes.insert(NewNode); 2607 } 2608 2609 assert(!RemainingNodes.empty() && "No error node found in the trimmed graph"); 2610 2611 // Perform a forward BFS to find all the shortest paths. 2612 std::queue<const ExplodedNode *> WS; 2613 2614 assert(TrimmedGraph->num_roots() == 1); 2615 WS.push(*TrimmedGraph->roots_begin()); 2616 unsigned Priority = 0; 2617 2618 while (!WS.empty()) { 2619 const ExplodedNode *Node = WS.front(); 2620 WS.pop(); 2621 2622 PriorityMapTy::iterator PriorityEntry; 2623 bool IsNew; 2624 std::tie(PriorityEntry, IsNew) = PriorityMap.insert({Node, Priority}); 2625 ++Priority; 2626 2627 if (!IsNew) { 2628 assert(PriorityEntry->second <= Priority); 2629 continue; 2630 } 2631 2632 if (RemainingNodes.erase(Node)) 2633 if (RemainingNodes.empty()) 2634 break; 2635 2636 for (const ExplodedNode *Succ : Node->succs()) 2637 WS.push(Succ); 2638 } 2639 2640 // Sort the error paths from longest to shortest. 2641 llvm::sort(ReportNodes, PriorityCompare<true>(PriorityMap)); 2642 } 2643 2644 BugPathInfo *BugPathGetter::getNextBugPath() { 2645 if (ReportNodes.empty()) 2646 return nullptr; 2647 2648 const ExplodedNode *OrigN; 2649 std::tie(CurrentBugPath.Report, OrigN) = ReportNodes.pop_back_val(); 2650 assert(PriorityMap.contains(OrigN) && "error node not accessible from root"); 2651 2652 // Create a new graph with a single path. This is the graph that will be 2653 // returned to the caller. 2654 auto GNew = std::make_unique<ExplodedGraph>(); 2655 2656 // Now walk from the error node up the BFS path, always taking the 2657 // predeccessor with the lowest number. 2658 ExplodedNode *Succ = nullptr; 2659 while (true) { 2660 // Create the equivalent node in the new graph with the same state 2661 // and location. 2662 ExplodedNode *NewN = GNew->createUncachedNode( 2663 OrigN->getLocation(), OrigN->getState(), 2664 OrigN->getID(), OrigN->isSink()); 2665 2666 // Link up the new node with the previous node. 2667 if (Succ) 2668 Succ->addPredecessor(NewN, *GNew); 2669 else 2670 CurrentBugPath.ErrorNode = NewN; 2671 2672 Succ = NewN; 2673 2674 // Are we at the final node? 2675 if (OrigN->pred_empty()) { 2676 GNew->addRoot(NewN); 2677 break; 2678 } 2679 2680 // Find the next predeccessor node. We choose the node that is marked 2681 // with the lowest BFS number. 2682 OrigN = *std::min_element(OrigN->pred_begin(), OrigN->pred_end(), 2683 PriorityCompare<false>(PriorityMap)); 2684 } 2685 2686 CurrentBugPath.BugPath = std::move(GNew); 2687 2688 return &CurrentBugPath; 2689 } 2690 2691 /// CompactMacroExpandedPieces - This function postprocesses a PathDiagnostic 2692 /// object and collapses PathDiagosticPieces that are expanded by macros. 2693 static void CompactMacroExpandedPieces(PathPieces &path, 2694 const SourceManager& SM) { 2695 using MacroStackTy = std::vector< 2696 std::pair<std::shared_ptr<PathDiagnosticMacroPiece>, SourceLocation>>; 2697 2698 using PiecesTy = std::vector<PathDiagnosticPieceRef>; 2699 2700 MacroStackTy MacroStack; 2701 PiecesTy Pieces; 2702 2703 for (PathPieces::const_iterator I = path.begin(), E = path.end(); 2704 I != E; ++I) { 2705 const auto &piece = *I; 2706 2707 // Recursively compact calls. 2708 if (auto *call = dyn_cast<PathDiagnosticCallPiece>(&*piece)) { 2709 CompactMacroExpandedPieces(call->path, SM); 2710 } 2711 2712 // Get the location of the PathDiagnosticPiece. 2713 const FullSourceLoc Loc = piece->getLocation().asLocation(); 2714 2715 // Determine the instantiation location, which is the location we group 2716 // related PathDiagnosticPieces. 2717 SourceLocation InstantiationLoc = Loc.isMacroID() ? 2718 SM.getExpansionLoc(Loc) : 2719 SourceLocation(); 2720 2721 if (Loc.isFileID()) { 2722 MacroStack.clear(); 2723 Pieces.push_back(piece); 2724 continue; 2725 } 2726 2727 assert(Loc.isMacroID()); 2728 2729 // Is the PathDiagnosticPiece within the same macro group? 2730 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) { 2731 MacroStack.back().first->subPieces.push_back(piece); 2732 continue; 2733 } 2734 2735 // We aren't in the same group. Are we descending into a new macro 2736 // or are part of an old one? 2737 std::shared_ptr<PathDiagnosticMacroPiece> MacroGroup; 2738 2739 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ? 2740 SM.getExpansionLoc(Loc) : 2741 SourceLocation(); 2742 2743 // Walk the entire macro stack. 2744 while (!MacroStack.empty()) { 2745 if (InstantiationLoc == MacroStack.back().second) { 2746 MacroGroup = MacroStack.back().first; 2747 break; 2748 } 2749 2750 if (ParentInstantiationLoc == MacroStack.back().second) { 2751 MacroGroup = MacroStack.back().first; 2752 break; 2753 } 2754 2755 MacroStack.pop_back(); 2756 } 2757 2758 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) { 2759 // Create a new macro group and add it to the stack. 2760 auto NewGroup = std::make_shared<PathDiagnosticMacroPiece>( 2761 PathDiagnosticLocation::createSingleLocation(piece->getLocation())); 2762 2763 if (MacroGroup) 2764 MacroGroup->subPieces.push_back(NewGroup); 2765 else { 2766 assert(InstantiationLoc.isFileID()); 2767 Pieces.push_back(NewGroup); 2768 } 2769 2770 MacroGroup = NewGroup; 2771 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc)); 2772 } 2773 2774 // Finally, add the PathDiagnosticPiece to the group. 2775 MacroGroup->subPieces.push_back(piece); 2776 } 2777 2778 // Now take the pieces and construct a new PathDiagnostic. 2779 path.clear(); 2780 2781 path.insert(path.end(), Pieces.begin(), Pieces.end()); 2782 } 2783 2784 /// Generate notes from all visitors. 2785 /// Notes associated with @c ErrorNode are generated using 2786 /// @c getEndPath, and the rest are generated with @c VisitNode. 2787 static std::unique_ptr<VisitorsDiagnosticsTy> 2788 generateVisitorsDiagnostics(PathSensitiveBugReport *R, 2789 const ExplodedNode *ErrorNode, 2790 BugReporterContext &BRC) { 2791 std::unique_ptr<VisitorsDiagnosticsTy> Notes = 2792 std::make_unique<VisitorsDiagnosticsTy>(); 2793 PathSensitiveBugReport::VisitorList visitors; 2794 2795 // Run visitors on all nodes starting from the node *before* the last one. 2796 // The last node is reserved for notes generated with @c getEndPath. 2797 const ExplodedNode *NextNode = ErrorNode->getFirstPred(); 2798 while (NextNode) { 2799 2800 // At each iteration, move all visitors from report to visitor list. This is 2801 // important, because the Profile() functions of the visitors make sure that 2802 // a visitor isn't added multiple times for the same node, but it's fine 2803 // to add the a visitor with Profile() for different nodes (e.g. tracking 2804 // a region at different points of the symbolic execution). 2805 for (std::unique_ptr<BugReporterVisitor> &Visitor : R->visitors()) 2806 visitors.push_back(std::move(Visitor)); 2807 2808 R->clearVisitors(); 2809 2810 const ExplodedNode *Pred = NextNode->getFirstPred(); 2811 if (!Pred) { 2812 PathDiagnosticPieceRef LastPiece; 2813 for (auto &V : visitors) { 2814 V->finalizeVisitor(BRC, ErrorNode, *R); 2815 2816 if (auto Piece = V->getEndPath(BRC, ErrorNode, *R)) { 2817 assert(!LastPiece && 2818 "There can only be one final piece in a diagnostic."); 2819 assert(Piece->getKind() == PathDiagnosticPiece::Kind::Event && 2820 "The final piece must contain a message!"); 2821 LastPiece = std::move(Piece); 2822 (*Notes)[ErrorNode].push_back(LastPiece); 2823 } 2824 } 2825 break; 2826 } 2827 2828 for (auto &V : visitors) { 2829 auto P = V->VisitNode(NextNode, BRC, *R); 2830 if (P) 2831 (*Notes)[NextNode].push_back(std::move(P)); 2832 } 2833 2834 if (!R->isValid()) 2835 break; 2836 2837 NextNode = Pred; 2838 } 2839 2840 return Notes; 2841 } 2842 2843 std::optional<PathDiagnosticBuilder> PathDiagnosticBuilder::findValidReport( 2844 ArrayRef<PathSensitiveBugReport *> &bugReports, 2845 PathSensitiveBugReporter &Reporter) { 2846 Z3CrosscheckOracle Z3Oracle(Reporter.getAnalyzerOptions()); 2847 2848 BugPathGetter BugGraph(&Reporter.getGraph(), bugReports); 2849 2850 while (BugPathInfo *BugPath = BugGraph.getNextBugPath()) { 2851 // Find the BugReport with the original location. 2852 PathSensitiveBugReport *R = BugPath->Report; 2853 assert(R && "No original report found for sliced graph."); 2854 assert(R->isValid() && "Report selected by trimmed graph marked invalid."); 2855 const ExplodedNode *ErrorNode = BugPath->ErrorNode; 2856 2857 // Register refutation visitors first, if they mark the bug invalid no 2858 // further analysis is required 2859 R->addVisitor<LikelyFalsePositiveSuppressionBRVisitor>(); 2860 2861 // Register additional node visitors. 2862 R->addVisitor<NilReceiverBRVisitor>(); 2863 R->addVisitor<ConditionBRVisitor>(); 2864 R->addVisitor<TagVisitor>(); 2865 2866 BugReporterContext BRC(Reporter); 2867 2868 // Run all visitors on a given graph, once. 2869 std::unique_ptr<VisitorsDiagnosticsTy> visitorNotes = 2870 generateVisitorsDiagnostics(R, ErrorNode, BRC); 2871 2872 if (R->isValid()) { 2873 if (Reporter.getAnalyzerOptions().ShouldCrosscheckWithZ3) { 2874 // If crosscheck is enabled, remove all visitors, add the refutation 2875 // visitor and check again 2876 R->clearVisitors(); 2877 Z3CrosscheckVisitor::Z3Result CrosscheckResult; 2878 R->addVisitor<Z3CrosscheckVisitor>(CrosscheckResult, 2879 Reporter.getAnalyzerOptions()); 2880 2881 // We don't overwrite the notes inserted by other visitors because the 2882 // refutation manager does not add any new note to the path 2883 generateVisitorsDiagnostics(R, BugPath->ErrorNode, BRC); 2884 switch (Z3Oracle.interpretQueryResult(CrosscheckResult)) { 2885 case Z3CrosscheckOracle::RejectReport: 2886 ++NumTimesReportRefuted; 2887 R->markInvalid("Infeasible constraints", /*Data=*/nullptr); 2888 continue; 2889 case Z3CrosscheckOracle::RejectEQClass: 2890 ++NumTimesReportEQClassAborted; 2891 return {}; 2892 case Z3CrosscheckOracle::AcceptReport: 2893 ++NumTimesReportPassesZ3; 2894 break; 2895 } 2896 } 2897 2898 assert(R->isValid()); 2899 return PathDiagnosticBuilder(std::move(BRC), std::move(BugPath->BugPath), 2900 BugPath->Report, BugPath->ErrorNode, 2901 std::move(visitorNotes)); 2902 } 2903 } 2904 2905 ++NumTimesReportEQClassWasExhausted; 2906 return {}; 2907 } 2908 2909 std::unique_ptr<DiagnosticForConsumerMapTy> 2910 PathSensitiveBugReporter::generatePathDiagnostics( 2911 ArrayRef<PathDiagnosticConsumer *> consumers, 2912 ArrayRef<PathSensitiveBugReport *> &bugReports) { 2913 assert(!bugReports.empty()); 2914 2915 auto Out = std::make_unique<DiagnosticForConsumerMapTy>(); 2916 2917 std::optional<PathDiagnosticBuilder> PDB = 2918 PathDiagnosticBuilder::findValidReport(bugReports, *this); 2919 2920 if (PDB) { 2921 for (PathDiagnosticConsumer *PC : consumers) { 2922 if (std::unique_ptr<PathDiagnostic> PD = PDB->generate(PC)) { 2923 (*Out)[PC] = std::move(PD); 2924 } 2925 } 2926 } 2927 2928 return Out; 2929 } 2930 2931 void BugReporter::emitReport(std::unique_ptr<BugReport> R) { 2932 bool ValidSourceLoc = R->getLocation().isValid(); 2933 assert(ValidSourceLoc); 2934 // If we mess up in a release build, we'd still prefer to just drop the bug 2935 // instead of trying to go on. 2936 if (!ValidSourceLoc) 2937 return; 2938 2939 // If the user asked to suppress this report, we should skip it. 2940 if (UserSuppressions.isSuppressed(*R)) 2941 return; 2942 2943 // Compute the bug report's hash to determine its equivalence class. 2944 llvm::FoldingSetNodeID ID; 2945 R->Profile(ID); 2946 2947 // Lookup the equivance class. If there isn't one, create it. 2948 void *InsertPos; 2949 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos); 2950 2951 if (!EQ) { 2952 EQ = new BugReportEquivClass(std::move(R)); 2953 EQClasses.InsertNode(EQ, InsertPos); 2954 EQClassesVector.push_back(EQ); 2955 } else 2956 EQ->AddReport(std::move(R)); 2957 } 2958 2959 void PathSensitiveBugReporter::emitReport(std::unique_ptr<BugReport> R) { 2960 if (auto PR = dyn_cast<PathSensitiveBugReport>(R.get())) 2961 if (const ExplodedNode *E = PR->getErrorNode()) { 2962 // An error node must either be a sink or have a tag, otherwise 2963 // it could get reclaimed before the path diagnostic is created. 2964 assert((E->isSink() || E->getLocation().getTag()) && 2965 "Error node must either be a sink or have a tag"); 2966 2967 const AnalysisDeclContext *DeclCtx = 2968 E->getLocationContext()->getAnalysisDeclContext(); 2969 // The source of autosynthesized body can be handcrafted AST or a model 2970 // file. The locations from handcrafted ASTs have no valid source 2971 // locations and have to be discarded. Locations from model files should 2972 // be preserved for processing and reporting. 2973 if (DeclCtx->isBodyAutosynthesized() && 2974 !DeclCtx->isBodyAutosynthesizedFromModelFile()) 2975 return; 2976 } 2977 2978 BugReporter::emitReport(std::move(R)); 2979 } 2980 2981 //===----------------------------------------------------------------------===// 2982 // Emitting reports in equivalence classes. 2983 //===----------------------------------------------------------------------===// 2984 2985 namespace { 2986 2987 struct FRIEC_WLItem { 2988 const ExplodedNode *N; 2989 ExplodedNode::const_succ_iterator I, E; 2990 2991 FRIEC_WLItem(const ExplodedNode *n) 2992 : N(n), I(N->succ_begin()), E(N->succ_end()) {} 2993 }; 2994 2995 } // namespace 2996 2997 BugReport *PathSensitiveBugReporter::findReportInEquivalenceClass( 2998 BugReportEquivClass &EQ, SmallVectorImpl<BugReport *> &bugReports) { 2999 // If we don't need to suppress any of the nodes because they are 3000 // post-dominated by a sink, simply add all the nodes in the equivalence class 3001 // to 'Nodes'. Any of the reports will serve as a "representative" report. 3002 assert(EQ.getReports().size() > 0); 3003 const BugType& BT = EQ.getReports()[0]->getBugType(); 3004 if (!BT.isSuppressOnSink()) { 3005 BugReport *R = EQ.getReports()[0].get(); 3006 for (auto &J : EQ.getReports()) { 3007 if (auto *PR = dyn_cast<PathSensitiveBugReport>(J.get())) { 3008 R = PR; 3009 bugReports.push_back(PR); 3010 } 3011 } 3012 return R; 3013 } 3014 3015 // For bug reports that should be suppressed when all paths are post-dominated 3016 // by a sink node, iterate through the reports in the equivalence class 3017 // until we find one that isn't post-dominated (if one exists). We use a 3018 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write 3019 // this as a recursive function, but we don't want to risk blowing out the 3020 // stack for very long paths. 3021 BugReport *exampleReport = nullptr; 3022 3023 for (const auto &I: EQ.getReports()) { 3024 auto *R = dyn_cast<PathSensitiveBugReport>(I.get()); 3025 if (!R) 3026 continue; 3027 3028 const ExplodedNode *errorNode = R->getErrorNode(); 3029 if (errorNode->isSink()) { 3030 llvm_unreachable( 3031 "BugType::isSuppressSink() should not be 'true' for sink end nodes"); 3032 } 3033 // No successors? By definition this nodes isn't post-dominated by a sink. 3034 if (errorNode->succ_empty()) { 3035 bugReports.push_back(R); 3036 if (!exampleReport) 3037 exampleReport = R; 3038 continue; 3039 } 3040 3041 // See if we are in a no-return CFG block. If so, treat this similarly 3042 // to being post-dominated by a sink. This works better when the analysis 3043 // is incomplete and we have never reached the no-return function call(s) 3044 // that we'd inevitably bump into on this path. 3045 if (const CFGBlock *ErrorB = errorNode->getCFGBlock()) 3046 if (ErrorB->isInevitablySinking()) 3047 continue; 3048 3049 // At this point we know that 'N' is not a sink and it has at least one 3050 // successor. Use a DFS worklist to find a non-sink end-of-path node. 3051 using WLItem = FRIEC_WLItem; 3052 using DFSWorkList = SmallVector<WLItem, 10>; 3053 3054 llvm::DenseMap<const ExplodedNode *, unsigned> Visited; 3055 3056 DFSWorkList WL; 3057 WL.push_back(errorNode); 3058 Visited[errorNode] = 1; 3059 3060 while (!WL.empty()) { 3061 WLItem &WI = WL.back(); 3062 assert(!WI.N->succ_empty()); 3063 3064 for (; WI.I != WI.E; ++WI.I) { 3065 const ExplodedNode *Succ = *WI.I; 3066 // End-of-path node? 3067 if (Succ->succ_empty()) { 3068 // If we found an end-of-path node that is not a sink. 3069 if (!Succ->isSink()) { 3070 bugReports.push_back(R); 3071 if (!exampleReport) 3072 exampleReport = R; 3073 WL.clear(); 3074 break; 3075 } 3076 // Found a sink? Continue on to the next successor. 3077 continue; 3078 } 3079 // Mark the successor as visited. If it hasn't been explored, 3080 // enqueue it to the DFS worklist. 3081 unsigned &mark = Visited[Succ]; 3082 if (!mark) { 3083 mark = 1; 3084 WL.push_back(Succ); 3085 break; 3086 } 3087 } 3088 3089 // The worklist may have been cleared at this point. First 3090 // check if it is empty before checking the last item. 3091 if (!WL.empty() && &WL.back() == &WI) 3092 WL.pop_back(); 3093 } 3094 } 3095 3096 // ExampleReport will be NULL if all the nodes in the equivalence class 3097 // were post-dominated by sinks. 3098 return exampleReport; 3099 } 3100 3101 void BugReporter::FlushReport(BugReportEquivClass& EQ) { 3102 SmallVector<BugReport*, 10> bugReports; 3103 BugReport *report = findReportInEquivalenceClass(EQ, bugReports); 3104 if (!report) 3105 return; 3106 3107 // See whether we need to silence the checker/package. 3108 for (const std::string &CheckerOrPackage : 3109 getAnalyzerOptions().SilencedCheckersAndPackages) { 3110 if (report->getBugType().getCheckerName().starts_with(CheckerOrPackage)) 3111 return; 3112 } 3113 3114 ArrayRef<PathDiagnosticConsumer*> Consumers = getPathDiagnosticConsumers(); 3115 std::unique_ptr<DiagnosticForConsumerMapTy> Diagnostics = 3116 generateDiagnosticForConsumerMap(report, Consumers, bugReports); 3117 3118 for (auto &P : *Diagnostics) { 3119 PathDiagnosticConsumer *Consumer = P.first; 3120 std::unique_ptr<PathDiagnostic> &PD = P.second; 3121 3122 // If the path is empty, generate a single step path with the location 3123 // of the issue. 3124 if (PD->path.empty()) { 3125 PathDiagnosticLocation L = report->getLocation(); 3126 auto piece = std::make_unique<PathDiagnosticEventPiece>( 3127 L, report->getDescription()); 3128 for (SourceRange Range : report->getRanges()) 3129 piece->addRange(Range); 3130 PD->setEndOfPath(std::move(piece)); 3131 } 3132 3133 PathPieces &Pieces = PD->getMutablePieces(); 3134 if (getAnalyzerOptions().ShouldDisplayNotesAsEvents) { 3135 // For path diagnostic consumers that don't support extra notes, 3136 // we may optionally convert those to path notes. 3137 for (const auto &I : llvm::reverse(report->getNotes())) { 3138 PathDiagnosticNotePiece *Piece = I.get(); 3139 auto ConvertedPiece = std::make_shared<PathDiagnosticEventPiece>( 3140 Piece->getLocation(), Piece->getString()); 3141 for (const auto &R: Piece->getRanges()) 3142 ConvertedPiece->addRange(R); 3143 3144 Pieces.push_front(std::move(ConvertedPiece)); 3145 } 3146 } else { 3147 for (const auto &I : llvm::reverse(report->getNotes())) 3148 Pieces.push_front(I); 3149 } 3150 3151 for (const auto &I : report->getFixits()) 3152 Pieces.back()->addFixit(I); 3153 3154 updateExecutedLinesWithDiagnosticPieces(*PD); 3155 3156 // If we are debugging, let's have the entry point as the first note. 3157 if (getAnalyzerOptions().AnalyzerDisplayProgress || 3158 getAnalyzerOptions().AnalyzerNoteAnalysisEntryPoints) { 3159 const Decl *EntryPoint = getAnalysisEntryPoint(); 3160 Pieces.push_front(std::make_shared<PathDiagnosticEventPiece>( 3161 PathDiagnosticLocation{EntryPoint->getLocation(), getSourceManager()}, 3162 "[debug] analyzing from " + 3163 AnalysisDeclContext::getFunctionName(EntryPoint))); 3164 } 3165 Consumer->HandlePathDiagnostic(std::move(PD)); 3166 } 3167 } 3168 3169 /// Insert all lines participating in the function signature \p Signature 3170 /// into \p ExecutedLines. 3171 static void populateExecutedLinesWithFunctionSignature( 3172 const Decl *Signature, const SourceManager &SM, 3173 FilesToLineNumsMap &ExecutedLines) { 3174 SourceRange SignatureSourceRange; 3175 const Stmt* Body = Signature->getBody(); 3176 if (const auto FD = dyn_cast<FunctionDecl>(Signature)) { 3177 SignatureSourceRange = FD->getSourceRange(); 3178 } else if (const auto OD = dyn_cast<ObjCMethodDecl>(Signature)) { 3179 SignatureSourceRange = OD->getSourceRange(); 3180 } else { 3181 return; 3182 } 3183 SourceLocation Start = SignatureSourceRange.getBegin(); 3184 SourceLocation End = Body ? Body->getSourceRange().getBegin() 3185 : SignatureSourceRange.getEnd(); 3186 if (!Start.isValid() || !End.isValid()) 3187 return; 3188 unsigned StartLine = SM.getExpansionLineNumber(Start); 3189 unsigned EndLine = SM.getExpansionLineNumber(End); 3190 3191 FileID FID = SM.getFileID(SM.getExpansionLoc(Start)); 3192 for (unsigned Line = StartLine; Line <= EndLine; Line++) 3193 ExecutedLines[FID].insert(Line); 3194 } 3195 3196 static void populateExecutedLinesWithStmt( 3197 const Stmt *S, const SourceManager &SM, 3198 FilesToLineNumsMap &ExecutedLines) { 3199 SourceLocation Loc = S->getSourceRange().getBegin(); 3200 if (!Loc.isValid()) 3201 return; 3202 SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc); 3203 FileID FID = SM.getFileID(ExpansionLoc); 3204 unsigned LineNo = SM.getExpansionLineNumber(ExpansionLoc); 3205 ExecutedLines[FID].insert(LineNo); 3206 } 3207 3208 /// \return all executed lines including function signatures on the path 3209 /// starting from \p N. 3210 static std::unique_ptr<FilesToLineNumsMap> 3211 findExecutedLines(const SourceManager &SM, const ExplodedNode *N) { 3212 auto ExecutedLines = std::make_unique<FilesToLineNumsMap>(); 3213 3214 while (N) { 3215 if (N->getFirstPred() == nullptr) { 3216 // First node: show signature of the entrance point. 3217 const Decl *D = N->getLocationContext()->getDecl(); 3218 populateExecutedLinesWithFunctionSignature(D, SM, *ExecutedLines); 3219 } else if (auto CE = N->getLocationAs<CallEnter>()) { 3220 // Inlined function: show signature. 3221 const Decl* D = CE->getCalleeContext()->getDecl(); 3222 populateExecutedLinesWithFunctionSignature(D, SM, *ExecutedLines); 3223 } else if (const Stmt *S = N->getStmtForDiagnostics()) { 3224 populateExecutedLinesWithStmt(S, SM, *ExecutedLines); 3225 3226 // Show extra context for some parent kinds. 3227 const Stmt *P = N->getParentMap().getParent(S); 3228 3229 // The path exploration can die before the node with the associated 3230 // return statement is generated, but we do want to show the whole 3231 // return. 3232 if (const auto *RS = dyn_cast_or_null<ReturnStmt>(P)) { 3233 populateExecutedLinesWithStmt(RS, SM, *ExecutedLines); 3234 P = N->getParentMap().getParent(RS); 3235 } 3236 3237 if (isa_and_nonnull<SwitchCase, LabelStmt>(P)) 3238 populateExecutedLinesWithStmt(P, SM, *ExecutedLines); 3239 } 3240 3241 N = N->getFirstPred(); 3242 } 3243 return ExecutedLines; 3244 } 3245 3246 std::unique_ptr<DiagnosticForConsumerMapTy> 3247 BugReporter::generateDiagnosticForConsumerMap( 3248 BugReport *exampleReport, ArrayRef<PathDiagnosticConsumer *> consumers, 3249 ArrayRef<BugReport *> bugReports) { 3250 auto *basicReport = cast<BasicBugReport>(exampleReport); 3251 auto Out = std::make_unique<DiagnosticForConsumerMapTy>(); 3252 for (auto *Consumer : consumers) 3253 (*Out)[Consumer] = 3254 generateDiagnosticForBasicReport(basicReport, AnalysisEntryPoint); 3255 return Out; 3256 } 3257 3258 static PathDiagnosticCallPiece * 3259 getFirstStackedCallToHeaderFile(PathDiagnosticCallPiece *CP, 3260 const SourceManager &SMgr) { 3261 SourceLocation CallLoc = CP->callEnter.asLocation(); 3262 3263 // If the call is within a macro, don't do anything (for now). 3264 if (CallLoc.isMacroID()) 3265 return nullptr; 3266 3267 assert(AnalysisManager::isInCodeFile(CallLoc, SMgr) && 3268 "The call piece should not be in a header file."); 3269 3270 // Check if CP represents a path through a function outside of the main file. 3271 if (!AnalysisManager::isInCodeFile(CP->callEnterWithin.asLocation(), SMgr)) 3272 return CP; 3273 3274 const PathPieces &Path = CP->path; 3275 if (Path.empty()) 3276 return nullptr; 3277 3278 // Check if the last piece in the callee path is a call to a function outside 3279 // of the main file. 3280 if (auto *CPInner = dyn_cast<PathDiagnosticCallPiece>(Path.back().get())) 3281 return getFirstStackedCallToHeaderFile(CPInner, SMgr); 3282 3283 // Otherwise, the last piece is in the main file. 3284 return nullptr; 3285 } 3286 3287 static void resetDiagnosticLocationToMainFile(PathDiagnostic &PD) { 3288 if (PD.path.empty()) 3289 return; 3290 3291 PathDiagnosticPiece *LastP = PD.path.back().get(); 3292 assert(LastP); 3293 const SourceManager &SMgr = LastP->getLocation().getManager(); 3294 3295 // We only need to check if the report ends inside headers, if the last piece 3296 // is a call piece. 3297 if (auto *CP = dyn_cast<PathDiagnosticCallPiece>(LastP)) { 3298 CP = getFirstStackedCallToHeaderFile(CP, SMgr); 3299 if (CP) { 3300 // Mark the piece. 3301 CP->setAsLastInMainSourceFile(); 3302 3303 // Update the path diagnostic message. 3304 const auto *ND = dyn_cast<NamedDecl>(CP->getCallee()); 3305 if (ND) { 3306 SmallString<200> buf; 3307 llvm::raw_svector_ostream os(buf); 3308 os << " (within a call to '" << ND->getDeclName() << "')"; 3309 PD.appendToDesc(os.str()); 3310 } 3311 3312 // Reset the report containing declaration and location. 3313 PD.setDeclWithIssue(CP->getCaller()); 3314 PD.setLocation(CP->getLocation()); 3315 3316 return; 3317 } 3318 } 3319 } 3320 3321 3322 3323 std::unique_ptr<DiagnosticForConsumerMapTy> 3324 PathSensitiveBugReporter::generateDiagnosticForConsumerMap( 3325 BugReport *exampleReport, ArrayRef<PathDiagnosticConsumer *> consumers, 3326 ArrayRef<BugReport *> bugReports) { 3327 std::vector<BasicBugReport *> BasicBugReports; 3328 std::vector<PathSensitiveBugReport *> PathSensitiveBugReports; 3329 if (isa<BasicBugReport>(exampleReport)) 3330 return BugReporter::generateDiagnosticForConsumerMap(exampleReport, 3331 consumers, bugReports); 3332 3333 // Generate the full path sensitive diagnostic, using the generation scheme 3334 // specified by the PathDiagnosticConsumer. Note that we have to generate 3335 // path diagnostics even for consumers which do not support paths, because 3336 // the BugReporterVisitors may mark this bug as a false positive. 3337 assert(!bugReports.empty()); 3338 MaxBugClassSize.updateMax(bugReports.size()); 3339 3340 // Avoid copying the whole array because there may be a lot of reports. 3341 ArrayRef<PathSensitiveBugReport *> convertedArrayOfReports( 3342 reinterpret_cast<PathSensitiveBugReport *const *>(&*bugReports.begin()), 3343 reinterpret_cast<PathSensitiveBugReport *const *>(&*bugReports.end())); 3344 std::unique_ptr<DiagnosticForConsumerMapTy> Out = generatePathDiagnostics( 3345 consumers, convertedArrayOfReports); 3346 3347 if (Out->empty()) 3348 return Out; 3349 3350 MaxValidBugClassSize.updateMax(bugReports.size()); 3351 3352 // Examine the report and see if the last piece is in a header. Reset the 3353 // report location to the last piece in the main source file. 3354 const AnalyzerOptions &Opts = getAnalyzerOptions(); 3355 for (auto const &P : *Out) 3356 if (Opts.ShouldReportIssuesInMainSourceFile && !Opts.AnalyzeAll) 3357 resetDiagnosticLocationToMainFile(*P.second); 3358 3359 return Out; 3360 } 3361 3362 void BugReporter::EmitBasicReport(const Decl *DeclWithIssue, 3363 const CheckerBase *Checker, StringRef Name, 3364 StringRef Category, StringRef Str, 3365 PathDiagnosticLocation Loc, 3366 ArrayRef<SourceRange> Ranges, 3367 ArrayRef<FixItHint> Fixits) { 3368 EmitBasicReport(DeclWithIssue, Checker->getCheckerName(), Name, Category, Str, 3369 Loc, Ranges, Fixits); 3370 } 3371 3372 void BugReporter::EmitBasicReport(const Decl *DeclWithIssue, 3373 CheckerNameRef CheckName, 3374 StringRef name, StringRef category, 3375 StringRef str, PathDiagnosticLocation Loc, 3376 ArrayRef<SourceRange> Ranges, 3377 ArrayRef<FixItHint> Fixits) { 3378 // 'BT' is owned by BugReporter. 3379 BugType *BT = getBugTypeForName(CheckName, name, category); 3380 auto R = std::make_unique<BasicBugReport>(*BT, str, Loc); 3381 R->setDeclWithIssue(DeclWithIssue); 3382 for (const auto &SR : Ranges) 3383 R->addRange(SR); 3384 for (const auto &FH : Fixits) 3385 R->addFixItHint(FH); 3386 emitReport(std::move(R)); 3387 } 3388 3389 BugType *BugReporter::getBugTypeForName(CheckerNameRef CheckName, 3390 StringRef name, StringRef category) { 3391 SmallString<136> fullDesc; 3392 llvm::raw_svector_ostream(fullDesc) << CheckName.getName() << ":" << name 3393 << ":" << category; 3394 std::unique_ptr<BugType> &BT = StrBugTypes[fullDesc]; 3395 if (!BT) 3396 BT = std::make_unique<BugType>(CheckName, name, category); 3397 return BT.get(); 3398 } 3399