[cfe-commits] r130003 - in /cfe/trunk: lib/StaticAnalyzer/Core/Environment.cpp lib/StaticAnalyzer/Core/ExprEngine.cpp test/Analysis/nullptr.cpp
Ted Kremenek
kremenek at apple.com
Fri Apr 22 11:01:30 PDT 2011
Author: kremenek
Date: Fri Apr 22 13:01:30 2011
New Revision: 130003
URL: http://llvm.org/viewvc/llvm-project?rev=130003&view=rev
Log:
Add static analyzer support for C++'0X nullptr. Patch by Jim Goodnow II.
Added:
cfe/trunk/test/Analysis/nullptr.cpp
Modified:
cfe/trunk/lib/StaticAnalyzer/Core/Environment.cpp
cfe/trunk/lib/StaticAnalyzer/Core/ExprEngine.cpp
Modified: cfe/trunk/lib/StaticAnalyzer/Core/Environment.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/Environment.cpp?rev=130003&r1=130002&r2=130003&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/Environment.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/Environment.cpp Fri Apr 22 13:01:30 2011
@@ -64,6 +64,9 @@
else
return svalBuilder.makeIntVal(cast<IntegerLiteral>(E));
}
+ // For special C0xx nullptr case, make a null pointer SVal.
+ case Stmt::CXXNullPtrLiteralExprClass:
+ return svalBuilder.makeNull();
case Stmt::ImplicitCastExprClass:
case Stmt::CXXFunctionalCastExprClass:
case Stmt::CStyleCastExprClass: {
Modified: cfe/trunk/lib/StaticAnalyzer/Core/ExprEngine.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/ExprEngine.cpp?rev=130003&r1=130002&r2=130003&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/ExprEngine.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/ExprEngine.cpp Fri Apr 22 13:01:30 2011
@@ -424,7 +424,6 @@
case Stmt::CXXCatchStmtClass:
case Stmt::CXXDependentScopeMemberExprClass:
case Stmt::CXXForRangeStmtClass:
- case Stmt::CXXNullPtrLiteralExprClass:
case Stmt::CXXPseudoDestructorExprClass:
case Stmt::CXXTemporaryObjectExprClass:
case Stmt::CXXThrowExprClass:
@@ -523,6 +522,7 @@
case Stmt::ExprWithCleanupsClass:
case Stmt::FloatingLiteralClass:
case Stmt::SizeOfPackExprClass:
+ case Stmt::CXXNullPtrLiteralExprClass:
Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
break;
Added: cfe/trunk/test/Analysis/nullptr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/nullptr.cpp?rev=130003&view=auto
==============================================================================
--- cfe/trunk/test/Analysis/nullptr.cpp (added)
+++ cfe/trunk/test/Analysis/nullptr.cpp Fri Apr 22 13:01:30 2011
@@ -0,0 +1,41 @@
+// RUN: %clang_cc1 -std=c++0x -analyze -analyzer-checker=core -analyzer-store region -verify %s
+
+// test to see if nullptr is detected as a null pointer
+void foo1(void) {
+ char *np = nullptr;
+ *np = 0; // expected-warning{{Dereference of null pointer}}
+}
+
+// check if comparing nullptr to nullptr is detected properly
+void foo2(void) {
+ char *np1 = nullptr;
+ char *np2 = np1;
+ char c;
+ if (np1 == np2)
+ np1 = &c;
+ *np1 = 0; // no-warning
+}
+
+// invoving a nullptr in a more complex operation should be cause a warning
+void foo3(void) {
+ struct foo {
+ int a, f;
+ };
+ char *np = nullptr;
+ // casting a nullptr to anything should be caught eventually
+ int *ip = &(((struct foo *)np)->f);
+ *ip = 0; // expected-warning{{Dereference of null pointer}}
+ // should be error here too, but analysis gets stopped
+// *np = 0;
+}
+
+// nullptr is implemented as a zero integer value, so should be able to compare
+void foo4(void) {
+ char *np = nullptr;
+ if (np != 0)
+ *np = 0; // no-warning
+ char *cp = 0;
+ if (np != cp)
+ *np = 0; // no-warning
+}
+
More information about the cfe-commits
mailing list