Skip to content

[NFC][TableGen] Code cleanup in StringMatcher #141118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2025

Conversation

jurahul
Copy link
Contributor

@jurahul jurahul commented May 22, 2025

  • Use ArrayRef instead of const vector reference.
  • Use range for loops with enumerate and structured bindings.

@jurahul jurahul marked this pull request as ready for review May 22, 2025 22:26
@llvmbot
Copy link
Member

llvmbot commented May 22, 2025

@llvm/pr-subscribers-tablegen

Author: Rahul Joshi (jurahul)

Changes
  • Use ArrayRef instead of const vector reference.
  • Use range for loops with enumerate and structured bindings.

Full diff: https://github.com/llvm/llvm-project/pull/141118.diff

2 Files Affected:

  • (modified) llvm/include/llvm/TableGen/StringMatcher.h (+6-6)
  • (modified) llvm/lib/TableGen/StringMatcher.cpp (+15-18)
diff --git a/llvm/include/llvm/TableGen/StringMatcher.h b/llvm/include/llvm/TableGen/StringMatcher.h
index 795b7a6d41dcc..49769883a98b4 100644
--- a/llvm/include/llvm/TableGen/StringMatcher.h
+++ b/llvm/include/llvm/TableGen/StringMatcher.h
@@ -13,10 +13,10 @@
 #ifndef LLVM_TABLEGEN_STRINGMATCHER_H
 #define LLVM_TABLEGEN_STRINGMATCHER_H
 
+#include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/StringRef.h"
 #include <string>
 #include <utility>
-#include <vector>
 
 namespace llvm {
 
@@ -33,18 +33,18 @@ class StringMatcher {
 
 private:
   StringRef StrVariableName;
-  const std::vector<StringPair> &Matches;
+  ArrayRef<StringPair> Matches;
   raw_ostream &OS;
 
 public:
-  StringMatcher(StringRef strVariableName,
-                const std::vector<StringPair> &matches, raw_ostream &os)
-    : StrVariableName(strVariableName), Matches(matches), OS(os) {}
+  StringMatcher(StringRef StrVariableName, ArrayRef<StringPair> Matches,
+                raw_ostream &OS)
+      : StrVariableName(StrVariableName), Matches(Matches), OS(OS) {}
 
   void Emit(unsigned Indent = 0, bool IgnoreDuplicates = false) const;
 
 private:
-  bool EmitStringMatcherForChar(const std::vector<const StringPair *> &Matches,
+  bool EmitStringMatcherForChar(ArrayRef<const StringPair *> Matches,
                                 unsigned CharNo, unsigned IndentCount,
                                 bool IgnoreDuplicates) const;
 };
diff --git a/llvm/lib/TableGen/StringMatcher.cpp b/llvm/lib/TableGen/StringMatcher.cpp
index c169b4e0a3620..b85849e40bb5e 100644
--- a/llvm/lib/TableGen/StringMatcher.cpp
+++ b/llvm/lib/TableGen/StringMatcher.cpp
@@ -26,16 +26,13 @@ using namespace llvm;
 /// string pairs that is not shared across the whole set of strings.  All
 /// strings are assumed to have the same length.
 static unsigned
-FindFirstNonCommonLetter(const std::vector<const
-                              StringMatcher::StringPair*> &Matches) {
+FindFirstNonCommonLetter(ArrayRef<const StringMatcher::StringPair *> Matches) {
   assert(!Matches.empty());
-  for (unsigned i = 0, e = Matches[0]->first.size(); i != e; ++i) {
-    // Check to see if letter i is the same across the set.
-    char Letter = Matches[0]->first[i];
-
+  for (auto [Idx, Letter] : enumerate(Matches[0]->first)) {
+    // Check to see if `Letter` is the same across the set.
     for (const StringMatcher::StringPair *Match : Matches)
-      if (Match->first[i] != Letter)
-        return i;
+      if (Match->first[Idx] != Letter)
+        return Idx;
   }
 
   return Matches[0]->first.size();
@@ -47,8 +44,8 @@ FindFirstNonCommonLetter(const std::vector<const
 ///
 /// \return - True if control can leave the emitted code fragment.
 bool StringMatcher::EmitStringMatcherForChar(
-    const std::vector<const StringPair *> &Matches, unsigned CharNo,
-    unsigned IndentCount, bool IgnoreDuplicates) const {
+    ArrayRef<const StringPair *> Matches, unsigned CharNo, unsigned IndentCount,
+    bool IgnoreDuplicates) const {
   assert(!Matches.empty() && "Must have at least one string to match!");
   std::string Indent(IndentCount * 2 + 4, ' ');
 
@@ -110,14 +107,14 @@ bool StringMatcher::EmitStringMatcherForChar(
   OS << Indent << "switch (" << StrVariableName << "[" << CharNo << "]) {\n";
   OS << Indent << "default: break;\n";
 
-  for (const auto &LI : MatchesByLetter) {
+  for (const auto &[Letter, Matches] : MatchesByLetter) {
     // TODO: escape hard stuff (like \n) if we ever care about it.
-    OS << Indent << "case '" << LI.first << "':\t // " << LI.second.size()
+    OS << Indent << "case '" << Letter << "':\t // " << Matches.size()
        << " string";
-    if (LI.second.size() != 1)
+    if (Matches.size() != 1)
       OS << 's';
     OS << " to match.\n";
-    if (EmitStringMatcherForChar(LI.second, CharNo + 1, IndentCount + 1,
+    if (EmitStringMatcherForChar(Matches, CharNo + 1, IndentCount + 1,
                                  IgnoreDuplicates))
       OS << Indent << "  break;\n";
   }
@@ -143,11 +140,11 @@ void StringMatcher::Emit(unsigned Indent, bool IgnoreDuplicates) const {
   OS.indent(Indent*2+2) << "switch (" << StrVariableName << ".size()) {\n";
   OS.indent(Indent*2+2) << "default: break;\n";
 
-  for (const auto &LI : MatchesByLength) {
+  for (const auto &[Length, Matches] : MatchesByLength) {
     OS.indent(Indent * 2 + 2)
-        << "case " << LI.first << ":\t // " << LI.second.size() << " string"
-        << (LI.second.size() == 1 ? "" : "s") << " to match.\n";
-    if (EmitStringMatcherForChar(LI.second, 0, Indent, IgnoreDuplicates))
+        << "case " << Length << ":\t // " << Matches.size() << " string"
+        << (Matches.size() == 1 ? "" : "s") << " to match.\n";
+    if (EmitStringMatcherForChar(Matches, 0, Indent, IgnoreDuplicates))
       OS.indent(Indent*2+4) << "break;\n";
   }
 

Copy link
Contributor

@kazutakahirata kazutakahirata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@jurahul jurahul merged commit 523d6c3 into llvm:main May 22, 2025
15 checks passed
@jurahul jurahul deleted the stringmatcher_code_cleanup branch May 22, 2025 22:55
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
- Use ArrayRef instead of const vector reference.
- Use range for loops with enumerate and structured bindings.
ajaden-codes pushed a commit to Jaddyen/llvm-project that referenced this pull request Jun 6, 2025
- Use ArrayRef instead of const vector reference.
- Use range for loops with enumerate and structured bindings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants