[clang-tools-extra] r353327 - [clang-tidy] modernize-avoid-c-arrays: avoid main function (PR40604)
Roman Lebedev via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 6 11:17:30 PST 2019
Author: lebedevri
Date: Wed Feb 6 11:17:30 2019
New Revision: 353327
URL: http://llvm.org/viewvc/llvm-project?rev=353327&view=rev
Log:
[clang-tidy] modernize-avoid-c-arrays: avoid main function (PR40604)
Summary:
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 [[ https://bugs.llvm.org/show_bug.cgi?id=40604 | PR40604 ]]
Reviewers: JonasToth, aaron.ballman
Reviewed By: aaron.ballman
Subscribers: xazax.hun, hans, cfe-commits
Tags: #clang-tools-extra, #clang
Differential Revision: https://reviews.llvm.org/D57787
Added:
clang-tools-extra/trunk/test/clang-tidy/modernize-avoid-c-arrays-ignores-main.cpp
clang-tools-extra/trunk/test/clang-tidy/modernize-avoid-c-arrays-ignores-three-arg-main.cpp
Modified:
clang-tools-extra/trunk/clang-tidy/modernize/AvoidCArraysCheck.cpp
clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-avoid-c-arrays.rst
Modified: clang-tools-extra/trunk/clang-tidy/modernize/AvoidCArraysCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/AvoidCArraysCheck.cpp?rev=353327&r1=353326&r2=353327&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/AvoidCArraysCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/AvoidCArraysCheck.cpp Wed Feb 6 11:17:30 2019
@@ -30,6 +30,12 @@ AST_MATCHER(clang::RecordDecl, isExternC
return Node.isExternCContext();
}
+AST_MATCHER(clang::ParmVarDecl, isArgvOfMain) {
+ const clang::DeclContext *DC = Node.getDeclContext();
+ const auto *FD = llvm::dyn_cast<clang::FunctionDecl>(DC);
+ return FD ? FD->isMain() : false;
+}
+
} // namespace
namespace clang {
@@ -43,7 +49,8 @@ void AvoidCArraysCheck::registerMatchers
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())))))
Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-avoid-c-arrays.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-avoid-c-arrays.rst?rev=353327&r1=353326&r2=353327&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-avoid-c-arrays.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-avoid-c-arrays.rst Wed Feb 6 11:17:30 2019
@@ -54,3 +54,7 @@ such headers between C code, and C++ cod
}
}
+
+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<>``.
Added: clang-tools-extra/trunk/test/clang-tidy/modernize-avoid-c-arrays-ignores-main.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-avoid-c-arrays-ignores-main.cpp?rev=353327&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-avoid-c-arrays-ignores-main.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-avoid-c-arrays-ignores-main.cpp Wed Feb 6 11:17:30 2019
@@ -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
+ };
+}
Added: clang-tools-extra/trunk/test/clang-tidy/modernize-avoid-c-arrays-ignores-three-arg-main.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-avoid-c-arrays-ignores-three-arg-main.cpp?rev=353327&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-avoid-c-arrays-ignores-three-arg-main.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-avoid-c-arrays-ignores-three-arg-main.cpp Wed Feb 6 11:17:30 2019
@@ -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
+ };
+}
More information about the cfe-commits
mailing list