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

Benjamin Kramer benny.kra at googlemail.com
Wed Jul 16 07:42:46 PDT 2014


Author: d0k
Date: Wed Jul 16 09:42:43 2014
New Revision: 213156

URL: http://llvm.org/viewvc/llvm-project?rev=213156&view=rev
Log:
[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

Modified:
    clang-tools-extra/trunk/clang-tidy/google/MemsetZeroLengthCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/google-memset-zero-length.cpp

Modified: clang-tools-extra/trunk/clang-tidy/google/MemsetZeroLengthCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/MemsetZeroLengthCheck.cpp?rev=213156&r1=213155&r2=213156&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/google/MemsetZeroLengthCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/google/MemsetZeroLengthCheck.cpp Wed Jul 16 09:42:43 2014
@@ -57,13 +57,17 @@ void MemsetZeroLengthCheck::check(const
   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");

Modified: clang-tools-extra/trunk/test/clang-tidy/google-memset-zero-length.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/google-memset-zero-length.cpp?rev=213156&r1=213155&r2=213156&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/google-memset-zero-length.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/google-memset-zero-length.cpp Wed Jul 16 09:42:43 2014
@@ -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>();
 }





More information about the cfe-commits mailing list