[clang-tools-extra] 269ef31 - [clang-tidy] Use compiled regex for AllowedRegexp in macro usage check

Nathan James via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 23 12:47:45 PST 2020


Author: smhc
Date: 2020-11-23T20:46:43Z
New Revision: 269ef315d1beaff534a038b60389226b0f0f5d4f

URL: https://github.com/llvm/llvm-project/commit/269ef315d1beaff534a038b60389226b0f0f5d4f
DIFF: https://github.com/llvm/llvm-project/commit/269ef315d1beaff534a038b60389226b0f0f5d4f.diff

LOG: [clang-tidy] Use compiled regex for AllowedRegexp in macro usage check

Current check compiles the regex on every attempt at matching. The check also populates and enables a regex value by default so the default behaviour results in regex re-compilation for every macro - if the check is enabled. If people used this check there's a reasonable chance they would have relatively complex regexes in use.

This is a quick and simple fix to store and use the compiled regex.

Reviewed By: njames93

Differential Revision: https://reviews.llvm.org/D91908

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp
index febc295d78e6..eb21bb44f63d 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp
@@ -32,8 +32,8 @@ bool isCapsOnly(StringRef Name) {
 class MacroUsageCallbacks : public PPCallbacks {
 public:
   MacroUsageCallbacks(MacroUsageCheck *Check, const SourceManager &SM,
-                      StringRef RegExp, bool CapsOnly, bool IgnoreCommandLine)
-      : Check(Check), SM(SM), RegExp(RegExp), CheckCapsOnly(CapsOnly),
+                      StringRef RegExpStr, bool CapsOnly, bool IgnoreCommandLine)
+      : Check(Check), SM(SM), RegExp(RegExpStr), CheckCapsOnly(CapsOnly),
         IgnoreCommandLineMacros(IgnoreCommandLine) {}
   void MacroDefined(const Token &MacroNameTok,
                     const MacroDirective *MD) override {
@@ -47,7 +47,7 @@ class MacroUsageCallbacks : public PPCallbacks {
       return;
 
     StringRef MacroName = MacroNameTok.getIdentifierInfo()->getName();
-    if (!CheckCapsOnly && !llvm::Regex(RegExp).match(MacroName))
+    if (!CheckCapsOnly && !RegExp.match(MacroName))
       Check->warnMacro(MD, MacroName);
 
     if (CheckCapsOnly && !isCapsOnly(MacroName))
@@ -57,7 +57,7 @@ class MacroUsageCallbacks : public PPCallbacks {
 private:
   MacroUsageCheck *Check;
   const SourceManager &SM;
-  StringRef RegExp;
+  const llvm::Regex RegExp;
   bool CheckCapsOnly;
   bool IgnoreCommandLineMacros;
 };


        


More information about the cfe-commits mailing list