1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "AvoidCArraysCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
using namespace clang::ast_matchers;
namespace clang::tidy::modernize {
namespace {
AST_MATCHER(clang::TypeLoc, hasValidBeginLoc) {
return Node.getBeginLoc().isValid();
}
AST_MATCHER_P(clang::TypeLoc, hasType,
clang::ast_matchers::internal::Matcher<clang::Type>,
InnerMatcher) {
const clang::Type *TypeNode = Node.getTypePtr();
return TypeNode != nullptr &&
InnerMatcher.matches(*TypeNode, Finder, Builder);
}
AST_MATCHER(clang::RecordDecl, isExternCContext) {
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;
}
template <typename TargetType, typename NodeType>
const TargetType *getAs(const NodeType *Node) {
if constexpr (std::is_same_v<NodeType, clang::DynTypedNode>)
return Node->template get<TargetType>();
else
return llvm::dyn_cast<TargetType>(Node);
}
AST_MATCHER(clang::TypeLoc, isWithinImplicitTemplateInstantiation) {
const auto IsImplicitTemplateInstantiation = [](const auto *Node) {
const auto IsImplicitInstantiation = [](const auto *Node) {
return (Node != nullptr) && (Node->getTemplateSpecializationKind() ==
TSK_ImplicitInstantiation);
};
return (IsImplicitInstantiation(getAs<clang::CXXRecordDecl>(Node)) ||
IsImplicitInstantiation(getAs<clang::FunctionDecl>(Node)) ||
IsImplicitInstantiation(getAs<clang::VarDecl>(Node)));
};
DynTypedNodeList ParentNodes = Finder->getASTContext().getParents(Node);
const clang::NamedDecl *ParentDecl = nullptr;
while (!ParentNodes.empty()) {
const DynTypedNode &ParentNode = ParentNodes[0];
if (IsImplicitTemplateInstantiation(&ParentNode))
return true;
// in case of a `NamedDecl` as parent node, it is more efficient to proceed
// with the upward traversal via DeclContexts (see below) instead of via
// parent nodes
if ((ParentDecl = ParentNode.template get<clang::NamedDecl>()))
break;
ParentNodes = Finder->getASTContext().getParents(ParentNode);
}
if (ParentDecl != nullptr) {
const clang::DeclContext *DeclContext = ParentDecl->getDeclContext();
while (DeclContext != nullptr) {
if (IsImplicitTemplateInstantiation(DeclContext))
return true;
DeclContext = DeclContext->getParent();
}
}
return false;
}
} // namespace
AvoidCArraysCheck::AvoidCArraysCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
AllowStringArrays(Options.get("AllowStringArrays", false)) {}
void AvoidCArraysCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "AllowStringArrays", AllowStringArrays);
}
void AvoidCArraysCheck::registerMatchers(MatchFinder *Finder) {
ast_matchers::internal::Matcher<TypeLoc> IgnoreStringArrayIfNeededMatcher =
anything();
if (AllowStringArrays)
IgnoreStringArrayIfNeededMatcher =
unless(typeLoc(loc(hasCanonicalType(incompleteArrayType(
hasElementType(isAnyCharacter())))),
hasParent(varDecl(hasInitializer(stringLiteral()),
unless(parmVarDecl())))));
Finder->addMatcher(
typeLoc(hasValidBeginLoc(), hasType(arrayType()),
optionally(hasParent(parmVarDecl().bind("param_decl"))),
unless(anyOf(hasParent(parmVarDecl(isArgvOfMain())),
hasParent(varDecl(isExternC())),
hasParent(fieldDecl(
hasParent(recordDecl(isExternCContext())))),
hasAncestor(functionDecl(isExternC())),
isWithinImplicitTemplateInstantiation())),
IgnoreStringArrayIfNeededMatcher)
.bind("typeloc"),
this);
Finder->addMatcher(
templateArgumentLoc(hasTypeLoc(
typeLoc(hasType(arrayType())).bind("template_arg_array_typeloc"))),
this);
}
void AvoidCArraysCheck::check(const MatchFinder::MatchResult &Result) {
TypeLoc ArrayTypeLoc{};
if (const auto *MatchedTypeLoc = Result.Nodes.getNodeAs<TypeLoc>("typeloc"))
ArrayTypeLoc = *MatchedTypeLoc;
if (const auto *TemplateArgArrayTypeLoc =
Result.Nodes.getNodeAs<TypeLoc>("template_arg_array_typeloc"))
ArrayTypeLoc = *TemplateArgArrayTypeLoc;
assert(!ArrayTypeLoc.isNull());
const bool IsInParam =
Result.Nodes.getNodeAs<ParmVarDecl>("param_decl") != nullptr;
const bool IsVLA = ArrayTypeLoc.getTypePtr()->isVariableArrayType();
enum class RecommendType { Array, Vector, Span };
llvm::SmallVector<const char *> RecommendTypes{};
if (IsVLA) {
RecommendTypes.push_back("'std::vector'");
} else if (ArrayTypeLoc.getTypePtr()->isIncompleteArrayType() && IsInParam) {
// in function parameter, we also don't know the size of
// IncompleteArrayType.
if (Result.Context->getLangOpts().CPlusPlus20)
RecommendTypes.push_back("'std::span'");
else {
RecommendTypes.push_back("'std::array'");
RecommendTypes.push_back("'std::vector'");
}
} else {
RecommendTypes.push_back("'std::array'");
}
diag(ArrayTypeLoc.getBeginLoc(),
"do not declare %select{C-style|C VLA}0 arrays, use %1 instead")
<< IsVLA << llvm::join(RecommendTypes, " or ")
<< ArrayTypeLoc.getSourceRange();
}
} // namespace clang::tidy::modernize
|