[cfe-commits] r126305 - in /cfe/trunk: lib/Analysis/CFG.cpp test/Sema/exprs.c

Ted Kremenek kremenek at apple.com
Tue Feb 22 21:11:46 PST 2011


Author: kremenek
Date: Tue Feb 22 23:11:46 2011
New Revision: 126305

URL: http://llvm.org/viewvc/llvm-project?rev=126305&view=rev
Log:
Teach CFGBuilder about null pointer constants in conditionals, and how they can be used to prune branches.  Fixes false null pointer dereference warning in PR 8183.

Modified:
    cfe/trunk/lib/Analysis/CFG.cpp
    cfe/trunk/test/Sema/exprs.c

Modified: cfe/trunk/lib/Analysis/CFG.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/CFG.cpp?rev=126305&r1=126304&r2=126305&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/CFG.cpp (original)
+++ cfe/trunk/lib/Analysis/CFG.cpp Tue Feb 22 23:11:46 2011
@@ -17,6 +17,7 @@
 #include "clang/AST/DeclCXX.h"
 #include "clang/AST/StmtVisitor.h"
 #include "clang/AST/PrettyPrinter.h"
+#include "clang/AST/CharUnits.h"
 #include "llvm/Support/GraphWriter.h"
 #include "llvm/Support/Allocator.h"
 #include "llvm/Support/Format.h"
@@ -413,9 +414,16 @@
 
     Expr::EvalResult Result;
     if (!S->isTypeDependent() && !S->isValueDependent() &&
-        S->Evaluate(Result, *Context) && Result.Val.isInt())
-      return Result.Val.getInt().getBoolValue();
-
+        S->Evaluate(Result, *Context)) {      
+      if (Result.Val.isInt())
+        return Result.Val.getInt().getBoolValue();
+      if (Result.Val.isLValue()) {
+        Expr *e = Result.Val.getLValueBase();
+        const CharUnits &c = Result.Val.getLValueOffset();        
+        if (!e && c.isZero())
+          return false;        
+      }
+    }
     return TryResult();
   }
 };

Modified: cfe/trunk/test/Sema/exprs.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/exprs.c?rev=126305&r1=126304&r2=126305&view=diff
==============================================================================
--- cfe/trunk/test/Sema/exprs.c (original)
+++ cfe/trunk/test/Sema/exprs.c Tue Feb 22 23:11:46 2011
@@ -18,6 +18,14 @@
   return 0;
 }
 
+// PR 8183 - Handle null pointer constants on the left-side of the '&&', and reason about
+// this when determining the reachability of the null pointer dereference on the right side.
+void pr8183(unsigned long long test)
+{
+  (void)((((void*)0)) && (*((unsigned long long*)(((void*)0))) = ((unsigned long long)((test)) % (unsigned long long)((1000000000)))));  // no-warning
+  (*((unsigned long long*)(((void*)0))) = ((unsigned long long)((test)) % (unsigned long long)((1000000000)))); // expected-warning {{indirection of non-volatile null pointer will be deleted, not trap}} expected-note {{consider using __builtin_trap() or qualifying pointer with 'volatile'}}
+}
+
 // PR1966
 _Complex double test1() {
   return __extension__ 1.0if;





More information about the cfe-commits mailing list