Skip to content

Commit eb4360b

Browse files
authored
[clang-tidy][NFC] Fix llvm-prefer-static-over-anonymous-namespace warnings N/N (#165172)
Continue #153885.
1 parent dfbb515 commit eb4360b

File tree

10 files changed

+83
-88
lines changed

10 files changed

+83
-88
lines changed

clang-tools-extra/clang-tidy/bugprone/InvalidEnumDefaultInitializationCheck.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ namespace clang::tidy::bugprone {
2020

2121
namespace {
2222

23+
// Preserve same name as AST_MATCHER(isCompleteAndHasNoZeroValue)
24+
// NOLINTNEXTLINE(llvm-prefer-static-over-anonymous-namespace)
2325
bool isCompleteAndHasNoZeroValue(const EnumDecl *D) {
2426
const EnumDecl *Definition = D->getDefinition();
2527
return Definition && Definition->isComplete() &&

clang-tools-extra/clang-tidy/bugprone/SmartPtrArrayMismatchCheck.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ using namespace clang::ast_matchers;
1515

1616
namespace clang::tidy::bugprone {
1717

18-
namespace {
18+
static constexpr char ConstructExprN[] = "found_construct_expr";
19+
static constexpr char NewExprN[] = "found_new_expr";
20+
static constexpr char ConstructorN[] = "found_constructor";
1921

20-
constexpr char ConstructExprN[] = "found_construct_expr";
21-
constexpr char NewExprN[] = "found_new_expr";
22-
constexpr char ConstructorN[] = "found_constructor";
23-
24-
bool isInSingleDeclStmt(const DeclaratorDecl *D) {
22+
static bool isInSingleDeclStmt(const DeclaratorDecl *D) {
2523
const DynTypedNodeList Parents =
2624
D->getASTContext().getParentMapContext().getParents(*D);
2725
for (const DynTypedNode &PNode : Parents)
@@ -30,8 +28,8 @@ bool isInSingleDeclStmt(const DeclaratorDecl *D) {
3028
return false;
3129
}
3230

33-
const DeclaratorDecl *getConstructedVarOrField(const Expr *FoundConstructExpr,
34-
ASTContext &Ctx) {
31+
static const DeclaratorDecl *
32+
getConstructedVarOrField(const Expr *FoundConstructExpr, ASTContext &Ctx) {
3533
const DynTypedNodeList ConstructParents =
3634
Ctx.getParentMapContext().getParents(*FoundConstructExpr);
3735
if (ConstructParents.size() != 1)
@@ -43,8 +41,6 @@ const DeclaratorDecl *getConstructedVarOrField(const Expr *FoundConstructExpr,
4341
return nullptr;
4442
}
4543

46-
} // namespace
47-
4844
const char SmartPtrArrayMismatchCheck::PointerTypeN[] = "pointer_type";
4945

5046
SmartPtrArrayMismatchCheck::SmartPtrArrayMismatchCheck(

clang-tools-extra/clang-tidy/google/FunctionNamingCheck.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ using namespace clang::ast_matchers;
1414

1515
namespace clang::tidy::google::objc {
1616

17-
namespace {
18-
19-
std::string validFunctionNameRegex(bool RequirePrefix) {
17+
static std::string validFunctionNameRegex(bool RequirePrefix) {
2018
// Allow the following name patterns for all functions:
2119
// • ABFoo (prefix + UpperCamelCase)
2220
// • ABURL (prefix + capitalized acronym/initialism)
@@ -43,7 +41,7 @@ std::string validFunctionNameRegex(bool RequirePrefix) {
4341
/// For now we will only fix functions of static storage class with names like
4442
/// 'functionName' or 'function_name' and convert them to 'FunctionName'. For
4543
/// other cases the user must determine an appropriate name on their own.
46-
FixItHint generateFixItHint(const FunctionDecl *Decl) {
44+
static FixItHint generateFixItHint(const FunctionDecl *Decl) {
4745
// A fixit can be generated for functions of static storage class but
4846
// otherwise the check cannot determine the appropriate function name prefix
4947
// to use.
@@ -82,8 +80,6 @@ FixItHint generateFixItHint(const FunctionDecl *Decl) {
8280
return {};
8381
}
8482

85-
} // namespace
86-
8783
void FunctionNamingCheck::registerMatchers(MatchFinder *Finder) {
8884
// Enforce Objective-C function naming conventions on all functions except:
8985
// • Functions defined in system headers.

clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ struct OptionEnumMapping<misc::UseInternalLinkageCheck::FixModeKind> {
4343

4444
namespace clang::tidy::misc {
4545

46-
namespace {
47-
48-
AST_MATCHER(Decl, isFirstDecl) { return Node.isFirstDecl(); }
49-
50-
AST_MATCHER(FunctionDecl, hasBody) { return Node.hasBody(); }
51-
5246
static bool isInMainFile(SourceLocation L, SourceManager &SM,
5347
const FileExtensionsSet &HeaderFileExtensions) {
5448
for (;;) {
@@ -65,6 +59,12 @@ static bool isInMainFile(SourceLocation L, SourceManager &SM,
6559
}
6660
}
6761

62+
namespace {
63+
64+
AST_MATCHER(Decl, isFirstDecl) { return Node.isFirstDecl(); }
65+
66+
AST_MATCHER(FunctionDecl, hasBody) { return Node.hasBody(); }
67+
6868
AST_MATCHER_P(Decl, isAllRedeclsInMainFile, FileExtensionsSet,
6969
HeaderFileExtensions) {
7070
return llvm::all_of(Node.redecls(), [&](const Decl *D) {

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ using namespace clang::ast_matchers;
1515

1616
namespace clang::tidy::modernize {
1717

18+
template <typename TargetType, typename NodeType>
19+
static const TargetType *getAs(const NodeType *Node) {
20+
if constexpr (std::is_same_v<NodeType, clang::DynTypedNode>)
21+
return Node->template get<TargetType>();
22+
else
23+
return llvm::dyn_cast<TargetType>(Node);
24+
}
25+
1826
namespace {
1927

2028
AST_MATCHER(clang::TypeLoc, hasValidBeginLoc) {
@@ -39,14 +47,6 @@ AST_MATCHER(clang::ParmVarDecl, isArgvOfMain) {
3947
return FD ? FD->isMain() : false;
4048
}
4149

42-
template <typename TargetType, typename NodeType>
43-
const TargetType *getAs(const NodeType *Node) {
44-
if constexpr (std::is_same_v<NodeType, clang::DynTypedNode>)
45-
return Node->template get<TargetType>();
46-
else
47-
return llvm::dyn_cast<TargetType>(Node);
48-
}
49-
5050
AST_MATCHER(clang::TypeLoc, isWithinImplicitTemplateInstantiation) {
5151
const auto IsImplicitTemplateInstantiation = [](const auto *Node) {
5252
const auto IsImplicitInstantiation = [](const auto *Node) {

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

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@ using namespace clang::ast_matchers;
2020
using namespace clang::ast_matchers::internal;
2121

2222
namespace clang::tidy::modernize {
23-
namespace {
2423

25-
const char IteratorDeclStmtId[] = "iterator_decl";
26-
const char DeclWithNewId[] = "decl_new";
27-
const char DeclWithCastId[] = "decl_cast";
28-
const char DeclWithTemplateCastId[] = "decl_template";
24+
static const char IteratorDeclStmtId[] = "iterator_decl";
25+
static const char DeclWithNewId[] = "decl_new";
26+
static const char DeclWithCastId[] = "decl_cast";
27+
static const char DeclWithTemplateCastId[] = "decl_template";
2928

30-
size_t getTypeNameLength(bool RemoveStars, StringRef Text) {
29+
static size_t getTypeNameLength(bool RemoveStars, StringRef Text) {
3130
enum CharType { Space, Alpha, Punctuation };
3231
CharType LastChar = Space, BeforeSpace = Punctuation;
3332
size_t NumChars = 0;
@@ -54,6 +53,7 @@ size_t getTypeNameLength(bool RemoveStars, StringRef Text) {
5453
return NumChars;
5554
}
5655

56+
namespace {
5757
/// Matches variable declarations that have explicit initializers that
5858
/// are not initializer lists.
5959
///
@@ -65,7 +65,7 @@ size_t getTypeNameLength(bool RemoveStars, StringRef Text) {
6565
/// MyType C;
6666
/// \endcode
6767
///
68-
/// varDecl(hasWrittenNonListInitializer()) maches \c I and \c A but not \c B
68+
/// varDecl(hasWrittenNonListInitializer()) matches \c I and \c A but not \c B
6969
/// or \c C.
7070
AST_MATCHER(VarDecl, hasWrittenNonListInitializer) {
7171
const Expr *Init = Node.getAnyInitializer();
@@ -108,6 +108,15 @@ AST_MATCHER_P(QualType, isSugarFor, Matcher<QualType>, SugarMatcher) {
108108
}
109109
}
110110

111+
/// Matches declaration reference or member expressions with explicit template
112+
/// arguments.
113+
AST_POLYMORPHIC_MATCHER(hasExplicitTemplateArgs,
114+
AST_POLYMORPHIC_SUPPORTED_TYPES(DeclRefExpr,
115+
MemberExpr)) {
116+
return Node.hasExplicitTemplateArgs();
117+
}
118+
} // namespace
119+
111120
/// Matches named declarations that have one of the standard iterator
112121
/// names: iterator, reverse_iterator, const_iterator, const_reverse_iterator.
113122
///
@@ -118,7 +127,7 @@ AST_MATCHER_P(QualType, isSugarFor, Matcher<QualType>, SugarMatcher) {
118127
/// \endcode
119128
///
120129
/// namedDecl(hasStdIteratorName()) matches \c I and \c CI.
121-
Matcher<NamedDecl> hasStdIteratorName() {
130+
static Matcher<NamedDecl> hasStdIteratorName() {
122131
static const StringRef IteratorNames[] = {"iterator", "reverse_iterator",
123132
"const_iterator",
124133
"const_reverse_iterator"};
@@ -137,7 +146,7 @@ Matcher<NamedDecl> hasStdIteratorName() {
137146
///
138147
/// recordDecl(hasStdContainerName()) matches \c vector and \c forward_list
139148
/// but not \c my_vec.
140-
Matcher<NamedDecl> hasStdContainerName() {
149+
static Matcher<NamedDecl> hasStdContainerName() {
141150
static StringRef ContainerNames[] = {"array", "deque",
142151
"forward_list", "list",
143152
"vector",
@@ -154,37 +163,29 @@ Matcher<NamedDecl> hasStdContainerName() {
154163
return hasAnyName(ContainerNames);
155164
}
156165

157-
/// Matches declaration reference or member expressions with explicit template
158-
/// arguments.
159-
AST_POLYMORPHIC_MATCHER(hasExplicitTemplateArgs,
160-
AST_POLYMORPHIC_SUPPORTED_TYPES(DeclRefExpr,
161-
MemberExpr)) {
162-
return Node.hasExplicitTemplateArgs();
163-
}
164-
165166
/// Returns a DeclarationMatcher that matches standard iterators nested
166167
/// inside records with a standard container name.
167-
DeclarationMatcher standardIterator() {
168+
static DeclarationMatcher standardIterator() {
168169
return decl(
169170
namedDecl(hasStdIteratorName()),
170171
hasDeclContext(recordDecl(hasStdContainerName(), isInStdNamespace())));
171172
}
172173

173174
/// Returns a TypeMatcher that matches typedefs for standard iterators
174175
/// inside records with a standard container name.
175-
TypeMatcher typedefIterator() {
176+
static TypeMatcher typedefIterator() {
176177
return typedefType(hasDeclaration(standardIterator()));
177178
}
178179

179180
/// Returns a TypeMatcher that matches records named for standard
180181
/// iterators nested inside records named for standard containers.
181-
TypeMatcher nestedIterator() {
182+
static TypeMatcher nestedIterator() {
182183
return recordType(hasDeclaration(standardIterator()));
183184
}
184185

185186
/// Returns a TypeMatcher that matches types declared with using
186187
/// declarations and which name standard iterators for standard containers.
187-
TypeMatcher iteratorFromUsingDeclaration() {
188+
static TypeMatcher iteratorFromUsingDeclaration() {
188189
auto HasIteratorDecl = hasDeclaration(namedDecl(hasStdIteratorName()));
189190
// Unwrap the nested name specifier to test for one of the standard
190191
// containers.
@@ -198,7 +199,7 @@ TypeMatcher iteratorFromUsingDeclaration() {
198199

199200
/// This matcher returns declaration statements that contain variable
200201
/// declarations with written non-list initializer for standard iterators.
201-
StatementMatcher makeIteratorDeclMatcher() {
202+
static StatementMatcher makeIteratorDeclMatcher() {
202203
return declStmt(unless(has(
203204
varDecl(anyOf(unless(hasWrittenNonListInitializer()),
204205
unless(hasType(isSugarFor(anyOf(
@@ -207,7 +208,7 @@ StatementMatcher makeIteratorDeclMatcher() {
207208
.bind(IteratorDeclStmtId);
208209
}
209210

210-
StatementMatcher makeDeclWithNewMatcher() {
211+
static StatementMatcher makeDeclWithNewMatcher() {
211212
return declStmt(
212213
unless(has(varDecl(anyOf(
213214
unless(hasInitializer(ignoringParenImpCasts(cxxNewExpr()))),
@@ -225,13 +226,13 @@ StatementMatcher makeDeclWithNewMatcher() {
225226
.bind(DeclWithNewId);
226227
}
227228

228-
StatementMatcher makeDeclWithCastMatcher() {
229+
static StatementMatcher makeDeclWithCastMatcher() {
229230
return declStmt(
230231
unless(has(varDecl(unless(hasInitializer(explicitCastExpr()))))))
231232
.bind(DeclWithCastId);
232233
}
233234

234-
StatementMatcher makeDeclWithTemplateCastMatcher() {
235+
static StatementMatcher makeDeclWithTemplateCastMatcher() {
235236
auto ST =
236237
substTemplateTypeParmType(hasReplacementType(equalsBoundNode("arg")));
237238

@@ -252,7 +253,7 @@ StatementMatcher makeDeclWithTemplateCastMatcher() {
252253
.bind(DeclWithTemplateCastId);
253254
}
254255

255-
StatementMatcher makeCombinedMatcher() {
256+
static StatementMatcher makeCombinedMatcher() {
256257
return declStmt(
257258
// At least one varDecl should be a child of the declStmt to ensure
258259
// it's a declaration list and avoid matching other declarations,
@@ -265,8 +266,6 @@ StatementMatcher makeCombinedMatcher() {
265266
makeDeclWithCastMatcher(), makeDeclWithTemplateCastMatcher()));
266267
}
267268

268-
} // namespace
269-
270269
UseAutoCheck::UseAutoCheck(StringRef Name, ClangTidyContext *Context)
271270
: ClangTidyCheck(Name, Context),
272271
MinTypeNameLength(Options.get("MinTypeNameLength", 5)),

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ static constexpr char StrictCppStandardComplianceName[] =
4040
"StrictCppStandardCompliance";
4141
static constexpr bool StrictCppStandardComplianceDefault = true;
4242

43+
static unsigned getNumberOfDesignated(const InitListExpr *SyntacticInitList) {
44+
return llvm::count_if(*SyntacticInitList, [](auto *InitExpr) {
45+
return isa<DesignatedInitExpr>(InitExpr);
46+
});
47+
}
48+
4349
namespace {
4450

4551
struct Designators {
@@ -74,12 +80,6 @@ struct Designators {
7480
}
7581
};
7682

77-
unsigned getNumberOfDesignated(const InitListExpr *SyntacticInitList) {
78-
return llvm::count_if(*SyntacticInitList, [](auto *InitExpr) {
79-
return isa<DesignatedInitExpr>(InitExpr);
80-
});
81-
}
82-
8383
AST_MATCHER(CXXRecordDecl, isAggregate) {
8484
return Node.hasDefinition() && Node.isAggregate();
8585
}

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,41 +81,44 @@ AST_MATCHER(CXXMemberCallExpr, hasSameNumArgsAsDeclNumParams) {
8181
AST_MATCHER(DeclRefExpr, hasExplicitTemplateArgs) {
8282
return Node.hasExplicitTemplateArgs();
8383
}
84+
} // namespace
8485

8586
// Helper Matcher which applies the given QualType Matcher either directly or by
8687
// resolving a pointer type to its pointee. Used to match v.push_back() as well
8788
// as p->push_back().
88-
auto hasTypeOrPointeeType(
89+
static auto hasTypeOrPointeeType(
8990
const ast_matchers::internal::Matcher<QualType> &TypeMatcher) {
9091
return anyOf(hasType(TypeMatcher),
9192
hasType(pointerType(pointee(TypeMatcher))));
9293
}
9394

9495
// Matches if the node has canonical type matching any of the given names.
95-
auto hasWantedType(llvm::ArrayRef<StringRef> TypeNames) {
96+
static auto hasWantedType(llvm::ArrayRef<StringRef> TypeNames) {
9697
return hasCanonicalType(hasDeclaration(cxxRecordDecl(hasAnyName(TypeNames))));
9798
}
9899

99100
// Matches member call expressions of the named method on the listed container
100101
// types.
101-
auto cxxMemberCallExprOnContainer(StringRef MethodName,
102-
llvm::ArrayRef<StringRef> ContainerNames) {
102+
static auto
103+
cxxMemberCallExprOnContainer(StringRef MethodName,
104+
llvm::ArrayRef<StringRef> ContainerNames) {
103105
return cxxMemberCallExpr(
104106
hasDeclaration(functionDecl(hasName(MethodName))),
105107
on(hasTypeOrPointeeType(hasWantedType(ContainerNames))));
106108
}
107109

108-
const auto DefaultContainersWithPushBack =
110+
static const auto DefaultContainersWithPushBack =
109111
"::std::vector; ::std::list; ::std::deque";
110-
const auto DefaultContainersWithPush =
112+
static const auto DefaultContainersWithPush =
111113
"::std::stack; ::std::queue; ::std::priority_queue";
112-
const auto DefaultContainersWithPushFront =
114+
static const auto DefaultContainersWithPushFront =
113115
"::std::forward_list; ::std::list; ::std::deque";
114-
const auto DefaultSmartPointers =
116+
static const auto DefaultSmartPointers =
115117
"::std::shared_ptr; ::std::unique_ptr; ::std::auto_ptr; ::std::weak_ptr";
116-
const auto DefaultTupleTypes = "::std::pair; ::std::tuple";
117-
const auto DefaultTupleMakeFunctions = "::std::make_pair; ::std::make_tuple";
118-
const auto DefaultEmplacyFunctions =
118+
static const auto DefaultTupleTypes = "::std::pair; ::std::tuple";
119+
static const auto DefaultTupleMakeFunctions =
120+
"::std::make_pair; ::std::make_tuple";
121+
static const auto DefaultEmplacyFunctions =
119122
"vector::emplace_back; vector::emplace;"
120123
"deque::emplace; deque::emplace_front; deque::emplace_back;"
121124
"forward_list::emplace_after; forward_list::emplace_front;"
@@ -129,7 +132,6 @@ const auto DefaultEmplacyFunctions =
129132
"unordered_multiset::emplace; unordered_multiset::emplace_hint;"
130133
"unordered_multimap::emplace; unordered_multimap::emplace_hint;"
131134
"stack::emplace; queue::emplace; priority_queue::emplace";
132-
} // namespace
133135

134136
UseEmplaceCheck::UseEmplaceCheck(StringRef Name, ClangTidyContext *Context)
135137
: ClangTidyCheck(Name, Context), IgnoreImplicitConstructors(Options.get(

clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ AST_MATCHER(Stmt, isMacroExpansion) {
2929

3030
AST_MATCHER(Stmt, isC23) { return Finder->getASTContext().getLangOpts().C23; }
3131

32+
// Preserve same name as AST_MATCHER(isNULLMacroExpansion)
33+
// NOLINTNEXTLINE(llvm-prefer-static-over-anonymous-namespace)
3234
bool isNULLMacroExpansion(const Stmt *Statement, ASTContext &Context) {
3335
SourceManager &SM = Context.getSourceManager();
3436
const LangOptions &LO = Context.getLangOpts();

0 commit comments

Comments
 (0)