[clang-tidy][NFC] Fix llvm-prefer-static-over-anonymous-namespace warnings 4/N (#164158)

Continue https://github.com/llvm/llvm-project/pull/153885.
This commit is contained in:
Baranov Victor
2025-10-20 12:02:10 +03:00
committed by GitHub
parent fbb15f58b5
commit d0ed8bc1a4
16 changed files with 60 additions and 89 deletions

View File

@@ -20,8 +20,10 @@ namespace {
AST_MATCHER(StringLiteral, lengthIsOne) { return Node.getLength() == 1; }
std::optional<std::string> makeCharacterLiteral(const StringLiteral *Literal,
const ASTContext &Context) {
} // anonymous namespace
static std::optional<std::string>
makeCharacterLiteral(const StringLiteral *Literal, const ASTContext &Context) {
assert(Literal->getLength() == 1 &&
"Only single character string should be matched");
assert(Literal->getCharByteWidth() == 1 &&
@@ -53,8 +55,6 @@ std::optional<std::string> makeCharacterLiteral(const StringLiteral *Literal,
return Result;
}
} // anonymous namespace
void FasterStrsplitDelimiterCheck::registerMatchers(MatchFinder *Finder) {
// Binds to one character string literals.
const auto SingleChar =

View File

@@ -51,6 +51,8 @@ private:
void extract(const CXXOperatorCallExpr *Op);
};
} // namespace
void ChainedComparisonData::add(const Expr *Operand) {
if (!Name.empty())
Name += ' ';
@@ -111,8 +113,6 @@ void ChainedComparisonData::extract(const Expr *Op) {
}
}
} // namespace
void ChainedComparisonCheck::registerMatchers(MatchFinder *Finder) {
const auto OperatorMatcher = expr(anyOf(
binaryOperator(isComparisonOperator(),

View File

@@ -17,9 +17,7 @@ using namespace clang::tidy::matchers;
namespace clang::tidy::bugprone {
namespace {
ast_matchers::internal::BindableMatcher<Stmt>
static ast_matchers::internal::BindableMatcher<Stmt>
handleFrom(const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle,
const ast_matchers::internal::Matcher<Expr> &Arg) {
return expr(
@@ -31,7 +29,7 @@ handleFrom(const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle,
on(Arg))));
}
ast_matchers::internal::Matcher<Stmt> handleFromTemporaryValue(
static ast_matchers::internal::Matcher<Stmt> handleFromTemporaryValue(
const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle) {
const auto TemporaryExpr = anyOf(
@@ -49,22 +47,22 @@ ast_matchers::internal::Matcher<Stmt> handleFromTemporaryValue(
return handleFrom(IsAHandle, anyOf(TemporaryExpr, TemporaryTernary));
}
ast_matchers::internal::Matcher<RecordDecl> isASequence() {
static ast_matchers::internal::Matcher<RecordDecl> isASequence() {
return hasAnyName("::std::deque", "::std::forward_list", "::std::list",
"::std::vector");
}
ast_matchers::internal::Matcher<RecordDecl> isASet() {
static ast_matchers::internal::Matcher<RecordDecl> isASet() {
return hasAnyName("::std::set", "::std::multiset", "::std::unordered_set",
"::std::unordered_multiset");
}
ast_matchers::internal::Matcher<RecordDecl> isAMap() {
static ast_matchers::internal::Matcher<RecordDecl> isAMap() {
return hasAnyName("::std::map", "::std::multimap", "::std::unordered_map",
"::std::unordered_multimap");
}
ast_matchers::internal::BindableMatcher<Stmt> makeContainerMatcher(
static ast_matchers::internal::BindableMatcher<Stmt> makeContainerMatcher(
const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle) {
// This matcher could be expanded to detect:
// - Constructors: eg. vector<string_view>(3, string("A"));
@@ -91,8 +89,6 @@ ast_matchers::internal::BindableMatcher<Stmt> makeContainerMatcher(
hasOverloadedOperatorName("[]"))));
}
} // anonymous namespace
DanglingHandleCheck::DanglingHandleCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),

View File

@@ -50,15 +50,15 @@ AST_MATCHER_P2(Expr, hasSizeOfDescendant, int, Depth,
AST_MATCHER(Expr, offsetOfExpr) { return isa<OffsetOfExpr>(Node); }
CharUnits getSizeOfType(const ASTContext &Ctx, const Type *Ty) {
} // namespace
static CharUnits getSizeOfType(const ASTContext &Ctx, const Type *Ty) {
if (!Ty || Ty->isIncompleteType() || Ty->isDependentType() ||
isa<DependentSizedArrayType>(Ty) || !Ty->isConstantSizeType())
return CharUnits::Zero();
return Ctx.getTypeSizeInChars(Ty);
}
} // namespace
SizeofExpressionCheck::SizeofExpressionCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),

View File

@@ -20,8 +20,9 @@ namespace {
AST_MATCHER_P(IntegerLiteral, isBiggerThan, unsigned, N) {
return Node.getValue().getZExtValue() > N;
}
} // namespace
const char DefaultStringNames[] =
static const char DefaultStringNames[] =
"::std::basic_string;::std::basic_string_view";
static std::vector<StringRef>
@@ -36,8 +37,6 @@ removeNamespaces(const std::vector<StringRef> &Names) {
return Result;
}
} // namespace
StringConstructorCheck::StringConstructorCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),

View File

@@ -21,7 +21,9 @@ namespace {
AST_MATCHER(VarDecl, isLocalVariable) { return Node.isLocalVarDecl(); }
FixItHint generateFixItHint(const VarDecl *Decl, bool IsConst) {
} // namespace
static FixItHint generateFixItHint(const VarDecl *Decl, bool IsConst) {
if (IsConst && (Decl->getStorageClass() != SC_Static)) {
// No fix available if it is not a static constant, since it is difficult
// to determine the proper fix in this case.
@@ -52,7 +54,6 @@ FixItHint generateFixItHint(const VarDecl *Decl, bool IsConst) {
CharSourceRange::getTokenRange(SourceRange(Decl->getLocation())),
llvm::StringRef(NewName));
}
} // namespace
void GlobalVariableDeclarationCheck::registerMatchers(MatchFinder *Finder) {
// need to add two matchers since we need to bind different ids to distinguish

View File

@@ -16,9 +16,7 @@ using namespace clang::ast_matchers;
namespace clang::tidy::llvm_libc {
namespace {
const TemplateParameterList *
static const TemplateParameterList *
getLastTemplateParameterList(const FunctionDecl *FuncDecl) {
const TemplateParameterList *ReturnList =
FuncDecl->getDescribedTemplateParams();
@@ -35,8 +33,6 @@ getLastTemplateParameterList(const FunctionDecl *FuncDecl) {
return ReturnList;
}
} // namespace
InlineFunctionDeclCheck::InlineFunctionDeclCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),

View File

@@ -22,15 +22,14 @@ using namespace clang::ast_matchers;
namespace clang::tidy::misc {
namespace {
bool isOverrideMethod(const FunctionDecl *Function) {
static bool isOverrideMethod(const FunctionDecl *Function) {
if (const auto *MD = dyn_cast<CXXMethodDecl>(Function))
return MD->size_overridden_methods() > 0 || MD->hasAttr<OverrideAttr>();
return false;
}
bool hasAttrAfterParam(const SourceManager *SourceManager,
const ParmVarDecl *Param) {
static bool hasAttrAfterParam(const SourceManager *SourceManager,
const ParmVarDecl *Param) {
for (const auto *Attr : Param->attrs()) {
if (SourceManager->isBeforeInTranslationUnit(Param->getLocation(),
Attr->getLocation())) {
@@ -39,7 +38,6 @@ bool hasAttrAfterParam(const SourceManager *SourceManager,
}
return false;
}
} // namespace
void UnusedParametersCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(functionDecl(isDefinition(), hasBody(stmt()),

View File

@@ -14,10 +14,8 @@ using namespace clang::ast_matchers;
namespace clang::tidy::modernize {
namespace {
// Determine if the given QualType is a nullary function or pointer to same.
bool protoTypeHasNoParms(QualType QT) {
static bool protoTypeHasNoParms(QualType QT) {
if (const auto *PT = QT->getAs<PointerType>())
QT = PT->getPointeeType();
if (auto *MPT = QT->getAs<MemberPointerType>())
@@ -27,16 +25,14 @@ bool protoTypeHasNoParms(QualType QT) {
return false;
}
const char FunctionId[] = "function";
const char TypedefId[] = "typedef";
const char FieldId[] = "field";
const char VarId[] = "var";
const char NamedCastId[] = "named-cast";
const char CStyleCastId[] = "c-style-cast";
const char ExplicitCastId[] = "explicit-cast";
const char LambdaId[] = "lambda";
} // namespace
static const char FunctionId[] = "function";
static const char TypedefId[] = "typedef";
static const char FieldId[] = "field";
static const char VarId[] = "var";
static const char NamedCastId[] = "named-cast";
static const char CStyleCastId[] = "c-style-cast";
static const char ExplicitCastId[] = "explicit-cast";
static const char LambdaId[] = "lambda";
void RedundantVoidArgCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(functionDecl(parameterCountIs(0), unless(isImplicit()),

View File

@@ -255,8 +255,10 @@ struct MatchBuilder {
double DiffThreshold;
};
std::string getCode(const StringRef Constant, const bool IsFloat,
const bool IsLongDouble) {
} // namespace
static std::string getCode(const StringRef Constant, const bool IsFloat,
const bool IsLongDouble) {
if (IsFloat) {
return ("std::numbers::" + Constant + "_v<float>").str();
}
@@ -266,9 +268,9 @@ std::string getCode(const StringRef Constant, const bool IsFloat,
return ("std::numbers::" + Constant).str();
}
bool isRangeOfCompleteMacro(const clang::SourceRange &Range,
const clang::SourceManager &SM,
const clang::LangOptions &LO) {
static bool isRangeOfCompleteMacro(const clang::SourceRange &Range,
const clang::SourceManager &SM,
const clang::LangOptions &LO) {
if (!Range.getBegin().isMacroID()) {
return false;
}
@@ -287,8 +289,6 @@ bool isRangeOfCompleteMacro(const clang::SourceRange &Range,
return true;
}
} // namespace
namespace clang::tidy::modernize {
UseStdNumbersCheck::UseStdNumbersCheck(const StringRef Name,
ClangTidyContext *const Context)

View File

@@ -35,7 +35,8 @@ const std::uint64_t Min32 =
std::imaxabs(std::numeric_limits<std::int32_t>::min());
const std::uint64_t Max32 = std::numeric_limits<std::int32_t>::max();
std::pair<const char *, std::uint32_t>
} // namespace
static std::pair<const char *, std::uint32_t>
getNewType(std::size_t Size, std::uint64_t Min, std::uint64_t Max) noexcept {
if (Min) {
if (Min <= Min8 && Max <= Max8) {
@@ -75,8 +76,6 @@ getNewType(std::size_t Size, std::uint64_t Min, std::uint64_t Max) noexcept {
return {"std::uint8_t", sizeof(std::uint8_t)};
}
} // namespace
EnumSizeCheck::EnumSizeCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
EnumIgnoreList(

View File

@@ -15,9 +15,8 @@
using namespace clang::ast_matchers;
namespace clang::tidy::readability {
namespace {
SourceRange getTypeRange(const ParmVarDecl &Param) {
static SourceRange getTypeRange(const ParmVarDecl &Param) {
return {Param.getBeginLoc(), Param.getLocation().getLocWithOffset(-1)};
}
@@ -39,8 +38,6 @@ findConstToRemove(const ParmVarDecl &Param,
tok::kw_const, FileRange, *Result.Context, *Result.SourceManager);
}
} // namespace
void AvoidConstParamsInDecls::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "IgnoreMacros", IgnoreMacros);
}

View File

@@ -129,8 +129,7 @@ void RedundantSmartptrGetCheck::registerMatchers(MatchFinder *Finder) {
registerMatchersForGetEquals(Finder, this);
}
namespace {
bool allReturnTypesMatch(const MatchFinder::MatchResult &Result) {
static bool allReturnTypesMatch(const MatchFinder::MatchResult &Result) {
if (Result.Nodes.getNodeAs<Decl>("duck_typing") == nullptr)
return true;
// Verify that the types match.
@@ -145,7 +144,6 @@ bool allReturnTypesMatch(const MatchFinder::MatchResult &Result) {
Result.Nodes.getNodeAs<Type>("getType")->getUnqualifiedDesugaredType();
return OpArrowType == OpStarType && OpArrowType == GetType;
}
} // namespace
void RedundantSmartptrGetCheck::check(const MatchFinder::MatchResult &Result) {
if (!allReturnTypesMatch(Result))

View File

@@ -21,20 +21,17 @@ using namespace clang::ast_matchers;
namespace clang::tidy::readability {
namespace {
StringRef getText(const ASTContext &Context, SourceRange Range) {
static StringRef getText(const ASTContext &Context, SourceRange Range) {
return Lexer::getSourceText(CharSourceRange::getTokenRange(Range),
Context.getSourceManager(),
Context.getLangOpts());
}
template <typename T> StringRef getText(const ASTContext &Context, T &Node) {
template <typename T>
static StringRef getText(const ASTContext &Context, T &Node) {
return getText(Context, Node.getSourceRange());
}
} // namespace
static constexpr char SimplifyOperatorDiagnostic[] =
"redundant boolean literal supplied to boolean operator";
static constexpr char SimplifyConditionDiagnostic[] =

View File

@@ -49,10 +49,8 @@ static SmallVector<const Stmt *, 1> getParentStmts(const Stmt *S,
return Result;
}
namespace {
bool isDescendantOrEqual(const Stmt *Descendant, const Stmt *Ancestor,
ASTContext *Context) {
static bool isDescendantOrEqual(const Stmt *Descendant, const Stmt *Ancestor,
ASTContext *Context) {
if (Descendant == Ancestor)
return true;
return llvm::any_of(getParentStmts(Descendant, Context),
@@ -61,15 +59,15 @@ bool isDescendantOrEqual(const Stmt *Descendant, const Stmt *Ancestor,
});
}
bool isDescendantOfArgs(const Stmt *Descendant, const CallExpr *Call,
ASTContext *Context) {
static bool isDescendantOfArgs(const Stmt *Descendant, const CallExpr *Call,
ASTContext *Context) {
return llvm::any_of(Call->arguments(),
[Descendant, Context](const Expr *Arg) {
return isDescendantOrEqual(Descendant, Arg, Context);
});
}
llvm::SmallVector<const InitListExpr *>
static llvm::SmallVector<const InitListExpr *>
getAllInitListForms(const InitListExpr *InitList) {
llvm::SmallVector<const InitListExpr *> Result = {InitList};
if (const InitListExpr *AltForm = InitList->getSyntacticForm())
@@ -79,8 +77,6 @@ getAllInitListForms(const InitListExpr *InitList) {
return Result;
}
} // namespace
ExprSequence::ExprSequence(const CFG *TheCFG, const Stmt *Root,
ASTContext *TheContext)
: Context(TheContext), Root(Root) {

View File

@@ -15,9 +15,8 @@
namespace clang::tidy {
namespace utils {
namespace {
StringRef removeFirstSuffix(StringRef Str, ArrayRef<const char *> Suffixes) {
static StringRef removeFirstSuffix(StringRef Str,
ArrayRef<const char *> Suffixes) {
for (StringRef Suffix : Suffixes) {
if (Str.consume_back(Suffix))
return Str;
@@ -25,7 +24,8 @@ StringRef removeFirstSuffix(StringRef Str, ArrayRef<const char *> Suffixes) {
return Str;
}
StringRef makeCanonicalName(StringRef Str, IncludeSorter::IncludeStyle Style) {
static StringRef makeCanonicalName(StringRef Str,
IncludeSorter::IncludeStyle Style) {
// The list of suffixes to remove from source file names to get the
// "canonical" file names.
// E.g. tools/sort_includes.cc and tools/sort_includes_test.cc
@@ -56,12 +56,12 @@ StringRef makeCanonicalName(StringRef Str, IncludeSorter::IncludeStyle Style) {
}
// Scan to the end of the line and return the offset of the next line.
size_t findNextLine(const char *Text) {
static size_t findNextLine(const char *Text) {
size_t EOLIndex = std::strcspn(Text, "\n");
return Text[EOLIndex] == '\0' ? EOLIndex : EOLIndex + 1;
}
IncludeSorter::IncludeKinds
static IncludeSorter::IncludeKinds
determineIncludeKind(StringRef CanonicalFile, StringRef IncludeFile,
bool IsAngled, IncludeSorter::IncludeStyle Style) {
// Compute the two "canonical" forms of the include's filename sans extension.
@@ -101,8 +101,8 @@ determineIncludeKind(StringRef CanonicalFile, StringRef IncludeFile,
return IncludeSorter::IK_NonSystemInclude;
}
int compareHeaders(StringRef LHS, StringRef RHS,
IncludeSorter::IncludeStyle Style) {
static int compareHeaders(StringRef LHS, StringRef RHS,
IncludeSorter::IncludeStyle Style) {
if (Style == IncludeSorter::IncludeStyle::IS_Google_ObjC) {
const std::pair<const char *, const char *> &Mismatch =
std::mismatch(LHS.begin(), LHS.end(), RHS.begin(), RHS.end());
@@ -118,8 +118,6 @@ int compareHeaders(StringRef LHS, StringRef RHS,
return LHS.compare(RHS);
}
} // namespace
IncludeSorter::IncludeSorter(const SourceManager *SourceMgr,
const FileID FileID, StringRef FileName,
IncludeStyle Style)