diff options
author | George Burgess IV <george.burgess.iv@gmail.com> | 2015-12-14 22:00:49 +0000 |
---|---|---|
committer | George Burgess IV <george.burgess.iv@gmail.com> | 2015-12-14 22:00:49 +0000 |
commit | 8d141e0120f4099a61ce8ee3ee5d1e60721c4328 (patch) | |
tree | 8a236a6c6c7596749503de9d112c77f5dddb414b /clang/lib/Sema/Sema.cpp | |
parent | fa54acedd1fd1183143c0a7f4b77554fd8cdb1ec (diff) | |
download | bcm5719-llvm-8d141e0120f4099a61ce8ee3ee5d1e60721c4328.tar.gz bcm5719-llvm-8d141e0120f4099a61ce8ee3ee5d1e60721c4328.zip |
[Sema] Make nullness warnings appear in C++.
Given the following code:
int *_Nullable ptr;
int *_Nonnull nn = ptr;
...In C, clang will warn you about `nn = ptr`, because you're assigning
a nonnull pointer to a nullable pointer. In C++, clang issues no such
warning. This patch helps ensure that clang doesn't ever miss an
opportunity to complain about C++ code.
N.B. Though this patch has a differential revision link, the actual
review took place over email.
Differential Revision: http://reviews.llvm.org/D14938
llvm-svn: 255556
Diffstat (limited to 'clang/lib/Sema/Sema.cpp')
-rw-r--r-- | clang/lib/Sema/Sema.cpp | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp index b87fb52f4d2..39b8cc9f0c6 100644 --- a/clang/lib/Sema/Sema.cpp +++ b/clang/lib/Sema/Sema.cpp @@ -349,6 +349,20 @@ void Sema::PrintStats() const { AnalysisWarnings.PrintStats(); } +void Sema::diagnoseNullableToNonnullConversion(QualType DstType, + QualType SrcType, + SourceLocation Loc) { + Optional<NullabilityKind> ExprNullability = SrcType->getNullability(Context); + if (!ExprNullability || *ExprNullability != NullabilityKind::Nullable) + return; + + Optional<NullabilityKind> TypeNullability = DstType->getNullability(Context); + if (!TypeNullability || *TypeNullability != NullabilityKind::NonNull) + return; + + Diag(Loc, diag::warn_nullability_lost) << SrcType << DstType; +} + /// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. /// If there is already an implicit cast, merge into the existing one. /// The result is of the given category. @@ -372,18 +386,7 @@ 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; - } - } - } - } + diagnoseNullableToNonnullConversion(Ty, E->getType(), E->getLocStart()); QualType ExprTy = Context.getCanonicalType(E->getType()); QualType TypeTy = Context.getCanonicalType(Ty); |