[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
Sat Apr 30 10:05:18 PDT 2016


szdominik created this revision.
szdominik added reviewers: alexfh, etienneb.
szdominik added subscribers: cfe-commits, xazax.hun.

Additional heuristic to misc-suspicious-missing-comma checker, based on the (in)equality of the explicitly given array size and the real array size.
Note: in these cases we don't know that the given size is wrong or there is a missing comma.

Original checker revision: http://reviews.llvm.org/D18457

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
@@ -86,9 +86,11 @@
   const auto ConcatenatedStringLiteral =
       stringLiteral(isConcatenatedLiteral(MaxConcatenatedTokens)).bind("str");
 
-  const auto StringsInitializerList =
-      initListExpr(hasType(constantArrayType()),
-                   has(expr(ignoringImpCasts(ConcatenatedStringLiteral))));
+  const auto StringsInitializerList = initListExpr(
+      hasType(constantArrayType()),
+      has(expr(ignoringImpCasts(ConcatenatedStringLiteral))),
+      hasParent(varDecl(allOf(hasType(arrayType()), isDefinition()))
+                    .bind("varDecl")));
 
   Finder->addMatcher(StringsInitializerList.bind("list"), this);
 }
@@ -98,10 +100,29 @@
   const auto *InitializerList = Result.Nodes.getNodeAs<InitListExpr>("list");
   const auto *ConcatenatedLiteral =
       Result.Nodes.getNodeAs<StringLiteral>("str");
-  assert(InitializerList && ConcatenatedLiteral);
-  
-  // Skip small arrays as they often generate false-positive.
+  const auto *VariableDeclaration = Result.Nodes.getNodeAs<VarDecl>("varDecl");
+  assert(InitializerList && ConcatenatedLiteral && VariableDeclaration);
+
   unsigned int Size = InitializerList->getNumInits();
+
+  // Warn when the explicit given array size isn't equal to the real array size.
+  QualType DeclType = VariableDeclaration->getTypeSourceInfo()->getType();
+  if (DeclType->isConstantArrayType()) {
+    unsigned int ExplicitArraySize = 0;
+    ExplicitArraySize =
+        ((Result.Context)->getAsConstantArrayType(DeclType)->getSize())
+            .getLimitedValue();
+
+    if (Size != ExplicitArraySize) {
+      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.
   if (Size < SizeThreshold) return;
 
   // Count the number of occurence of concatenated string literal.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D19769.55721.patch
Type: text/x-patch
Size: 3561 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160430/935d55c9/attachment-0001.bin>


More information about the cfe-commits mailing list