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