Merge pull request #23829 from drodriguez/path-sanitizing-filecheck-yaml-compatibility

[utils][test] PathSanitizingFileCheck more compatible with Windows.
diff --git a/test/ClangImporter/pch-bridging-header-deps.swift b/test/ClangImporter/pch-bridging-header-deps.swift
index a0cde16..b7e9434 100644
--- a/test/ClangImporter/pch-bridging-header-deps.swift
+++ b/test/ClangImporter/pch-bridging-header-deps.swift
@@ -6,13 +6,13 @@
 // RUN: %target-swift-frontend -emit-pch -o %t.pch %/S/Inputs/chained-unit-test-bridging-header-to-pch.h
 // RUN: %target-swift-frontend -module-name test -c -emit-dependencies-path %t.d -emit-reference-dependencies-path %t.swiftdeps -primary-file %s -import-objc-header %t.pch
 // RUN: %FileCheck --check-prefix CHECK-DEPS %s < %t.d
-// RUN: %FileCheck --check-prefix CHECK-SWIFTDEPS %s < %t.swiftdeps
-// RUN: %FileCheck --check-prefix CHECK-SWIFTDEPS2 %s < %t.swiftdeps
+// RUN: %FileCheck --check-prefix CHECK-SWIFTDEPS --enable-yaml-compatibility %s < %t.swiftdeps
+// RUN: %FileCheck --check-prefix CHECK-SWIFTDEPS2 --enable-yaml-compatibility %s < %t.swiftdeps
 
 // RUN: %target-swift-frontend -module-name test -c -emit-dependencies-path %t.persistent.d -emit-reference-dependencies-path %t.persistent.swiftdeps -primary-file %s -import-objc-header %/S/Inputs/chained-unit-test-bridging-header-to-pch.h -pch-output-dir %t/pch
 // RUN: %FileCheck --check-prefix CHECK-DEPS %s < %t.persistent.d
-// RUN: %FileCheck --check-prefix CHECK-SWIFTDEPS %s < %t.persistent.swiftdeps
-// RUN: %FileCheck --check-prefix CHECK-SWIFTDEPS2 %s < %t.persistent.swiftdeps
+// RUN: %FileCheck --check-prefix CHECK-SWIFTDEPS --enable-yaml-compatibility %s < %t.persistent.swiftdeps
+// RUN: %FileCheck --check-prefix CHECK-SWIFTDEPS2 --enable-yaml-compatibility %s < %t.persistent.swiftdeps
 
 print(app_function(1))
 
diff --git a/test/lit.cfg b/test/lit.cfg
index 275ef5f..945a8b6 100644
--- a/test/lit.cfg
+++ b/test/lit.cfg
@@ -1450,12 +1450,13 @@
 config.substitutions.append(('%llvm-cov', config.llvm_cov))
 
 config.substitutions.append(('%FileCheck',
-                             '%r %r --sanitize BUILD_DIR=%r --sanitize SOURCE_DIR=%r --use-filecheck %r' % (
+                             '%r %r --sanitize BUILD_DIR=%r --sanitize SOURCE_DIR=%r --use-filecheck %r %s' % (
         sys.executable,
         config.PathSanitizingFileCheck,
         swift_obj_root,
         config.swift_src_root,
-        config.filecheck)))
+        config.filecheck,
+        '--enable-windows-compatibility' if kIsWindows else '')))
 config.substitutions.append(('%raw-FileCheck', pipes.quote(config.filecheck)))
 
 # If static stdlib is present, enable static stdlib tests
diff --git a/utils/PathSanitizingFileCheck b/utils/PathSanitizingFileCheck
index d7aecb1..f3b2a90 100755
--- a/utils/PathSanitizingFileCheck
+++ b/utils/PathSanitizingFileCheck
@@ -43,12 +43,36 @@
         dest="file_check_path",
         default="FileCheck")
 
+    parser.add_argument(
+        "--enable-windows-compatibility",
+        help="Enable Windows path compatibility, which checks against both "
+             "forward slashes and backward slashes.",
+        action="store_true")
+
+    parser.add_argument(
+        "--enable-yaml-compatibility",
+        help="Enable YAML path compatibility. Since YAML double escapes "
+             "backward slashes, we need to check for them escaped. Only "
+             "available if Windows compatibility is enabled.",
+        action="store_true")
+
     args, unknown_args = parser.parse_known_args()
 
+    if args.enable_windows_compatibility:
+        if args.enable_yaml_compatibility:
+            slashes_re = r'(/|\\\\|\\\\\\\\)'
+        else:
+            slashes_re = r'(/|\\\\)'
+    else:
+        slashes_re = r'/'
+
     stdin = sys.stdin.read()
     for s in args.sanitize_strings:
         replacement, pattern = s.split('=', 1)
-        stdin = re.sub(re.sub(r'/', r'[/\\\\]', pattern), replacement, stdin)
+        # We are replacing the Unix path separators in the paths passed as
+        # arguments with a broader pattern to also allow forward slashes and
+        # double escaped slashes in the result that we are checking. Sigh.
+        stdin = re.sub(re.sub(r'/', slashes_re, pattern), replacement, stdin)
 
     p = subprocess.Popen(
         [args.file_check_path] + unknown_args, stdin=subprocess.PIPE)