[PATCH] D27318: Support escaping in TrigramIndex.

Ivan Krasin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 2 11:00:21 PST 2016


krasin updated this revision to Diff 80103.
krasin added a comment.

Fix handling escaped symbols: they are all self-escaped in LLVM.


https://reviews.llvm.org/D27318

Files:
  lib/Support/TrigramIndex.cpp
  unittests/Support/SpecialCaseListTest.cpp
  unittests/Support/TrigramIndexTest.cpp


Index: unittests/Support/TrigramIndexTest.cpp
===================================================================
--- unittests/Support/TrigramIndexTest.cpp
+++ unittests/Support/TrigramIndexTest.cpp
@@ -94,10 +94,17 @@
   EXPECT_TRUE(TI->isDefeated());
 }
 
-TEST_F(TrigramIndexTest, SpecialSymbol) {
+TEST_F(TrigramIndexTest, EscapedSymbols) {
   std::unique_ptr<TrigramIndex> TI =
-      makeTrigramIndex({"*c\\+\\+*"});
-  EXPECT_TRUE(TI->isDefeated());
+      makeTrigramIndex({"*c\\+\\+*", "*hello\\\\world*", "a\\tb"});
+  EXPECT_FALSE(TI->isDefeated());
+  EXPECT_FALSE(TI->isDefinitelyOut("c++"));
+  EXPECT_TRUE(TI->isDefinitelyOut("c\\+\\+"));
+  EXPECT_FALSE(TI->isDefinitelyOut("hello\\world"));
+  EXPECT_TRUE(TI->isDefinitelyOut("hello\\\\world"));
+  EXPECT_FALSE(TI->isDefinitelyOut("atb"));
+  EXPECT_TRUE(TI->isDefinitelyOut("a\\tb"));
+  EXPECT_TRUE(TI->isDefinitelyOut("a\tb"));
 }
 
 TEST_F(TrigramIndexTest, Sequence) {
Index: unittests/Support/SpecialCaseListTest.cpp
===================================================================
--- unittests/Support/SpecialCaseListTest.cpp
+++ unittests/Support/SpecialCaseListTest.cpp
@@ -178,4 +178,15 @@
   EXPECT_TRUE(SCL->inSection("fun", "aaaabbbaaa"));
 }
 
+TEST_F(SpecialCaseListTest, EscapedSymbols) {
+  std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:*c\\+\\+abi*\n"
+                                                             "src:*hello\\\\world*\n");
+  EXPECT_TRUE(SCL->inSection("src", "dir/c++abi"));
+  EXPECT_FALSE(SCL->inSection("src", "dir/c\\+\\+abi"));
+  EXPECT_FALSE(SCL->inSection("src", "c\\+\\+abi"));
+  EXPECT_TRUE(SCL->inSection("src", "C:\\hello\\world"));
+  EXPECT_TRUE(SCL->inSection("src", "hello\\world"));
+  EXPECT_FALSE(SCL->inSection("src", "hello\\\\world"));
+}
+
 }
Index: lib/Support/TrigramIndex.cpp
===================================================================
--- lib/Support/TrigramIndex.cpp
+++ lib/Support/TrigramIndex.cpp
@@ -26,28 +26,37 @@
 
 static const char RegexAdvancedMetachars[] = "()^$|+?[]\\{}";
 
-static bool isSimpleWildcard(StringRef Str) {
-  // Check for regex metacharacters other than '*' and '.'.
-  return Str.find_first_of(RegexAdvancedMetachars) == StringRef::npos;
+static bool isAdvancedMetachar(unsigned Char) {
+  return strchr(RegexAdvancedMetachars, Char) != NULL;
 }
 
 void TrigramIndex::insert(std::string Regex) {
   if (Defeated) return;
-  if (!isSimpleWildcard(Regex)) {
-    Defeated = true;
-    return;
-  }
-
   std::set<unsigned> Was;
   unsigned Cnt = 0;
   unsigned Tri = 0;
   unsigned Len = 0;
+  bool Escaped = false;
   for (unsigned Char : Regex) {
-    if (Char == '.' || Char == '*') {
-      Tri = 0;
-      Len = 0;
-      continue;
+    if (!Escaped) {
+      // Regular expressions allow escaping symbols by preceding it with '\'.
+      if (Char == '\\') {
+        Escaped = true;
+        continue;
+      }
+      if (isAdvancedMetachar(Char)) {
+        // This is a more complicated regex than we can handle here.
+        Defeated = true;
+        return;
+      }
+      if (Char == '.' || Char == '*') {
+        Tri = 0;
+        Len = 0;
+        continue;
+      }
     }
+    // We have already handled escaping and can reset the flag.
+    Escaped = false;
     Tri = ((Tri << 8) + Char) & 0xFFFFFF;
     Len++;
     if (Len < 3)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D27318.80103.patch
Type: text/x-patch
Size: 3341 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161202/80b3c508/attachment.bin>


More information about the llvm-commits mailing list