Index: lib/StaticAnalyzer/Core/BugReporter.cpp =================================================================== --- lib/StaticAnalyzer/Core/BugReporter.cpp +++ lib/StaticAnalyzer/Core/BugReporter.cpp @@ -3285,6 +3285,29 @@ }; } +static const CFGBlock *findBlockForNode(const ExplodedNode *N) { + ProgramPoint P = N->getLocation(); + if (auto BEP = P.getAs()) + return BEP->getBlock(); + + // Find the node's current statement in the CFG. + // FIXME: CFG lookup should be made easier. + const CFG &Cfg = N->getCFG(); + if (const Stmt *S = PathDiagnosticLocation::getStmt(N)) { + for (auto BI = Cfg.begin(), BE = Cfg.end(); BI != BE; ++BI) { + const CFGBlock *B = *BI; + for (auto EI = B->begin(), EE = B->end(); EI != EE; ++EI) { + const CFGElement &E = *EI; + if (auto SE = E.getAs()) + if (SE->getStmt() == S) + return B; + } + } + } + + return nullptr; +} + static BugReport * FindReportInEquivalenceClass(BugReportEquivClass& EQ, SmallVectorImpl &bugReports) { @@ -3333,6 +3356,17 @@ continue; } + // See if we are in a no-return CFG block. If so, treat this similarly + // to being post-dominated by a sink. This works better when the analysis + // is incomplete and we have never reached a no-return function + // we're post-dominated by. + // FIXME: This is far from enough to handle the incomplete analysis case. + // We may be post-dominated in subsequent blocks, or even + // inter-procedurally. + if (const CFGBlock *B = findBlockForNode(errorNode)) + if (B->hasNoReturnElement()) + continue; + // At this point we know that 'N' is not a sink and it has at least one // successor. Use a DFS worklist to find a non-sink end-of-path node. typedef FRIEC_WLItem WLItem; Index: test/Analysis/max-nodes-suppress-on-sink.c =================================================================== --- /dev/null +++ test/Analysis/max-nodes-suppress-on-sink.c @@ -0,0 +1,33 @@ +// RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-config max-nodes=12 -verify %s + +// Here we test how "suppress on sink" feature of certain bugtypes interacts +// with reaching analysis limits. + +// If we throw a warning of a bug-type with "suppress on sink" attribute set +// (such as MallocChecker's memory leak warning), then failing to reach the +// reason for the sink (eg. no-return function such as "exit()") due to analysis +// limits (eg. max-nodes option), we may produce a false positive. + +#include "Inputs/system-header-simulator.h" + +typedef __typeof(sizeof(int)) size_t; +void *malloc(size_t); + +extern void exit(int) __attribute__ ((__noreturn__)); + +void clang_analyzer_warnIfReached(void); + +void test_single_cfg_block_sink() { + void *p = malloc(1); // no-warning (wherever the leak warning may occur here) + + // Due to max-nodes option in the run line, we should reach the first call + // but bail out before the second call. + // If the test on these two lines starts failing, see if modifying + // the max-nodes run-line helps. + clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}} + clang_analyzer_warnIfReached(); // no-warning + + // Even though we do not reach this line, we should still suppress + // the leak report. + exit(0); +}