[PATCH] D57787: [clang-tidy] modernize-avoid-c-arrays: avoid main function (PR40604)

Roman Lebedev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 6 10:31:31 PST 2019


lebedev.ri updated this revision to Diff 185587.
lebedev.ri marked 4 inline comments as done and an inline comment as not done.
lebedev.ri added a comment.

And back to `dyn_cast()`.


Repository:
  rCTE Clang Tools Extra

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D57787/new/

https://reviews.llvm.org/D57787

Files:
  clang-tidy/modernize/AvoidCArraysCheck.cpp
  docs/clang-tidy/checks/modernize-avoid-c-arrays.rst
  test/clang-tidy/modernize-avoid-c-arrays-ignores-main.cpp
  test/clang-tidy/modernize-avoid-c-arrays-ignores-three-arg-main.cpp


Index: test/clang-tidy/modernize-avoid-c-arrays-ignores-three-arg-main.cpp
===================================================================
--- /dev/null
+++ test/clang-tidy/modernize-avoid-c-arrays-ignores-three-arg-main.cpp
@@ -0,0 +1,20 @@
+// RUN: %check_clang_tidy %s modernize-avoid-c-arrays %t
+
+int not_main(int argc, char *argv[], char *argw[]) {
+  // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: do not declare C-style arrays, use std::array<> instead
+  // CHECK-MESSAGES: :[[@LINE-2]]:38: warning: do not declare C-style arrays, use std::array<> instead
+  int f4[] = {1, 2};
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use std::array<> instead
+}
+
+int main(int argc, char *argv[], char *argw[]) {
+  int f5[] = {1, 2};
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use std::array<> instead
+
+  auto not_main = [](int argc, char *argv[], char *argw[]) {
+    // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: do not declare C-style arrays, use std::array<> instead
+    // CHECK-MESSAGES: :[[@LINE-2]]:46: warning: do not declare C-style arrays, use std::array<> instead
+    int f6[] = {1, 2};
+    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not declare C-style arrays, use std::array<> instead
+  };
+}
Index: test/clang-tidy/modernize-avoid-c-arrays-ignores-main.cpp
===================================================================
--- /dev/null
+++ test/clang-tidy/modernize-avoid-c-arrays-ignores-main.cpp
@@ -0,0 +1,18 @@
+// RUN: %check_clang_tidy %s modernize-avoid-c-arrays %t
+
+int not_main(int argc, char *argv[]) {
+  // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: do not declare C-style arrays, use std::array<> instead
+  int f4[] = {1, 2};
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use std::array<> instead
+}
+
+int main(int argc, char *argv[]) {
+  int f5[] = {1, 2};
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use std::array<> instead
+
+  auto not_main = [](int argc, char *argv[]) {
+    // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: do not declare C-style arrays, use std::array<> instead
+    int f6[] = {1, 2};
+    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not declare C-style arrays, use std::array<> instead
+  };
+}
Index: docs/clang-tidy/checks/modernize-avoid-c-arrays.rst
===================================================================
--- docs/clang-tidy/checks/modernize-avoid-c-arrays.rst
+++ docs/clang-tidy/checks/modernize-avoid-c-arrays.rst
@@ -54,3 +54,7 @@
   }
 
   }
+
+Similarly, the ``main()`` function is ignored. Its second and third parameters
+can be either ``char* argv[]`` or ``char** argv``, but can not be
+``std::array<>``.
Index: clang-tidy/modernize/AvoidCArraysCheck.cpp
===================================================================
--- clang-tidy/modernize/AvoidCArraysCheck.cpp
+++ clang-tidy/modernize/AvoidCArraysCheck.cpp
@@ -30,6 +30,14 @@
   return Node.isExternCContext();
 }
 
+AST_MATCHER(clang::ParmVarDecl, isArgvOfMain) {
+  const clang::DeclContext *DC = Node.getDeclContext();
+  const auto *FD = llvm::dyn_cast<clang::FunctionDecl>(DC);
+  if (!FD) // If not a FunctionDecl, then certainly not main entry function.
+    return false;
+  return FD->isMain();
+}
+
 } // namespace
 
 namespace clang {
@@ -43,7 +51,8 @@
 
   Finder->addMatcher(
       typeLoc(hasValidBeginLoc(), hasType(arrayType()),
-              unless(anyOf(hasParent(varDecl(isExternC())),
+              unless(anyOf(hasParent(parmVarDecl(isArgvOfMain())),
+                           hasParent(varDecl(isExternC())),
                            hasParent(fieldDecl(
                                hasParent(recordDecl(isExternCContext())))),
                            hasAncestor(functionDecl(isExternC())))))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D57787.185587.patch
Type: text/x-patch
Size: 3826 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190206/277183b7/attachment-0001.bin>


More information about the cfe-commits mailing list