felix642 created this revision.
Herald added subscribers: PiotrZSL, carlosgalvezp, xazax.hun.
Herald added a reviewer: njames93.
Herald added a project: All.
felix642 requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

Fixes: https://github.com/llvm/llvm-project/issues/59119


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152764

Files:
  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

Index: clang-tools-extra/test/clang-tidy/checkers/bugprone/reserved-identifier-invert.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/bugprone/reserved-identifier-invert.cpp
+++ 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
Index: clang-tools-extra/docs/clang-tidy/checks/bugprone/reserved-identifier.rst
===================================================================
--- clang-tools-extra/docs/clang-tidy/checks/bugprone/reserved-identifier.rst
+++ 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.
Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ 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
+  <clang-tidy/checks/bugprone/reserved-identifier>` to support regular
+  expressions.
+
 Removed checks
 ^^^^^^^^^^^^^^
 
Index: clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.h
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.h
+++ clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.h
@@ -31,6 +31,7 @@
 class ReservedIdentifierCheck final : public RenamerClangTidyCheck {
   const bool Invert;
   const std::vector<StringRef> AllowedIdentifiers;
+  llvm::SmallVector<llvm::Regex> AllowedIdentifiersRegex;
 
 public:
   ReservedIdentifierCheck(StringRef Name, ClangTidyContext *Context);
Index: clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.cpp
+++ 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<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
@@ -159,14 +168,14 @@
          "Decl must be an explicit identifier with a name.");
   return getFailureInfoImpl(Decl->getName(),
                             isa<TranslationUnitDecl>(Decl->getDeclContext()),
-                            getLangOpts(), Invert, AllowedIdentifiers);
+                            getLangOpts(), Invert, AllowedIdentifiersRegex);
 }
 
 std::optional<RenamerClangTidyCheck::FailureInfo>
 ReservedIdentifierCheck::getMacroFailureInfo(const Token &MacroNameTok,
                                              const SourceManager &) const {
   return getFailureInfoImpl(MacroNameTok.getIdentifierInfo()->getName(), true,
-                            getLangOpts(), Invert, AllowedIdentifiers);
+                            getLangOpts(), Invert, AllowedIdentifiersRegex);
 }
 
 RenamerClangTidyCheck::DiagInfo
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D152764:... Félix-Antoine Constantin via Phabricator via cfe-commits

Reply via email to