[clang-tidy][NFC] Don't store a redundant copy of HeaderFileExtensions in every check (#190302)

#80333 removed local `HeaderFileExtensions` options; now there's just
the global option, and its value lives in the `ClangTidyContext`. But
every check still stores its own copy of it, a vestige of the old design
which this change fixes.
This commit is contained in:
Victor Chernyakin
2026-04-07 20:08:22 -07:00
committed by GitHub
parent 7071feb157
commit 413e580895
21 changed files with 50 additions and 76 deletions

View File

@@ -480,6 +480,16 @@ public:
ClangTidyContext *Context;
};
/// Returns the list of header file extensions from the context.
const FileExtensionsSet &getHeaderFileExtensions() const {
return Context->getHeaderFileExtensions();
}
/// Returns the list of implementation file extensions from the context.
const FileExtensionsSet &getImplementationFileExtensions() const {
return Context->getImplementationFileExtensions();
}
private:
void run(const ast_matchers::MatchFinder::MatchResult &Result) override;
std::string CheckName;

View File

@@ -33,8 +33,7 @@ AST_MATCHER(VarDecl, hasConstantDeclaration) {
DynamicStaticInitializersCheck::DynamicStaticInitializersCheck(
StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
HeaderFileExtensions(Context->getHeaderFileExtensions()) {}
: ClangTidyCheck(Name, Context) {}
void DynamicStaticInitializersCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
@@ -46,8 +45,9 @@ void DynamicStaticInitializersCheck::check(
const MatchFinder::MatchResult &Result) {
const auto *Var = Result.Nodes.getNodeAs<VarDecl>("var");
const SourceLocation Loc = Var->getLocation();
if (!Loc.isValid() || !utils::isPresumedLocInHeaderFile(
Loc, *Result.SourceManager, HeaderFileExtensions))
if (!Loc.isValid() ||
!utils::isPresumedLocInHeaderFile(Loc, *Result.SourceManager,
getHeaderFileExtensions()))
return;
// If the initializer is a constant expression, then the compiler
// doesn't have to dynamically initialize it.

View File

@@ -10,7 +10,6 @@
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_DYNAMICSTATICINITIALIZERSCHECK_H
#include "../ClangTidyCheck.h"
#include "../FileExtensionsSet.h"
namespace clang::tidy::bugprone {
@@ -23,9 +22,6 @@ public:
}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
private:
FileExtensionsSet HeaderFileExtensions;
};
} // namespace clang::tidy::bugprone

View File

@@ -39,15 +39,13 @@ private:
SuspiciousIncludeCheck::SuspiciousIncludeCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
HeaderFileExtensions(Context->getHeaderFileExtensions()),
ImplementationFileExtensions(Context->getImplementationFileExtensions()),
IgnoredRegexString(Options.get("IgnoredRegex").value_or(StringRef{})),
IgnoredRegex(IgnoredRegexString) {}
void SuspiciousIncludeCheck::registerPPCallbacks(
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
PP->addPPCallbacks(
::std::make_unique<SuspiciousIncludePPCallbacks>(*this, SM, PP));
std::make_unique<SuspiciousIncludePPCallbacks>(*this, SM, PP));
}
void SuspiciousIncludeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
@@ -68,15 +66,15 @@ void SuspiciousIncludePPCallbacks::InclusionDirective(
const SourceLocation DiagLoc = FilenameRange.getBegin().getLocWithOffset(1);
const std::optional<StringRef> IFE =
utils::getFileExtension(FileName, Check.ImplementationFileExtensions);
const std::optional<StringRef> IFE = utils::getFileExtension(
FileName, Check.getImplementationFileExtensions());
if (!IFE)
return;
Check.diag(DiagLoc, "suspicious #%0 of file with '%1' extension")
<< IncludeTok.getIdentifierInfo()->getName() << *IFE;
for (const auto &HFE : Check.HeaderFileExtensions) {
for (const auto &HFE : Check.getHeaderFileExtensions()) {
SmallString<128> GuessedFileName(FileName);
llvm::sys::path::replace_extension(GuessedFileName,
(!HFE.empty() ? "." : "") + HFE);

View File

@@ -29,8 +29,6 @@ public:
Preprocessor *ModuleExpanderPP) override;
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
FileExtensionsSet HeaderFileExtensions;
FileExtensionsSet ImplementationFileExtensions;
StringRef IgnoredRegexString;
llvm::Regex IgnoredRegex;
};

View File

@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "GlobalNamesInHeadersCheck.h"
#include "../utils/FileExtensionsUtils.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Lex/Lexer.h"
@@ -17,8 +18,7 @@ namespace clang::tidy::google::readability {
GlobalNamesInHeadersCheck::GlobalNamesInHeadersCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
HeaderFileExtensions(Context->getHeaderFileExtensions()) {}
: ClangTidyCheck(Name, Context) {}
void GlobalNamesInHeadersCheck::registerMatchers(
ast_matchers::MatchFinder *Finder) {
@@ -39,7 +39,7 @@ void GlobalNamesInHeadersCheck::check(const MatchFinder::MatchResult &Result) {
Result.SourceManager->getExpansionLoc(D->getBeginLoc()))) {
// unless that file is a header.
if (!utils::isSpellingLocInHeaderFile(
D->getBeginLoc(), *Result.SourceManager, HeaderFileExtensions))
D->getBeginLoc(), *Result.SourceManager, getHeaderFileExtensions()))
return;
}

View File

@@ -10,7 +10,6 @@
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_GLOBALNAMESINHEADERSCHECK_H
#include "../ClangTidyCheck.h"
#include "../utils/FileExtensionsUtils.h"
namespace clang::tidy::google::readability {
@@ -24,9 +23,6 @@ public:
GlobalNamesInHeadersCheck(StringRef Name, ClangTidyContext *Context);
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
private:
FileExtensionsSet HeaderFileExtensions;
};
} // namespace clang::tidy::google::readability

View File

@@ -35,8 +35,7 @@ getLastTemplateParameterList(const FunctionDecl *FuncDecl) {
InlineFunctionDeclCheck::InlineFunctionDeclCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
HeaderFileExtensions(Context->getHeaderFileExtensions()) {}
: ClangTidyCheck(Name, Context) {}
void InlineFunctionDeclCheck::registerMatchers(MatchFinder *Finder) {
// Ignore functions that have been deleted.
@@ -67,7 +66,7 @@ void InlineFunctionDeclCheck::check(const MatchFinder::MatchResult &Result) {
// Consider functions only in header files.
if (!utils::isSpellingLocInHeaderFile(SrcBegin, *Result.SourceManager,
HeaderFileExtensions))
getHeaderFileExtensions()))
return;
// Ignore lambda functions as they are internal and implicit.

View File

@@ -10,7 +10,6 @@
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVMLIBC_INLINEFUNCTIONDECLCHECK_H
#include "../ClangTidyCheck.h"
#include "../FileExtensionsSet.h"
namespace clang::tidy::llvm_libc {
@@ -37,9 +36,6 @@ public:
std::optional<TraversalKind> getCheckTraversalKind() const override {
return TK_IgnoreUnlessSpelledInSource;
}
private:
FileExtensionsSet HeaderFileExtensions;
};
} // namespace clang::tidy::llvm_libc

View File

@@ -17,8 +17,7 @@ namespace clang::tidy::misc {
AnonymousNamespaceInHeaderCheck::AnonymousNamespaceInHeaderCheck(
StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
HeaderFileExtensions(Context->getHeaderFileExtensions()) {}
: ClangTidyCheck(Name, Context) {}
void AnonymousNamespaceInHeaderCheck::registerMatchers(
ast_matchers::MatchFinder *Finder) {
@@ -34,7 +33,7 @@ void AnonymousNamespaceInHeaderCheck::check(
return;
if (utils::isPresumedLocInHeaderFile(Loc, *Result.SourceManager,
HeaderFileExtensions))
getHeaderFileExtensions()))
diag(Loc, "do not use unnamed namespaces in header files");
}

View File

@@ -29,9 +29,6 @@ public:
}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
private:
FileExtensionsSet HeaderFileExtensions;
};
} // namespace clang::tidy::misc

View File

@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "DefinitionsInHeadersCheck.h"
#include "../utils/FileExtensionsUtils.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
@@ -27,17 +28,17 @@ AST_MATCHER_P(NamedDecl, usesHeaderFileExtension, FileExtensionsSet,
DefinitionsInHeadersCheck::DefinitionsInHeadersCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
HeaderFileExtensions(Context->getHeaderFileExtensions()) {}
: ClangTidyCheck(Name, Context) {}
void DefinitionsInHeadersCheck::registerMatchers(MatchFinder *Finder) {
auto DefinitionMatcher =
anyOf(functionDecl(isDefinition(), unless(isDeleted())),
varDecl(isDefinition()));
Finder->addMatcher(namedDecl(DefinitionMatcher,
usesHeaderFileExtension(HeaderFileExtensions))
.bind("name-decl"),
this);
Finder->addMatcher(
namedDecl(DefinitionMatcher,
usesHeaderFileExtension(getHeaderFileExtensions()))
.bind("name-decl"),
this);
}
void DefinitionsInHeadersCheck::check(const MatchFinder::MatchResult &Result) {

View File

@@ -10,7 +10,6 @@
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_DEFINITIONSINHEADERSCHECK_H
#include "../ClangTidyCheck.h"
#include "../utils/FileExtensionsUtils.h"
namespace clang::tidy::misc {
@@ -31,9 +30,6 @@ public:
}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
private:
FileExtensionsSet HeaderFileExtensions;
};
} // namespace clang::tidy::misc

View File

@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "UnusedUsingDeclsCheck.h"
#include "../utils/FileExtensionsUtils.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
@@ -47,13 +48,12 @@ static bool shouldCheckDecl(const Decl *TargetDecl) {
UnusedUsingDeclsCheck::UnusedUsingDeclsCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
HeaderFileExtensions(Context->getHeaderFileExtensions()) {}
: ClangTidyCheck(Name, Context) {}
void UnusedUsingDeclsCheck::registerMatchers(MatchFinder *Finder) {
// We don't emit warnings on unused-using-decls from headers, so bail out if
// the main file is a header.
if (utils::isFileExtension(getCurrentMainFile(), HeaderFileExtensions))
if (utils::isFileExtension(getCurrentMainFile(), getHeaderFileExtensions()))
return;
Finder->addMatcher(usingDecl(isExpansionInMainFile()).bind("using"), this);
auto DeclMatcher = hasDeclaration(namedDecl().bind("used"));

View File

@@ -10,7 +10,6 @@
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSEDUSINGDECLSCHECK_H
#include "../ClangTidyCheck.h"
#include "../utils/FileExtensionsUtils.h"
#include "llvm/ADT/SmallPtrSet.h"
#include <vector>
@@ -50,8 +49,6 @@ private:
std::vector<UsingDeclContext> Contexts;
llvm::SmallPtrSet<const Decl *, 32> UsingTargetDeclsCache;
FileExtensionsSet HeaderFileExtensions;
};
} // namespace clang::tidy::misc

View File

@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "UseAnonymousNamespaceCheck.h"
#include "../utils/FileExtensionsUtils.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
@@ -17,10 +18,10 @@ namespace {
AST_POLYMORPHIC_MATCHER_P(isInHeaderFile,
AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
VarDecl),
FileExtensionsSet, HeaderFileExtensions) {
const FileExtensionsSet *, HeaderFileExtensions) {
return utils::isExpansionLocInHeaderFile(
Node.getBeginLoc(), Finder->getASTContext().getSourceManager(),
HeaderFileExtensions);
*HeaderFileExtensions);
}
AST_MATCHER(FunctionDecl, isMemberFunction) {
@@ -31,19 +32,18 @@ AST_MATCHER(VarDecl, isStaticDataMember) { return Node.isStaticDataMember(); }
UseAnonymousNamespaceCheck::UseAnonymousNamespaceCheck(
StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
HeaderFileExtensions(Context->getHeaderFileExtensions()) {}
: ClangTidyCheck(Name, Context) {}
void UseAnonymousNamespaceCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
functionDecl(isStaticStorageClass(),
unless(anyOf(isInHeaderFile(HeaderFileExtensions),
unless(anyOf(isInHeaderFile(&getHeaderFileExtensions()),
isInAnonymousNamespace(), isMemberFunction())))
.bind("x"),
this);
Finder->addMatcher(
varDecl(isStaticStorageClass(),
unless(anyOf(isInHeaderFile(HeaderFileExtensions),
unless(anyOf(isInHeaderFile(&getHeaderFileExtensions()),
isInAnonymousNamespace(), isStaticLocal(),
isStaticDataMember(), hasType(isConstQualified()))))
.bind("x"),

View File

@@ -10,7 +10,6 @@
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_USEANONYMOUSNAMESPACECHECK_H
#include "../ClangTidyCheck.h"
#include "../utils/FileExtensionsUtils.h"
namespace clang::tidy::misc {
@@ -27,9 +26,6 @@ public:
}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
private:
FileExtensionsSet HeaderFileExtensions;
};
} // namespace clang::tidy::misc

View File

@@ -75,12 +75,12 @@ AST_MATCHER(Decl, isInImportableModuleUnit) {
return false;
}
AST_MATCHER_P(Decl, isAllRedeclsInMainFile, FileExtensionsSet,
AST_MATCHER_P(Decl, isAllRedeclsInMainFile, const FileExtensionsSet *,
HeaderFileExtensions) {
return llvm::all_of(Node.redecls(), [&](const Decl *D) {
return isInMainFile(D->getLocation(),
Finder->getASTContext().getSourceManager(),
HeaderFileExtensions);
*HeaderFileExtensions);
});
}
@@ -124,7 +124,6 @@ AST_MATCHER(CXXRecordDecl, isExplicitTemplateInstantiation) {
UseInternalLinkageCheck::UseInternalLinkageCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
HeaderFileExtensions(Context->getHeaderFileExtensions()),
FixMode(Options.get("FixMode", FixModeKind::UseStatic)),
AnalyzeFunctions(Options.get("AnalyzeFunctions", true)),
AnalyzeVariables(Options.get("AnalyzeVariables", true)),
@@ -144,8 +143,8 @@ void UseInternalLinkageCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
}
void UseInternalLinkageCheck::registerMatchers(MatchFinder *Finder) {
auto Common =
allOf(isFirstDecl(), isAllRedeclsInMainFile(HeaderFileExtensions),
const auto Common =
allOf(isFirstDecl(), isAllRedeclsInMainFile(&getHeaderFileExtensions()),
unless(anyOf(isInAnonymousNamespace(), isInImportableModuleUnit(),
hasAncestor(decl(friendDecl())))));

View File

@@ -34,7 +34,6 @@ public:
};
private:
FileExtensionsSet HeaderFileExtensions;
FixModeKind FixMode;
const bool AnalyzeFunctions;
const bool AnalyzeVariables;

View File

@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "HeaderGuard.h"
#include "../utils/FileExtensionsUtils.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/PPCallbacks.h"
#include "clang/Lex/Preprocessor.h"
@@ -283,16 +284,17 @@ std::string HeaderGuardCheck::sanitizeHeaderGuard(StringRef Guard) {
}
bool HeaderGuardCheck::shouldSuggestEndifComment(StringRef FileName) {
return utils::isFileExtension(FileName, HeaderFileExtensions);
return utils::isFileExtension(FileName, getHeaderFileExtensions());
}
bool HeaderGuardCheck::shouldFixHeaderGuard(StringRef FileName) { return true; }
bool HeaderGuardCheck::shouldSuggestToAddHeaderGuard(StringRef FileName) {
return utils::isFileExtension(FileName, HeaderFileExtensions);
return utils::isFileExtension(FileName, getHeaderFileExtensions());
}
std::string HeaderGuardCheck::formatEndIf(StringRef HeaderGuard) {
return "endif // " + HeaderGuard.str();
}
} // namespace clang::tidy::utils

View File

@@ -10,7 +10,6 @@
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H
#include "../ClangTidyCheck.h"
#include "../utils/FileExtensionsUtils.h"
namespace clang::tidy::utils {
@@ -18,8 +17,7 @@ namespace clang::tidy::utils {
class HeaderGuardCheck : public ClangTidyCheck {
public:
HeaderGuardCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
HeaderFileExtensions(Context->getHeaderFileExtensions()) {}
: ClangTidyCheck(Name, Context) {}
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
Preprocessor *ModuleExpanderPP) override;
@@ -43,9 +41,6 @@ public:
/// Gets the canonical header guard for a file.
virtual std::string getHeaderGuard(StringRef Filename,
StringRef OldGuard = StringRef()) = 0;
private:
FileExtensionsSet HeaderFileExtensions;
};
} // namespace clang::tidy::utils