Skip to content

Commit 29cfb29

Browse files
committed
[clang-tidy] do not diagn. array types in implicit templ. instantiations
So far, the clang-tidy check `modernize-avoid-c-arrays` also diagnosed array types for type template parameters even though no actual array type got written there but it got deduced to one. In such case, there is nothing a developer can do at that location to fix the diagnostic. Since actually, the location where the template got instantiated would have to be adjusted. And this is in most cases some totally distant code where implementers of a template do not have access to. Also adding suppressions to the declaration of the template is not an option since that would clutter the code unnecessarily and is in many cases also simply not possible. Hence, we propose to not diagnose any occurrence of an array type in an implicit instantiation of a template but rather at the point where template arguments involve array types.
1 parent a643ac4 commit 29cfb29

File tree

3 files changed

+243
-5
lines changed

3 files changed

+243
-5
lines changed

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

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

42+
bool isWithinImplicitTemplateInstantiation(const TypeLoc *MatchedTypeLoc,
43+
ASTContext *Context) {
44+
const auto IsImplicitTemplateInstantiation = [](const auto *Node) {
45+
return (Node != nullptr) &&
46+
(Node->getTemplateSpecializationKind() == TSK_ImplicitInstantiation);
47+
};
48+
49+
auto ParentNodes = Context->getParents(*MatchedTypeLoc);
50+
while (!ParentNodes.empty()) {
51+
const auto &ParentNode = ParentNodes[0];
52+
if (IsImplicitTemplateInstantiation(
53+
ParentNode.template get<clang::CXXRecordDecl>()) ||
54+
IsImplicitTemplateInstantiation(
55+
ParentNode.template get<clang::FunctionDecl>()) ||
56+
IsImplicitTemplateInstantiation(
57+
ParentNode.template get<clang::VarDecl>())) {
58+
return true;
59+
}
60+
ParentNodes = Context->getParents(ParentNode);
61+
}
62+
63+
return false;
64+
}
65+
4266
} // namespace
4367

4468
AvoidCArraysCheck::AvoidCArraysCheck(StringRef Name, ClangTidyContext *Context)
@@ -70,18 +94,41 @@ void AvoidCArraysCheck::registerMatchers(MatchFinder *Finder) {
7094
std::move(IgnoreStringArrayIfNeededMatcher))
7195
.bind("typeloc"),
7296
this);
97+
98+
Finder->addMatcher(templateArgumentLoc(hasTypeLoc(hasType(arrayType())))
99+
.bind("template_arg_with_array_type_loc"),
100+
this);
73101
}
74102

75103
void AvoidCArraysCheck::check(const MatchFinder::MatchResult &Result) {
76-
const auto *ArrayType = Result.Nodes.getNodeAs<TypeLoc>("typeloc");
104+
TypeLoc ArrayTypeLoc{};
105+
106+
if (const auto *MatchedTypeLoc = Result.Nodes.getNodeAs<TypeLoc>("typeloc");
107+
MatchedTypeLoc != nullptr && not(isWithinImplicitTemplateInstantiation(
108+
MatchedTypeLoc, Result.Context))) {
109+
ArrayTypeLoc = *MatchedTypeLoc;
110+
}
111+
112+
if (const auto *TemplateArgLoc = Result.Nodes.getNodeAs<TemplateArgumentLoc>(
113+
"template_arg_with_array_type_loc");
114+
TemplateArgLoc != nullptr &&
115+
TemplateArgLoc->getTypeSourceInfo() != nullptr) {
116+
ArrayTypeLoc = TemplateArgLoc->getTypeSourceInfo()->getTypeLoc();
117+
}
118+
119+
// check whether an actual array type got matched (see checks above)
120+
if (ArrayTypeLoc.isNull()) {
121+
return;
122+
}
123+
77124
const bool IsInParam =
78125
Result.Nodes.getNodeAs<ParmVarDecl>("param_decl") != nullptr;
79-
const bool IsVLA = ArrayType->getTypePtr()->isVariableArrayType();
126+
const bool IsVLA = ArrayTypeLoc.getTypePtr()->isVariableArrayType();
80127
enum class RecommendType { Array, Vector, Span };
81128
llvm::SmallVector<const char *> RecommendTypes{};
82129
if (IsVLA) {
83130
RecommendTypes.push_back("'std::vector'");
84-
} else if (ArrayType->getTypePtr()->isIncompleteArrayType() && IsInParam) {
131+
} else if (ArrayTypeLoc.getTypePtr()->isIncompleteArrayType() && IsInParam) {
85132
// in function parameter, we also don't know the size of
86133
// IncompleteArrayType.
87134
if (Result.Context->getLangOpts().CPlusPlus20)
@@ -93,9 +140,11 @@ void AvoidCArraysCheck::check(const MatchFinder::MatchResult &Result) {
93140
} else {
94141
RecommendTypes.push_back("'std::array'");
95142
}
96-
diag(ArrayType->getBeginLoc(),
143+
144+
diag(ArrayTypeLoc.getBeginLoc(),
97145
"do not declare %select{C-style|C VLA}0 arrays, use %1 instead")
98-
<< IsVLA << llvm::join(RecommendTypes, " or ");
146+
<< IsVLA << llvm::join(RecommendTypes, " or ")
147+
<< ArrayTypeLoc.getSourceRange();
99148
}
100149

101150
} // namespace clang::tidy::modernize

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,15 @@ const char array[] = {'n', 'a', 'm', 'e', '\0'};
77

88
void takeCharArray(const char name[]);
99
// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not declare C-style arrays, use 'std::array' or 'std::vector' instead [modernize-avoid-c-arrays]
10+
11+
template <typename T = const char[10], typename U = char[10], char[10]>
12+
// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
13+
// CHECK-MESSAGES: :[[@LINE-2]]:53: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
14+
// CHECK-MESSAGES: :[[@LINE-3]]:63: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
15+
void func() {}
16+
17+
template <typename T = const char[], typename U = char[], char[]>
18+
// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
19+
// CHECK-MESSAGES: :[[@LINE-2]]:51: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
20+
// CHECK-MESSAGES: :[[@LINE-3]]:59: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
21+
void fun() {}

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

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ array<int[4], 2> d;
3232
using k = int[4];
3333
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: do not declare C-style arrays, use 'std::array' instead
3434

35+
k ak;
36+
// no diagnostic expected here since no concrete c-array type is written here
37+
3538
array<k, 2> dk;
39+
// no diagnostic expected here since no concrete c-array type is written here
40+
41+
array<decltype(ak), 3> ek;
42+
// no diagnostic expected here since no concrete c-array type is written here
3643

3744
template <typename T>
3845
class unique_ptr {
@@ -92,3 +99,173 @@ const char name[] = "Some string";
9299

93100
void takeCharArray(const char name[]);
94101
// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not declare C-style arrays, use 'std::array' or 'std::vector' instead [modernize-avoid-c-arrays]
102+
103+
namespace std {
104+
template<class T, class U>
105+
struct is_same { constexpr static bool value{false}; };
106+
107+
template<class T>
108+
struct is_same<T, T> { constexpr static bool value{true}; };
109+
110+
template<class T, class U>
111+
constexpr bool is_same_v = is_same<T, U>::value;
112+
113+
template<class T> struct remove_const { typedef T type; };
114+
template<class T> struct remove_const<const T> { typedef T type; };
115+
116+
template<class T>
117+
using remove_const_t = typename remove_const<T>::type;
118+
119+
template<bool B, class T = void> struct enable_if {};
120+
template<class T> struct enable_if<true, T> { typedef T type; };
121+
122+
template< bool B, class T = void >
123+
using enable_if_t = typename enable_if<B, T>::type;
124+
}
125+
126+
// below, no array type findings are expected within the template parameter declarations since no array type gets written explicitly
127+
template <typename T,
128+
bool = std::is_same_v<T, int>,
129+
bool = std::is_same<T, int>::value,
130+
bool = std::is_same_v<std::remove_const_t<T>, int>,
131+
bool = std::is_same<std::remove_const_t<T>, int>::value,
132+
bool = std::is_same_v<typename std::remove_const<T>::type, int>,
133+
bool = std::is_same<typename std::remove_const<T>::type, int>::value,
134+
std::enable_if_t<not(std::is_same_v<std::remove_const_t<T>, int>) && not(std::is_same_v<typename std::remove_const<T>::type, char>), bool> = true,
135+
typename std::enable_if<not(std::is_same_v<std::remove_const_t<T>, int>) && not(std::is_same_v<typename std::remove_const<T>::type, char>), bool>::type = true,
136+
typename = std::enable_if_t<not(std::is_same_v<std::remove_const_t<T>, int>) && not(std::is_same_v<typename std::remove_const<T>::type, char>)>,
137+
typename = typename std::remove_const<T>::type,
138+
typename = std::remove_const_t<T>>
139+
class MyClassTemplate {
140+
public:
141+
// here, plenty of array type findings are expected for below template parameter declarations since array types get written explicitly
142+
template <typename U = T,
143+
bool = std::is_same_v<U, int[]>,
144+
// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
145+
bool = std::is_same<U, int[10]>::value,
146+
// CHECK-MESSAGES: :[[@LINE-1]]:36: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
147+
std::enable_if_t<not(std::is_same_v<std::remove_const_t<U>, int[]>) && not(std::is_same_v<typename std::remove_const<U>::type, char[10]>), bool> = true,
148+
// CHECK-MESSAGES: :[[@LINE-1]]:73: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
149+
// CHECK-MESSAGES: :[[@LINE-2]]:140: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
150+
typename = typename std::remove_const<int[10]>::type,
151+
// CHECK-MESSAGES: :[[@LINE-1]]:51: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
152+
typename = std::remove_const_t<int[]>>
153+
// CHECK-MESSAGES: :[[@LINE-1]]:44: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
154+
class MyInnerClassTemplate {
155+
public:
156+
MyInnerClassTemplate(const U&) {}
157+
private:
158+
U field[3];
159+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
160+
};
161+
162+
MyClassTemplate(const T&) {}
163+
164+
private:
165+
T field[7];
166+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
167+
};
168+
169+
// an explicit instantiation
170+
template
171+
class MyClassTemplate<int[2]>;
172+
// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
173+
174+
using MyArrayType = int[3];
175+
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
176+
177+
// another explicit instantiation
178+
template
179+
class MyClassTemplate<MyArrayType>;
180+
181+
// below, no array type findings are expected within the template parameter declarations since no array type gets written explicitly
182+
template <typename T,
183+
bool = std::is_same_v<T, int>,
184+
bool = std::is_same<T, int>::value,
185+
bool = std::is_same_v<std::remove_const_t<T>, int>,
186+
bool = std::is_same<std::remove_const_t<T>, int>::value,
187+
bool = std::is_same_v<typename std::remove_const<T>::type, int>,
188+
bool = std::is_same<typename std::remove_const<T>::type, int>::value,
189+
std::enable_if_t<not(std::is_same_v<std::remove_const_t<T>, int>) && not(std::is_same_v<typename std::remove_const<T>::type, char>), bool> = true,
190+
typename std::enable_if<not(std::is_same_v<std::remove_const_t<T>, int>) && not(std::is_same_v<typename std::remove_const<T>::type, char>), bool>::type = true,
191+
typename = std::enable_if_t<not(std::is_same_v<std::remove_const_t<T>, int>) && not(std::is_same_v<typename std::remove_const<T>::type, char>)>,
192+
typename = typename std::remove_const<T>::type,
193+
typename = std::remove_const_t<T>>
194+
void func(const T& param) {
195+
int array1[1];
196+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
197+
198+
T array2[2];
199+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
200+
201+
T value;
202+
}
203+
204+
// here, plenty of array type findings are expected for below template parameter declarations since array types get written explicitly
205+
template <typename T = int[],
206+
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
207+
bool = std::is_same_v<T, int[]>,
208+
// CHECK-MESSAGES: :[[@LINE-1]]:36: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
209+
bool = std::is_same<T, int[10]>::value,
210+
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
211+
std::enable_if_t<not(std::is_same_v<std::remove_const_t<T>, int[]>) && not(std::is_same_v<typename std::remove_const<T>::type, char[10]>), bool> = true,
212+
// CHECK-MESSAGES: :[[@LINE-1]]:71: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
213+
// CHECK-MESSAGES: :[[@LINE-2]]:138: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
214+
typename = typename std::remove_const<int[10]>::type,
215+
// CHECK-MESSAGES: :[[@LINE-1]]:49: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
216+
typename = std::remove_const_t<int[]>>
217+
// CHECK-MESSAGES: :[[@LINE-1]]:42: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
218+
void fun(const T& param) {
219+
int array3[3];
220+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
221+
222+
T array4[4];
223+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
224+
225+
T value;
226+
}
227+
228+
template<typename T>
229+
T some_constant{};
230+
231+
// explicit instantiations
232+
template
233+
int some_constant<int[5]>[5];
234+
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
235+
236+
template
237+
int some_constant<decltype(ak)>[4];
238+
// FIXME: why no diagnostic here?
239+
240+
MyArrayType mk;
241+
// no diagnostic expected here since no concrete c-array type is written here
242+
243+
// explicit specializations
244+
template<>
245+
int some_constant<int[7]>[7]{};
246+
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
247+
// CHECK-MESSAGES: :[[@LINE-2]]:19: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
248+
249+
template<>
250+
int some_constant<decltype(mk)>[3]{};
251+
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
252+
253+
void testArrayInTemplateType() {
254+
int t[10];
255+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
256+
257+
func(t);
258+
fun(t);
259+
260+
func<decltype(t)>({});
261+
fun<decltype(t)>({});
262+
263+
MyClassTemplate var1{t};
264+
MyClassTemplate<decltype(t)> var2{{}};
265+
266+
decltype(var1)::MyInnerClassTemplate var3{t};
267+
decltype(var1)::MyInnerClassTemplate<decltype(t)> var4{{}};
268+
269+
MyClassTemplate<decltype(t)>::MyInnerClassTemplate var5{t};
270+
MyClassTemplate<decltype(t)>::MyInnerClassTemplate<decltype(t)> var6{{}};
271+
}

0 commit comments

Comments
 (0)