Skip to content

Commit 7399f70

Browse files
committed
Merging r353327:
------------------------------------------------------------------------ r353327 | lebedevri | 2019-02-06 20:17:30 +0100 (Wed, 06 Feb 2019) | 18 lines [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 ------------------------------------------------------------------------ llvm-svn: 353391
1 parent 33fc712 commit 7399f70

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ AST_MATCHER(clang::RecordDecl, isExternCContext) {
3131
return Node.isExternCContext();
3232
}
3333

34+
AST_MATCHER(clang::ParmVarDecl, isArgvOfMain) {
35+
const clang::DeclContext *DC = Node.getDeclContext();
36+
const auto *FD = llvm::dyn_cast<clang::FunctionDecl>(DC);
37+
return FD ? FD->isMain() : false;
38+
}
39+
3440
} // namespace
3541

3642
namespace clang {
@@ -44,7 +50,8 @@ void AvoidCArraysCheck::registerMatchers(MatchFinder *Finder) {
4450

4551
Finder->addMatcher(
4652
typeLoc(hasValidBeginLoc(), hasType(arrayType()),
47-
unless(anyOf(hasParent(varDecl(isExternC())),
53+
unless(anyOf(hasParent(parmVarDecl(isArgvOfMain())),
54+
hasParent(varDecl(isExternC())),
4855
hasParent(fieldDecl(
4956
hasParent(recordDecl(isExternCContext())))),
5057
hasAncestor(functionDecl(isExternC())))))

clang-tools-extra/docs/clang-tidy/checks/modernize-avoid-c-arrays.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,7 @@ such headers between C code, and C++ code.
5454
}
5555

5656
}
57+
58+
Similarly, the ``main()`` function is ignored. Its second and third parameters
59+
can be either ``char* argv[]`` or ``char** argv``, but can not be
60+
``std::array<>``.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %check_clang_tidy %s modernize-avoid-c-arrays %t
2+
3+
int not_main(int argc, char *argv[]) {
4+
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: do not declare C-style arrays, use std::array<> instead
5+
int f4[] = {1, 2};
6+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use std::array<> instead
7+
}
8+
9+
int main(int argc, char *argv[]) {
10+
int f5[] = {1, 2};
11+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use std::array<> instead
12+
13+
auto not_main = [](int argc, char *argv[]) {
14+
// CHECK-MESSAGES: :[[@LINE-1]]:32: warning: do not declare C-style arrays, use std::array<> instead
15+
int f6[] = {1, 2};
16+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not declare C-style arrays, use std::array<> instead
17+
};
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %check_clang_tidy %s modernize-avoid-c-arrays %t
2+
3+
int not_main(int argc, char *argv[], char *argw[]) {
4+
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: do not declare C-style arrays, use std::array<> instead
5+
// CHECK-MESSAGES: :[[@LINE-2]]:38: warning: do not declare C-style arrays, use std::array<> instead
6+
int f4[] = {1, 2};
7+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use std::array<> instead
8+
}
9+
10+
int main(int argc, char *argv[], char *argw[]) {
11+
int f5[] = {1, 2};
12+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use std::array<> instead
13+
14+
auto not_main = [](int argc, char *argv[], char *argw[]) {
15+
// CHECK-MESSAGES: :[[@LINE-1]]:32: warning: do not declare C-style arrays, use std::array<> instead
16+
// CHECK-MESSAGES: :[[@LINE-2]]:46: warning: do not declare C-style arrays, use std::array<> instead
17+
int f6[] = {1, 2};
18+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not declare C-style arrays, use std::array<> instead
19+
};
20+
}

0 commit comments

Comments
 (0)