From e73006ee45b16d2d9d5aab8de62f09946cc7498c Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Mon, 7 Apr 2008 23:35:17 +0000 Subject: [PATCH] Improve BugReport diagnostics for loops and ? operator. llvm-svn: 49356 --- clang/lib/Analysis/BugReporter.cpp | 61 ++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/clang/lib/Analysis/BugReporter.cpp b/clang/lib/Analysis/BugReporter.cpp index 8eb200017f2b..14f98fa1f4af 100644 --- a/clang/lib/Analysis/BugReporter.cpp +++ b/clang/lib/Analysis/BugReporter.cpp @@ -41,6 +41,11 @@ static inline Stmt* GetStmt(const ProgramPoint& P) { return NULL; } +static inline Stmt* GetStmt(const CFGBlock* B) { + assert (!B->empty()); + return (*B)[0]; +} + PathDiagnosticPiece* BugDescription::getEndPath(ASTContext& Ctx, ExplodedNode *N) const { @@ -142,7 +147,7 @@ void BugReporter::GeneratePathDiagnostic(PathDiagnostic& PD, ASTContext& Ctx, std::ostringstream os; os << "Control jumps to line " - << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n"; + << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n"; PD.push_front(new PathDiagnosticPiece(L, os.str())); break; @@ -206,7 +211,7 @@ void BugReporter::GeneratePathDiagnostic(PathDiagnostic& PD, ASTContext& Ctx, } os << ":' at line " - << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n"; + << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n"; break; @@ -216,11 +221,59 @@ void BugReporter::GeneratePathDiagnostic(PathDiagnostic& PD, ASTContext& Ctx, PD.push_front(new PathDiagnosticPiece(L, os.str())); break; } + + case Stmt::ConditionalOperatorClass: { + std::ostringstream os; + os << "'?' condition evaluates to "; + + if (*(Src->succ_begin()+1) == Dst) + os << "false."; + else + os << "true."; + + PD.push_front(new PathDiagnosticPiece(L, os.str())); + + break; + } + + case Stmt::DoStmtClass: { + + if (*(Src->succ_begin()) == Dst) { + + std::ostringstream os; + + os << "Loop condition is true. Execution continues on line " + << SMgr.getLogicalLineNumber(GetStmt(Dst)->getLocStart()) << '.'; + + PD.push_front(new PathDiagnosticPiece(L, os.str())); + } + else + PD.push_front(new PathDiagnosticPiece(L, + "Loop condition is false. Exiting loop.")); + + break; + } - case Stmt::DoStmtClass: case Stmt::WhileStmtClass: - case Stmt::ForStmtClass: + case Stmt::ForStmtClass: { + + if (*(Src->succ_begin()+1) == Dst) { + + std::ostringstream os; + + os << "Loop condition is false. Execution continues on line " + << SMgr.getLogicalLineNumber(GetStmt(Dst)->getLocStart()) << '.'; + + PD.push_front(new PathDiagnosticPiece(L, os.str())); + } + else + PD.push_front(new PathDiagnosticPiece(L, + "Loop condition is true. Entering loop body.")); + + break; + } + case Stmt::IfStmtClass: { if (*(Src->succ_begin()+1) == Dst)