Coverage Report

Created: 2026-06-08 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/keystone/llvm/lib/Support/Regex.cpp
Line
Count
Source
1
//===-- Regex.cpp - Regular Expression matcher implementation -------------===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// This file implements a POSIX regular expression matcher.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "llvm/Support/Regex.h"
15
#include "regex_impl.h"
16
#include "llvm/ADT/SmallVector.h"
17
#include "llvm/ADT/StringRef.h"
18
#include "llvm/ADT/Twine.h"
19
#include <string>
20
using namespace llvm_ks;
21
22
1.13k
Regex::Regex(StringRef regex, unsigned Flags) {
23
1.13k
  unsigned flags = 0;
24
1.13k
  preg = new llvm_regex();
25
1.13k
  preg->re_endp = regex.end();
26
1.13k
  if (Flags & IgnoreCase) 
27
0
    flags |= REG_ICASE;
28
1.13k
  if (Flags & Newline)
29
0
    flags |= REG_NEWLINE;
30
1.13k
  if (!(Flags & BasicRegex))
31
1.13k
    flags |= REG_EXTENDED;
32
1.13k
  error = llvm_regcomp(preg, regex.data(), flags|REG_PEND);
33
1.13k
}
34
35
1.13k
Regex::~Regex() {
36
1.13k
  if (preg) {
37
1.13k
    llvm_regfree(preg);
38
1.13k
    delete preg;
39
1.13k
  }
40
1.13k
}
41
42
0
bool Regex::isValid(std::string &Error) {
43
0
  if (!error)
44
0
    return true;
45
  
46
0
  size_t len = llvm_regerror(error, preg, nullptr, 0);
47
  
48
0
  Error.resize(len - 1);
49
0
  llvm_regerror(error, preg, &Error[0], len);
50
0
  return false;
51
0
}
52
53
/// getNumMatches - In a valid regex, return the number of parenthesized
54
/// matches it contains.
55
0
unsigned Regex::getNumMatches() const {
56
0
  return preg->re_nsub;
57
0
}
58
59
1.13k
bool Regex::match(StringRef String, SmallVectorImpl<StringRef> *Matches){
60
1.13k
  unsigned nmatch = Matches ? preg->re_nsub+1 : 0;
61
62
  // pmatch needs to have at least one element.
63
1.13k
  SmallVector<llvm_regmatch_t, 8> pm;
64
1.13k
  pm.resize(nmatch > 0 ? nmatch : 1);
65
1.13k
  pm[0].rm_so = 0;
66
1.13k
  pm[0].rm_eo = String.size();
67
68
1.13k
  int rc = llvm_regexec(preg, String.data(), nmatch, pm.data(), REG_STARTEND);
69
70
1.13k
  if (rc == REG_NOMATCH)
71
1.13k
    return false;
72
0
  if (rc != 0) {
73
    // regexec can fail due to invalid pattern or running out of memory.
74
0
    error = rc;
75
0
    return false;
76
0
  }
77
78
  // There was a match.
79
80
0
  if (Matches) { // match position requested
81
0
    Matches->clear();
82
    
83
0
    for (unsigned i = 0; i != nmatch; ++i) {
84
0
      if (pm[i].rm_so == -1) {
85
        // this group didn't match
86
0
        Matches->push_back(StringRef());
87
0
        continue;
88
0
      }
89
0
      assert(pm[i].rm_eo >= pm[i].rm_so);
90
0
      Matches->push_back(StringRef(String.data()+pm[i].rm_so,
91
0
                                   pm[i].rm_eo-pm[i].rm_so));
92
0
    }
93
0
  }
94
95
0
  return true;
96
0
}
97
98
std::string Regex::sub(StringRef Repl, StringRef String,
99
0
                       std::string *Error) {
100
0
  SmallVector<StringRef, 8> Matches;
101
102
  // Reset error, if given.
103
0
  if (Error && !Error->empty()) *Error = "";
104
105
  // Return the input if there was no match.
106
0
  if (!match(String, &Matches))
107
0
    return String;
108
109
  // Otherwise splice in the replacement string, starting with the prefix before
110
  // the match.
111
0
  std::string Res(String.begin(), Matches[0].begin());
112
113
  // Then the replacement string, honoring possible substitutions.
114
0
  while (!Repl.empty()) {
115
    // Skip to the next escape.
116
0
    std::pair<StringRef, StringRef> Split = Repl.split('\\');
117
118
    // Add the skipped substring.
119
0
    Res += Split.first;
120
121
    // Check for terminimation and trailing backslash.
122
0
    if (Split.second.empty()) {
123
0
      if (Repl.size() != Split.first.size() &&
124
0
          Error && Error->empty())
125
0
        *Error = "replacement string contained trailing backslash";
126
0
      break;
127
0
    }
128
129
    // Otherwise update the replacement string and interpret escapes.
130
0
    Repl = Split.second;
131
132
    // FIXME: We should have a StringExtras function for mapping C99 escapes.
133
0
    switch (Repl[0]) {
134
      // Treat all unrecognized characters as self-quoting.
135
0
    default:
136
0
      Res += Repl[0];
137
0
      Repl = Repl.substr(1);
138
0
      break;
139
140
      // Single character escapes.
141
0
    case 't':
142
0
      Res += '\t';
143
0
      Repl = Repl.substr(1);
144
0
      break;
145
0
    case 'n':
146
0
      Res += '\n';
147
0
      Repl = Repl.substr(1);
148
0
      break;
149
150
      // Decimal escapes are backreferences.
151
0
    case '0': case '1': case '2': case '3': case '4':
152
0
    case '5': case '6': case '7': case '8': case '9': {
153
      // Extract the backreference number.
154
0
      StringRef Ref = Repl.slice(0, Repl.find_first_not_of("0123456789"));
155
0
      Repl = Repl.substr(Ref.size());
156
157
0
      unsigned RefValue;
158
0
      if (!Ref.getAsInteger(10, RefValue) &&
159
0
          RefValue < Matches.size())
160
0
        Res += Matches[RefValue];
161
0
      else if (Error && Error->empty())
162
0
        *Error = ("invalid backreference string '" + Twine(Ref) + "'").str();
163
0
      break;
164
0
    }
165
0
    }
166
0
  }
167
168
  // And finally the suffix.
169
0
  Res += StringRef(Matches[0].end(), String.end() - Matches[0].end());
170
171
0
  return Res;
172
0
}
173
174
// These are the special characters matched in functions like "p_ere_exp".
175
static const char RegexMetachars[] = "()^$|*+?.[]\\{}";
176
177
0
bool Regex::isLiteralERE(StringRef Str) {
178
  // Check for regex metacharacters.  This list was derived from our regex
179
  // implementation in regcomp.c and double checked against the POSIX extended
180
  // regular expression specification.
181
0
  return Str.find_first_of(RegexMetachars) == StringRef::npos;
182
0
}
183
184
0
std::string Regex::escape(StringRef String) {
185
0
  std::string RegexStr;
186
0
  for (unsigned i = 0, e = String.size(); i != e; ++i) {
187
0
    if (strchr(RegexMetachars, String[i]))
188
0
      RegexStr += '\\';
189
0
    RegexStr += String[i];
190
0
  }
191
192
0
  return RegexStr;
193
0
}