2009-08-30 10:24:09 +02:00
|
|
|
//===-- Regex.cpp - Regular Expression matcher implementation -------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-08-30 10:24:09 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements a POSIX regular expression matcher.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-09-24 22:15:51 +02:00
|
|
|
|
2009-08-30 10:24:09 +02:00
|
|
|
#include "llvm/Support/Regex.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2015-03-23 19:07:13 +01:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2015-03-30 17:42:36 +02:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2020-03-02 18:24:11 +01:00
|
|
|
#include <cassert>
|
2009-08-30 10:24:09 +02:00
|
|
|
#include <string>
|
2018-03-12 12:01:05 +01:00
|
|
|
|
|
|
|
// Important this comes last because it defines "_REGEX_H_". At least on
|
|
|
|
// Darwin, if included before any header that (transitively) includes
|
|
|
|
// xlocale.h, this will cause trouble, because of missing regex-related types.
|
|
|
|
#include "regex_impl.h"
|
|
|
|
|
2009-08-30 10:24:09 +02:00
|
|
|
using namespace llvm;
|
2009-09-24 22:15:51 +02:00
|
|
|
|
2016-09-01 11:31:02 +02:00
|
|
|
Regex::Regex() : preg(nullptr), error(REG_BADPAT) {}
|
2016-09-01 10:00:28 +02:00
|
|
|
|
2020-07-02 15:52:24 +02:00
|
|
|
Regex::Regex(StringRef regex, RegexFlags Flags) {
|
2009-08-30 10:24:09 +02:00
|
|
|
unsigned flags = 0;
|
2009-09-24 23:47:32 +02:00
|
|
|
preg = new llvm_regex();
|
2009-08-30 10:24:09 +02:00
|
|
|
preg->re_endp = regex.end();
|
2018-03-12 12:01:05 +01:00
|
|
|
if (Flags & IgnoreCase)
|
2009-08-30 10:24:09 +02:00
|
|
|
flags |= REG_ICASE;
|
|
|
|
if (Flags & Newline)
|
|
|
|
flags |= REG_NEWLINE;
|
Add backreference matching capabilities to Support/Regex, with
appropriate unit tests. This change in itself is not expected to
affect any functionality at this point, but it will serve as a
stepping stone to improve FileCheck's variable matching capabilities.
Luckily, our regex implementation already supports backreferences,
although a bit of hacking is required to enable it. It supports both
Basic Regular Expressions (BREs) and Extended Regular Expressions
(EREs), without supporting backrefs for EREs, following POSIX strictly
in this respect. And EREs is what we actually use (rightly). This is
contrary to many implementations (including the default on Linux) of
POSIX regexes, that do allow backrefs in EREs.
Adding backref support to our EREs is a very simple change in the
regcomp parsing code. I fail to think of significant cases where it
would clash with existing things, and can bring more versatility to
the regexes we write. There's always the danger of a backref in a
specially crafted regex causing exponential matching times, but since
we mainly use them for testing purposes I don't think it's a big
problem. [it can also be placed behind a flag specific to FileCheck,
if needed].
For more details, see:
* http://lists.cs.uiuc.edu/pipermail/llvmdev/2012-November/055840.html
* http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20121126/156878.html
llvm-svn: 168802
2012-11-28 20:00:02 +01:00
|
|
|
if (!(Flags & BasicRegex))
|
|
|
|
flags |= REG_EXTENDED;
|
|
|
|
error = llvm_regcomp(preg, regex.data(), flags|REG_PEND);
|
2009-08-30 10:24:09 +02:00
|
|
|
}
|
|
|
|
|
2020-07-02 15:52:24 +02:00
|
|
|
Regex::Regex(StringRef regex, unsigned Flags)
|
|
|
|
: Regex(regex, static_cast<RegexFlags>(Flags)) {}
|
|
|
|
|
2016-09-02 10:44:46 +02:00
|
|
|
Regex::Regex(Regex &®ex) {
|
|
|
|
preg = regex.preg;
|
|
|
|
error = regex.error;
|
|
|
|
regex.preg = nullptr;
|
|
|
|
regex.error = REG_BADPAT;
|
|
|
|
}
|
|
|
|
|
2009-09-26 23:27:04 +02:00
|
|
|
Regex::~Regex() {
|
2014-01-02 20:04:59 +01:00
|
|
|
if (preg) {
|
|
|
|
llvm_regfree(preg);
|
|
|
|
delete preg;
|
|
|
|
}
|
2009-09-26 23:27:04 +02:00
|
|
|
}
|
|
|
|
|
2019-09-24 16:42:36 +02:00
|
|
|
namespace {
|
2018-03-12 12:01:05 +01:00
|
|
|
|
2019-09-24 16:42:36 +02:00
|
|
|
/// Utility to convert a regex error code into a human-readable string.
|
|
|
|
void RegexErrorToString(int error, struct llvm_regex *preg,
|
|
|
|
std::string &Error) {
|
2014-04-07 06:17:22 +02:00
|
|
|
size_t len = llvm_regerror(error, preg, nullptr, 0);
|
2018-03-12 12:01:05 +01:00
|
|
|
|
2013-08-08 19:32:45 +02:00
|
|
|
Error.resize(len - 1);
|
2009-09-24 22:15:51 +02:00
|
|
|
llvm_regerror(error, preg, &Error[0], len);
|
2019-09-24 16:42:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
bool Regex::isValid(std::string &Error) const {
|
|
|
|
if (!error)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
RegexErrorToString(error, preg, Error);
|
2009-08-30 10:24:09 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-26 23:27:04 +02:00
|
|
|
/// getNumMatches - In a valid regex, return the number of parenthesized
|
|
|
|
/// matches it contains.
|
|
|
|
unsigned Regex::getNumMatches() const {
|
|
|
|
return preg->re_nsub;
|
2009-08-30 10:24:09 +02:00
|
|
|
}
|
|
|
|
|
2019-09-24 16:42:36 +02:00
|
|
|
bool Regex::match(StringRef String, SmallVectorImpl<StringRef> *Matches,
|
|
|
|
std::string *Error) const {
|
|
|
|
// Reset error, if given.
|
|
|
|
if (Error && !Error->empty())
|
|
|
|
*Error = "";
|
|
|
|
|
|
|
|
// Check if the regex itself didn't successfully compile.
|
|
|
|
if (Error ? !isValid(*Error) : !isValid())
|
2016-09-02 10:44:46 +02:00
|
|
|
return false;
|
|
|
|
|
2009-08-30 10:24:09 +02:00
|
|
|
unsigned nmatch = Matches ? preg->re_nsub+1 : 0;
|
|
|
|
|
|
|
|
// pmatch needs to have at least one element.
|
2009-09-24 23:47:32 +02:00
|
|
|
SmallVector<llvm_regmatch_t, 8> pm;
|
2009-08-30 10:24:09 +02:00
|
|
|
pm.resize(nmatch > 0 ? nmatch : 1);
|
|
|
|
pm[0].rm_so = 0;
|
|
|
|
pm[0].rm_eo = String.size();
|
|
|
|
|
|
|
|
int rc = llvm_regexec(preg, String.data(), nmatch, pm.data(), REG_STARTEND);
|
|
|
|
|
2019-09-24 16:42:36 +02:00
|
|
|
// Failure to match is not an error, it's just a normal return value.
|
|
|
|
// Any other error code is considered abnormal, and is logged in the Error.
|
2009-08-30 10:24:09 +02:00
|
|
|
if (rc == REG_NOMATCH)
|
|
|
|
return false;
|
|
|
|
if (rc != 0) {
|
2019-09-24 16:42:36 +02:00
|
|
|
if (Error)
|
|
|
|
RegexErrorToString(error, preg, *Error);
|
2009-08-30 10:24:09 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// There was a match.
|
|
|
|
|
|
|
|
if (Matches) { // match position requested
|
2009-09-26 23:27:04 +02:00
|
|
|
Matches->clear();
|
2018-03-12 12:01:05 +01:00
|
|
|
|
2009-09-24 22:15:51 +02:00
|
|
|
for (unsigned i = 0; i != nmatch; ++i) {
|
2009-08-30 10:24:09 +02:00
|
|
|
if (pm[i].rm_so == -1) {
|
|
|
|
// this group didn't match
|
|
|
|
Matches->push_back(StringRef());
|
|
|
|
continue;
|
|
|
|
}
|
2011-04-09 08:29:24 +02:00
|
|
|
assert(pm[i].rm_eo >= pm[i].rm_so);
|
2009-08-30 10:24:09 +02:00
|
|
|
Matches->push_back(StringRef(String.data()+pm[i].rm_so,
|
|
|
|
pm[i].rm_eo-pm[i].rm_so));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2010-02-17 21:08:42 +01:00
|
|
|
|
|
|
|
std::string Regex::sub(StringRef Repl, StringRef String,
|
2019-09-24 16:42:36 +02:00
|
|
|
std::string *Error) const {
|
2010-02-17 21:08:42 +01:00
|
|
|
SmallVector<StringRef, 8> Matches;
|
|
|
|
|
|
|
|
// Return the input if there was no match.
|
2019-09-24 16:42:36 +02:00
|
|
|
if (!match(String, &Matches, Error))
|
2020-01-28 20:23:46 +01:00
|
|
|
return std::string(String);
|
2010-02-17 21:08:42 +01:00
|
|
|
|
|
|
|
// Otherwise splice in the replacement string, starting with the prefix before
|
|
|
|
// the match.
|
|
|
|
std::string Res(String.begin(), Matches[0].begin());
|
|
|
|
|
|
|
|
// Then the replacement string, honoring possible substitutions.
|
|
|
|
while (!Repl.empty()) {
|
|
|
|
// Skip to the next escape.
|
|
|
|
std::pair<StringRef, StringRef> Split = Repl.split('\\');
|
|
|
|
|
|
|
|
// Add the skipped substring.
|
|
|
|
Res += Split.first;
|
|
|
|
|
|
|
|
// Check for terminimation and trailing backslash.
|
|
|
|
if (Split.second.empty()) {
|
|
|
|
if (Repl.size() != Split.first.size() &&
|
|
|
|
Error && Error->empty())
|
|
|
|
*Error = "replacement string contained trailing backslash";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise update the replacement string and interpret escapes.
|
|
|
|
Repl = Split.second;
|
|
|
|
|
|
|
|
// FIXME: We should have a StringExtras function for mapping C99 escapes.
|
|
|
|
switch (Repl[0]) {
|
|
|
|
// Treat all unrecognized characters as self-quoting.
|
|
|
|
default:
|
|
|
|
Res += Repl[0];
|
|
|
|
Repl = Repl.substr(1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Single character escapes.
|
|
|
|
case 't':
|
|
|
|
Res += '\t';
|
|
|
|
Repl = Repl.substr(1);
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
Res += '\n';
|
|
|
|
Repl = Repl.substr(1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Decimal escapes are backreferences.
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9': {
|
|
|
|
// Extract the backreference number.
|
|
|
|
StringRef Ref = Repl.slice(0, Repl.find_first_not_of("0123456789"));
|
|
|
|
Repl = Repl.substr(Ref.size());
|
|
|
|
|
|
|
|
unsigned RefValue;
|
|
|
|
if (!Ref.getAsInteger(10, RefValue) &&
|
|
|
|
RefValue < Matches.size())
|
|
|
|
Res += Matches[RefValue];
|
|
|
|
else if (Error && Error->empty())
|
2015-03-30 17:42:36 +02:00
|
|
|
*Error = ("invalid backreference string '" + Twine(Ref) + "'").str();
|
2010-02-17 21:08:42 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// And finally the suffix.
|
|
|
|
Res += StringRef(Matches[0].end(), String.end() - Matches[0].end());
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
2013-08-05 19:47:59 +02:00
|
|
|
|
2013-12-12 03:51:58 +01:00
|
|
|
// These are the special characters matched in functions like "p_ere_exp".
|
|
|
|
static const char RegexMetachars[] = "()^$|*+?.[]\\{}";
|
|
|
|
|
2013-08-05 19:47:59 +02:00
|
|
|
bool Regex::isLiteralERE(StringRef Str) {
|
|
|
|
// Check for regex metacharacters. This list was derived from our regex
|
|
|
|
// implementation in regcomp.c and double checked against the POSIX extended
|
|
|
|
// regular expression specification.
|
2013-12-12 03:51:58 +01:00
|
|
|
return Str.find_first_of(RegexMetachars) == StringRef::npos;
|
2013-08-05 19:47:59 +02:00
|
|
|
}
|
2013-12-12 01:06:41 +01:00
|
|
|
|
|
|
|
std::string Regex::escape(StringRef String) {
|
|
|
|
std::string RegexStr;
|
|
|
|
for (unsigned i = 0, e = String.size(); i != e; ++i) {
|
2013-12-12 03:51:58 +01:00
|
|
|
if (strchr(RegexMetachars, String[i]))
|
2013-12-12 01:06:41 +01:00
|
|
|
RegexStr += '\\';
|
2013-12-12 03:51:58 +01:00
|
|
|
RegexStr += String[i];
|
2013-12-12 01:06:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return RegexStr;
|
|
|
|
}
|