[PATCH] D19769: [clang-tidy] Add explicitly given array size heuristic to misc-suspicious-missing-comma check.

Dominik Szabó via cfe-commits cfe-commits at lists.llvm.org
Sun May 1 03:08:58 PDT 2016


szdominik updated this revision to Diff 55741.
szdominik added a comment.

Implement the heuristic in a different (and simpler) way.
Based on the array fillers.


http://reviews.llvm.org/D19769

Files:
  clang-tidy/misc/SuspiciousMissingCommaCheck.cpp
  docs/clang-tidy/checks/misc-suspicious-missing-comma.rst
  test/clang-tidy/misc-suspicious-missing-comma.cpp

Index: test/clang-tidy/misc-suspicious-missing-comma.cpp
===================================================================
--- test/clang-tidy/misc-suspicious-missing-comma.cpp
+++ test/clang-tidy/misc-suspicious-missing-comma.cpp
@@ -80,3 +80,21 @@
   "Dummy line",
   "Dummy line",
 };
+
+// Missing comma or wrong explicit array size.
+const char* TheThreeMusketeers[4] = {
+  "Athos",
+  "Porthos",
+  "Aramis"
+  "D'Artagnan"
+};
+// CHECK-MESSAGES: :[[@LINE-6]]:3: warning: wrong string array initialization: the explicit given size and the real number of elements aren't equal [misc-suspicious-missing-comma]
+
+// Correctly given array size should avoid warning.
+const char* TheFourMusketeers[4] = {
+  "Athos",
+  "Porthos",
+  "Aramis",
+  "Charles de Batz de Castelmore"
+  "D'Artagnan"
+};
Index: docs/clang-tidy/checks/misc-suspicious-missing-comma.rst
===================================================================
--- docs/clang-tidy/checks/misc-suspicious-missing-comma.rst
+++ docs/clang-tidy/checks/misc-suspicious-missing-comma.rst
@@ -42,3 +42,14 @@
     "Warning %s",
   };
 
+This checker is also capable of warn on cases when the explicitly given array size
+isn't equal to the real array size.
+
+.. code:: c++
+
+  const char* TheThreeMusketeers[4] = {
+      "Athos",
+      "Porthos",
+      "Aramis"
+      "D'Artagnan"
+    };
Index: clang-tidy/misc/SuspiciousMissingCommaCheck.cpp
===================================================================
--- clang-tidy/misc/SuspiciousMissingCommaCheck.cpp
+++ clang-tidy/misc/SuspiciousMissingCommaCheck.cpp
@@ -100,6 +100,16 @@
       Result.Nodes.getNodeAs<StringLiteral>("str");
   assert(InitializerList && ConcatenatedLiteral);
   
+  // InitializerList has an array filler when the explicitly given size is
+  // bigger than the real array size - e.g. because there is a missing comma.
+  if (InitializerList->hasArrayFiller()) {
+      diag(InitializerList->getExprLoc(),
+           "wrong string array initialization: "
+           "the explicit given size and the "
+           "real number of elements aren't equal");
+      return;
+  }
+
   // Skip small arrays as they often generate false-positive.
   unsigned int Size = InitializerList->getNumInits();
   if (Size < SizeThreshold) return;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D19769.55741.patch
Type: text/x-patch
Size: 2289 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160501/e7771d75/attachment.bin>


More information about the cfe-commits mailing list