22
22
import argparse
23
23
import io
24
24
import os
25
+ import re
25
26
import subprocess
26
27
import sys
27
- from typing import List
28
+ from typing import FrozenSet , List
28
29
29
30
UNCRUSTIFY_SUPPORTED_VERSION = "0.75.1"
30
31
CONFIG_FILE = ".uncrustify.cfg"
31
32
UNCRUSTIFY_EXE = "uncrustify"
32
33
UNCRUSTIFY_ARGS = ["-c" , CONFIG_FILE ]
33
34
STDOUT_UTF8 = io .TextIOWrapper (sys .stdout .buffer , encoding = 'utf-8' )
34
35
STDERR_UTF8 = io .TextIOWrapper (sys .stderr .buffer , encoding = 'utf-8' )
36
+ CHECK_GENERATED_FILES = "tests/scripts/check-generated-files.sh"
35
37
36
38
def print_err (* args ):
37
39
print ("Error: " , * args , file = STDERR_UTF8 )
38
40
41
+ # Match FILENAME(s) in "check SCRIPT (FILENAME...)"
42
+ CHECK_CALL_RE = re .compile (r"\n\s*check\s+[^\s#$&*?;|]+([^\n#$&*?;|]+)" ,
43
+ re .ASCII )
44
+ def list_generated_files () -> FrozenSet [str ]:
45
+ """Return the names of generated files.
46
+
47
+ We don't reformat generated files, since the result might be different
48
+ from the output of the generator. Ideally the result of the generator
49
+ would conform to the code style, but this would be difficult, especially
50
+ with respect to the placement of line breaks in long logical lines.
51
+ """
52
+ # Parse check-generated-files.sh to get an up-to-date list of
53
+ # generated files. Read the file rather than calling it so that
54
+ # this script only depends on Git, Python and uncrustify, and not other
55
+ # tools such as sh or grep which might not be available on Windows.
56
+ # This introduces a limitation: check-generated-files.sh must have
57
+ # the expected format and must list the files explicitly, not through
58
+ # wildcards or command substitution.
59
+ content = open (CHECK_GENERATED_FILES , encoding = "utf-8" ).read ()
60
+ checks = re .findall (CHECK_CALL_RE , content )
61
+ return frozenset (word for s in checks for word in s .split ())
62
+
39
63
def get_src_files () -> List [str ]:
40
64
"""
41
65
Use git ls-files to get a list of the source files
@@ -52,11 +76,14 @@ def get_src_files() -> List[str]:
52
76
print_err ("git ls-files returned: " + str (result .returncode ))
53
77
return []
54
78
else :
79
+ generated_files = list_generated_files ()
55
80
src_files = str (result .stdout , "utf-8" ).split ()
56
- # Don't correct style for files in 3rdparty/
57
- src_files = list (filter ( \
58
- lambda filename : not filename .startswith ("3rdparty/" ), \
59
- src_files ))
81
+ # Don't correct style for third-party files (and, for simplicity,
82
+ # companion files in the same subtree), or for automatically
83
+ # generated files (we're correcting the templates instead).
84
+ src_files = [filename for filename in src_files
85
+ if not (filename .startswith ("3rdparty/" ) or
86
+ filename in generated_files )]
60
87
return src_files
61
88
62
89
def get_uncrustify_version () -> str :
0 commit comments