From: Nick Lewycky Date: Wed, 23 Jan 2013 05:08:29 +0000 (+0000) Subject: Make __attribute__((nonnull)) use the general expression evaluator to search for X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3edf387e4338ff98d9874b4c1b41d787e49d6c2b;p=clang Make __attribute__((nonnull)) use the general expression evaluator to search for nulls instead of limiting itself to the language-defined "null pointer constant". git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173227 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index 3d4c5d3a27..0d8f764df5 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -1837,8 +1837,20 @@ Sema::CheckNonNullArguments(const NonNullAttr *NonNull, 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()) + if (const CompoundLiteralExpr *CLE = + dyn_cast(ArgExpr)) + if (const InitListExpr *ILE = + dyn_cast(CLE->getInitializer())) + ArgExpr = ILE->getInit(0); + } + + bool Result; + if (ArgExpr->EvaluateAsBooleanCondition(Result, Context) && !Result) Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange(); } } diff --git a/test/SemaCXX/attr-nonnull.cpp b/test/SemaCXX/attr-nonnull.cpp index 09c054c197..76e1b74068 100644 --- a/test/SemaCXX/attr-nonnull.cpp +++ b/test/SemaCXX/attr-nonnull.cpp @@ -31,3 +31,12 @@ namespace rdar8769025 { 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((void*)0)); // expected-warning{{null passed}} + f(static_cast(0)); // expected-warning{{null passed}} +} +}