From d6a499077a2ec818af8657b3376de987d0b51e22 Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Wed, 16 Jul 2014 14:42:43 +0000 Subject: [PATCH] [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 --- .../clang-tidy/google/MemsetZeroLengthCheck.cpp | 12 ++++++++---- .../test/clang-tidy/google-memset-zero-length.cpp | 2 ++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/clang-tools-extra/clang-tidy/google/MemsetZeroLengthCheck.cpp b/clang-tools-extra/clang-tidy/google/MemsetZeroLengthCheck.cpp index 218a237..98c6246 100644 --- a/clang-tools-extra/clang-tidy/google/MemsetZeroLengthCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/MemsetZeroLengthCheck.cpp @@ -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"); diff --git a/clang-tools-extra/test/clang-tidy/google-memset-zero-length.cpp b/clang-tools-extra/test/clang-tidy/google-memset-zero-length.cpp index 262eb51..5b23271 100644 --- a/clang-tools-extra/test/clang-tidy/google-memset-zero-length.cpp +++ b/clang-tools-extra/test/clang-tidy/google-memset-zero-length.cpp @@ -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>(); } -- 2.7.4