[Sema] Don't emit -Wmemset-transposed-args for memset(p,0,0)

Thanks to Arthur O'Dwyer for the suggestion!

llvm-svn: 337706
This commit is contained in:
Erik Pilkington 2018-07-23 16:24:14 +00:00
parent 307e5b31ce
commit c79b881ccd
2 changed files with 8 additions and 4 deletions

View File

@ -8788,11 +8788,14 @@ static void CheckMemaccessSize(Sema &S, unsigned BId, const CallExpr *Call) {
const Expr *SizeArg =
Call->getArg(BId == Builtin::BImemset ? 2 : 1)->IgnoreImpCasts();
auto isLiteralZero = [](const Expr *E) {
return isa<IntegerLiteral>(E) && cast<IntegerLiteral>(E)->getValue() == 0;
};
// If we're memsetting or bzeroing 0 bytes, then this is likely an error.
SourceLocation CallLoc = Call->getRParenLoc();
SourceManager &SM = S.getSourceManager();
if (isa<IntegerLiteral>(SizeArg) &&
cast<IntegerLiteral>(SizeArg)->getValue() == 0 &&
if (isLiteralZero(SizeArg) &&
!isArgumentExpandedFromMacro(SM, CallLoc, SizeArg->getExprLoc())) {
SourceLocation DiagLoc = SizeArg->getExprLoc();
@ -8804,7 +8807,7 @@ static void CheckMemaccessSize(Sema &S, unsigned BId, const CallExpr *Call) {
CallLoc, SM, S.getLangOpts()) == "bzero")) {
S.Diag(DiagLoc, diag::warn_suspicious_bzero_size);
S.Diag(DiagLoc, diag::note_suspicious_bzero_size_silence);
} else {
} else if (!isLiteralZero(Call->getArg(1)->IgnoreImpCasts())) {
S.Diag(DiagLoc, diag::warn_suspicious_sizeof_memset) << 0;
S.Diag(DiagLoc, diag::note_suspicious_sizeof_memset_silence) << 0;
}

View File

@ -22,6 +22,7 @@ int main() {
memset(array, (int)sizeof(array), (0)); // no warning
memset(array, (int)sizeof(array), 32); // no warning
memset(array, 32, (0)); // no warning
memset(array, 0, 0); // no warning
bzero(ptr, 0); // expected-warning{{'size' argument to bzero is '0'}} expected-note{{parenthesize the second argument to silence}}
real_bzero(ptr, 0); // expected-warning{{'size' argument to bzero is '0'}} expected-note{{parenthesize the second argument to silence}}
@ -55,6 +56,6 @@ void macros() {
__builtin_memset(array, 0, ZERO); // no warning
__builtin_bzero(array, ZERO);
__builtin_memset(array, 0, 0); // expected-warning{{'size' argument to memset}} // expected-note{{parenthesize}}
__builtin_memset(array, 1, 0); // expected-warning{{'size' argument to memset}} // expected-note{{parenthesize}}
__builtin_bzero(array, 0); // expected-warning{{'size' argument to bzero}} // expected-note{{parenthesize}}
}