File: generate_private_header_tests.py

package info (click to toggle)
llvm-toolchain-13 1%3A13.0.1-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,418,840 kB
  • sloc: cpp: 5,290,826; ansic: 996,570; asm: 544,593; python: 188,212; objc: 72,027; lisp: 30,291; f90: 25,395; sh: 24,898; javascript: 9,780; pascal: 9,398; perl: 7,484; ml: 5,432; awk: 3,523; makefile: 2,913; xml: 953; cs: 573; fortran: 539
file content (83 lines) | stat: -rwxr-xr-x 2,901 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python

import os
import re
import shutil
from pathlib import Path


def get_libcxx_paths():
    utils_path = os.path.dirname(os.path.abspath(__file__))
    script_name = os.path.basename(__file__)
    assert os.path.exists(utils_path)
    src_root = os.path.dirname(utils_path)
    include_path = os.path.join(src_root, 'include')
    assert os.path.exists(include_path)
    docs_path = os.path.join(src_root, 'docs')
    assert os.path.exists(docs_path)
    detail_header_test_root = os.path.join(src_root, 'test', 'libcxx',
                                           'diagnostics', 'detail.headers')
    assert os.path.exists(detail_header_test_root)
    shutil.rmtree(detail_header_test_root)
    Path(f'{detail_header_test_root}').mkdir()
    assert os.path.exists(detail_header_test_root)
    return script_name, src_root, include_path, docs_path, detail_header_test_root


script_name, source_root, include_path, docs_path, detail_header_test_root = get_libcxx_paths(
)


def generate_test(header):
    return f'''// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// REQUIRES: modules-build

// WARNING: This test was generated by '{script_name}'
// and should not be edited manually.

// expected-error@*:* {{{{use of private header from outside its module: '{header}'}}}}
#include <{header}>
'''


def relative_path(path):
    return path.as_posix()[len(include_path + '/'):]


def is_still_public(path):
    rp = relative_path(path)
    return not rp.startswith('__support') and rp not in [
        "__bsd_locale_defaults.h", "__bsd_locale_fallbacks.h", "__config",
        "__config_site.in", "__debug", "__hash_table", "__functional_base",
        "__libcpp_version", "__nullptr", "__threading_support", "__tree",
        "__undef_macros"
    ]


def main():
    paths = [
        relative_path(p) for p in Path(include_path).rglob('*')
        if relative_path(p).startswith('__') and not p.is_dir()
        and is_still_public(p)
    ]
    for path in paths:
        path_with_subdir = re.search(r'__(\w+)/(\w+)', path)
        directory = path_with_subdir.group(1) + '/' if path_with_subdir else ""
        file = path_with_subdir.group(2) if path_with_subdir else path[2:]
        path_to_write = f'{detail_header_test_root}/{directory}{file}.module.verify.cpp'
        Path(f'{detail_header_test_root}/{directory}').mkdir(exist_ok=True)
        assert os.path.exists(f'{detail_header_test_root}/{directory}')
        with open(path_to_write, 'w') as f:
            f.write(generate_test(path))


if __name__ == '__main__':
    main()