]> granicus.if.org Git - clang/commitdiff
[analyzer] Do not highlight the range of the statement in case of leak.
authorAnna Zaks <ganna@apple.com>
Thu, 10 May 2012 01:37:40 +0000 (01:37 +0000)
committerAnna Zaks <ganna@apple.com>
Thu, 10 May 2012 01:37:40 +0000 (01:37 +0000)
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

lib/StaticAnalyzer/Checkers/MallocChecker.cpp
test/Analysis/malloc-plist.c

index 141518403344df3225d27489ad01d3d06cac6fba..4df77a845eb68f7f9bd3ce7ca062510aaa48db65 100644 (file)
@@ -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);
 }
 
index 8a36ab30856b20010ee9f696486dc71cdb24bc0a..248abc21c88955d81266d294bf7f41d7efda6180 100644 (file)
@@ -350,21 +350,6 @@ void use_function_with_leak7() {
 //CHECK:        <key>col</key><integer>5</integer>
 //CHECK:        <key>file</key><integer>0</integer>
 //CHECK:       </dict>
-//CHECK:       <key>ranges</key>
-//CHECK:       <array>
-//CHECK:         <array>
-//CHECK:          <dict>
-//CHECK:           <key>line</key><integer>14</integer>
-//CHECK:           <key>col</key><integer>5</integer>
-//CHECK:           <key>file</key><integer>0</integer>
-//CHECK:          </dict>
-//CHECK:          <dict>
-//CHECK:           <key>line</key><integer>14</integer>
-//CHECK:           <key>col</key><integer>6</integer>
-//CHECK:           <key>file</key><integer>0</integer>
-//CHECK:          </dict>
-//CHECK:         </array>
-//CHECK:       </array>
 //CHECK:       <key>depth</key><integer>0</integer>
 //CHECK:       <key>extended_message</key>
 //CHECK:       <string>Memory is never released; potential leak of memory pointed to by &apos;p&apos;</string>
@@ -910,21 +895,6 @@ void use_function_with_leak7() {
 //CHECK:        <key>col</key><integer>9</integer>
 //CHECK:        <key>file</key><integer>0</integer>
 //CHECK:       </dict>
-//CHECK:       <key>ranges</key>
-//CHECK:       <array>
-//CHECK:         <array>
-//CHECK:          <dict>
-//CHECK:           <key>line</key><integer>28</integer>
-//CHECK:           <key>col</key><integer>9</integer>
-//CHECK:           <key>file</key><integer>0</integer>
-//CHECK:          </dict>
-//CHECK:          <dict>
-//CHECK:           <key>line</key><integer>28</integer>
-//CHECK:           <key>col</key><integer>14</integer>
-//CHECK:           <key>file</key><integer>0</integer>
-//CHECK:          </dict>
-//CHECK:         </array>
-//CHECK:       </array>
 //CHECK:       <key>depth</key><integer>0</integer>
 //CHECK:       <key>extended_message</key>
 //CHECK:       <string>Memory is never released; potential leak of memory pointed to by &apos;buf&apos;</string>
@@ -2400,21 +2370,6 @@ void use_function_with_leak7() {
 //CHECK:        <key>col</key><integer>5</integer>
 //CHECK:        <key>file</key><integer>0</integer>
 //CHECK:       </dict>
-//CHECK:       <key>ranges</key>
-//CHECK:       <array>
-//CHECK:         <array>
-//CHECK:          <dict>
-//CHECK:           <key>line</key><integer>76</integer>
-//CHECK:           <key>col</key><integer>5</integer>
-//CHECK:           <key>file</key><integer>0</integer>
-//CHECK:          </dict>
-//CHECK:          <dict>
-//CHECK:           <key>line</key><integer>76</integer>
-//CHECK:           <key>col</key><integer>13</integer>
-//CHECK:           <key>file</key><integer>0</integer>
-//CHECK:          </dict>
-//CHECK:         </array>
-//CHECK:       </array>
 //CHECK:       <key>depth</key><integer>0</integer>
 //CHECK:       <key>extended_message</key>
 //CHECK:       <string>Memory is never released; potential leak of memory pointed to by &apos;buf&apos;</string>
@@ -2843,21 +2798,6 @@ void use_function_with_leak7() {
 //CHECK:        <key>col</key><integer>5</integer>
 //CHECK:        <key>file</key><integer>0</integer>
 //CHECK:       </dict>
-//CHECK:       <key>ranges</key>
-//CHECK:       <array>
-//CHECK:         <array>
-//CHECK:          <dict>
-//CHECK:           <key>line</key><integer>97</integer>
-//CHECK:           <key>col</key><integer>5</integer>
-//CHECK:           <key>file</key><integer>0</integer>
-//CHECK:          </dict>
-//CHECK:          <dict>
-//CHECK:           <key>line</key><integer>97</integer>
-//CHECK:           <key>col</key><integer>8</integer>
-//CHECK:           <key>file</key><integer>0</integer>
-//CHECK:          </dict>
-//CHECK:         </array>
-//CHECK:       </array>
 //CHECK:       <key>depth</key><integer>0</integer>
 //CHECK:       <key>extended_message</key>
 //CHECK:       <string>Memory is never released; potential leak of memory pointed to by &apos;m&apos;</string>
@@ -3062,21 +3002,6 @@ void use_function_with_leak7() {
 //CHECK:        <key>col</key><integer>5</integer>
 //CHECK:        <key>file</key><integer>0</integer>
 //CHECK:       </dict>
-//CHECK:       <key>ranges</key>
-//CHECK:       <array>
-//CHECK:         <array>
-//CHECK:          <dict>
-//CHECK:           <key>line</key><integer>102</integer>
-//CHECK:           <key>col</key><integer>5</integer>
-//CHECK:           <key>file</key><integer>0</integer>
-//CHECK:          </dict>
-//CHECK:          <dict>
-//CHECK:           <key>line</key><integer>102</integer>
-//CHECK:           <key>col</key><integer>11</integer>
-//CHECK:           <key>file</key><integer>0</integer>
-//CHECK:          </dict>
-//CHECK:         </array>
-//CHECK:       </array>
 //CHECK:       <key>depth</key><integer>1</integer>
 //CHECK:       <key>extended_message</key>
 //CHECK:       <string>Memory is never released; potential leak of memory pointed to by &apos;x&apos;</string>
@@ -3281,21 +3206,6 @@ void use_function_with_leak7() {
 //CHECK:        <key>col</key><integer>5</integer>
 //CHECK:        <key>file</key><integer>0</integer>
 //CHECK:       </dict>
-//CHECK:       <key>ranges</key>
-//CHECK:       <array>
-//CHECK:         <array>
-//CHECK:          <dict>
-//CHECK:           <key>line</key><integer>111</integer>
-//CHECK:           <key>col</key><integer>5</integer>
-//CHECK:           <key>file</key><integer>0</integer>
-//CHECK:          </dict>
-//CHECK:          <dict>
-//CHECK:           <key>line</key><integer>111</integer>
-//CHECK:           <key>col</key><integer>9</integer>
-//CHECK:           <key>file</key><integer>0</integer>
-//CHECK:          </dict>
-//CHECK:         </array>
-//CHECK:       </array>
 //CHECK:       <key>depth</key><integer>1</integer>
 //CHECK:       <key>extended_message</key>
 //CHECK:       <string>Memory is never released; potential leak of memory pointed to by &apos;x&apos;</string>
@@ -3568,21 +3478,6 @@ void use_function_with_leak7() {
 //CHECK:        <key>col</key><integer>9</integer>
 //CHECK:        <key>file</key><integer>0</integer>
 //CHECK:       </dict>
-//CHECK:       <key>ranges</key>
-//CHECK:       <array>
-//CHECK:         <array>
-//CHECK:          <dict>
-//CHECK:           <key>line</key><integer>120</integer>
-//CHECK:           <key>col</key><integer>9</integer>
-//CHECK:           <key>file</key><integer>0</integer>
-//CHECK:          </dict>
-//CHECK:          <dict>
-//CHECK:           <key>line</key><integer>120</integer>
-//CHECK:           <key>col</key><integer>9</integer>
-//CHECK:           <key>file</key><integer>0</integer>
-//CHECK:          </dict>
-//CHECK:         </array>
-//CHECK:       </array>
 //CHECK:       <key>depth</key><integer>1</integer>
 //CHECK:       <key>extended_message</key>
 //CHECK:       <string>Memory is never released; potential leak of memory pointed to by &apos;x&apos;</string>
@@ -3855,21 +3750,6 @@ void use_function_with_leak7() {
 //CHECK:        <key>col</key><integer>9</integer>
 //CHECK:        <key>file</key><integer>0</integer>
 //CHECK:       </dict>
-//CHECK:       <key>ranges</key>
-//CHECK:       <array>
-//CHECK:         <array>
-//CHECK:          <dict>
-//CHECK:           <key>line</key><integer>131</integer>
-//CHECK:           <key>col</key><integer>9</integer>
-//CHECK:           <key>file</key><integer>0</integer>
-//CHECK:          </dict>
-//CHECK:          <dict>
-//CHECK:           <key>line</key><integer>131</integer>
-//CHECK:           <key>col</key><integer>9</integer>
-//CHECK:           <key>file</key><integer>0</integer>
-//CHECK:          </dict>
-//CHECK:         </array>
-//CHECK:       </array>
 //CHECK:       <key>depth</key><integer>1</integer>
 //CHECK:       <key>extended_message</key>
 //CHECK:       <string>Memory is never released; potential leak of memory pointed to by &apos;x&apos;</string>
@@ -4074,21 +3954,6 @@ void use_function_with_leak7() {
 //CHECK:        <key>col</key><integer>12</integer>
 //CHECK:        <key>file</key><integer>0</integer>
 //CHECK:       </dict>
-//CHECK:       <key>ranges</key>
-//CHECK:       <array>
-//CHECK:         <array>
-//CHECK:          <dict>
-//CHECK:           <key>line</key><integer>142</integer>
-//CHECK:           <key>col</key><integer>12</integer>
-//CHECK:           <key>file</key><integer>0</integer>
-//CHECK:          </dict>
-//CHECK:          <dict>
-//CHECK:           <key>line</key><integer>142</integer>
-//CHECK:           <key>col</key><integer>29</integer>
-//CHECK:           <key>file</key><integer>0</integer>
-//CHECK:          </dict>
-//CHECK:         </array>
-//CHECK:       </array>
 //CHECK:       <key>depth</key><integer>1</integer>
 //CHECK:       <key>extended_message</key>
 //CHECK:       <string>Memory is never released; potential leak of memory pointed to by &apos;x&apos;</string>
@@ -4293,21 +4158,6 @@ void use_function_with_leak7() {
 //CHECK:        <key>col</key><integer>5</integer>
 //CHECK:        <key>file</key><integer>0</integer>
 //CHECK:       </dict>
-//CHECK:       <key>ranges</key>
-//CHECK:       <array>
-//CHECK:         <array>
-//CHECK:          <dict>
-//CHECK:           <key>line</key><integer>153</integer>
-//CHECK:           <key>col</key><integer>5</integer>
-//CHECK:           <key>file</key><integer>0</integer>
-//CHECK:          </dict>
-//CHECK:          <dict>
-//CHECK:           <key>line</key><integer>153</integer>
-//CHECK:           <key>col</key><integer>23</integer>
-//CHECK:           <key>file</key><integer>0</integer>
-//CHECK:          </dict>
-//CHECK:         </array>
-//CHECK:       </array>
 //CHECK:       <key>depth</key><integer>1</integer>
 //CHECK:       <key>extended_message</key>
 //CHECK:       <string>Memory is never released; potential leak of memory pointed to by &apos;x&apos;</string>