Skip NonNull sema checks in unevaluated contexts.

Summary:
Currently when a function annotated with __attribute__((nonnull)) is called in an unevaluated context with a null argument a -Wnonnull warning is emitted. 
This warning seems like a false positive unless the call expression is potentially evaluated. Change this behavior so that the non-null warnings use DiagRuntimeBehavior so they wont emit when they won't be evaluated.

Reviewers: majnemer, rsmith

Subscribers: mclow.lists, cfe-commits

Differential Revision: http://reviews.llvm.org/D13408

llvm-svn: 249787
This commit is contained in:
Eric Fiselier 2015-10-09 00:17:57 +00:00
parent ebef256269
commit 18677d51e0
2 changed files with 5 additions and 1 deletions

View File

@ -1151,7 +1151,8 @@ static void CheckNonNullArgument(Sema &S,
const Expr *ArgExpr,
SourceLocation CallSiteLoc) {
if (CheckNonNullExpr(S, ArgExpr))
S.Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange();
S.DiagRuntimeBehavior(CallSiteLoc, ArgExpr,
S.PDiag(diag::warn_null_arg) << ArgExpr->getSourceRange());
}
bool Sema::GetFormatNSStringIdx(const FormatAttr *Format, unsigned &Idx) {

View File

@ -37,6 +37,9 @@ int * ret_nonnull() {
return 0; // expected-warning {{null returned from function that requires a non-null return value}}
}
#define SAFE_CALL(X) if (X) foo(X)
int main () {
foo(0); // expected-warning {{null passed to a callee that requires a non-null argument}}
(void)sizeof(foo(0)); // expect no diagnostic in unevaluated context.
SAFE_CALL(0); // expect no diagnostic for unreachable code.
}