Skip to content

Commit 269ef31

Browse files
smhcnjames93
authored andcommitted
[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
1 parent 5073e7e commit 269ef31

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ bool isCapsOnly(StringRef Name) {
3232
class MacroUsageCallbacks : public PPCallbacks {
3333
public:
3434
MacroUsageCallbacks(MacroUsageCheck *Check, const SourceManager &SM,
35-
StringRef RegExp, bool CapsOnly, bool IgnoreCommandLine)
36-
: Check(Check), SM(SM), RegExp(RegExp), CheckCapsOnly(CapsOnly),
35+
StringRef RegExpStr, bool CapsOnly, bool IgnoreCommandLine)
36+
: Check(Check), SM(SM), RegExp(RegExpStr), CheckCapsOnly(CapsOnly),
3737
IgnoreCommandLineMacros(IgnoreCommandLine) {}
3838
void MacroDefined(const Token &MacroNameTok,
3939
const MacroDirective *MD) override {
@@ -47,7 +47,7 @@ class MacroUsageCallbacks : public PPCallbacks {
4747
return;
4848

4949
StringRef MacroName = MacroNameTok.getIdentifierInfo()->getName();
50-
if (!CheckCapsOnly && !llvm::Regex(RegExp).match(MacroName))
50+
if (!CheckCapsOnly && !RegExp.match(MacroName))
5151
Check->warnMacro(MD, MacroName);
5252

5353
if (CheckCapsOnly && !isCapsOnly(MacroName))
@@ -57,7 +57,7 @@ class MacroUsageCallbacks : public PPCallbacks {
5757
private:
5858
MacroUsageCheck *Check;
5959
const SourceManager &SM;
60-
StringRef RegExp;
60+
const llvm::Regex RegExp;
6161
bool CheckCapsOnly;
6262
bool IgnoreCommandLineMacros;
6363
};

0 commit comments

Comments
 (0)