[prev in list] [next in list] [prev in thread] [next in thread] 

List:       cfe-commits
Subject:    [PATCH] D19769: [clang-tidy] Add explicitly given array size heuristic to misc-suspicious-missing-co
From:       Dominik_Szabó via cfe-commits <cfe-commits () lists ! llvm ! org>
Date:       2016-04-30 17:05:18
Message-ID: differential-rev-PHID-DREV-da3wg66726thu272mix6-req () reviews ! llvm ! org
[Download RAW message or body]

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.


["D19769.55721.patch" (text/x-patch)]

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.


[Attachment #4 (text/plain)]

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic