Skip to content

Commit d01ee0c

Browse files
committed
[BoundsSafety] Set the default bounds check mode for driver tests
Previously the `%clang` substitution was using the default bounds check mode rather than the default of the diretory (i.e. new checks for `test/BoundsSafety` and legacy checks for `BoundsSafety-legacy-checks`). This patch 1. Adds the relevant flag bounds check flag to the `%clang` substitution. 2. Adds a new `%clang_no_bounds_check_mode_specified` substitution. This is the same as `%clang` but it does not have the bounds check flag present (e.g. `-fbounds-safety-bringup-missing-checks=all`). This is needed for some test cases. Note some tests in `BoundsSafety/Frontend` needed fixing because they were trying to invoke cc1 using `%clang` instead of `%clang_cc1`. rdar://164123351
1 parent 7ac30b4 commit d01ee0c

File tree

8 files changed

+130
-26
lines changed

8 files changed

+130
-26
lines changed
Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,62 @@
1+
import lit
12
import os
23

4+
# clang/test uses external shell by default, but all tests in clang/test/BoundsSafety
5+
# support lit's internal shell, so use that instead
6+
config.test_format = lit.formats.ShTest(False)
7+
38
cc1_substituted = [ False ]
9+
clang_substituted = [ False ]
410
extra_flags = '-fno-bounds-safety-bringup-missing-checks=all'
511

6-
def patch_cc1(sub_tuple):
12+
def get_subst(subst_name):
13+
subst_re = r'%\b{}\b'.format(subst_name)
14+
for subst_name, value in config.substitutions:
15+
if subst_re not in subst_name:
16+
continue
17+
return value
18+
raise Exception(f'Could not find %{subst_name} substitution')
19+
20+
def patch_subst_impl(sub_tuple, subst_to_patch):
721
assert isinstance(sub_tuple, tuple)
822
assert len(sub_tuple) == 2
923
subst_name = sub_tuple[0]
1024

11-
# Match `%clang_cc1`. The `ToolSubst` class inside `lit` adds a bunch of
12-
# regexes around this substitution so we try to match them here to avoid
13-
# matching a substitution that has `%clang_cc1` as a substring. This is
14-
# fragile
15-
if r'%\bclang_cc1\b' not in subst_name:
16-
return sub_tuple
25+
# Match `%subst_name` (e.g. `%clang_cc1`). The `ToolSubst` class inside
26+
# `lit` adds a bunch of regexes around this substitution so we try to match
27+
# them here to avoid matching a substitution that has `%clang_cc1` as a
28+
# substring. This is fragile
29+
subst_re = r'%\b{}\b'.format(subst_to_patch)
30+
if subst_re not in subst_name:
31+
return (sub_tuple[0], sub_tuple[1], False)
1732
patched_sub = sub_tuple[1] + f' {extra_flags}'
18-
cc1_substituted[0] = True
19-
return (sub_tuple[0], patched_sub)
33+
return (sub_tuple[0], patched_sub, True)
34+
35+
def patch_cc1_subst(sub_tuple):
36+
subst_name, subst, patched = patch_subst_impl(sub_tuple, 'clang_cc1')
37+
if patched:
38+
cc1_substituted[0] = True
39+
return (subst_name, subst)
40+
41+
def patch_clang_subst(sub_tuple):
42+
subst_name, subst, patched = patch_subst_impl(sub_tuple, 'clang')
43+
if patched:
44+
clang_substituted[0] = True
45+
return (subst_name, subst)
46+
47+
48+
# Provide an un-patched substitution
49+
default_clang_subst = get_subst('clang')
50+
config.substitutions.append(
51+
(r'%\bclang_no_bounds_check_mode_specified\b', default_clang_subst)
52+
)
2053

21-
config.substitutions = list(map(patch_cc1, config.substitutions))
54+
config.substitutions = list(map(patch_cc1_subst, config.substitutions))
55+
config.substitutions = list(map(patch_clang_subst, config.substitutions))
2256

2357
dir_with_patched_sub = os.path.dirname(__file__)
24-
if not cc1_substituted[0]:
25-
lit_config.fatal(f'Failed to add `{extra_flags}` to %clang_cc1 invocations under {dir_with_patched_sub}')
26-
else:
27-
lit_config.note(f'Implicitly passing `{extra_flags}` to %clang_cc1 invocations under {dir_with_patched_sub}')
58+
for subst, patched in [('%clang_cc1', cc1_substituted[0]), ('%clang', clang_substituted[0])]:
59+
if not patched:
60+
lit_config.fatal(f'Failed to add `{extra_flags}` to {subst} invocations under {dir_with_patched_sub}')
61+
else:
62+
lit_config.note(f'Implicitly passing `{extra_flags}` to {subst} invocations under {dir_with_patched_sub}')

clang/test/BoundsSafety/Driver/bounds-safety-attributes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// RUN: %clang -c %s -### 2>&1 | not grep fexperimental-bounds-safety-attributes
66

7-
// RUN: %clang -fexperimental-bounds-safety-attributes -c %s -### 2>&1 | not grep fbounds-safety
7+
// RUN: %clang_no_bounds_check_mode_specified -fexperimental-bounds-safety-attributes -c %s -### 2>&1 | not grep fbounds-safety
88

99
// RUN: %clang -fexperimental-bounds-safety-attributes -c %s -### 2>&1 | FileCheck -check-prefixes ALL,T0 %s
1010
// T0: -fexperimental-bounds-safety-attributes

clang/test/BoundsSafety/Driver/driver.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// ALL-NOT: unknown argument
22

33
// Warning: careful to not grep some directory in the buid
4-
// RUN: %clang -c %s -### 2>&1 | not grep fbounds-safety
4+
// RUN: %clang_no_bounds_check_mode_specified -c %s -### 2>&1 | not grep fbounds-safety
55

66
// RUN: %clang -fbounds-safety -### %s 2>&1 | FileCheck -check-prefixes ALL,T0 %s
77
// T0: -fbounds-safety
88
// T0: -enable-constraint-elimination
99

10-
// RUN: %clang -fbounds-safety -fno-bounds-safety -c %s -### 2>&1 | not grep -e fbounds-safety -e enable-constraint-elimination
10+
// RUN: %clang_no_bounds_check_mode_specified -fbounds-safety -fno-bounds-safety -c %s -### 2>&1 | not grep -e fbounds-safety -e enable-constraint-elimination
1111

1212
// RUN: %clang -fno-bounds-safety -fbounds-safety -c %s -### 2>&1 | FileCheck -check-prefixes ALL,T4 %s
1313
// T4: -fbounds-safety

clang/test/BoundsSafety/Frontend/cmdl_opts.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
// Making sure the Frontend accepts the -fbounds-safety flags - it would otherwise produce error: unknown argument '...'
44

5-
// RUN: %clang -cc1 -fbounds-safety -fsyntax-only %s
5+
// RUN: %clang_cc1 -fbounds-safety -fsyntax-only %s
66

7-
// RUN: %clang -cc1 -fbounds-safety -fexperimental-bounds-safety-cxx -fsyntax-only %s
7+
// RUN: %clang_cc1 -fbounds-safety -fexperimental-bounds-safety-cxx -fsyntax-only %s
88

9-
// RUN: %clang -cc1 -fbounds-safety -fexperimental-bounds-safety-objc -fsyntax-only %s
9+
// RUN: %clang_cc1 -fbounds-safety -fexperimental-bounds-safety-objc -fsyntax-only %s
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11

2-
// RUN: %clang -cc1 -fbounds-safety -fexperimental-bounds-safety-objc -fsyntax-only %s
2+
// RUN: %clang_cc1 -fbounds-safety -fexperimental-bounds-safety-objc -fsyntax-only %s
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

22

3-
// RUN: %clang -cc1 -fexperimental-bounds-safety-objc -fsyntax-only %s 2>&1 | FileCheck %s
3+
// RUN: %clang_cc1 -fexperimental-bounds-safety-objc -fsyntax-only %s 2>&1 | FileCheck %s
44

55
// CHECK: warning: -fexperimental-bounds-safety-objc without -fbounds-safety is ignored
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11

2-
// RUN: not %clang -cc1 -fbounds-safety -x c++ %s 2>&1 | FileCheck %s
2+
// RUN: not %clang_cc1 -fbounds-safety -x c++ %s 2>&1 | FileCheck %s
33

4-
// RUN: not %clang -cc1 -fbounds-safety -x objective-c %s 2>&1 | FileCheck %s
4+
// RUN: not %clang_cc1 -fbounds-safety -x objective-c %s 2>&1 | FileCheck %s
55

6-
// RUN: not %clang -cc1 -fbounds-safety -x objective-c++ %s 2>&1 | FileCheck %s
6+
// RUN: not %clang_cc1 -fbounds-safety -x objective-c++ %s 2>&1 | FileCheck %s
77

8-
// RUN: not %clang -cc1 -fbounds-safety -x objective-c++ %s 2>&1 | FileCheck %s
8+
// RUN: not %clang_cc1 -fbounds-safety -x objective-c++ %s 2>&1 | FileCheck %s
99

1010
// CHECK: error: -fbounds-safety is supported only for C language
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import lit.formats
2+
import os
3+
4+
# clang/test uses external shell by default, but all tests in clang/test/BoundsSafety
5+
# support lit's internal shell, so use that instead
6+
config.test_format = lit.formats.ShTest(False)
7+
8+
cc1_substituted = [ False ]
9+
clang_substituted = [ False ]
10+
extra_flags = '-fbounds-safety-bringup-missing-checks=all'
11+
12+
def get_subst(subst_name):
13+
subst_re = r'%\b{}\b'.format(subst_name)
14+
for subst_name, value in config.substitutions:
15+
if subst_re not in subst_name:
16+
continue
17+
return value
18+
raise Exception(f'Could not find %{subst_name} substitution')
19+
20+
def patch_subst_impl(sub_tuple, subst_to_patch):
21+
assert isinstance(sub_tuple, tuple)
22+
assert len(sub_tuple) == 2
23+
subst_name = sub_tuple[0]
24+
25+
# Match `%subst_name` (e.g. `%clang_cc1`). The `ToolSubst` class inside
26+
# `lit` adds a bunch of regexes around this substitution so we try to match
27+
# them here to avoid matching a substitution that has `%clang_cc1` as a
28+
# substring. This is fragile
29+
subst_re = r'%\b{}\b'.format(subst_to_patch)
30+
if subst_re not in subst_name:
31+
return (sub_tuple[0], sub_tuple[1], False)
32+
patched_sub = sub_tuple[1] + f' {extra_flags}'
33+
return (sub_tuple[0], patched_sub, True)
34+
35+
def patch_cc1_subst(sub_tuple):
36+
subst_name, subst, patched = patch_subst_impl(sub_tuple, 'clang_cc1')
37+
if patched:
38+
cc1_substituted[0] = True
39+
return (subst_name, subst)
40+
41+
def patch_clang_subst(sub_tuple):
42+
subst_name, subst, patched = patch_subst_impl(sub_tuple, 'clang')
43+
if patched:
44+
clang_substituted[0] = True
45+
return (subst_name, subst)
46+
47+
48+
force_new_bounds_checks_on = False
49+
50+
# Provide an un-patched substitution
51+
default_clang_subst = get_subst('clang')
52+
config.substitutions.append(
53+
(r'%\bclang_no_bounds_check_mode_specified\b', default_clang_subst)
54+
)
55+
56+
if False:
57+
force_new_bounds_checks_on = True
58+
59+
if force_new_bounds_checks_on:
60+
config.substitutions = list(map(patch_cc1_subst, config.substitutions))
61+
config.substitutions = list(map(patch_clang_subst, config.substitutions))
62+
63+
dir_with_patched_sub = os.path.dirname(__file__)
64+
for subst, patched in [('%clang_cc1', cc1_substituted[0]), ('%clang', clang_substituted[0])]:
65+
if not patched:
66+
lit_config.fatal(f'Failed to add `{extra_flags}` to {subst} invocations under {dir_with_patched_sub}')
67+
else:
68+
lit_config.note(f'Implicitly passing `{extra_flags}` to {subst} invocations under {dir_with_patched_sub}')
69+

0 commit comments

Comments
 (0)