[clang-tools-extra] 87a3253 - [clang-tidy] Reserved-identifier: Improved AllowedIdentifiers option to support regular expressions
Piotr Zegar via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 19 00:23:03 PDT 2023
Author: Felix
Date: 2023-06-19T07:22:20Z
New Revision: 87a3253a394ddf3a799b452df5d290fea2473fe2
URL: https://github.com/llvm/llvm-project/commit/87a3253a394ddf3a799b452df5d290fea2473fe2
DIFF: https://github.com/llvm/llvm-project/commit/87a3253a394ddf3a799b452df5d290fea2473fe2.diff
LOG: [clang-tidy] Reserved-identifier: Improved AllowedIdentifiers option to support regular expressions
Fixes: https://github.com/llvm/llvm-project/issues/59119
Reviewed By: PiotrZSL
Differential Revision: https://reviews.llvm.org/D152764
Added:
Modified:
clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.cpp
clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.h
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/docs/clang-tidy/checks/bugprone/reserved-identifier.rst
clang-tools-extra/test/clang-tidy/checkers/bugprone/reserved-identifier-invert.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.cpp
index 7ecbf74c9cbb6..aaf10f63fb070 100644
--- a/clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.cpp
@@ -39,18 +39,35 @@ static int getMessageSelectIndex(StringRef Tag) {
return 0;
}
+llvm::SmallVector<llvm::Regex>
+ReservedIdentifierCheck::parseAllowedIdentifiers() const {
+ llvm::SmallVector<llvm::Regex> AllowedIdentifiers;
+ AllowedIdentifiers.reserve(AllowedIdentifiersRaw.size());
+
+ for (const auto &Identifier : AllowedIdentifiersRaw) {
+ AllowedIdentifiers.emplace_back(Identifier.str());
+ if (!AllowedIdentifiers.back().isValid()) {
+ configurationDiag("Invalid allowed identifier regex '%0'") << Identifier;
+ AllowedIdentifiers.pop_back();
+ }
+ }
+
+ return AllowedIdentifiers;
+}
+
ReservedIdentifierCheck::ReservedIdentifierCheck(StringRef Name,
ClangTidyContext *Context)
: RenamerClangTidyCheck(Name, Context),
Invert(Options.get("Invert", false)),
- AllowedIdentifiers(utils::options::parseStringList(
- Options.get("AllowedIdentifiers", ""))) {}
+ AllowedIdentifiersRaw(utils::options::parseStringList(
+ Options.get("AllowedIdentifiers", ""))),
+ AllowedIdentifiers(parseAllowedIdentifiers()) {}
void ReservedIdentifierCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
RenamerClangTidyCheck::storeOptions(Opts);
Options.store(Opts, "Invert", Invert);
Options.store(Opts, "AllowedIdentifiers",
- utils::options::serializeStringList(AllowedIdentifiers));
+ utils::options::serializeStringList(AllowedIdentifiersRaw));
}
static std::string collapseConsecutive(StringRef Str, char C) {
@@ -108,11 +125,14 @@ static std::string getNonReservedFixup(std::string Name) {
static std::optional<RenamerClangTidyCheck::FailureInfo>
getFailureInfoImpl(StringRef Name, bool IsInGlobalNamespace,
const LangOptions &LangOpts, bool Invert,
- ArrayRef<StringRef> AllowedIdentifiers) {
+ ArrayRef<llvm::Regex> 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
diff --git a/clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.h b/clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.h
index e7a410119e579..474dc25f6386c 100644
--- a/clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.h
+++ b/clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.h
@@ -30,7 +30,8 @@ namespace clang::tidy::bugprone {
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/reserved-identifier.html
class ReservedIdentifierCheck final : public RenamerClangTidyCheck {
const bool Invert;
- const std::vector<StringRef> AllowedIdentifiers;
+ const std::vector<StringRef> AllowedIdentifiersRaw;
+ const llvm::SmallVector<llvm::Regex> AllowedIdentifiers;
public:
ReservedIdentifierCheck(StringRef Name, ClangTidyContext *Context);
@@ -46,6 +47,7 @@ class ReservedIdentifierCheck final : public RenamerClangTidyCheck {
const SourceManager &SM) const override;
DiagInfo getDiagInfo(const NamingCheckId &ID,
const NamingCheckFailure &Failure) const override;
+ llvm::SmallVector<llvm::Regex> parseAllowedIdentifiers() const;
};
} // namespace clang::tidy::bugprone
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 1ca2db986ef90..ce66669168711 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -271,6 +271,10 @@ Changes in existing checks
- Improved the performance of the :doc:`bugprone-reserved-identifier
<clang-tidy/checks/bugprone/reserved-identifier>` check through optimizations.
+- Improved the :doc:`bugprone-reserved-identifier
+ <clang-tidy/checks/bugprone/reserved-identifier>` check by enhancing the
+ `AllowedIdentifiers` option to support regular expressions.
+
- Deprecated check-local options `HeaderFileExtensions` and `ImplementationFileExtensions`
in :doc:`bugprone-suspicious-include
<clang-tidy/checks/bugprone/suspicious-include>` check.
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
index 9548fbe84ee4c..a498ff8409af3 100644
--- 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 @@ Options
.. 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
index 3edfb8dca6cb3..d6ad01a667387 100644
--- 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
More information about the cfe-commits
mailing list