[cfe-commits] r99236 - in /cfe/trunk: lib/Checker/DereferenceChecker.cpp test/Analysis/inline.c test/Analysis/null-deref-ps.c test/Analysis/plist-output.m

Ted Kremenek kremenek at apple.com
Mon Mar 22 18:11:38 PDT 2010


Author: kremenek
Date: Mon Mar 22 20:11:38 2010
New Revision: 99236

URL: http://llvm.org/viewvc/llvm-project?rev=99236&view=rev
Log:
Tweak null dereference diagnostics to give clearer diagnostics when
a null dereference results from a field access.

Modified:
    cfe/trunk/lib/Checker/DereferenceChecker.cpp
    cfe/trunk/test/Analysis/inline.c
    cfe/trunk/test/Analysis/null-deref-ps.c
    cfe/trunk/test/Analysis/plist-output.m

Modified: cfe/trunk/lib/Checker/DereferenceChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Checker/DereferenceChecker.cpp?rev=99236&r1=99235&r2=99236&view=diff
==============================================================================
--- cfe/trunk/lib/Checker/DereferenceChecker.cpp (original)
+++ cfe/trunk/lib/Checker/DereferenceChecker.cpp Mon Mar 22 20:11:38 2010
@@ -29,9 +29,9 @@
   DereferenceChecker() : BT_null(0), BT_undef(0) {}
   static void *getTag() { static int tag = 0; return &tag; }
   void VisitLocation(CheckerContext &C, const Stmt *S, SVal location);
-  
+
   std::pair<ExplodedNode * const*, ExplodedNode * const*>
-  getImplicitNodes() const {    
+  getImplicitNodes() const {
     return std::make_pair(ImplicitNullDerefNodes.data(),
                           ImplicitNullDerefNodes.data() +
                           ImplicitNullDerefNodes.size());
@@ -59,7 +59,7 @@
     if (ExplodedNode *N = C.GenerateSink()) {
       if (!BT_undef)
         BT_undef = new BuiltinBug("Dereference of undefined pointer value");
-      
+
       EnhancedBugReport *report =
         new EnhancedBugReport(*BT_undef, BT_undef->getDescription(), N);
       report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue,
@@ -68,31 +68,32 @@
     }
     return;
   }
-  
+
   DefinedOrUnknownSVal location = cast<DefinedOrUnknownSVal>(l);
-  
-  // Check for null dereferences.  
+
+  // Check for null dereferences.
   if (!isa<Loc>(location))
     return;
-  
+
   const GRState *state = C.getState();
   const GRState *notNullState, *nullState;
   llvm::tie(notNullState, nullState) = state->Assume(location);
-  
+
   // The explicit NULL case.
   if (nullState) {
-    if (!notNullState) {    
+    if (!notNullState) {
       // Generate an error node.
       ExplodedNode *N = C.GenerateSink(nullState);
       if (!N)
         return;
-      
+
       // We know that 'location' cannot be non-null.  This is what
-      // we call an "explicit" null dereference.        
+      // we call an "explicit" null dereference.
       if (!BT_null)
         BT_null = new BuiltinBug("Dereference of null pointer");
-      
+
       llvm::SmallString<100> buf;
+      llvm::SmallVector<SourceRange, 2> Ranges;
 
       switch (S->getStmtClass()) {
         case Stmt::UnaryOperatorClass: {
@@ -101,10 +102,26 @@
           if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(SU)) {
             if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
               llvm::raw_svector_ostream os(buf);
-              os << "Dereference of null pointer loaded from variable '"
-                 << VD->getName() << '\'';
+              os << "Dereference of null pointer (loaded from variable '"
+                 << VD->getName() << "')";
+              Ranges.push_back(DR->getSourceRange());
             }
           }
+          break;
+        }
+        case Stmt::MemberExprClass: {
+          const MemberExpr *M = cast<MemberExpr>(S);
+          if (M->isArrow())
+            if (DeclRefExpr *DR =
+                dyn_cast<DeclRefExpr>(M->getBase()->IgnoreParenCasts())) {
+              if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
+                llvm::raw_svector_ostream os(buf);
+                os << "Field access results in a dereference of a null pointer "
+                      "(loaded from variable '" << VD->getName() << "')";
+                Ranges.push_back(M->getBase()->getSourceRange());
+              }
+            }
+          break;
         }
         default:
           break;
@@ -117,19 +134,23 @@
 
       report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue,
                                 bugreporter::GetDerefExpr(N));
-      
+
+      for (llvm::SmallVectorImpl<SourceRange>::iterator
+            I = Ranges.begin(), E = Ranges.end(); I!=E; ++I)
+        report->addRange(*I);
+
       C.EmitReport(report);
       return;
     }
     else {
       // Otherwise, we have the case where the location could either be
       // null or not-null.  Record the error node as an "implicit" null
-      // dereference.      
+      // dereference.
       if (ExplodedNode *N = C.GenerateSink(nullState))
         ImplicitNullDerefNodes.push_back(N);
     }
   }
-  
+
   // From this point forward, we know that the location is not null.
   C.addTransition(notNullState);
 }

Modified: cfe/trunk/test/Analysis/inline.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/inline.c?rev=99236&r1=99235&r2=99236&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/inline.c (original)
+++ cfe/trunk/test/Analysis/inline.c Mon Mar 22 20:11:38 2010
@@ -15,6 +15,6 @@
   }
   if (x == 2) {
     int *p = 0;
-    *p = 3; // expected-warning{{Dereference of null pointer loaded from variable}}
+    *p = 3; // expected-warning{{Dereference of null pointer (loaded from variable 'p')}}
   }
 }

Modified: cfe/trunk/test/Analysis/null-deref-ps.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/null-deref-ps.c?rev=99236&r1=99235&r2=99236&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/null-deref-ps.c (original)
+++ cfe/trunk/test/Analysis/null-deref-ps.c Mon Mar 22 20:11:38 2010
@@ -26,7 +26,7 @@
   if (p)
     p->x = 1;
     
-  return p->x++; // expected-warning{{Dereference of null pointer}}
+  return p->x++; // expected-warning{{Field access results in a dereference of a null pointer (loaded from variable 'p')}}
 }
 
 int f3(char* x) {
@@ -57,7 +57,7 @@
     return 1;
     
   int *q = (int*) x;
-  return *q; // expected-warning{{Dereference of null pointer loaded from variable 'q'}}
+  return *q; // expected-warning{{Dereference of null pointer (loaded from variable 'q')}}
 }
 
 int f4_b() {

Modified: cfe/trunk/test/Analysis/plist-output.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/plist-output.m?rev=99236&r1=99235&r2=99236&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/plist-output.m (original)
+++ cfe/trunk/test/Analysis/plist-output.m Mon Mar 22 20:11:38 2010
@@ -124,7 +124,7 @@
 // CHECK:        <array>
 // CHECK:         <dict>
 // CHECK:          <key>line</key><integer>5</integer>
-// CHECK:          <key>col</key><integer>3</integer>
+// CHECK:          <key>col</key><integer>4</integer>
 // CHECK:          <key>file</key><integer>0</integer>
 // CHECK:         </dict>
 // CHECK:         <dict>
@@ -135,12 +135,12 @@
 // CHECK:        </array>
 // CHECK:      </array>
 // CHECK:      <key>extended_message</key>
-// CHECK:      <string>Dereference of null pointer loaded from variable 'p'</string>
+// CHECK:      <string>Dereference of null pointer (loaded from variable 'p')</string>
 // CHECK:      <key>message</key>
-// CHECK: <string>Dereference of null pointer loaded from variable 'p'</string>
+// CHECK: <string>Dereference of null pointer (loaded from variable 'p')</string>
 // CHECK:     </dict>
 // CHECK:    </array>
-// CHECK:    <key>description</key><string>Dereference of null pointer loaded from variable 'p'</string>
+// CHECK:    <key>description</key><string>Dereference of null pointer (loaded from variable 'p')</string>
 // CHECK:    <key>category</key><string>Logic error</string>
 // CHECK:    <key>type</key><string>Dereference of null pointer</string>
 // CHECK:   <key>location</key>
@@ -262,7 +262,7 @@
 // CHECK:        <array>
 // CHECK:         <dict>
 // CHECK:          <key>line</key><integer>11</integer>
-// CHECK:          <key>col</key><integer>3</integer>
+// CHECK:          <key>col</key><integer>4</integer>
 // CHECK:          <key>file</key><integer>0</integer>
 // CHECK:         </dict>
 // CHECK:         <dict>
@@ -273,12 +273,12 @@
 // CHECK:        </array>
 // CHECK:      </array>
 // CHECK:      <key>extended_message</key>
-// CHECK:      <string>Dereference of null pointer loaded from variable 'p'</string>
+// CHECK:      <string>Dereference of null pointer (loaded from variable 'p')</string>
 // CHECK:      <key>message</key>
-// CHECK: <string>Dereference of null pointer loaded from variable 'p'</string>
+// CHECK: <string>Dereference of null pointer (loaded from variable 'p')</string>
 // CHECK:     </dict>
 // CHECK:    </array>
-// CHECK:    <key>description</key><string>Dereference of null pointer loaded from variable 'p'</string>
+// CHECK:    <key>description</key><string>Dereference of null pointer (loaded from variable 'p')</string>
 // CHECK:    <key>category</key><string>Logic error</string>
 // CHECK:    <key>type</key><string>Dereference of null pointer</string>
 // CHECK:   <key>location</key>
@@ -400,7 +400,7 @@
 // CHECK:        <array>
 // CHECK:         <dict>
 // CHECK:          <key>line</key><integer>18</integer>
-// CHECK:          <key>col</key><integer>3</integer>
+// CHECK:          <key>col</key><integer>4</integer>
 // CHECK:          <key>file</key><integer>0</integer>
 // CHECK:         </dict>
 // CHECK:         <dict>
@@ -411,12 +411,12 @@
 // CHECK:        </array>
 // CHECK:      </array>
 // CHECK:      <key>extended_message</key>
-// CHECK:      <string>Dereference of null pointer loaded from variable 'q'</string>
+// CHECK:      <string>Dereference of null pointer (loaded from variable 'q')</string>
 // CHECK:      <key>message</key>
-// CHECK: <string>Dereference of null pointer loaded from variable 'q'</string>
+// CHECK: <string>Dereference of null pointer (loaded from variable 'q')</string>
 // CHECK:     </dict>
 // CHECK:    </array>
-// CHECK:    <key>description</key><string>Dereference of null pointer loaded from variable 'q'</string>
+// CHECK:    <key>description</key><string>Dereference of null pointer (loaded from variable 'q')</string>
 // CHECK:    <key>category</key><string>Logic error</string>
 // CHECK:    <key>type</key><string>Dereference of null pointer</string>
 // CHECK:   <key>location</key>
@@ -538,7 +538,7 @@
 // CHECK:        <array>
 // CHECK:         <dict>
 // CHECK:          <key>line</key><integer>23</integer>
-// CHECK:          <key>col</key><integer>5</integer>
+// CHECK:          <key>col</key><integer>6</integer>
 // CHECK:          <key>file</key><integer>0</integer>
 // CHECK:         </dict>
 // CHECK:         <dict>
@@ -549,12 +549,12 @@
 // CHECK:        </array>
 // CHECK:      </array>
 // CHECK:      <key>extended_message</key>
-// CHECK:      <string>Dereference of null pointer loaded from variable 'p'</string>
+// CHECK:      <string>Dereference of null pointer (loaded from variable 'p')</string>
 // CHECK:      <key>message</key>
-// CHECK: <string>Dereference of null pointer loaded from variable 'p'</string>
+// CHECK: <string>Dereference of null pointer (loaded from variable 'p')</string>
 // CHECK:     </dict>
 // CHECK:    </array>
-// CHECK:    <key>description</key><string>Dereference of null pointer loaded from variable 'p'</string>
+// CHECK:    <key>description</key><string>Dereference of null pointer (loaded from variable 'p')</string>
 // CHECK:    <key>category</key><string>Logic error</string>
 // CHECK:    <key>type</key><string>Dereference of null pointer</string>
 // CHECK:   <key>location</key>
@@ -710,7 +710,7 @@
 // CHECK:        <array>
 // CHECK:         <dict>
 // CHECK:          <key>line</key><integer>30</integer>
-// CHECK:          <key>col</key><integer>5</integer>
+// CHECK:          <key>col</key><integer>6</integer>
 // CHECK:          <key>file</key><integer>0</integer>
 // CHECK:         </dict>
 // CHECK:         <dict>
@@ -721,12 +721,12 @@
 // CHECK:        </array>
 // CHECK:      </array>
 // CHECK:      <key>extended_message</key>
-// CHECK:      <string>Dereference of null pointer loaded from variable 'p'</string>
+// CHECK:      <string>Dereference of null pointer (loaded from variable 'p')</string>
 // CHECK:      <key>message</key>
-// CHECK: <string>Dereference of null pointer loaded from variable 'p'</string>
+// CHECK: <string>Dereference of null pointer (loaded from variable 'p')</string>
 // CHECK:     </dict>
 // CHECK:    </array>
-// CHECK:    <key>description</key><string>Dereference of null pointer loaded from variable 'p'</string>
+// CHECK:    <key>description</key><string>Dereference of null pointer (loaded from variable 'p')</string>
 // CHECK:    <key>category</key><string>Logic error</string>
 // CHECK:    <key>type</key><string>Dereference of null pointer</string>
 // CHECK:   <key>location</key>





More information about the cfe-commits mailing list