1 //===- StringMatcher.cpp - Generate a matcher for input strings -----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the StringMatcher class.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/TableGen/StringMatcher.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/Support/raw_ostream.h"
25 /// FindFirstNonCommonLetter - Find the first character in the keys of the
26 /// string pairs that is not shared across the whole set of strings. All
27 /// strings are assumed to have the same length.
29 FindFirstNonCommonLetter(const std::vector
<const
30 StringMatcher::StringPair
*> &Matches
) {
31 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
];
36 for (const StringMatcher::StringPair
*Match
: Matches
)
37 if (Match
->first
[i
] != Letter
)
41 return Matches
[0]->first
.size();
44 /// EmitStringMatcherForChar - Given a set of strings that are known to be the
45 /// same length and whose characters leading up to CharNo are the same, emit
46 /// code to verify that CharNo and later are the same.
48 /// \return - True if control can leave the emitted code fragment.
49 bool StringMatcher::EmitStringMatcherForChar(
50 const std::vector
<const StringPair
*> &Matches
, unsigned CharNo
,
51 unsigned IndentCount
, bool IgnoreDuplicates
) const {
52 assert(!Matches
.empty() && "Must have at least one string to match!");
53 std::string
Indent(IndentCount
* 2 + 4, ' ');
55 // If we have verified that the entire string matches, we're done: output the
57 if (CharNo
== Matches
[0]->first
.size()) {
58 if (Matches
.size() > 1 && !IgnoreDuplicates
)
59 report_fatal_error("Had duplicate keys to match on");
61 // If the to-execute code has \n's in it, indent each subsequent line.
62 StringRef Code
= Matches
[0]->second
;
64 std::pair
<StringRef
, StringRef
> Split
= Code
.split('\n');
65 OS
<< Indent
<< Split
.first
<< "\t // \"" << Matches
[0]->first
<< "\"\n";
68 while (!Code
.empty()) {
69 Split
= Code
.split('\n');
70 OS
<< Indent
<< Split
.first
<< "\n";
76 // Bucket the matches by the character we are comparing.
77 std::map
<char, std::vector
<const StringPair
*>> MatchesByLetter
;
79 for (const StringPair
*Match
: Matches
)
80 MatchesByLetter
[Match
->first
[CharNo
]].push_back(Match
);
82 // If we have exactly one bucket to match, see how many characters are common
83 // across the whole set and match all of them at once.
84 if (MatchesByLetter
.size() == 1) {
85 unsigned FirstNonCommonLetter
= FindFirstNonCommonLetter(Matches
);
86 unsigned NumChars
= FirstNonCommonLetter
-CharNo
;
88 // Emit code to break out if the prefix doesn't match.
90 // Do the comparison with if (Str[1] != 'f')
91 // FIXME: Need to escape general characters.
92 OS
<< Indent
<< "if (" << StrVariableName
<< "[" << CharNo
<< "] != '"
93 << Matches
[0]->first
[CharNo
] << "')\n";
94 OS
<< Indent
<< " break;\n";
96 // Do the comparison with if memcmp(Str.data()+1, "foo", 3).
97 // FIXME: Need to escape general strings.
98 OS
<< Indent
<< "if (memcmp(" << StrVariableName
<< ".data()+" << CharNo
99 << ", \"" << Matches
[0]->first
.substr(CharNo
, NumChars
) << "\", "
100 << NumChars
<< ") != 0)\n";
101 OS
<< Indent
<< " break;\n";
104 return EmitStringMatcherForChar(Matches
, FirstNonCommonLetter
, IndentCount
,
108 // Otherwise, we have multiple possible things, emit a switch on the
110 OS
<< Indent
<< "switch (" << StrVariableName
<< "[" << CharNo
<< "]) {\n";
111 OS
<< Indent
<< "default: break;\n";
113 for (const auto &LI
: MatchesByLetter
) {
114 // TODO: escape hard stuff (like \n) if we ever care about it.
115 OS
<< Indent
<< "case '" << LI
.first
<< "':\t // " << LI
.second
.size()
117 if (LI
.second
.size() != 1)
119 OS
<< " to match.\n";
120 if (EmitStringMatcherForChar(LI
.second
, CharNo
+ 1, IndentCount
+ 1,
122 OS
<< Indent
<< " break;\n";
125 OS
<< Indent
<< "}\n";
129 /// Emit - Top level entry point.
131 void StringMatcher::Emit(unsigned Indent
, bool IgnoreDuplicates
) const {
132 // If nothing to match, just fall through.
133 if (Matches
.empty()) return;
135 // First level categorization: group strings by length.
136 std::map
<unsigned, std::vector
<const StringPair
*>> MatchesByLength
;
138 for (const StringPair
&Match
: Matches
)
139 MatchesByLength
[Match
.first
.size()].push_back(&Match
);
141 // Output a switch statement on length and categorize the elements within each
143 OS
.indent(Indent
*2+2) << "switch (" << StrVariableName
<< ".size()) {\n";
144 OS
.indent(Indent
*2+2) << "default: break;\n";
146 for (const auto &LI
: MatchesByLength
) {
147 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
))
151 OS
.indent(Indent
*2+4) << "break;\n";
154 OS
.indent(Indent
*2+2) << "}\n";