From fbd58743fa6c793b84ed60a0e2325335a53da6c4 Mon Sep 17 00:00:00 2001 From: Anna Zaks Date: Fri, 16 Mar 2012 23:44:28 +0000 Subject: [PATCH] [analyzer] Shorten the stack hint diagnostic. Do not display the standard "Returning from 'foo'", when a stack hint is available. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@152964 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Core/BugReporter/PathDiagnostic.h | 2 +- lib/StaticAnalyzer/Checkers/MallocChecker.cpp | 15 +++++++------ lib/StaticAnalyzer/Core/PathDiagnostic.cpp | 6 +++--- test/Analysis/malloc-plist.c | 21 ++++++++++--------- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h b/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h index b40bc71010..6460124b76 100644 --- a/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h +++ b/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h @@ -398,7 +398,7 @@ public: /// 'Msg via Nth parameter' virtual std::string getMessageForArg(const Expr *ArgE, unsigned ArgIndex); virtual std::string getMessageForReturn(const CallExpr *CallExpr) { - return Msg + " returned"; + return Msg; } virtual std::string getMessageForSymbolNotFound() { return Msg; diff --git a/lib/StaticAnalyzer/Checkers/MallocChecker.cpp b/lib/StaticAnalyzer/Checkers/MallocChecker.cpp index 2926fd5c04..133482fcce 100644 --- a/lib/StaticAnalyzer/Checkers/MallocChecker.cpp +++ b/lib/StaticAnalyzer/Checkers/MallocChecker.cpp @@ -254,7 +254,7 @@ private: SmallString<200> buf; llvm::raw_svector_ostream os(buf); - os << "; reallocation of "; + os << "Reallocation of "; // Printed parameters start at 1, not 0. printOrdinal(++ArgIndex, os); os << " parameter failed"; @@ -263,7 +263,7 @@ private: } virtual std::string getMessageForReturn(const CallExpr *CallExpr) { - return "; reallocation of returned value failed"; + return "Reallocation of returned value failed"; } }; }; @@ -1292,15 +1292,17 @@ MallocChecker::MallocBugVisitor::VisitNode(const ExplodedNode *N, if (Mode == Normal) { if (isAllocated(RS, RSPrev, S)) { Msg = "Memory is allocated"; - StackHint = new StackHintGeneratorForSymbol(Sym, "; allocated memory"); + StackHint = new StackHintGeneratorForSymbol(Sym, + "Returned allocated memory"); } else if (isReleased(RS, RSPrev, S)) { Msg = "Memory is released"; - StackHint = new StackHintGeneratorForSymbol(Sym, "; released memory"); + StackHint = new StackHintGeneratorForSymbol(Sym, + "Returned released memory"); } else if (isReallocFailedCheck(RS, RSPrev, S)) { Mode = ReallocationFailed; Msg = "Reallocation failed"; StackHint = new StackHintGeneratorForReallocationFailed(Sym, - "; reallocation failed"); + "Reallocation failed"); } // We are in a special mode if a reallocation failed later in the path. @@ -1320,7 +1322,8 @@ MallocChecker::MallocBugVisitor::VisitNode(const ExplodedNode *N, if (!(FunName.equals("realloc") || FunName.equals("reallocf"))) return 0; Msg = "Attempt to reallocate memory"; - StackHint = new StackHintGeneratorForSymbol(Sym, "; reallocated memory"); + StackHint = new StackHintGeneratorForSymbol(Sym, + "Returned reallocated memory"); Mode = Normal; } diff --git a/lib/StaticAnalyzer/Core/PathDiagnostic.cpp b/lib/StaticAnalyzer/Core/PathDiagnostic.cpp index 2a2b9c615e..989553e375 100644 --- a/lib/StaticAnalyzer/Core/PathDiagnostic.cpp +++ b/lib/StaticAnalyzer/Core/PathDiagnostic.cpp @@ -568,12 +568,12 @@ PathDiagnosticCallPiece::getCallExitEvent() const { return 0; SmallString<256> buf; llvm::raw_svector_ostream Out(buf); - if (const NamedDecl *ND = dyn_cast_or_null(Callee)) + if (!CallStackMessage.empty()) + Out << CallStackMessage; + else if (const NamedDecl *ND = dyn_cast_or_null(Callee)) Out << "Returning from '" << *ND << "'"; else Out << "Returning to caller"; - if (!CallStackMessage.empty()) - Out << CallStackMessage; return new PathDiagnosticEventPiece(callReturn, Out.str()); } diff --git a/test/Analysis/malloc-plist.c b/test/Analysis/malloc-plist.c index d08ed07d18..326161b06a 100644 --- a/test/Analysis/malloc-plist.c +++ b/test/Analysis/malloc-plist.c @@ -87,6 +87,7 @@ void use_ret() { v = malloc_wrapper_ret(); } + // CHECK: // CHECK: // CHECK: @@ -1129,9 +1130,9 @@ void use_ret() { // CHECK: // CHECK: depth1 // CHECK: extended_message -// CHECK: Returning from 'wrapper'; allocated memory returned +// CHECK: Returned allocated memory // CHECK: message -// CHECK: Returning from 'wrapper'; allocated memory returned +// CHECK: Returned allocated memory // CHECK: // CHECK: // CHECK: kindcontrol @@ -1568,9 +1569,9 @@ void use_ret() { // CHECK: // CHECK: depth2 // CHECK: extended_message -// CHECK: Returning from 'my_free'; released memory via 1st parameter +// CHECK: Returned released memory via 1st parameter // CHECK: message -// CHECK: Returning from 'my_free'; released memory via 1st parameter +// CHECK: Returned released memory via 1st parameter // CHECK: // CHECK: // CHECK: kindcontrol @@ -1631,9 +1632,9 @@ void use_ret() { // CHECK: // CHECK: depth1 // CHECK: extended_message -// CHECK: Returning from 'my_malloc_and_free'; released memory via 1st parameter +// CHECK: Returned released memory via 1st parameter // CHECK: message -// CHECK: Returning from 'my_malloc_and_free'; released memory via 1st parameter +// CHECK: Returned released memory via 1st parameter // CHECK: // CHECK: // CHECK: kindcontrol @@ -2139,9 +2140,9 @@ void use_ret() { // CHECK: // CHECK: depth1 // CHECK: extended_message -// CHECK: Returning from 'my_realloc'; reallocation of 1st parameter failed +// CHECK: Reallocation of 1st parameter failed // CHECK: message -// CHECK: Returning from 'my_realloc'; reallocation of 1st parameter failed +// CHECK: Reallocation of 1st parameter failed // CHECK: // CHECK: // CHECK: kindcontrol @@ -2419,9 +2420,9 @@ void use_ret() { // CHECK: // CHECK: depth1 // CHECK: extended_message -// CHECK: Returning from 'malloc_wrapper_ret'; allocated memory returned +// CHECK: Returned allocated memory // CHECK: message -// CHECK: Returning from 'malloc_wrapper_ret'; allocated memory returned +// CHECK: Returned allocated memory // CHECK: // CHECK: // CHECK: kindcontrol -- 2.40.0