Skip to content

Commit 523d6c3

Browse files
authored
[NFC][TableGen] Code cleanup in StringMatcher (#141118)
- Use ArrayRef instead of const vector reference. - Use range for loops with enumerate and structured bindings.
1 parent 05494f3 commit 523d6c3

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

llvm/include/llvm/TableGen/StringMatcher.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
#ifndef LLVM_TABLEGEN_STRINGMATCHER_H
1414
#define LLVM_TABLEGEN_STRINGMATCHER_H
1515

16+
#include "llvm/ADT/ArrayRef.h"
1617
#include "llvm/ADT/StringRef.h"
1718
#include <string>
1819
#include <utility>
19-
#include <vector>
2020

2121
namespace llvm {
2222

@@ -33,18 +33,18 @@ class StringMatcher {
3333

3434
private:
3535
StringRef StrVariableName;
36-
const std::vector<StringPair> &Matches;
36+
ArrayRef<StringPair> Matches;
3737
raw_ostream &OS;
3838

3939
public:
40-
StringMatcher(StringRef strVariableName,
41-
const std::vector<StringPair> &matches, raw_ostream &os)
42-
: StrVariableName(strVariableName), Matches(matches), OS(os) {}
40+
StringMatcher(StringRef StrVariableName, ArrayRef<StringPair> Matches,
41+
raw_ostream &OS)
42+
: StrVariableName(StrVariableName), Matches(Matches), OS(OS) {}
4343

4444
void Emit(unsigned Indent = 0, bool IgnoreDuplicates = false) const;
4545

4646
private:
47-
bool EmitStringMatcherForChar(const std::vector<const StringPair *> &Matches,
47+
bool EmitStringMatcherForChar(ArrayRef<const StringPair *> Matches,
4848
unsigned CharNo, unsigned IndentCount,
4949
bool IgnoreDuplicates) const;
5050
};

llvm/lib/TableGen/StringMatcher.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,13 @@ using namespace llvm;
2626
/// string pairs that is not shared across the whole set of strings. All
2727
/// strings are assumed to have the same length.
2828
static unsigned
29-
FindFirstNonCommonLetter(const std::vector<const
30-
StringMatcher::StringPair*> &Matches) {
29+
FindFirstNonCommonLetter(ArrayRef<const StringMatcher::StringPair *> Matches) {
3130
assert(!Matches.empty());
32-
for (unsigned i = 0, e = Matches[0]->first.size(); i != e; ++i) {
33-
// Check to see if letter i is the same across the set.
34-
char Letter = Matches[0]->first[i];
35-
31+
for (auto [Idx, Letter] : enumerate(Matches[0]->first)) {
32+
// Check to see if `Letter` is the same across the set.
3633
for (const StringMatcher::StringPair *Match : Matches)
37-
if (Match->first[i] != Letter)
38-
return i;
34+
if (Match->first[Idx] != Letter)
35+
return Idx;
3936
}
4037

4138
return Matches[0]->first.size();
@@ -47,8 +44,8 @@ FindFirstNonCommonLetter(const std::vector<const
4744
///
4845
/// \return - True if control can leave the emitted code fragment.
4946
bool StringMatcher::EmitStringMatcherForChar(
50-
const std::vector<const StringPair *> &Matches, unsigned CharNo,
51-
unsigned IndentCount, bool IgnoreDuplicates) const {
47+
ArrayRef<const StringPair *> Matches, unsigned CharNo, unsigned IndentCount,
48+
bool IgnoreDuplicates) const {
5249
assert(!Matches.empty() && "Must have at least one string to match!");
5350
std::string Indent(IndentCount * 2 + 4, ' ');
5451

@@ -110,14 +107,14 @@ bool StringMatcher::EmitStringMatcherForChar(
110107
OS << Indent << "switch (" << StrVariableName << "[" << CharNo << "]) {\n";
111108
OS << Indent << "default: break;\n";
112109

113-
for (const auto &LI : MatchesByLetter) {
110+
for (const auto &[Letter, Matches] : MatchesByLetter) {
114111
// TODO: escape hard stuff (like \n) if we ever care about it.
115-
OS << Indent << "case '" << LI.first << "':\t // " << LI.second.size()
112+
OS << Indent << "case '" << Letter << "':\t // " << Matches.size()
116113
<< " string";
117-
if (LI.second.size() != 1)
114+
if (Matches.size() != 1)
118115
OS << 's';
119116
OS << " to match.\n";
120-
if (EmitStringMatcherForChar(LI.second, CharNo + 1, IndentCount + 1,
117+
if (EmitStringMatcherForChar(Matches, CharNo + 1, IndentCount + 1,
121118
IgnoreDuplicates))
122119
OS << Indent << " break;\n";
123120
}
@@ -143,11 +140,11 @@ void StringMatcher::Emit(unsigned Indent, bool IgnoreDuplicates) const {
143140
OS.indent(Indent*2+2) << "switch (" << StrVariableName << ".size()) {\n";
144141
OS.indent(Indent*2+2) << "default: break;\n";
145142

146-
for (const auto &LI : MatchesByLength) {
143+
for (const auto &[Length, Matches] : MatchesByLength) {
147144
OS.indent(Indent * 2 + 2)
148-
<< "case " << LI.first << ":\t // " << LI.second.size() << " string"
149-
<< (LI.second.size() == 1 ? "" : "s") << " to match.\n";
150-
if (EmitStringMatcherForChar(LI.second, 0, Indent, IgnoreDuplicates))
145+
<< "case " << Length << ":\t // " << Matches.size() << " string"
146+
<< (Matches.size() == 1 ? "" : "s") << " to match.\n";
147+
if (EmitStringMatcherForChar(Matches, 0, Indent, IgnoreDuplicates))
151148
OS.indent(Indent*2+4) << "break;\n";
152149
}
153150

0 commit comments

Comments
 (0)