1 | //===- StringMatcher.cpp - Generate a matcher for input strings -----------===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file implements the StringMatcher class. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "llvm/TableGen/StringMatcher.h" |
14 | #include "llvm/ADT/STLExtras.h" |
15 | #include "llvm/Support/ErrorHandling.h" |
16 | #include "llvm/Support/raw_ostream.h" |
17 | #include <cassert> |
18 | #include <map> |
19 | #include <string> |
20 | #include <utility> |
21 | #include <vector> |
22 | |
23 | using namespace llvm; |
24 | |
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. |
28 | static unsigned |
29 | FindFirstNonCommonLetter(ArrayRef<const StringMatcher::StringPair *> Matches) { |
30 | assert(!Matches.empty()); |
31 | for (auto [Idx, Letter] : enumerate(First: Matches[0]->first)) { |
32 | // Check to see if `Letter` is the same across the set. Since the letter is |
33 | // from `Matches[0]`, we can skip `Matches[0]` in the loop below. |
34 | for (const StringMatcher::StringPair *Match : Matches.drop_front()) |
35 | if (Match->first[Idx] != Letter) |
36 | return Idx; |
37 | } |
38 | |
39 | return Matches[0]->first.size(); |
40 | } |
41 | |
42 | /// EmitStringMatcherForChar - Given a set of strings that are known to be the |
43 | /// same length and whose characters leading up to CharNo are the same, emit |
44 | /// code to verify that CharNo and later are the same. |
45 | /// |
46 | /// \return - True if control can leave the emitted code fragment. |
47 | bool StringMatcher::EmitStringMatcherForChar( |
48 | ArrayRef<const StringPair *> Matches, unsigned CharNo, unsigned IndentCount, |
49 | bool IgnoreDuplicates) const { |
50 | assert(!Matches.empty() && "Must have at least one string to match!" ); |
51 | std::string Indent(IndentCount * 2 + 4, ' '); |
52 | |
53 | // If we have verified that the entire string matches, we're done: output the |
54 | // matching code. |
55 | if (CharNo == Matches[0]->first.size()) { |
56 | if (Matches.size() > 1 && !IgnoreDuplicates) |
57 | report_fatal_error(reason: "Had duplicate keys to match on" ); |
58 | |
59 | // If the to-execute code has \n's in it, indent each subsequent line. |
60 | StringRef Code = Matches[0]->second; |
61 | |
62 | std::pair<StringRef, StringRef> Split = Code.split(Separator: '\n'); |
63 | OS << Indent << Split.first << "\t // \"" << Matches[0]->first << "\"\n" ; |
64 | |
65 | Code = Split.second; |
66 | while (!Code.empty()) { |
67 | Split = Code.split(Separator: '\n'); |
68 | OS << Indent << Split.first << "\n" ; |
69 | Code = Split.second; |
70 | } |
71 | return false; |
72 | } |
73 | |
74 | // Bucket the matches by the character we are comparing. |
75 | std::map<char, std::vector<const StringPair*>> MatchesByLetter; |
76 | |
77 | for (const StringPair *Match : Matches) |
78 | MatchesByLetter[Match->first[CharNo]].push_back(x: Match); |
79 | |
80 | // If we have exactly one bucket to match, see how many characters are common |
81 | // across the whole set and match all of them at once. |
82 | if (MatchesByLetter.size() == 1) { |
83 | unsigned FirstNonCommonLetter = FindFirstNonCommonLetter(Matches); |
84 | unsigned NumChars = FirstNonCommonLetter-CharNo; |
85 | |
86 | // Emit code to break out if the prefix doesn't match. |
87 | if (NumChars == 1) { |
88 | // Do the comparison with if (Str[1] != 'f') |
89 | // FIXME: Need to escape general characters. |
90 | OS << Indent << "if (" << StrVariableName << "[" << CharNo << "] != '" |
91 | << Matches[0]->first[CharNo] << "')\n" ; |
92 | OS << Indent << " break;\n" ; |
93 | } else { |
94 | // Do the comparison with if memcmp(Str.data()+1, "foo", 3). |
95 | // FIXME: Need to escape general strings. |
96 | OS << Indent << "if (memcmp(" << StrVariableName << ".data()+" << CharNo |
97 | << ", \"" << Matches[0]->first.substr(pos: CharNo, n: NumChars) << "\", " |
98 | << NumChars << ") != 0)\n" ; |
99 | OS << Indent << " break;\n" ; |
100 | } |
101 | |
102 | return EmitStringMatcherForChar(Matches, CharNo: FirstNonCommonLetter, IndentCount, |
103 | IgnoreDuplicates); |
104 | } |
105 | |
106 | // Otherwise, we have multiple possible things, emit a switch on the |
107 | // character. |
108 | OS << Indent << "switch (" << StrVariableName << "[" << CharNo << "]) {\n" ; |
109 | OS << Indent << "default: break;\n" ; |
110 | |
111 | for (const auto &[Letter, Matches] : MatchesByLetter) { |
112 | // TODO: escape hard stuff (like \n) if we ever care about it. |
113 | OS << Indent << "case '" << Letter << "':\t // " << Matches.size() |
114 | << " string" ; |
115 | if (Matches.size() != 1) |
116 | OS << 's'; |
117 | OS << " to match.\n" ; |
118 | if (EmitStringMatcherForChar(Matches, CharNo: CharNo + 1, IndentCount: IndentCount + 1, |
119 | IgnoreDuplicates)) |
120 | OS << Indent << " break;\n" ; |
121 | } |
122 | |
123 | OS << Indent << "}\n" ; |
124 | return true; |
125 | } |
126 | |
127 | /// Emit - Top level entry point. |
128 | /// |
129 | void StringMatcher::Emit(unsigned Indent, bool IgnoreDuplicates) const { |
130 | // If nothing to match, just fall through. |
131 | if (Matches.empty()) return; |
132 | |
133 | // First level categorization: group strings by length. |
134 | std::map<unsigned, std::vector<const StringPair*>> MatchesByLength; |
135 | |
136 | for (const StringPair &Match : Matches) |
137 | MatchesByLength[Match.first.size()].push_back(x: &Match); |
138 | |
139 | // Output a switch statement on length and categorize the elements within each |
140 | // bin. |
141 | OS.indent(NumSpaces: Indent*2+2) << "switch (" << StrVariableName << ".size()) {\n" ; |
142 | OS.indent(NumSpaces: Indent*2+2) << "default: break;\n" ; |
143 | |
144 | for (const auto &[Length, Matches] : MatchesByLength) { |
145 | OS.indent(NumSpaces: Indent * 2 + 2) |
146 | << "case " << Length << ":\t // " << Matches.size() << " string" |
147 | << (Matches.size() == 1 ? "" : "s" ) << " to match.\n" ; |
148 | if (EmitStringMatcherForChar(Matches, CharNo: 0, IndentCount: Indent, IgnoreDuplicates)) |
149 | OS.indent(NumSpaces: Indent*2+4) << "break;\n" ; |
150 | } |
151 | |
152 | OS.indent(NumSpaces: Indent*2+2) << "}\n" ; |
153 | } |
154 | |