[PATCH] D68054: Regex: Add static convenience functions for "match" and "sub"

Nicolas Guillemot via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 25 14:41:23 PDT 2019


nlguillemot created this revision.
nlguillemot added a reviewer: thopre.
Herald added subscribers: llvm-commits, kristina.
Herald added a project: LLVM.

There are many cases where a Regex object used only once: It is created,
used in a single call to match() or sub(), then thrown away. This was
done in various places in LLVM using the following idiom:

  Regex(Pattern).match(String)

The problem with this idiom is that if the compilation of the Regex fails,
then match() gets called with an invalid regex pattern. This scenario was
being handled by checking inside match() if the pattern compiled successfully.
This gives match() the double-duty of handling match errors and handling regex
compilation errors. To improve the separation of concerns with respect to this
double-duty, and to be more strict about the usage of the Regex API, we decided
to replace the idiom above with the following alternative:

  Regex::match(Pattern, String)

This static member function version of the idiom behaves like syntactical sugar
for the idiom at the beginning, but it checks that the regex compiled without
error before making the call to match(). This relieves match() from having to
handle the case where the regex pattern failed to compile, and allows us to
define match() more strictly, by requiring a successfully compiled pattern as
a precondition.

A similar convenience function was added to sub(). The constructor of Regex
was also extended to be able to return an error without requiring a subsequent
call to isValid(), for the sake of convenience and code reuse.

Uses of the previous idiom in the codebase were updated to the new style.


Repository:
  rL LLVM

https://reviews.llvm.org/D68054

Files:
  include/llvm/Support/Regex.h
  lib/Support/FileCheck.cpp
  lib/Support/Regex.cpp
  lib/Transforms/Utils/SymbolRewriter.cpp
  tools/llvm-cov/CoverageFilters.cpp
  tools/sancov/sancov.cpp
  unittests/Support/RegexTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D68054.221833.patch
Type: text/x-patch
Size: 10351 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190925/40a466d2/attachment-0001.bin>


More information about the llvm-commits mailing list