From bb77e9b908658b354b058509d3801f3aed052bec Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Thu, 1 May 2008 22:50:36 +0000 Subject: [PATCH] Do not highlight bogus ranges for leaks. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@50549 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Analysis/PathSensitive/BugReporter.h | 14 ++-- lib/Analysis/BasicObjCFoundationChecks.cpp | 3 +- lib/Analysis/BugReporter.cpp | 80 ++++++++++--------- lib/Analysis/CFRefCount.cpp | 25 +++++- 4 files changed, 74 insertions(+), 48 deletions(-) diff --git a/include/clang/Analysis/PathSensitive/BugReporter.h b/include/clang/Analysis/PathSensitive/BugReporter.h index 5a8606f071..b823906364 100644 --- a/include/clang/Analysis/PathSensitive/BugReporter.h +++ b/include/clang/Analysis/PathSensitive/BugReporter.h @@ -64,7 +64,7 @@ public: class BugReport { BugType& Desc; ExplodedNode *N; - + SourceRange R; public: BugReport(BugType& D, ExplodedNode *n) : Desc(D), N(n) {} virtual ~BugReport(); @@ -74,7 +74,7 @@ public: ExplodedNode* getEndNode() const { return N; } - Stmt* getStmt() const; + Stmt* getStmt(BugReporter& BR) const; const char* getName() const { return getBugType().getName(); } @@ -87,12 +87,12 @@ public: } virtual PathDiagnosticPiece* getEndPath(BugReporter& BR, - ExplodedNode* N) const; + ExplodedNode* N); virtual FullSourceLoc getLocation(SourceManager& Mgr); - virtual void getRanges(const SourceRange*& beg, - const SourceRange*& end) const; + virtual void getRanges(BugReporter& BR,const SourceRange*& beg, + const SourceRange*& end); virtual PathDiagnosticPiece* VisitNode(ExplodedNode* N, ExplodedNode* PrevN, @@ -110,8 +110,8 @@ public: void addRange(SourceRange R) { Ranges.push_back(R); } - virtual void getRanges(const SourceRange*& beg, - const SourceRange*& end) const { + virtual void getRanges(BugReporter& BR,const SourceRange*& beg, + const SourceRange*& end) { if (Ranges.empty()) { beg = NULL; diff --git a/lib/Analysis/BasicObjCFoundationChecks.cpp b/lib/Analysis/BasicObjCFoundationChecks.cpp index bb16df91c6..8e8feec1d1 100644 --- a/lib/Analysis/BasicObjCFoundationChecks.cpp +++ b/lib/Analysis/BasicObjCFoundationChecks.cpp @@ -89,7 +89,8 @@ public: virtual const char* getDescription() const { return s; } - virtual void getRanges(const SourceRange*& B, const SourceRange*& E) const { + virtual void getRanges(BugReporter& BR, + const SourceRange*& B, const SourceRange*& E) { B = &R; E = B+1; } diff --git a/lib/Analysis/BugReporter.cpp b/lib/Analysis/BugReporter.cpp index 465acf03f3..785e4cf4ae 100644 --- a/lib/Analysis/BugReporter.cpp +++ b/lib/Analysis/BugReporter.cpp @@ -56,27 +56,11 @@ static inline Stmt* GetStmt(const CFGBlock* B) { return (*B)[0]; } -Stmt* BugReport::getStmt() const { - return N ? GetStmt(N->getLocation()) : NULL; -} - static inline ExplodedNode* GetNextNode(ExplodedNode* N) { return N->pred_empty() ? NULL : *(N->pred_begin()); } -static void ExecutionContinues(std::ostream& os, SourceManager& SMgr, - ExplodedNode* N) { - - Stmt* S = GetStmt(N->getLocation()); - - if (!S) - return; - - os << "Execution continue on line " - << SMgr.getLogicalLineNumber(S->getLocStart()) << '.'; -} - static Stmt* GetLastStmt(ExplodedNode* N) { assert (isa(N->getLocation())); @@ -91,18 +75,39 @@ static Stmt* GetLastStmt(ExplodedNode* N) { return NULL; } -PathDiagnosticPiece* -BugReport::getEndPath(BugReporter& BR, - ExplodedNode* EndPathNode) const { + +static void ExecutionContinues(std::ostream& os, SourceManager& SMgr, + ExplodedNode* N) { + + Stmt* S = GetStmt(N->getLocation()); + + if (!S) + return; + + os << "Execution continue on line " + << SMgr.getLogicalLineNumber(S->getLocStart()) << '.'; +} - ProgramPoint ProgP = EndPathNode->getLocation(); + +Stmt* BugReport::getStmt(BugReporter& BR) const { + + ProgramPoint ProgP = N->getLocation(); Stmt *S = NULL; if (BlockEntrance* BE = dyn_cast(&ProgP)) if (BE->getBlock() == &BR.getCFG().getExit()) - S = GetLastStmt(EndPathNode); + S = GetLastStmt(N); if (!S) - S = GetStmt(ProgP); + S = GetStmt(ProgP); + + return S; +} + +PathDiagnosticPiece* +BugReport::getEndPath(BugReporter& BR, + ExplodedNode* EndPathNode) { + + Stmt* S = getStmt(BR); if (!S) return NULL; @@ -113,25 +118,24 @@ BugReport::getEndPath(BugReporter& BR, new PathDiagnosticPiece(L, getDescription()); const SourceRange *Beg, *End; - getRanges(Beg, End); - - if (Beg == End) { - if (Expr* E = dyn_cast(S)) - P->addRange(E->getSourceRange()); - } - else { - assert (Beg < End); - for (; Beg != End; ++Beg) - P->addRange(*Beg); - } + getRanges(BR, Beg, End); + + for (; Beg != End; ++Beg) + P->addRange(*Beg); return P; } -void BugReport::getRanges(const SourceRange*& beg, - const SourceRange*& end) const { - beg = NULL; - end = NULL; +void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg, + const SourceRange*& end) { + + if (Expr* E = dyn_cast_or_null(getStmt(BR))) { + R = E->getSourceRange(); + beg = &R; + end = beg+1; + } + else + beg = end = 0; } FullSourceLoc BugReport::getLocation(SourceManager& Mgr) { @@ -479,7 +483,7 @@ void BugReporter::EmitWarning(BugReport& R) { End = D->back()->ranges_end(); } else - R.getRanges(Beg, End); + R.getRanges(*this, Beg, End); if (PD) { PathDiagnosticPiece* piece = new PathDiagnosticPiece(L, R.getDescription()); diff --git a/lib/Analysis/CFRefCount.cpp b/lib/Analysis/CFRefCount.cpp index 98287921d3..ea036f047b 100644 --- a/lib/Analysis/CFRefCount.cpp +++ b/lib/Analysis/CFRefCount.cpp @@ -1444,7 +1444,9 @@ namespace { public: CFRefBug(CFRefCount& tf) : TF(tf) {} - CFRefCount& getTF() { return TF; } + CFRefCount& getTF() { return TF; } + + virtual bool ReportRanges() const { return true; } }; class VISIBILITY_HIDDEN UseAfterRelease : public CFRefBug { @@ -1491,7 +1493,8 @@ namespace { } virtual void EmitWarnings(BugReporter& BR); - virtual void GetErrorNodes(std::vector*>& Nodes); + virtual void GetErrorNodes(std::vector*>& Nodes); + virtual bool ReportRanges() const { return false; } }; //===---------===// @@ -1506,6 +1509,24 @@ namespace { virtual ~CFRefReport() {} + CFRefBug& getBugType() { + return (CFRefBug&) RangedBugReport::getBugType(); + } + const CFRefBug& getBugType() const { + return (const CFRefBug&) RangedBugReport::getBugType(); + } + + virtual void getRanges(BugReporter& BR, const SourceRange*& beg, + const SourceRange*& end) { + + if (getBugType().ReportRanges()) + RangedBugReport::getRanges(BR, beg, end); + else { + beg = 0; + end = 0; + } + } + virtual std::pair getExtraDescriptiveText(); virtual PathDiagnosticPiece* VisitNode(ExplodedNode* N, -- 2.40.0