diff options
author | Dávid Bolvanský <david.bolvansky@gmail.com> | 2019-11-07 20:47:09 +0100 |
---|---|---|
committer | Dávid Bolvanský <david.bolvansky@gmail.com> | 2019-11-07 22:43:27 +0100 |
commit | 01b10bc7b14963902405d202ac78cd88d44adbc5 (patch) | |
tree | 7f74a71a56d6973a5db7275673c1d46ea72eecb6 /clang/lib/Sema/SemaExpr.cpp | |
parent | b95bb0847a1ea366dda69901c24415e0d00a9527 (diff) | |
download | bcm5719-llvm-01b10bc7b14963902405d202ac78cd88d44adbc5.tar.gz bcm5719-llvm-01b10bc7b14963902405d202ac78cd88d44adbc5.zip |
[Diagnostics] Teach -Wnull-dereference about address_space attribute
Summary:
Clang should not warn for:
> test.c:2:12: warning: indirection of non-volatile null pointer will be deleted,
> not trap [-Wnull-dereference]
> return *(int __attribute__((address_space(256))) *) 0;
> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Solves PR42292.
Reviewers: aaron.ballman, rsmith
Reviewed By: aaron.ballman
Subscribers: cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D69664
Diffstat (limited to 'clang/lib/Sema/SemaExpr.cpp')
-rw-r--r-- | clang/lib/Sema/SemaExpr.cpp | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index e41cd5b6653..9e917eb7db4 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -481,16 +481,21 @@ static void CheckForNullPointerDereference(Sema &S, Expr *E) { // optimizer will delete, so warn about it. People sometimes try to use this // to get a deterministic trap and are surprised by clang's behavior. This // only handles the pattern "*null", which is a very syntactic check. - if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts())) - if (UO->getOpcode() == UO_Deref && - UO->getSubExpr()->IgnoreParenCasts()-> - isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull) && + const auto *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts()); + if (UO && UO->getOpcode() == UO_Deref) { + const LangAS AS = + UO->getSubExpr()->getType()->getPointeeType().getAddressSpace(); + if ((!isTargetAddressSpace(AS) || + (isTargetAddressSpace(AS) && toTargetAddressSpace(AS) == 0)) && + UO->getSubExpr()->IgnoreParenCasts()->isNullPointerConstant( + S.Context, Expr::NPC_ValueDependentIsNotNull) && !UO->getType().isVolatileQualified()) { - S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, - S.PDiag(diag::warn_indirection_through_null) - << UO->getSubExpr()->getSourceRange()); - S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, - S.PDiag(diag::note_indirection_through_null)); + S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, + S.PDiag(diag::warn_indirection_through_null) + << UO->getSubExpr()->getSourceRange()); + S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, + S.PDiag(diag::note_indirection_through_null)); + } } } |