Does this version look better?  I am not sure if this the right place
to put the new helper, so let me know if there is a better spot for it.

        gcc/testsuite/lib/
        * prune.exp (prune_file_path): Sanitize path used in regex.
        (escape_regex_chars): New.

Signed-off-by: Zachary T Welch <zwe...@codesourcery.com>
---
 gcc/testsuite/lib/prune.exp | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/gcc/testsuite/lib/prune.exp b/gcc/testsuite/lib/prune.exp
index 8e4c203..fd3c4ea 100644
--- a/gcc/testsuite/lib/prune.exp
+++ b/gcc/testsuite/lib/prune.exp
@@ -73,12 +73,33 @@ proc prune_gcc_output { text } {
     return $text
 }
 
+# escape metacharacters in literal string, so it can be used in regex
+
+proc escape_regex_chars { line } {
+    return [string map {"^" "\\^"
+                       "$" "\\$"
+                       "(" "\\("
+                       ")" "\\)"
+                       "[" "\\["
+                       "]" "\\]"
+                       "{" "\\{"
+                       "}" "\\}"
+                       "." "\\."
+                       "\\" "\\\\"
+                       "?" "\\?"
+                       "+" "\\+"
+                       "*" "\\*"
+                       "|" "\\|"} $line]
+}
+
 proc prune_file_path { text } {
     global srcdir
 
+    set safedir [escape_regex_chars $srcdir]
+    regsub -all "$safedir\/"  $text "" text
+
     # Truncate absolute file path into relative path.
-    set topdir "[file dirname [file dirname [file dirname $srcdir]]]"
-    regsub -all "$srcdir\/" $text "" text
+    set topdir "[file dirname [file dirname [file dirname $safedir]]]"
     regsub -all "$topdir\/" $text "" text
 
     return $text
-- 
1.8.1.1

Reply via email to