summaryrefslogtreecommitdiffstats
path: root/clang/lib/Sema/Sema.cpp
diff options
context:
space:
mode:
authorDouglas Gregor <dgregor@apple.com>2015-06-19 18:13:19 +0000
committerDouglas Gregor <dgregor@apple.com>2015-06-19 18:13:19 +0000
commitb4866e85e5ffa0d352d496958582f0983172dc01 (patch)
treebfc790bef93a450379f8936f20589b99a524657b /clang/lib/Sema/Sema.cpp
parentbb3a01cfaf679413b5e9ee3f3187e06bb246dcf3 (diff)
downloadbcm5719-llvm-b4866e85e5ffa0d352d496958582f0983172dc01.tar.gz
bcm5719-llvm-b4866e85e5ffa0d352d496958582f0983172dc01.zip
Diagnose unsafe uses of nil and __nonnull pointers.
This generalizes the checking of null arguments to also work with values of pointer-to-function, reference-to-function, and block pointer type, using the nullability information within the underling function prototype to extend non-null checking, and diagnoses returns of 'nil' within a function with a __nonnull return type. Note that we don't warn about nil returns from Objective-C methods, because it's common for Objective-C methods to mimic the nil-swallowing behavior of the receiver by checking ostensibly non-null parameters and returning nil from otherwise non-null methods in that case. It also diagnoses (via a separate flag) conversions from nullable to nonnull pointers. It's a separate flag because this warning can be noisy. llvm-svn: 240153
Diffstat (limited to 'clang/lib/Sema/Sema.cpp')
-rw-r--r--clang/lib/Sema/Sema.cpp13
1 files changed, 13 insertions, 0 deletions
diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index 46fec7324bc..db1251f097f 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -356,6 +356,19 @@ ExprResult Sema::ImpCastExprToType(Expr *E, QualType Ty,
assert((VK == VK_RValue || !E->isRValue()) && "can't cast rvalue to lvalue");
#endif
+ // Check whether we're implicitly casting from a nullable type to a nonnull
+ // type.
+ if (auto exprNullability = E->getType()->getNullability(Context)) {
+ if (*exprNullability == NullabilityKind::Nullable) {
+ if (auto typeNullability = Ty->getNullability(Context)) {
+ if (*typeNullability == NullabilityKind::NonNull) {
+ Diag(E->getLocStart(), diag::warn_nullability_lost)
+ << E->getType() << Ty;
+ }
+ }
+ }
+ }
+
QualType ExprTy = Context.getCanonicalType(E->getType());
QualType TypeTy = Context.getCanonicalType(Ty);
OpenPOWER on IntegriCloud