[PATCH] D24102: [LLVM/Support] - Disallow match() method of llvm::Regex to change object state.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 31 14:39:58 PDT 2016

grimar created this revision.
grimar added reviewers: ruiu, rafael, davide, dblaikie.
grimar added subscribers: llvm-commits, grimar.

match() method previously was able to change the error filed of a object.
That looked as excessive because pattern is compiled in constructor,
so to the moment of match() call should be already known if Regex state is valid or not.

match() returns a bool flag what should be enough to know was there some fail during the call or not.

This change allows to make match() to be const what is useful if we have constant reference to Regex
and want to call match().



Index: lib/Support/Regex.cpp
--- lib/Support/Regex.cpp
+++ lib/Support/Regex.cpp
@@ -56,7 +56,7 @@
   return preg->re_nsub;
-bool Regex::match(StringRef String, SmallVectorImpl<StringRef> *Matches){
+bool Regex::match(StringRef String, SmallVectorImpl<StringRef> *Matches) const {
   unsigned nmatch = Matches ? preg->re_nsub+1 : 0;
   // pmatch needs to have at least one element.
@@ -67,13 +67,8 @@
   int rc = llvm_regexec(preg, String.data(), nmatch, pm.data(), REG_STARTEND);
-  if (rc == REG_NOMATCH)
+  if (rc == REG_NOMATCH || !rc)
     return false;
-  if (rc != 0) {
-    // regexec can fail due to invalid pattern or running out of memory.
-    error = rc;
-    return false;
-  }
   // There was a match.
Index: include/llvm/Support/Regex.h
--- include/llvm/Support/Regex.h
+++ include/llvm/Support/Regex.h
@@ -74,7 +74,7 @@
     /// the first group is always the entire pattern.
     /// This returns true on a successful match.
-    bool match(StringRef String, SmallVectorImpl<StringRef> *Matches = nullptr);
+    bool match(StringRef String, SmallVectorImpl<StringRef> *Matches = nullptr) const;
     /// sub - Return the result of replacing the first match of the regex in
     /// \p String with the \p Repl string. Backreferences like "\0" in the

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24102.69904.patch
Type: text/x-patch
Size: 1458 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160831/6c65fb2a/attachment.bin>

More information about the llvm-commits mailing list