From: Anna Zaks Date: Thu, 10 May 2012 01:37:40 +0000 (+0000) Subject: [analyzer] Do not highlight the range of the statement in case of leak. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=88feba05142651618aba0a0e57b0b98e026de336;p=clang [analyzer] Do not highlight the range of the statement in case of leak. We report a leak at a point a leaked variable is no longer accessible. The statement that happens to be at that point is not relevant to the leak diagnostic and, thus, should not be highlighted. radar://11178519 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@156530 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/StaticAnalyzer/Checkers/MallocChecker.cpp b/lib/StaticAnalyzer/Checkers/MallocChecker.cpp index 1415184033..4df77a845e 100644 --- a/lib/StaticAnalyzer/Checkers/MallocChecker.cpp +++ b/lib/StaticAnalyzer/Checkers/MallocChecker.cpp @@ -210,15 +210,17 @@ private: // The allocated region symbol tracked by the main analysis. SymbolRef Sym; - // The mode we are in, i.e. what kind of diagnostics will be emitted. - NotificationMode Mode; + // The mode we are in, i.e. what kind of diagnostics will be emitted. + NotificationMode Mode; - // A symbol from when the primary region should have been reallocated. - SymbolRef FailedReallocSymbol; + // A symbol from when the primary region should have been reallocated. + SymbolRef FailedReallocSymbol; - public: - MallocBugVisitor(SymbolRef S) - : Sym(S), Mode(Normal), FailedReallocSymbol(0) {} + bool IsLeak; + + public: + MallocBugVisitor(SymbolRef S, bool isLeak = false) + : Sym(S), Mode(Normal), FailedReallocSymbol(0), IsLeak(isLeak) {} virtual ~MallocBugVisitor() {} @@ -256,6 +258,20 @@ private: const ExplodedNode *PrevN, BugReporterContext &BRC, BugReport &BR); + + PathDiagnosticPiece* getEndPath(BugReporterContext &BRC, + const ExplodedNode *EndPathNode, + BugReport &BR) { + if (!IsLeak) + return 0; + + PathDiagnosticLocation L = + PathDiagnosticLocation::createEndOfPath(EndPathNode, + BRC.getSourceManager()); + // Do not add the statement itself as a range in case of leak. + return new PathDiagnosticEventPiece(L, BR.getDescription(), false); + } + private: class StackHintGeneratorForReallocationFailed : public StackHintGeneratorForSymbol { @@ -895,7 +911,7 @@ void MallocChecker::reportLeak(SymbolRef Sym, ExplodedNode *N, BugReport *R = new BugReport(*BT_Leak, os.str(), N, LocUsedForUniqueing); R->markInteresting(Sym); - R->addVisitor(new MallocBugVisitor(Sym)); + R->addVisitor(new MallocBugVisitor(Sym, true)); C.EmitReport(R); } diff --git a/test/Analysis/malloc-plist.c b/test/Analysis/malloc-plist.c index 8a36ab3085..248abc21c8 100644 --- a/test/Analysis/malloc-plist.c +++ b/test/Analysis/malloc-plist.c @@ -350,21 +350,6 @@ void use_function_with_leak7() { //CHECK: col5 //CHECK: file0 //CHECK: -//CHECK: ranges -//CHECK: -//CHECK: -//CHECK: -//CHECK: line14 -//CHECK: col5 -//CHECK: file0 -//CHECK: -//CHECK: -//CHECK: line14 -//CHECK: col6 -//CHECK: file0 -//CHECK: -//CHECK: -//CHECK: //CHECK: depth0 //CHECK: extended_message //CHECK: Memory is never released; potential leak of memory pointed to by 'p' @@ -910,21 +895,6 @@ void use_function_with_leak7() { //CHECK: col9 //CHECK: file0 //CHECK: -//CHECK: ranges -//CHECK: -//CHECK: -//CHECK: -//CHECK: line28 -//CHECK: col9 -//CHECK: file0 -//CHECK: -//CHECK: -//CHECK: line28 -//CHECK: col14 -//CHECK: file0 -//CHECK: -//CHECK: -//CHECK: //CHECK: depth0 //CHECK: extended_message //CHECK: Memory is never released; potential leak of memory pointed to by 'buf' @@ -2400,21 +2370,6 @@ void use_function_with_leak7() { //CHECK: col5 //CHECK: file0 //CHECK: -//CHECK: ranges -//CHECK: -//CHECK: -//CHECK: -//CHECK: line76 -//CHECK: col5 -//CHECK: file0 -//CHECK: -//CHECK: -//CHECK: line76 -//CHECK: col13 -//CHECK: file0 -//CHECK: -//CHECK: -//CHECK: //CHECK: depth0 //CHECK: extended_message //CHECK: Memory is never released; potential leak of memory pointed to by 'buf' @@ -2843,21 +2798,6 @@ void use_function_with_leak7() { //CHECK: col5 //CHECK: file0 //CHECK: -//CHECK: ranges -//CHECK: -//CHECK: -//CHECK: -//CHECK: line97 -//CHECK: col5 -//CHECK: file0 -//CHECK: -//CHECK: -//CHECK: line97 -//CHECK: col8 -//CHECK: file0 -//CHECK: -//CHECK: -//CHECK: //CHECK: depth0 //CHECK: extended_message //CHECK: Memory is never released; potential leak of memory pointed to by 'm' @@ -3062,21 +3002,6 @@ void use_function_with_leak7() { //CHECK: col5 //CHECK: file0 //CHECK: -//CHECK: ranges -//CHECK: -//CHECK: -//CHECK: -//CHECK: line102 -//CHECK: col5 -//CHECK: file0 -//CHECK: -//CHECK: -//CHECK: line102 -//CHECK: col11 -//CHECK: file0 -//CHECK: -//CHECK: -//CHECK: //CHECK: depth1 //CHECK: extended_message //CHECK: Memory is never released; potential leak of memory pointed to by 'x' @@ -3281,21 +3206,6 @@ void use_function_with_leak7() { //CHECK: col5 //CHECK: file0 //CHECK: -//CHECK: ranges -//CHECK: -//CHECK: -//CHECK: -//CHECK: line111 -//CHECK: col5 -//CHECK: file0 -//CHECK: -//CHECK: -//CHECK: line111 -//CHECK: col9 -//CHECK: file0 -//CHECK: -//CHECK: -//CHECK: //CHECK: depth1 //CHECK: extended_message //CHECK: Memory is never released; potential leak of memory pointed to by 'x' @@ -3568,21 +3478,6 @@ void use_function_with_leak7() { //CHECK: col9 //CHECK: file0 //CHECK: -//CHECK: ranges -//CHECK: -//CHECK: -//CHECK: -//CHECK: line120 -//CHECK: col9 -//CHECK: file0 -//CHECK: -//CHECK: -//CHECK: line120 -//CHECK: col9 -//CHECK: file0 -//CHECK: -//CHECK: -//CHECK: //CHECK: depth1 //CHECK: extended_message //CHECK: Memory is never released; potential leak of memory pointed to by 'x' @@ -3855,21 +3750,6 @@ void use_function_with_leak7() { //CHECK: col9 //CHECK: file0 //CHECK: -//CHECK: ranges -//CHECK: -//CHECK: -//CHECK: -//CHECK: line131 -//CHECK: col9 -//CHECK: file0 -//CHECK: -//CHECK: -//CHECK: line131 -//CHECK: col9 -//CHECK: file0 -//CHECK: -//CHECK: -//CHECK: //CHECK: depth1 //CHECK: extended_message //CHECK: Memory is never released; potential leak of memory pointed to by 'x' @@ -4074,21 +3954,6 @@ void use_function_with_leak7() { //CHECK: col12 //CHECK: file0 //CHECK: -//CHECK: ranges -//CHECK: -//CHECK: -//CHECK: -//CHECK: line142 -//CHECK: col12 -//CHECK: file0 -//CHECK: -//CHECK: -//CHECK: line142 -//CHECK: col29 -//CHECK: file0 -//CHECK: -//CHECK: -//CHECK: //CHECK: depth1 //CHECK: extended_message //CHECK: Memory is never released; potential leak of memory pointed to by 'x' @@ -4293,21 +4158,6 @@ void use_function_with_leak7() { //CHECK: col5 //CHECK: file0 //CHECK: -//CHECK: ranges -//CHECK: -//CHECK: -//CHECK: -//CHECK: line153 -//CHECK: col5 -//CHECK: file0 -//CHECK: -//CHECK: -//CHECK: line153 -//CHECK: col23 -//CHECK: file0 -//CHECK: -//CHECK: -//CHECK: //CHECK: depth1 //CHECK: extended_message //CHECK: Memory is never released; potential leak of memory pointed to by 'x'