[cfe-commits] r173227 - in /cfe/trunk: lib/Sema/SemaChecking.cpp test/SemaCXX/attr-nonnull.cpp

Nick Lewycky nicholas at mxc.ca
Tue Jan 22 21:08:30 PST 2013


Author: nicholas
Date: Tue Jan 22 23:08:29 2013
New Revision: 173227

URL: http://llvm.org/viewvc/llvm-project?rev=173227&view=rev
Log:
Make __attribute__((nonnull)) use the general expression evaluator to search for
nulls instead of limiting itself to the language-defined "null pointer
constant".

Modified:
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/SemaCXX/attr-nonnull.cpp

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=173227&r1=173226&r2=173227&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Tue Jan 22 23:08:29 2013
@@ -1837,8 +1837,20 @@
                                   e = NonNull->args_end();
        i != e; ++i) {
     const Expr *ArgExpr = ExprArgs[*i];
-    if (ArgExpr->isNullPointerConstant(Context,
-                                       Expr::NPC_ValueDependentIsNotNull))
+
+    // As a special case, transparent unions initialized with zero are
+    // considered null for the purposes of the nonnull attribute.
+    if (const RecordType *UT = ArgExpr->getType()->getAsUnionType()) {
+      if (UT->getDecl()->hasAttr<TransparentUnionAttr>())
+        if (const CompoundLiteralExpr *CLE =
+            dyn_cast<CompoundLiteralExpr>(ArgExpr))
+          if (const InitListExpr *ILE =
+              dyn_cast<InitListExpr>(CLE->getInitializer()))
+            ArgExpr = ILE->getInit(0);
+    }
+
+    bool Result;
+    if (ArgExpr->EvaluateAsBooleanCondition(Result, Context) && !Result)
       Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange();
   }
 }

Modified: cfe/trunk/test/SemaCXX/attr-nonnull.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/attr-nonnull.cpp?rev=173227&r1=173226&r2=173227&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/attr-nonnull.cpp (original)
+++ cfe/trunk/test/SemaCXX/attr-nonnull.cpp Tue Jan 22 23:08:29 2013
@@ -31,3 +31,12 @@
     f2(0, 0); // expected-warning{{null passed to a callee which requires a non-null argument}}
   }
 }
+
+namespace test3 {
+__attribute__((nonnull(1))) void f(void *ptr);
+
+void g() {
+  f(static_cast<char*>((void*)0));  // expected-warning{{null passed}}
+  f(static_cast<char*>(0));  // expected-warning{{null passed}}
+}
+}





More information about the cfe-commits mailing list