diff --git a/clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.h b/clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.h --- a/clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.h +++ b/clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.h @@ -31,6 +31,7 @@ class ReservedIdentifierCheck final : public RenamerClangTidyCheck { const bool Invert; const std::vector AllowedIdentifiers; + llvm::SmallVector AllowedIdentifiersRegex; public: ReservedIdentifierCheck(StringRef Name, ClangTidyContext *Context); diff --git a/clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.cpp --- a/clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.cpp @@ -44,7 +44,13 @@ : RenamerClangTidyCheck(Name, Context), Invert(Options.get("Invert", false)), AllowedIdentifiers(utils::options::parseStringList( - Options.get("AllowedIdentifiers", ""))) {} + Options.get("AllowedIdentifiers", ""))) { + for (const auto &Identifier : AllowedIdentifiers) { + if (!llvm::Regex(Identifier).isValid()) + configurationDiag("Invalid allowed identifier regex '%0'") << Identifier; + AllowedIdentifiersRegex.emplace_back(Identifier.str()); + } +} void ReservedIdentifierCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { RenamerClangTidyCheck::storeOptions(Opts); @@ -108,11 +114,14 @@ static std::optional getFailureInfoImpl(StringRef Name, bool IsInGlobalNamespace, const LangOptions &LangOpts, bool Invert, - ArrayRef AllowedIdentifiers) { + ArrayRef AllowedIdentifiers) { assert(!Name.empty()); - if (llvm::is_contained(AllowedIdentifiers, Name)) - return std::nullopt; + if (llvm::any_of(AllowedIdentifiers, [&](const llvm::Regex &Regex) { + return Regex.match(Name); + })) { + return std::nullopt; + } // TODO: Check for names identical to language keywords, and other names // specifically reserved by language standards, e.g. C++ 'zombie names' and C // future library directions @@ -159,14 +168,14 @@ "Decl must be an explicit identifier with a name."); return getFailureInfoImpl(Decl->getName(), isa(Decl->getDeclContext()), - getLangOpts(), Invert, AllowedIdentifiers); + getLangOpts(), Invert, AllowedIdentifiersRegex); } std::optional ReservedIdentifierCheck::getMacroFailureInfo(const Token &MacroNameTok, const SourceManager &) const { return getFailureInfoImpl(MacroNameTok.getIdentifierInfo()->getName(), true, - getLangOpts(), Invert, AllowedIdentifiers); + getLangOpts(), Invert, AllowedIdentifiersRegex); } RenamerClangTidyCheck::DiagInfo diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -411,6 +411,10 @@ support unscoped enumerations through instances and fixed usage of anonymous structs or classes. +- Improved option `AllowedIdentifiers` from :doc:`bugprone-reserved-identifier + ` to support regular + expressions. + Removed checks ^^^^^^^^^^^^^^ diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/reserved-identifier.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/reserved-identifier.rst --- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/reserved-identifier.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/reserved-identifier.rst @@ -53,5 +53,5 @@ .. option:: AllowedIdentifiers - Semicolon-separated list of names that the check ignores. Default is an + Semicolon-separated list of regular expressions that the check ignores. Default is an empty list. diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/reserved-identifier-invert.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/reserved-identifier-invert.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/reserved-identifier-invert.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/reserved-identifier-invert.cpp @@ -1,7 +1,7 @@ // RUN: %check_clang_tidy %s bugprone-reserved-identifier %t -- \ // RUN: -config='{CheckOptions: [ \ // RUN: {key: bugprone-reserved-identifier.Invert, value: true}, \ -// RUN: {key: bugprone-reserved-identifier.AllowedIdentifiers, value: std;reference_wrapper;ref;cref;type;get}, \ +// RUN: {key: bugprone-reserved-identifier.AllowedIdentifiers, value: "std;reference_wrapper;^c?ref;type;get"}, \ // RUN: ]}' -- \ // RUN: -I%S/Inputs/reserved-identifier \ // RUN: -isystem %S/Inputs/reserved-identifier/system