Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / lib / Support / Regex.cpp
blob8fa71a749cc8e10e34a55cbb7a0ba296a2fdc3bf
1 //===-- Regex.cpp - Regular Expression matcher implementation -------------===//
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 a POSIX regular expression matcher.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/Regex.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/Twine.h"
17 #include "regex_impl.h"
19 #include <cassert>
20 #include <string>
22 using namespace llvm;
24 Regex::Regex() : preg(nullptr), error(REG_BADPAT) {}
26 Regex::Regex(StringRef regex, RegexFlags Flags) {
27 unsigned flags = 0;
28 preg = new llvm_regex();
29 preg->re_endp = regex.end();
30 if (Flags & IgnoreCase)
31 flags |= REG_ICASE;
32 if (Flags & Newline)
33 flags |= REG_NEWLINE;
34 if (!(Flags & BasicRegex))
35 flags |= REG_EXTENDED;
36 error = llvm_regcomp(preg, regex.data(), flags|REG_PEND);
39 Regex::Regex(StringRef regex, unsigned Flags)
40 : Regex(regex, static_cast<RegexFlags>(Flags)) {}
42 Regex::Regex(Regex &&regex) {
43 preg = regex.preg;
44 error = regex.error;
45 regex.preg = nullptr;
46 regex.error = REG_BADPAT;
49 Regex::~Regex() {
50 if (preg) {
51 llvm_regfree(preg);
52 delete preg;
56 namespace {
58 /// Utility to convert a regex error code into a human-readable string.
59 void RegexErrorToString(int error, struct llvm_regex *preg,
60 std::string &Error) {
61 size_t len = llvm_regerror(error, preg, nullptr, 0);
63 Error.resize(len - 1);
64 llvm_regerror(error, preg, &Error[0], len);
67 } // namespace
69 bool Regex::isValid(std::string &Error) const {
70 if (!error)
71 return true;
73 RegexErrorToString(error, preg, Error);
74 return false;
77 /// getNumMatches - In a valid regex, return the number of parenthesized
78 /// matches it contains.
79 unsigned Regex::getNumMatches() const {
80 return preg->re_nsub;
83 bool Regex::match(StringRef String, SmallVectorImpl<StringRef> *Matches,
84 std::string *Error) const {
85 // Reset error, if given.
86 if (Error && !Error->empty())
87 *Error = "";
89 // Check if the regex itself didn't successfully compile.
90 if (Error ? !isValid(*Error) : !isValid())
91 return false;
93 unsigned nmatch = Matches ? preg->re_nsub+1 : 0;
95 // pmatch needs to have at least one element.
96 SmallVector<llvm_regmatch_t, 8> pm;
97 pm.resize(nmatch > 0 ? nmatch : 1);
98 pm[0].rm_so = 0;
99 pm[0].rm_eo = String.size();
101 int rc = llvm_regexec(preg, String.data(), nmatch, pm.data(), REG_STARTEND);
103 // Failure to match is not an error, it's just a normal return value.
104 // Any other error code is considered abnormal, and is logged in the Error.
105 if (rc == REG_NOMATCH)
106 return false;
107 if (rc != 0) {
108 if (Error)
109 RegexErrorToString(error, preg, *Error);
110 return false;
113 // There was a match.
115 if (Matches) { // match position requested
116 Matches->clear();
118 for (unsigned i = 0; i != nmatch; ++i) {
119 if (pm[i].rm_so == -1) {
120 // this group didn't match
121 Matches->push_back(StringRef());
122 continue;
124 assert(pm[i].rm_eo >= pm[i].rm_so);
125 Matches->push_back(StringRef(String.data()+pm[i].rm_so,
126 pm[i].rm_eo-pm[i].rm_so));
130 return true;
133 std::string Regex::sub(StringRef Repl, StringRef String,
134 std::string *Error) const {
135 SmallVector<StringRef, 8> Matches;
137 // Return the input if there was no match.
138 if (!match(String, &Matches, Error))
139 return std::string(String);
141 // Otherwise splice in the replacement string, starting with the prefix before
142 // the match.
143 std::string Res(String.begin(), Matches[0].begin());
145 // Then the replacement string, honoring possible substitutions.
146 while (!Repl.empty()) {
147 // Skip to the next escape.
148 std::pair<StringRef, StringRef> Split = Repl.split('\\');
150 // Add the skipped substring.
151 Res += Split.first;
153 // Check for terminimation and trailing backslash.
154 if (Split.second.empty()) {
155 if (Repl.size() != Split.first.size() &&
156 Error && Error->empty())
157 *Error = "replacement string contained trailing backslash";
158 break;
161 // Otherwise update the replacement string and interpret escapes.
162 Repl = Split.second;
164 // FIXME: We should have a StringExtras function for mapping C99 escapes.
165 switch (Repl[0]) {
167 // Backreference with the "\g<ref>" syntax
168 case 'g':
169 if (Repl.size() >= 4 && Repl[1] == '<') {
170 size_t End = Repl.find('>');
171 StringRef Ref = Repl.slice(2, End);
172 unsigned RefValue;
173 if (End != StringRef::npos && !Ref.getAsInteger(10, RefValue)) {
174 Repl = Repl.substr(End + 1);
175 if (RefValue < Matches.size())
176 Res += Matches[RefValue];
177 else if (Error && Error->empty())
178 *Error =
179 ("invalid backreference string 'g<" + Twine(Ref) + ">'").str();
180 break;
183 [[fallthrough]];
185 // Treat all unrecognized characters as self-quoting.
186 default:
187 Res += Repl[0];
188 Repl = Repl.substr(1);
189 break;
191 // Single character escapes.
192 case 't':
193 Res += '\t';
194 Repl = Repl.substr(1);
195 break;
196 case 'n':
197 Res += '\n';
198 Repl = Repl.substr(1);
199 break;
201 // Decimal escapes are backreferences.
202 case '0': case '1': case '2': case '3': case '4':
203 case '5': case '6': case '7': case '8': case '9': {
204 // Extract the backreference number.
205 StringRef Ref = Repl.slice(0, Repl.find_first_not_of("0123456789"));
206 Repl = Repl.substr(Ref.size());
208 unsigned RefValue;
209 if (!Ref.getAsInteger(10, RefValue) &&
210 RefValue < Matches.size())
211 Res += Matches[RefValue];
212 else if (Error && Error->empty())
213 *Error = ("invalid backreference string '" + Twine(Ref) + "'").str();
214 break;
219 // And finally the suffix.
220 Res += StringRef(Matches[0].end(), String.end() - Matches[0].end());
222 return Res;
225 // These are the special characters matched in functions like "p_ere_exp".
226 static const char RegexMetachars[] = "()^$|*+?.[]\\{}";
228 bool Regex::isLiteralERE(StringRef Str) {
229 // Check for regex metacharacters. This list was derived from our regex
230 // implementation in regcomp.c and double checked against the POSIX extended
231 // regular expression specification.
232 return Str.find_first_of(RegexMetachars) == StringRef::npos;
235 std::string Regex::escape(StringRef String) {
236 std::string RegexStr;
237 for (char C : String) {
238 if (strchr(RegexMetachars, C))
239 RegexStr += '\\';
240 RegexStr += C;
243 return RegexStr;