llvm.org GIT mirror llvm / 76eb77d
Expose FileCheck's AddFixedStringToRegEx as Regex::escape Both FileCheck and clang's -verify need to escape strings for regexes, so let's expose this as a utility in the Regex class. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@197096 91177308-0d34-0410-b5e6-96231b3b80d8 Hans Wennborg 6 years ago
3 changed file(s) with 35 addition(s) and 31 deletion(s). Raw diff Collapse all Expand all
8080 /// expression that matches Str and only Str.
8181 static bool isLiteralERE(StringRef Str);
8282
83 /// \brief Turn String into a regex by escaping its special characters.
84 static std::string escape(StringRef String);
85
8386 private:
8487 struct llvm_regex *preg;
8588 int error;
174174 // regular expression specification.
175175 return Str.find_first_of("()^$|*+?.[]\\{}") == StringRef::npos;
176176 }
177
178 std::string Regex::escape(StringRef String) {
179 std::string RegexStr;
180
181 for (unsigned i = 0, e = String.size(); i != e; ++i) {
182 switch (String[i]) {
183 // These are the special characters matched in "p_ere_exp".
184 case '(':
185 case ')':
186 case '^':
187 case '$':
188 case '|':
189 case '*':
190 case '+':
191 case '?':
192 case '.':
193 case '[':
194 case '\\':
195 case '{':
196 RegexStr += '\\';
197 // FALL THROUGH.
198 default:
199 RegexStr += String[i];
200 break;
201 }
202 }
203
204 return RegexStr;
205 }
135135 Check::CheckType getCheckTy() const { return CheckTy; }
136136
137137 private:
138 static void AddFixedStringToRegEx(StringRef FixedStr, std::string &TheStr);
139138 bool AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM);
140139 void AddBackrefToRegEx(unsigned BackrefNum);
141140
313312 // Find the end, which is the start of the next regex.
314313 size_t FixedMatchEnd = PatternStr.find("{{");
315314 FixedMatchEnd = std::min(FixedMatchEnd, PatternStr.find("[["));
316 AddFixedStringToRegEx(PatternStr.substr(0, FixedMatchEnd), RegExStr);
315 RegExStr += Regex::escape(PatternStr.substr(0, FixedMatchEnd));
317316 PatternStr = PatternStr.substr(FixedMatchEnd);
318317 }
319318
320319 return false;
321 }
322
323 void Pattern::AddFixedStringToRegEx(StringRef FixedStr, std::string &TheStr) {
324 // Add the characters from FixedStr to the regex, escaping as needed. This
325 // avoids "leaning toothpicks" in common patterns.
326 for (unsigned i = 0, e = FixedStr.size(); i != e; ++i) {
327 switch (FixedStr[i]) {
328 // These are the special characters matched in "p_ere_exp".
329 case '(':
330 case ')':
331 case '^':
332 case '$':
333 case '|':
334 case '*':
335 case '+':
336 case '?':
337 case '.':
338 case '[':
339 case '\\':
340 case '{':
341 TheStr += '\\';
342 // FALL THROUGH.
343 default:
344 TheStr += FixedStr[i];
345 break;
346 }
347 }
348320 }
349321
350322 bool Pattern::AddRegExToRegEx(StringRef RS, unsigned &CurParen,
427399 if (it == VariableTable.end())
428400 return StringRef::npos;
429401
430 // Look up the value and escape it so that we can plop it into the regex.
431 AddFixedStringToRegEx(it->second, Value);
402 // Look up the value and escape it so that we can put it into the regex.
403 Value += Regex::escape(it->second);
432404 }
433405
434406 // Plop it into the regex at the adjusted offset.