[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
Tue Feb 5 13:58:44 PST 2019


lebedev.ri created this revision.
lebedev.ri added reviewers: JonasToth, aaron.ballman.
lebedev.ri added a project: clang-tools-extra.
Herald added a subscriber: xazax.hun.
Herald added a project: clang.

The check should ignore the main function, the program entry point.
It is not possible to use `std::array<>` for the `argv`.
The alternative is to use `char** argv`.

Fixes PR40604 <https://bugs.llvm.org/show_bug.cgi?id=40604>


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D57787

Files:
  clang-tidy/modernize/AvoidCArraysCheck.cpp
  test/clang-tidy/modernize-avoid-c-arrays.cpp


Index: test/clang-tidy/modernize-avoid-c-arrays.cpp
===================================================================
--- test/clang-tidy/modernize-avoid-c-arrays.cpp
+++ test/clang-tidy/modernize-avoid-c-arrays.cpp
@@ -86,3 +86,20 @@
   int j[1];
 };
 }
+
+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: 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 clang::FunctionDecl *FD = llvm::dyn_cast<clang::FunctionDecl>(DC);
+  if (!FD)
+    return false; // If not a function, then certainly not 'main'.
+  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.185400.patch
Type: text/x-patch
Size: 2073 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190205/9b0dbdb4/attachment.bin>


More information about the cfe-commits mailing list