Skip to content

Commit 7284caf

Browse files
committed
test/lit.cfg: add a new substitution '%FileCheck'
'%FileCheck' removes absolute paths of the source and build directory from the input. Overwhelming majority of tests don't intend to match these paths. Also add a substitution '%raw-FileCheck' that does not sanitize the input.
1 parent ca7beda commit 7284caf

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

docs/Testing.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,16 @@ Other substitutions:
358358
* ``%{python}``: run the same Python interpreter that's being used to run the
359359
current ``lit`` test.
360360

361+
* ``%FileCheck``: like the LLVM ``FileCheck`` utility, but occurrences of full
362+
paths to the source and build directories in the input text are replaced with
363+
path-independent constants.
364+
365+
* ``%raw-FileCheck``: the LLVM ``FileCheck`` utility.
366+
361367
When writing a test where output (or IR, SIL) depends on the bitness of the
362368
target CPU, use this pattern::
363369

364-
// RUN: %target-swift-frontend ... | FileCheck --check-prefix=CHECK --check-prefix=CHECK-%target-ptrsize %s
370+
// RUN: %target-swift-frontend ... | %FileCheck --check-prefix=CHECK --check-prefix=CHECK-%target-ptrsize %s
365371

366372
// CHECK: common line
367373
// CHECK-32: only for 32-bit
@@ -377,7 +383,7 @@ target CPU, use this pattern::
377383
When writing a test where output (or IR, SIL) depends on the target CPU itself,
378384
use this pattern::
379385

380-
// RUN: %target-swift-frontend ... | FileCheck --check-prefix=CHECK --check-prefix=CHECK-%target-cpu %s
386+
// RUN: %target-swift-frontend ... | %FileCheck --check-prefix=CHECK --check-prefix=CHECK-%target-cpu %s
381387

382388
// CHECK: common line
383389
// CHECK-i386: only for i386

test/lit.cfg

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import sys
2626
import tempfile
2727
import socket
2828
import glob
29+
import pipes
2930

3031
import lit
3132
import lit.formats
@@ -289,6 +290,7 @@ config.swift_utils = os.path.join(config.swift_src_root, 'utils')
289290
config.line_directive = os.path.join(config.swift_utils, 'line-directive')
290291
config.gyb = os.path.join(config.swift_utils, 'gyb')
291292
config.rth = os.path.join(config.swift_utils, 'rth') # Resilience test helper
293+
config.PathSanitizingFileCheck = os.path.join(config.swift_utils, 'PathSanitizingFileCheck')
292294
config.swift_lib_dir = os.path.join(os.path.dirname(os.path.dirname(config.swift)), 'lib')
293295

294296
# Find the resource directory. Assume it's near the swift compiler if not set.
@@ -413,6 +415,7 @@ disallow('sil-extract')
413415
disallow('lldb-moduleimport-test')
414416
disallow('swift-ide-test')
415417
disallow('clang')
418+
disallow('FileCheck')
416419

417420
config.substitutions.insert(0,
418421
('%p',
@@ -998,6 +1001,14 @@ config.substitutions.append(('%target-resilience-test', config.target_resilience
9981001
config.substitutions.append(('%llvm-profdata', config.llvm_profdata))
9991002
config.substitutions.append(('%llvm-cov', config.llvm_cov))
10001003

1004+
config.substitutions.append(
1005+
('%FileCheck',
1006+
'%s --sanitize \'BUILD_DIR=%s\' --sanitize \'SOURCE_DIR=%s\'' %
1007+
(config.PathSanitizingFileCheck,
1008+
pipes.quote(swift_obj_root),
1009+
pipes.quote(config.swift_src_root))))
1010+
config.substitutions.append(('%raw-FileCheck', 'FileCheck'))
1011+
10011012
# If static stdlib is present, enable static stdlib tests
10021013
static_stdlib_path = os.path.join(os.path.join(config.swift_lib_dir,"swift_static"), config.target_sdk_name)
10031014
libswiftCore_path = os.path.join(static_stdlib_path,"libswiftCore.a")

utils/PathSanitizingFileCheck

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python
2+
# utils/PathSanitizingFileCheck -*- python -*-
3+
#
4+
# This source file is part of the Swift.org open source project
5+
#
6+
# Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
7+
# Licensed under Apache License v2.0 with Runtime Library Exception
8+
#
9+
# See http://swift.org/LICENSE.txt for license information
10+
# See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
11+
12+
from __future__ import print_function
13+
14+
import argparse
15+
import subprocess
16+
import sys
17+
18+
def main():
19+
parser = argparse.ArgumentParser(
20+
formatter_class=argparse.RawDescriptionHelpFormatter,
21+
description="""
22+
PathSanitizingFileCheck is a wrapper around LLVM's FileCheck. In addition
23+
to all FileCheck features, PathSanitizingFileCheck can replace given
24+
strings in the input with other strings. This feature is used to replace
25+
paths to the source and build directories with path-independent
26+
constants.""")
27+
28+
parser.add_argument(
29+
"--sanitize",
30+
help="replace the given string with another string",
31+
metavar="REPLACEMENT=SOURCE",
32+
action="append",
33+
dest="sanitize_strings",
34+
default=[])
35+
36+
args, unknown_args = parser.parse_known_args()
37+
38+
stdin = sys.stdin.read()
39+
for s in args.sanitize_strings:
40+
replacement, pattern = s.split('=', 1)
41+
stdin = stdin.replace(pattern, replacement)
42+
43+
p = subprocess.Popen(["FileCheck"] + unknown_args, stdin=subprocess.PIPE)
44+
stdout, stderr = p.communicate(stdin)
45+
if stdout is not None:
46+
print(stdout)
47+
if stderr is not None:
48+
print(stderr, file=sys.stderr)
49+
return p.wait()
50+
51+
52+
if __name__ == '__main__':
53+
exit(main())
54+

0 commit comments

Comments
 (0)