================
@@ -29,55 +29,79 @@
 
 namespace llvm {
 
-Error SpecialCaseList::Matcher::insert(StringRef Pattern, unsigned LineNumber,
-                                       bool UseGlobs) {
+Error SpecialCaseList::RegexMatcher::insert(StringRef Pattern,
+                                            unsigned LineNumber) {
   if (Pattern.empty())
     return createStringError(errc::invalid_argument,
-                             Twine("Supplied ") +
-                                 (UseGlobs ? "glob" : "regex") + " was blank");
-
-  if (!UseGlobs) {
-    // Replace * with .*
-    auto Regexp = Pattern.str();
-    for (size_t pos = 0; (pos = Regexp.find('*', pos)) != std::string::npos;
-         pos += strlen(".*")) {
-      Regexp.replace(pos, strlen("*"), ".*");
-    }
+                             "Supplied regex was blank");
+
+  // Replace * with .*
+  auto Regexp = Pattern.str();
+  for (size_t pos = 0; (pos = Regexp.find('*', pos)) != std::string::npos;
+       pos += strlen(".*")) {
+    Regexp.replace(pos, strlen("*"), ".*");
+  }
 
-    Regexp = (Twine("^(") + StringRef(Regexp) + ")$").str();
+  Regexp = (Twine("^(") + StringRef(Regexp) + ")$").str();
 
-    // Check that the regexp is valid.
-    Regex CheckRE(Regexp);
-    std::string REError;
-    if (!CheckRE.isValid(REError))
-      return createStringError(errc::invalid_argument, REError);
+  // Check that the regexp is valid.
+  Regex CheckRE(Regexp);
+  std::string REError;
+  if (!CheckRE.isValid(REError))
+    return createStringError(errc::invalid_argument, REError);
 
-    auto Rg =
-        std::make_unique<Matcher::Reg>(Pattern, LineNumber, 
std::move(CheckRE));
-    RegExes.emplace_back(std::move(Rg));
+  auto Rg = std::make_unique<Reg>(Pattern, LineNumber, std::move(CheckRE));
+  RegExes.emplace_back(std::move(Rg));
 
-    return Error::success();
-  }
+  return Error::success();
+}
+
+void SpecialCaseList::RegexMatcher::match(
+    StringRef Query,
+    llvm::function_ref<void(StringRef Rule, unsigned LineNo)> Cb) const {
+  for (const auto &Regex : reverse(RegExes))
+    if (Regex->Rg.match(Query))
+      Cb(Regex->Name, Regex->LineNo);
+}
+
+Error SpecialCaseList::GlobMatcher::insert(StringRef Pattern,
+                                           unsigned LineNumber) {
+  if (Pattern.empty())
+    return createStringError(errc::invalid_argument, "Supplied glob was 
blank");
 
-  auto Glob = std::make_unique<Matcher::Glob>(Pattern, LineNumber);
+  auto G = std::make_unique<Glob>(Pattern, LineNumber);
   // We must be sure to use the string in `Glob` rather than the provided
   // reference which could be destroyed before match() is called
-  if (auto Err = GlobPattern::create(Glob->Name, /*MaxSubPatterns=*/1024)
-                     .moveInto(Glob->Pattern))
+  if (auto Err = GlobPattern::create(G->Name, /*MaxSubPatterns=*/1024)
+                     .moveInto(G->Pattern))
     return Err;
-  Globs.push_back(std::move(Glob));
+  Globs.emplace_back(std::move(G));
   return Error::success();
 }
 
-void SpecialCaseList::Matcher::match(
+void SpecialCaseList::GlobMatcher::match(
     StringRef Query,
     llvm::function_ref<void(StringRef Rule, unsigned LineNo)> Cb) const {
   for (const auto &Glob : reverse(Globs))
     if (Glob->Pattern.match(Query))
       Cb(Glob->Name, Glob->LineNo);
-  for (const auto &Regex : reverse(RegExes))
-    if (Regex->Rg.match(Query))
-      Cb(Regex->Name, Regex->LineNo);
+}
+
+SpecialCaseList::Matcher::Matcher(bool UseGlobs) {
+  if (UseGlobs)
+    M.emplace<GlobMatcher>();
+  else
+    M.emplace<RegexMatcher>();
+}
+
+void SpecialCaseList::Matcher::match(
+    StringRef Query,
+    llvm::function_ref<void(StringRef Rule, unsigned LineNo)> Cb) const {
+  return std::visit([&](auto &V) { return V.match(Query, Cb); }, M);
----------------
fmayer wrote:

isn't that just hand-rolled virtual calls?

https://github.com/llvm/llvm-project/pull/162303
_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to