Skip to content

Commit 695cf01

Browse files
authored
[clang-tidy][NFC] Fix llvm-prefer-static-over-anonymous-namespace warnings 2/N (#164083)
Continue llvm/llvm-project#153885.
1 parent 0f6f13b commit 695cf01

File tree

8 files changed

+78
-65
lines changed

8 files changed

+78
-65
lines changed

clang-tools-extra/clang-tidy/abseil/RedundantStrcatCallsCheck.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ struct StrCatCheckResult {
4545
std::vector<FixItHint> Hints;
4646
};
4747

48-
void removeCallLeaveArgs(const CallExpr *Call, StrCatCheckResult *CheckResult) {
48+
} // namespace
49+
50+
static void removeCallLeaveArgs(const CallExpr *Call,
51+
StrCatCheckResult *CheckResult) {
4952
if (Call->getNumArgs() == 0)
5053
return;
5154
// Remove 'Foo('
@@ -58,9 +61,9 @@ void removeCallLeaveArgs(const CallExpr *Call, StrCatCheckResult *CheckResult) {
5861
Call->getRParenLoc(), Call->getEndLoc().getLocWithOffset(1))));
5962
}
6063

61-
const clang::CallExpr *processArgument(const Expr *Arg,
62-
const MatchFinder::MatchResult &Result,
63-
StrCatCheckResult *CheckResult) {
64+
static const clang::CallExpr *
65+
processArgument(const Expr *Arg, const MatchFinder::MatchResult &Result,
66+
StrCatCheckResult *CheckResult) {
6467
const auto IsAlphanum = hasDeclaration(cxxMethodDecl(hasName("AlphaNum")));
6568
static const auto *const Strcat = new auto(hasName("::absl::StrCat"));
6669
const auto IsStrcat = cxxBindTemporaryExpr(
@@ -78,8 +81,8 @@ const clang::CallExpr *processArgument(const Expr *Arg,
7881
return nullptr;
7982
}
8083

81-
StrCatCheckResult processCall(const CallExpr *RootCall, bool IsAppend,
82-
const MatchFinder::MatchResult &Result) {
84+
static StrCatCheckResult processCall(const CallExpr *RootCall, bool IsAppend,
85+
const MatchFinder::MatchResult &Result) {
8386
StrCatCheckResult CheckResult;
8487
std::deque<const CallExpr *> CallsToProcess = {RootCall};
8588

@@ -101,7 +104,6 @@ StrCatCheckResult processCall(const CallExpr *RootCall, bool IsAppend,
101104
}
102105
return CheckResult;
103106
}
104-
} // namespace
105107

106108
void RedundantStrcatCallsCheck::check(const MatchFinder::MatchResult &Result) {
107109
bool IsAppend = false;

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ namespace {
1818

1919
AST_MATCHER(Expr, isInMacro) { return Node.getBeginLoc().isMacroID(); }
2020

21+
} // namespace
22+
2123
/// Find the next statement after `S`.
22-
const Stmt *nextStmt(const MatchFinder::MatchResult &Result, const Stmt *S) {
24+
static const Stmt *nextStmt(const MatchFinder::MatchResult &Result,
25+
const Stmt *S) {
2326
auto Parents = Result.Context->getParents(*S);
2427
if (Parents.empty())
2528
return nullptr;
@@ -40,8 +43,8 @@ using ExpansionRanges = std::vector<SourceRange>;
4043
/// \brief Get all the macro expansion ranges related to `Loc`.
4144
///
4245
/// The result is ordered from most inner to most outer.
43-
ExpansionRanges getExpansionRanges(SourceLocation Loc,
44-
const MatchFinder::MatchResult &Result) {
46+
static ExpansionRanges
47+
getExpansionRanges(SourceLocation Loc, const MatchFinder::MatchResult &Result) {
4548
ExpansionRanges Locs;
4649
while (Loc.isMacroID()) {
4750
Locs.push_back(
@@ -51,8 +54,6 @@ ExpansionRanges getExpansionRanges(SourceLocation Loc,
5154
return Locs;
5255
}
5356

54-
} // namespace
55-
5657
void MultipleStatementMacroCheck::registerMatchers(MatchFinder *Finder) {
5758
const auto Inner = expr(isInMacro(), unless(compoundStmt())).bind("inner");
5859
Finder->addMatcher(

clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ AST_MATCHER(CXXRecordDecl, hasDefaultConstructor) {
2828
return Node.hasDefaultConstructor();
2929
}
3030

31+
} // namespace
32+
3133
// Iterate over all the fields in a record type, both direct and indirect (e.g.
3234
// if the record contains an anonymous struct).
3335
template <typename T, typename Func>
34-
void forEachField(const RecordDecl &Record, const T &Fields, const Func &Fn) {
36+
static void forEachField(const RecordDecl &Record, const T &Fields,
37+
const Func &Fn) {
3538
for (const FieldDecl *F : Fields) {
3639
if (F->isAnonymousStructOrUnion()) {
3740
if (const CXXRecordDecl *R = F->getType()->getAsCXXRecordDecl())
@@ -43,8 +46,9 @@ void forEachField(const RecordDecl &Record, const T &Fields, const Func &Fn) {
4346
}
4447

4548
template <typename T, typename Func>
46-
void forEachFieldWithFilter(const RecordDecl &Record, const T &Fields,
47-
bool &AnyMemberHasInitPerUnion, const Func &Fn) {
49+
static void forEachFieldWithFilter(const RecordDecl &Record, const T &Fields,
50+
bool &AnyMemberHasInitPerUnion,
51+
const Func &Fn) {
4852
for (const FieldDecl *F : Fields) {
4953
if (F->isAnonymousStructOrUnion()) {
5054
if (const CXXRecordDecl *R = F->getType()->getAsCXXRecordDecl()) {
@@ -59,8 +63,9 @@ void forEachFieldWithFilter(const RecordDecl &Record, const T &Fields,
5963
}
6064
}
6165

62-
void removeFieldInitialized(const FieldDecl *M,
63-
SmallPtrSetImpl<const FieldDecl *> &FieldDecls) {
66+
static void
67+
removeFieldInitialized(const FieldDecl *M,
68+
SmallPtrSetImpl<const FieldDecl *> &FieldDecls) {
6469
const RecordDecl *R = M->getParent();
6570
if (R && R->isUnion()) {
6671
// Erase all members in a union if any member of it is initialized.
@@ -70,9 +75,9 @@ void removeFieldInitialized(const FieldDecl *M,
7075
FieldDecls.erase(M);
7176
}
7277

73-
void removeFieldsInitializedInBody(
74-
const Stmt &Stmt, ASTContext &Context,
75-
SmallPtrSetImpl<const FieldDecl *> &FieldDecls) {
78+
static void
79+
removeFieldsInitializedInBody(const Stmt &Stmt, ASTContext &Context,
80+
SmallPtrSetImpl<const FieldDecl *> &FieldDecls) {
7681
auto Matches =
7782
match(findAll(binaryOperator(
7883
hasOperatorName("="),
@@ -82,9 +87,9 @@ void removeFieldsInitializedInBody(
8287
removeFieldInitialized(Match.getNodeAs<FieldDecl>("fieldDecl"), FieldDecls);
8388
}
8489

85-
StringRef getName(const FieldDecl *Field) { return Field->getName(); }
90+
static StringRef getName(const FieldDecl *Field) { return Field->getName(); }
8691

87-
StringRef getName(const RecordDecl *Record) {
92+
static StringRef getName(const RecordDecl *Record) {
8893
// Get the typedef name if this is a C-style anonymous struct and typedef.
8994
if (const TypedefNameDecl *Typedef = Record->getTypedefNameForAnonDecl())
9095
return Typedef->getName();
@@ -94,7 +99,7 @@ StringRef getName(const RecordDecl *Record) {
9499
// Creates comma separated list of decls requiring initialization in order of
95100
// declaration.
96101
template <typename R, typename T>
97-
std::string
102+
static std::string
98103
toCommaSeparatedString(const R &OrderedDecls,
99104
const SmallPtrSetImpl<const T *> &DeclsToInit) {
100105
SmallVector<StringRef, 16> Names;
@@ -105,12 +110,14 @@ toCommaSeparatedString(const R &OrderedDecls,
105110
return llvm::join(Names.begin(), Names.end(), ", ");
106111
}
107112

108-
SourceLocation getLocationForEndOfToken(const ASTContext &Context,
109-
SourceLocation Location) {
113+
static SourceLocation getLocationForEndOfToken(const ASTContext &Context,
114+
SourceLocation Location) {
110115
return Lexer::getLocForEndOfToken(Location, 0, Context.getSourceManager(),
111116
Context.getLangOpts());
112117
}
113118

119+
namespace {
120+
114121
// There are 3 kinds of insertion placements:
115122
enum class InitializerPlacement {
116123
// 1. The fields are inserted after an existing CXXCtorInitializer stored in
@@ -187,15 +194,17 @@ struct InitializerInsertion {
187194
SmallVector<std::string, 4> Initializers;
188195
};
189196

197+
} // namespace
198+
190199
// Convenience utility to get a RecordDecl from a QualType.
191-
const RecordDecl *getCanonicalRecordDecl(const QualType &Type) {
200+
static const RecordDecl *getCanonicalRecordDecl(const QualType &Type) {
192201
if (const auto *RT = Type->getAsCanonical<RecordType>())
193202
return RT->getDecl();
194203
return nullptr;
195204
}
196205

197206
template <typename R, typename T>
198-
SmallVector<InitializerInsertion, 16>
207+
static SmallVector<InitializerInsertion, 16>
199208
computeInsertions(const CXXConstructorDecl::init_const_range &Inits,
200209
const R &OrderedDecls,
201210
const SmallPtrSetImpl<const T *> &DeclsToInit) {
@@ -239,8 +248,9 @@ computeInsertions(const CXXConstructorDecl::init_const_range &Inits,
239248

240249
// Gets the list of bases and members that could possibly be initialized, in
241250
// order as they appear in the class declaration.
242-
void getInitializationsInOrder(const CXXRecordDecl &ClassDecl,
243-
SmallVectorImpl<const NamedDecl *> &Decls) {
251+
static void
252+
getInitializationsInOrder(const CXXRecordDecl &ClassDecl,
253+
SmallVectorImpl<const NamedDecl *> &Decls) {
244254
Decls.clear();
245255
for (const auto &Base : ClassDecl.bases()) {
246256
// Decl may be null if the base class is a template parameter.
@@ -253,9 +263,10 @@ void getInitializationsInOrder(const CXXRecordDecl &ClassDecl,
253263
}
254264

255265
template <typename T>
256-
void fixInitializerList(const ASTContext &Context, DiagnosticBuilder &Diag,
257-
const CXXConstructorDecl *Ctor,
258-
const SmallPtrSetImpl<const T *> &DeclsToInit) {
266+
static void fixInitializerList(const ASTContext &Context,
267+
DiagnosticBuilder &Diag,
268+
const CXXConstructorDecl *Ctor,
269+
const SmallPtrSetImpl<const T *> &DeclsToInit) {
259270
// Do not propose fixes in macros since we cannot place them correctly.
260271
if (Ctor->getBeginLoc().isMacroID())
261272
return;
@@ -271,8 +282,6 @@ void fixInitializerList(const ASTContext &Context, DiagnosticBuilder &Diag,
271282
}
272283
}
273284

274-
} // anonymous namespace
275-
276285
ProTypeMemberInitCheck::ProTypeMemberInitCheck(StringRef Name,
277286
ClangTidyContext *Context)
278287
: ClangTidyCheck(Name, Context),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ AST_MATCHER_P(CoawaitExpr, awaitable, ast_matchers::internal::Matcher<Expr>,
6060
return InnerMatcher.matches(*E, Finder, Builder);
6161
return false;
6262
}
63+
} // namespace
6364

64-
auto typeWithNameIn(const std::vector<StringRef> &Names) {
65+
static auto typeWithNameIn(const std::vector<StringRef> &Names) {
6566
return hasType(
6667
hasCanonicalType(hasDeclaration(namedDecl(hasAnyName(Names)))));
6768
}
68-
} // namespace
6969

7070
CoroutineHostileRAIICheck::CoroutineHostileRAIICheck(StringRef Name,
7171
ClangTidyContext *Context)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,12 @@ constexpr unsigned SmallSCCSize = 32;
151151
using CallStackTy =
152152
llvm::SmallVector<CallGraphNode::CallRecord, SmallCallStackSize>;
153153

154+
} // namespace
155+
154156
// In given SCC, find *some* call stack that will be cyclic.
155157
// This will only find *one* such stack, it might not be the smallest one,
156158
// and there may be other loops.
157-
CallStackTy pathfindSomeCycle(ArrayRef<CallGraphNode *> SCC) {
159+
static CallStackTy pathfindSomeCycle(ArrayRef<CallGraphNode *> SCC) {
158160
// We'll need to be able to performantly look up whether some CallGraphNode
159161
// is in SCC or not, so cache all the SCC elements in a set.
160162
const ImmutableSmallSet<CallGraphNode *, SmallSCCSize> SCCElts(SCC);
@@ -190,8 +192,6 @@ CallStackTy pathfindSomeCycle(ArrayRef<CallGraphNode *> SCC) {
190192
return CallStack;
191193
}
192194

193-
} // namespace
194-
195195
void NoRecursionCheck::registerMatchers(MatchFinder *Finder) {
196196
Finder->addMatcher(translationUnitDecl().bind("TUDecl"), this);
197197
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ struct NewSuffix {
5555
std::optional<FixItHint> FixIt;
5656
};
5757

58-
std::optional<SourceLocation> getMacroAwareLocation(SourceLocation Loc,
59-
const SourceManager &SM) {
58+
} // namespace
59+
60+
static std::optional<SourceLocation>
61+
getMacroAwareLocation(SourceLocation Loc, const SourceManager &SM) {
6062
// Do nothing if the provided location is invalid.
6163
if (Loc.isInvalid())
6264
return std::nullopt;
@@ -67,8 +69,8 @@ std::optional<SourceLocation> getMacroAwareLocation(SourceLocation Loc,
6769
return SpellingLoc;
6870
}
6971

70-
std::optional<SourceRange> getMacroAwareSourceRange(SourceRange Loc,
71-
const SourceManager &SM) {
72+
static std::optional<SourceRange>
73+
getMacroAwareSourceRange(SourceRange Loc, const SourceManager &SM) {
7274
std::optional<SourceLocation> Begin =
7375
getMacroAwareLocation(Loc.getBegin(), SM);
7476
std::optional<SourceLocation> End = getMacroAwareLocation(Loc.getEnd(), SM);
@@ -77,7 +79,7 @@ std::optional<SourceRange> getMacroAwareSourceRange(SourceRange Loc,
7779
return SourceRange(*Begin, *End);
7880
}
7981

80-
std::optional<std::string>
82+
static std::optional<std::string>
8183
getNewSuffix(llvm::StringRef OldSuffix,
8284
const std::vector<StringRef> &NewSuffixes) {
8385
// If there is no config, just uppercase the entirety of the suffix.
@@ -96,7 +98,7 @@ getNewSuffix(llvm::StringRef OldSuffix,
9698
}
9799

98100
template <typename LiteralType>
99-
std::optional<NewSuffix>
101+
static std::optional<NewSuffix>
100102
shouldReplaceLiteralSuffix(const Expr &Literal,
101103
const std::vector<StringRef> &NewSuffixes,
102104
const SourceManager &SM, const LangOptions &LO) {
@@ -174,8 +176,6 @@ shouldReplaceLiteralSuffix(const Expr &Literal,
174176
return ReplacementDsc;
175177
}
176178

177-
} // namespace
178-
179179
UppercaseLiteralSuffixCheck::UppercaseLiteralSuffixCheck(
180180
StringRef Name, ClangTidyContext *Context)
181181
: ClangTidyCheck(Name, Context),

clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ namespace clang::tidy::utils::decl_ref_expr {
1919
using namespace ::clang::ast_matchers;
2020
using llvm::SmallPtrSet;
2121

22-
namespace {
23-
24-
template <typename S> bool isSetDifferenceEmpty(const S &S1, const S &S2) {
22+
template <typename S>
23+
static bool isSetDifferenceEmpty(const S &S1, const S &S2) {
2524
for (auto E : S1)
2625
if (S2.count(E) == 0)
2726
return false;
@@ -30,15 +29,15 @@ template <typename S> bool isSetDifferenceEmpty(const S &S1, const S &S2) {
3029

3130
// Extracts all Nodes keyed by ID from Matches and inserts them into Nodes.
3231
template <typename Node>
33-
void extractNodesByIdTo(ArrayRef<BoundNodes> Matches, StringRef ID,
34-
SmallPtrSet<const Node *, 16> &Nodes) {
32+
static void extractNodesByIdTo(ArrayRef<BoundNodes> Matches, StringRef ID,
33+
SmallPtrSet<const Node *, 16> &Nodes) {
3534
for (const auto &Match : Matches)
3635
Nodes.insert(Match.getNodeAs<Node>(ID));
3736
}
3837

3938
// Returns true if both types refer to the same type,
4039
// ignoring the const-qualifier.
41-
bool isSameTypeIgnoringConst(QualType A, QualType B) {
40+
static bool isSameTypeIgnoringConst(QualType A, QualType B) {
4241
A = A.getCanonicalType();
4342
B = B.getCanonicalType();
4443
A.addConst();
@@ -47,7 +46,8 @@ bool isSameTypeIgnoringConst(QualType A, QualType B) {
4746
}
4847

4948
// Returns true if `D` and `O` have the same parameter types.
50-
bool hasSameParameterTypes(const CXXMethodDecl &D, const CXXMethodDecl &O) {
49+
static bool hasSameParameterTypes(const CXXMethodDecl &D,
50+
const CXXMethodDecl &O) {
5151
if (D.getNumParams() != O.getNumParams())
5252
return false;
5353
for (int I = 0, E = D.getNumParams(); I < E; ++I) {
@@ -60,7 +60,7 @@ bool hasSameParameterTypes(const CXXMethodDecl &D, const CXXMethodDecl &O) {
6060

6161
// If `D` has a const-qualified overload with otherwise identical
6262
// ref-qualifiers and parameter types, returns that overload.
63-
const CXXMethodDecl *findConstOverload(const CXXMethodDecl &D) {
63+
static const CXXMethodDecl *findConstOverload(const CXXMethodDecl &D) {
6464
assert(!D.isConst());
6565

6666
DeclContext::lookup_result LookupResult =
@@ -81,7 +81,7 @@ const CXXMethodDecl *findConstOverload(const CXXMethodDecl &D) {
8181

8282
// Returns true if both types are pointers or reference to the same type,
8383
// ignoring the const-qualifier.
84-
bool pointsToSameTypeIgnoringConst(QualType A, QualType B) {
84+
static bool pointsToSameTypeIgnoringConst(QualType A, QualType B) {
8585
assert(A->isPointerType() || A->isReferenceType());
8686
assert(B->isPointerType() || B->isReferenceType());
8787
return isSameTypeIgnoringConst(A->getPointeeType(), B->getPointeeType());
@@ -122,7 +122,7 @@ bool pointsToSameTypeIgnoringConst(QualType A, QualType B) {
122122
//
123123
// This function checks (A) ad (B), but the caller should make sure that the
124124
// object is not mutated through the return value.
125-
bool isLikelyShallowConst(const CXXMethodDecl &M) {
125+
static bool isLikelyShallowConst(const CXXMethodDecl &M) {
126126
assert(!M.isConst());
127127
// The method can mutate our variable.
128128

@@ -146,6 +146,8 @@ bool isLikelyShallowConst(const CXXMethodDecl &M) {
146146
return isSameTypeIgnoringConst(CallTy, OverloadTy);
147147
}
148148

149+
namespace {
150+
149151
// A matcher that matches DeclRefExprs that are used in ways such that the
150152
// underlying declaration is not modified.
151153
// If the declaration is of pointer type, `Indirections` specifies the level

0 commit comments

Comments
 (0)