|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +from collections.abc import Callable |
| 4 | +from collections.abc import Iterable |
| 5 | +import dataclasses |
| 6 | +import json |
| 7 | +import re |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | + |
| 11 | + |
| 12 | +def make_compile_commands(path: str) -> list[dict]: |
| 13 | + output = subprocess.check_output( |
| 14 | + ['ya', 'dump', 'compile-commands'], |
| 15 | + cwd=path, |
| 16 | + encoding='utf-8', |
| 17 | + stderr=subprocess.DEVNULL, |
| 18 | + ) |
| 19 | + return json.loads(output) |
| 20 | + |
| 21 | + |
| 22 | +def read_compile_commands() -> list[dict]: |
| 23 | + with open('compile_commands.json') as ifile: |
| 24 | + return json.load(ifile) |
| 25 | + |
| 26 | + |
| 27 | +def compile_command_yamake(cc_rule: dict) -> str: |
| 28 | + cmd = cc_rule['command'] |
| 29 | + |
| 30 | + # clang++ ... -> ya tool c++ ... |
| 31 | + cmd = 'ya tool c++ ' + cmd.split(' ', 1)[1] |
| 32 | + |
| 33 | + # remove '-o xxx.cpp.o' |
| 34 | + cmd = re.sub(' -o [^ ]* ', ' -o /dev/null ', cmd) |
| 35 | + |
| 36 | + cmd = re.sub(' -fcolor', ' -fno-color', cmd) |
| 37 | + |
| 38 | + return cmd |
| 39 | + |
| 40 | + |
| 41 | +def compile_command_cmake(cc_rule: dict) -> str: |
| 42 | + cmd = cc_rule['command'] |
| 43 | + |
| 44 | + # remove '-o xxx.cpp.o' |
| 45 | + cmd = re.sub(' -o [^ ]* ', ' -o /dev/null ', cmd) |
| 46 | + |
| 47 | + cmd = re.sub(' -fcolor', ' -fno-color', cmd) |
| 48 | + |
| 49 | + return cmd |
| 50 | + |
| 51 | + |
| 52 | +@dataclasses.dataclass |
| 53 | +class Message: |
| 54 | + line: int |
| 55 | + text: str |
| 56 | + |
| 57 | + |
| 58 | +FAIL_RE = re.compile(r'.*FAIL\(([^)]*)\).*') |
| 59 | +FAIL_NEXT_RE = re.compile(r'.*FAILNEXTLINE\(([^)]*)\).*') |
| 60 | + |
| 61 | + |
| 62 | +def read_file_messages(filename: str) -> list[Message]: |
| 63 | + result = [] |
| 64 | + |
| 65 | + with open(filename) as ifile: |
| 66 | + for linenum, line in enumerate(ifile): |
| 67 | + match = FAIL_RE.match(line) |
| 68 | + if match: |
| 69 | + result.append(Message(line=linenum + 1, text=match.group(1))) |
| 70 | + |
| 71 | + match = FAIL_NEXT_RE.match(line) |
| 72 | + if match: |
| 73 | + result.append(Message(line=linenum + 2, text=match.group(1))) |
| 74 | + |
| 75 | + return result |
| 76 | + |
| 77 | + |
| 78 | +def find_if(collection: Iterable, pred: Callable): |
| 79 | + for item in collection: |
| 80 | + if pred(item): |
| 81 | + return item |
| 82 | + return None |
| 83 | + |
| 84 | + |
| 85 | +class CheckFailure(Exception): |
| 86 | + pass |
| 87 | + |
| 88 | + |
| 89 | +def handle_rule(cc_rule: dict) -> None: |
| 90 | + cpp = cc_rule['file'] |
| 91 | + |
| 92 | + cmd = compile_command_cmake(cc_rule) |
| 93 | + stderr, errors = compile_for_errors(cmd) |
| 94 | + |
| 95 | + asserts = [] |
| 96 | + |
| 97 | + expected_msgs = read_file_messages(cpp) |
| 98 | + for msg in expected_msgs: |
| 99 | + # search for FAIL(...) line |
| 100 | + if not find_if(errors, lambda x: x.file == cpp and x.line == msg.line): |
| 101 | + asserts.append(f'Expected to get a compilation error/note at {cpp}:{msg.line}, but failed.') |
| 102 | + continue |
| 103 | + |
| 104 | + # search for FAIL(...) message |
| 105 | + if not find_if(errors, lambda x: msg.text in x.text): |
| 106 | + asserts.append(f'Expected to get a compilation error with text "{msg.text}", but failed.') |
| 107 | + |
| 108 | + if not asserts: |
| 109 | + return |
| 110 | + |
| 111 | + for line in asserts: |
| 112 | + print('error: ', line) |
| 113 | + |
| 114 | + print('\nexpected errors:') |
| 115 | + for msg in expected_msgs: |
| 116 | + print(f' {cpp}:{msg.line}: {msg.text}') |
| 117 | + |
| 118 | + print('\nactual compiler output:') |
| 119 | + print(stderr) |
| 120 | + |
| 121 | + raise CheckFailure() |
| 122 | + |
| 123 | + |
| 124 | +@dataclasses.dataclass |
| 125 | +class CompilationMessage: |
| 126 | + file: str |
| 127 | + line: int |
| 128 | + level: str |
| 129 | + text: str |
| 130 | + |
| 131 | + orig_text: str |
| 132 | + |
| 133 | + |
| 134 | +def compile_for_errors(cmd: str) -> tuple[str, list[CompilationMessage]]: |
| 135 | + proc = subprocess.run( |
| 136 | + cmd, |
| 137 | + shell=True, |
| 138 | + encoding='utf-8', |
| 139 | + capture_output=True, |
| 140 | + ) |
| 141 | + |
| 142 | + result = [] |
| 143 | + stderr = proc.stderr |
| 144 | + for line in stderr.splitlines(): |
| 145 | + if not line.startswith('/'): |
| 146 | + continue |
| 147 | + |
| 148 | + parts = line.split(':', 4) |
| 149 | + |
| 150 | + try: |
| 151 | + linenum = int(parts[1]) |
| 152 | + except ValueError: |
| 153 | + continue |
| 154 | + |
| 155 | + if len(parts) >= 5: |
| 156 | + level = parts[3].strip() |
| 157 | + text = parts[4].strip() |
| 158 | + else: |
| 159 | + level = '<none>' |
| 160 | + text = parts[3].strip() |
| 161 | + |
| 162 | + result.append( |
| 163 | + CompilationMessage( |
| 164 | + file=parts[0], |
| 165 | + line=linenum, |
| 166 | + level=level, |
| 167 | + text=text, |
| 168 | + orig_text=line, |
| 169 | + ) |
| 170 | + ) |
| 171 | + return stderr, result |
| 172 | + |
| 173 | + |
| 174 | +def matches_prefix(rule, src_prefix): |
| 175 | + file = rule['file'] |
| 176 | + if not file.startswith(src_prefix): |
| 177 | + return False |
| 178 | + |
| 179 | + return file.endswith('_compilefailtest.cpp') |
| 180 | + |
| 181 | + |
| 182 | +def main() -> None: |
| 183 | + src_prefix = sys.argv[1] |
| 184 | + cc = read_compile_commands() |
| 185 | + |
| 186 | + status = 0 |
| 187 | + for rule in cc: |
| 188 | + if not matches_prefix(rule, src_prefix): |
| 189 | + continue |
| 190 | + |
| 191 | + try: |
| 192 | + handle_rule(rule) |
| 193 | + except CheckFailure: |
| 194 | + status = 1 |
| 195 | + return status |
| 196 | + |
| 197 | + |
| 198 | +sys.exit(main()) |
0 commit comments