|
| 1 | +import lit |
1 | 2 | import os |
2 | 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 | + |
3 | 8 | cc1_substituted = [ False ] |
| 9 | +clang_substituted = [ False ] |
4 | 10 | extra_flags = '-fno-bounds-safety-bringup-missing-checks=all' |
5 | 11 |
|
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): |
7 | 21 | assert isinstance(sub_tuple, tuple) |
8 | 22 | assert len(sub_tuple) == 2 |
9 | 23 | subst_name = sub_tuple[0] |
10 | 24 |
|
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) |
17 | 32 | 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 | +) |
20 | 53 |
|
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)) |
22 | 56 |
|
23 | 57 | 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}') |
0 commit comments