Introduce Regex::isLiteralERE function.

This will be used to implement an optimisation for literal entries
in special case lists.

Differential Revision: http://llvm-reviews.chandlerc.com/D1278

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@187731 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/llvm/Support/Regex.h b/include/llvm/Support/Regex.h
index 82df2c6..3d071be 100644
--- a/include/llvm/Support/Regex.h
+++ b/include/llvm/Support/Regex.h
@@ -77,6 +77,10 @@
     /// string.
     std::string sub(StringRef Repl, StringRef String, std::string *Error = 0);
 
+    /// \brief If this function returns true, ^Str$ is an extended regular
+    /// expression that matches Str and only Str.
+    static bool isLiteralERE(StringRef Str);
+
   private:
     struct llvm_regex *preg;
     int error;
diff --git a/lib/Support/Regex.cpp b/lib/Support/Regex.cpp
index efc8b90..dec967e 100644
--- a/lib/Support/Regex.cpp
+++ b/lib/Support/Regex.cpp
@@ -168,3 +168,10 @@
 
   return Res;
 }
+
+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.
+  return Str.find_first_of("()^$|*+?.[]\\{}") == StringRef::npos;
+}
diff --git a/unittests/Support/RegexTest.cpp b/unittests/Support/RegexTest.cpp
index 3577d10..02869b3 100644
--- a/unittests/Support/RegexTest.cpp
+++ b/unittests/Support/RegexTest.cpp
@@ -112,4 +112,19 @@
   EXPECT_EQ(Error, "invalid backreference string '100'");
 }
 
+TEST_F(RegexTest, IsLiteralERE) {
+  EXPECT_TRUE(Regex::isLiteralERE("abc"));
+  EXPECT_FALSE(Regex::isLiteralERE("a(bc)"));
+  EXPECT_FALSE(Regex::isLiteralERE("^abc"));
+  EXPECT_FALSE(Regex::isLiteralERE("abc$"));
+  EXPECT_FALSE(Regex::isLiteralERE("a|bc"));
+  EXPECT_FALSE(Regex::isLiteralERE("abc*"));
+  EXPECT_FALSE(Regex::isLiteralERE("abc+"));
+  EXPECT_FALSE(Regex::isLiteralERE("abc?"));
+  EXPECT_FALSE(Regex::isLiteralERE("abc."));
+  EXPECT_FALSE(Regex::isLiteralERE("a[bc]"));
+  EXPECT_FALSE(Regex::isLiteralERE("abc\\1"));
+  EXPECT_FALSE(Regex::isLiteralERE("abc{1,2}"));
+}
+
 }