Skip to content

Commit 9a3771e

Browse files
Don't touch the style of generated files
Ideally the result of the generator would conform to the code style, but this would be difficult, especially with respect to the placement of line breaks in long logical lines. So, to avoid surprises when checking the style of generated files (which happens in releases and in long-time support branches), systematically skip generated files. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
1 parent e162b47 commit 9a3771e

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

scripts/code_style.py

+32-5
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,44 @@
2222
import argparse
2323
import io
2424
import os
25+
import re
2526
import subprocess
2627
import sys
27-
from typing import List
28+
from typing import FrozenSet, List
2829

2930
UNCRUSTIFY_SUPPORTED_VERSION = "0.75.1"
3031
CONFIG_FILE = ".uncrustify.cfg"
3132
UNCRUSTIFY_EXE = "uncrustify"
3233
UNCRUSTIFY_ARGS = ["-c", CONFIG_FILE]
3334
STDOUT_UTF8 = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
3435
STDERR_UTF8 = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
36+
CHECK_GENERATED_FILES = "tests/scripts/check-generated-files.sh"
3537

3638
def print_err(*args):
3739
print("Error: ", *args, file=STDERR_UTF8)
3840

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+
3963
def get_src_files() -> List[str]:
4064
"""
4165
Use git ls-files to get a list of the source files
@@ -52,11 +76,14 @@ def get_src_files() -> List[str]:
5276
print_err("git ls-files returned: " + str(result.returncode))
5377
return []
5478
else:
79+
generated_files = list_generated_files()
5580
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)]
6087
return src_files
6188

6289
def get_uncrustify_version() -> str:

tests/scripts/check-generated-files.sh

+5
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ check()
116116
fi
117117
}
118118

119+
# Note: if the format of calls to the "check" function changes, update
120+
# scripts/code_style.py accordingly. For generated C source files (*.h or *.c),
121+
# the format must be "check SCRIPT FILENAME...". For other source files,
122+
# any shell syntax is permitted (including e.g. command substitution).
123+
119124
check scripts/generate_errors.pl library/error.c
120125
check scripts/generate_query_config.pl programs/test/query_config.c
121126
check scripts/generate_driver_wrappers.py library/psa_crypto_driver_wrappers.c

0 commit comments

Comments
 (0)