diff options
author | Nick Lewycky <nicholas@mxc.ca> | 2013-01-23 05:08:29 +0000 |
---|---|---|
committer | Nick Lewycky <nicholas@mxc.ca> | 2013-01-23 05:08:29 +0000 |
commit | c77c8e95e3f3a3f9161da179abf4b59f5dc78e74 (patch) | |
tree | 7adfbb1b9cbc37e19f503beebc77d7d0c9d4de5e | |
parent | 761263bf47402a39205f79c168ef509371dd0a2e (diff) | |
download | bcm5719-llvm-c77c8e95e3f3a3f9161da179abf4b59f5dc78e74.tar.gz bcm5719-llvm-c77c8e95e3f3a3f9161da179abf4b59f5dc78e74.zip |
Make __attribute__((nonnull)) use the general expression evaluator to search for
nulls instead of limiting itself to the language-defined "null pointer
constant".
llvm-svn: 173227
-rw-r--r-- | clang/lib/Sema/SemaChecking.cpp | 16 | ||||
-rw-r--r-- | clang/test/SemaCXX/attr-nonnull.cpp | 9 |
2 files changed, 23 insertions, 2 deletions
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 3d4c5d3a27a..0d8f764df5d 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/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<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(); } } diff --git a/clang/test/SemaCXX/attr-nonnull.cpp b/clang/test/SemaCXX/attr-nonnull.cpp index 09c054c1977..76e1b74068c 100644 --- a/clang/test/SemaCXX/attr-nonnull.cpp +++ b/clang/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<char*>((void*)0)); // expected-warning{{null passed}} + f(static_cast<char*>(0)); // expected-warning{{null passed}} +} +} |