From d8c938b0f6b7a7156181be575239e4c6e15a2adb Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Fri, 27 Mar 2009 21:16:25 +0000 Subject: [PATCH] BugReporter: For control-flow edges from 'if', 'for', 'do', 'while' to successor, using 'getEnclosingStmt()' to have the end location be the top-level Stmt* enclosing the target Expr*. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67869 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/BugReporter.cpp | 44 ++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/lib/Analysis/BugReporter.cpp b/lib/Analysis/BugReporter.cpp index 88d4fa15ed..ac3d96191a 100644 --- a/lib/Analysis/BugReporter.cpp +++ b/lib/Analysis/BugReporter.cpp @@ -108,6 +108,8 @@ public: return *PM.get(); } + PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S); + bool supportsLogicalOpControlFlow() const { return PDC ? PDC->supportsLogicalOpControlFlow() : true; } @@ -142,6 +144,23 @@ PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream& os, return Loc; } +PathDiagnosticLocation +PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) { + assert(S && "Null Stmt* passed to getEnclosingStmtLocation"); + ParentMap &P = getParentMap(); + while (isa(S)) { + const Stmt *Parent = P.getParent(S); + + if (!Parent || isa(Parent) || isa(Parent)) + return PathDiagnosticLocation(S, SMgr); + + S = Parent; + } + + assert(S && "Cannot have null Stmt for PathDiagnosticLocation"); + return PathDiagnosticLocation(S, SMgr); +} + //===----------------------------------------------------------------------===// // Methods for BugType and subclasses. //===----------------------------------------------------------------------===// @@ -772,7 +791,7 @@ void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD, std::string sbuf; llvm::raw_string_ostream os(sbuf); - PathDiagnosticLocation End(S->getLocStart(), SMgr); + const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S); os << "Control jumps to line " << End.asLocation().getInstantiationLineNumber(); @@ -921,12 +940,20 @@ void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD, llvm::raw_string_ostream os(sbuf); os << "Loop condition is true. "; - PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); + PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); + + if (const Stmt *S = End.asStmt()) + End = PDB.getEnclosingStmtLocation(S); + PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, os.str())); } else { PathDiagnosticLocation End = PDB.ExecutionContinues(N); + + if (const Stmt *S = End.asStmt()) + End = PDB.getEnclosingStmtLocation(S); + PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, "Loop condition is false. Exiting loop")); } @@ -935,18 +962,23 @@ void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD, } case Stmt::WhileStmtClass: - case Stmt::ForStmtClass: { + case Stmt::ForStmtClass: { if (*(Src->succ_begin()+1) == Dst) { std::string sbuf; llvm::raw_string_ostream os(sbuf); os << "Loop condition is false. "; PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); + if (const Stmt *S = End.asStmt()) + End = PDB.getEnclosingStmtLocation(S); + PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, os.str())); } else { PathDiagnosticLocation End = PDB.ExecutionContinues(N); + if (const Stmt *S = End.asStmt()) + End = PDB.getEnclosingStmtLocation(S); PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, "Loop condition is true. Entering loop body")); @@ -956,7 +988,11 @@ void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD, } case Stmt::IfStmtClass: { - PathDiagnosticLocation End = PDB.ExecutionContinues(N); + PathDiagnosticLocation End = PDB.ExecutionContinues(N); + + if (const Stmt *S = End.asStmt()) + End = PDB.getEnclosingStmtLocation(S); + if (*(Src->succ_begin()+1) == Dst) PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, "Taking false branch")); -- 2.40.0