[clang-tidy] Also emit a warning for memset(x, 0, 0)

It doesn't make sense to suggest swapping the arguments here but it's
still useless code

llvm-svn: 213156
This commit is contained in:
Benjamin Kramer 2014-07-16 14:42:43 +00:00
parent ebab1500e0
commit d6a499077a
2 changed files with 10 additions and 4 deletions

View File

@ -57,13 +57,17 @@ void MemsetZeroLengthCheck::check(const MatchFinder::MatchResult &Result) {
const Expr *Arg2 = Call->getArg(2);
// Try to evaluate the second argument so we can also find values that are not
// just literals. We don't emit a warning if the second argument also
// evaluates to zero.
// just literals.
llvm::APSInt Value1, Value2;
if (!Arg2->EvaluateAsInt(Value2, *Result.Context) || Value2 != 0 ||
(Arg1->EvaluateAsInt(Value1, *Result.Context) && Value1 == 0))
if (!Arg2->EvaluateAsInt(Value2, *Result.Context) || Value2 != 0)
return;
// If both arguments evaluate to zero emit a warning without fix suggestions.
if (Arg1->EvaluateAsInt(Value1, *Result.Context) && Value1 == 0) {
diag(Call->getLocStart(), "memset of size zero");
return;
}
// Emit a warning and fix-its to swap the arguments.
auto D = diag(Call->getLocStart(),
"memset of size zero, potentially swapped arguments");

View File

@ -46,6 +46,8 @@ void foo(void *a, int xsize, int ysize) {
memset(a, -1, sizeof(int));
memset(a, 0xcd, 1);
memset(a, v, 0);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: memset of size zero
// CHECK-FIXES: memset(a, v, 0);
memtmpl<0>();
}