[clang-tidy] Use compiled regex for AllowedRegexp in macro usage check
authorsmhc <shanehird@gmail.com>
Mon, 23 Nov 2020 20:45:50 +0000 (20:45 +0000)
committerNathan James <n.james93@hotmail.co.uk>
Mon, 23 Nov 2020 20:46:43 +0000 (20:46 +0000)
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

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

index febc295d78e6a6dbc035bda801da3a38d25df51e..eb21bb44f63d167b3c9a900df1b24512a049118b 100644 (file)
@@ -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 @@ public:
       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 @@ public:
 private:
   MacroUsageCheck *Check;
   const SourceManager &SM;
-  StringRef RegExp;
+  const llvm::Regex RegExp;
   bool CheckCapsOnly;
   bool IgnoreCommandLineMacros;
 };