Skip to content

Commit f7399cf

Browse files
committed
use std::array<> or std::vector<>
1 parent 3912ba8 commit f7399cf

File tree

7 files changed

+28
-26
lines changed

7 files changed

+28
-26
lines changed

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,6 @@ AST_MATCHER(clang::ParmVarDecl, isArgvOfMain) {
3939
return FD ? FD->isMain() : false;
4040
}
4141

42-
AST_MATCHER_P(clang::TypeLoc, alwaysTrue,
43-
clang::ast_matchers::internal::Matcher<clang::TypeLoc>,
44-
InnerMatcher) {
45-
InnerMatcher.matches(Node, Finder, Builder);
46-
return true;
47-
}
48-
4942
} // namespace
5043

5144
AvoidCArraysCheck::AvoidCArraysCheck(StringRef Name, ClangTidyContext *Context)
@@ -68,7 +61,7 @@ void AvoidCArraysCheck::registerMatchers(MatchFinder *Finder) {
6861

6962
Finder->addMatcher(
7063
typeLoc(hasValidBeginLoc(), hasType(arrayType()),
71-
alwaysTrue(hasParent(parmVarDecl().bind("param_decl"))),
64+
optionally(hasParent(parmVarDecl().bind("param_decl"))),
7265
unless(anyOf(hasParent(parmVarDecl(isArgvOfMain())),
7366
hasParent(varDecl(isExternC())),
7467
hasParent(fieldDecl(
@@ -85,20 +78,25 @@ void AvoidCArraysCheck::check(const MatchFinder::MatchResult &Result) {
8578
Result.Nodes.getNodeAs<ParmVarDecl>("param_decl") != nullptr;
8679
const bool IsVLA = ArrayType->getTypePtr()->isVariableArrayType();
8780
enum class RecommendType { Array, Vector, Span };
88-
RecommendType RecommendType = RecommendType::Array;
81+
llvm::SmallVector<const char *> RecommendTypes{};
8982
if (IsVLA) {
90-
RecommendType = RecommendType::Vector;
83+
RecommendTypes.push_back("std::vector<>");
9184
} else if (ArrayType->getTypePtr()->isIncompleteArrayType() && IsInParam) {
9285
// in function parameter, we also don't know the size of
9386
// IncompleteArrayType.
94-
RecommendType = Result.Context->getLangOpts().CPlusPlus20
95-
? RecommendType::Span
96-
: RecommendType::Vector;
87+
if (Result.Context->getLangOpts().CPlusPlus20)
88+
RecommendTypes.push_back("std::span<>");
89+
else {
90+
RecommendTypes.push_back("std::array<>");
91+
RecommendTypes.push_back("std::vector<>");
92+
}
93+
} else {
94+
RecommendTypes.push_back("std::array<>");
9795
}
96+
llvm::errs() << llvm::join(RecommendTypes, " or ") << "\n";
9897
diag(ArrayType->getBeginLoc(),
99-
"do not declare %select{C-style|C VLA}0 arrays, use "
100-
"%select{std::array<>|std::vector<>|std::span<>}1 instead")
101-
<< IsVLA << static_cast<int>(RecommendType);
98+
"do not declare %select{C-style|C VLA}0 arrays, use %1 instead")
99+
<< IsVLA << llvm::join(RecommendTypes, " or ");
102100
}
103101

104102
} // namespace clang::tidy::modernize

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ Changes in existing checks
112112
the offending code with ``reinterpret_cast``, to more clearly express intent.
113113

114114
- Improved :doc:`modernize-avoid-c-arrays
115-
<clang-tidy/checks/modernize/avoid-c-arrays>` check to suggest use std::span
116-
as replacement of c array in C++20.
115+
<clang-tidy/checks/modernize/avoid-c-arrays>` check to suggest using
116+
``std::span`` as replacement of incomplete C array in C++20 and ``std::vector``
117+
in the versions before C++20.
117118

118119
- Improved :doc:`modernize-use-std-format
119120
<clang-tidy/checks/modernize/use-std-format>` check to support replacing

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ modernize-avoid-c-arrays
1010
Finds C-style array types and recommend to use ``std::array<>`` /
1111
``std::vector<>``. All types of C arrays are diagnosed.
1212

13+
For incomplete C-style array types appeared in parameters, It would be better to
14+
use ``std::span`` / ``gsl::span`` as replacement.
15+
1316
However, fix-it are potentially dangerous in header files and are therefore not
1417
emitted right now.
1518

clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-c-arrays-ignores-main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %check_clang_tidy -std=c++17 %s modernize-avoid-c-arrays %t
22

33
int not_main(int argc, char *argv[]) {
4-
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: do not declare C-style arrays, use std::vector<> instead
4+
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: do not declare C-style arrays, use std::array<> or std::vector<> instead
55
int f4[] = {1, 2};
66
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use std::array<> instead
77
}
@@ -11,7 +11,7 @@ int main(int argc, char *argv[]) {
1111
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use std::array<> instead
1212

1313
auto not_main = [](int argc, char *argv[]) {
14-
// CHECK-MESSAGES: :[[@LINE-1]]:32: warning: do not declare C-style arrays, use std::vector<> instead
14+
// CHECK-MESSAGES: :[[@LINE-1]]:32: warning: do not declare C-style arrays, use std::array<> or std::vector<> instead
1515
int f6[] = {1, 2};
1616
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not declare C-style arrays, use std::array<> instead
1717
};

clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-c-arrays-ignores-strings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ const char array[] = {'n', 'a', 'm', 'e', '\0'};
66
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not declare C-style arrays, use std::array<> instead [modernize-avoid-c-arrays]
77

88
void takeCharArray(const char name[]);
9-
// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not declare C-style arrays, use std::vector<> instead [modernize-avoid-c-arrays]
9+
// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not declare C-style arrays, use std::array<> or std::vector<> instead [modernize-avoid-c-arrays]

clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-c-arrays-ignores-three-arg-main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// RUN: %check_clang_tidy -std=c++17 %s modernize-avoid-c-arrays %t
22

33
int not_main(int argc, char *argv[], char *argw[]) {
4-
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: do not declare C-style arrays, use std::vector<> instead
5-
// CHECK-MESSAGES: :[[@LINE-2]]:38: warning: do not declare C-style arrays, use std::vector<> instead
4+
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: do not declare C-style arrays, use std::array<> or std::vector<> instead
5+
// CHECK-MESSAGES: :[[@LINE-2]]:38: warning: do not declare C-style arrays, use std::array<> or std::vector<> instead
66
int f4[] = {1, 2};
77
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use std::array<> instead
88
}
@@ -12,8 +12,8 @@ int main(int argc, char *argv[], char *argw[]) {
1212
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use std::array<> instead
1313

1414
auto not_main = [](int argc, char *argv[], char *argw[]) {
15-
// CHECK-MESSAGES: :[[@LINE-1]]:32: warning: do not declare C-style arrays, use std::vector<> instead
16-
// CHECK-MESSAGES: :[[@LINE-2]]:46: warning: do not declare C-style arrays, use std::vector<> instead
15+
// CHECK-MESSAGES: :[[@LINE-1]]:32: warning: do not declare C-style arrays, use std::array<> or std::vector<> instead
16+
// CHECK-MESSAGES: :[[@LINE-2]]:46: warning: do not declare C-style arrays, use std::array<> or std::vector<> instead
1717
int f6[] = {1, 2};
1818
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not declare C-style arrays, use std::array<> instead
1919
};

clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-c-arrays.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,4 @@ const char name[] = "Some string";
9191
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not declare C-style arrays, use std::array<> instead [modernize-avoid-c-arrays]
9292

9393
void takeCharArray(const char name[]);
94-
// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not declare C-style arrays, use std::vector<> instead [modernize-avoid-c-arrays]
94+
// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not declare C-style arrays, use std::array<> or std::vector<> instead [modernize-avoid-c-arrays]

0 commit comments

Comments
 (0)