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>
This commit is contained in:
Gilles Peskine 2022-12-19 00:48:58 +01:00
parent e162b4725c
commit 9a3771e1b3
2 changed files with 37 additions and 5 deletions

View file

@ -22,9 +22,10 @@ change of code style.
import argparse import argparse
import io import io
import os import os
import re
import subprocess import subprocess
import sys import sys
from typing import List from typing import FrozenSet, List
UNCRUSTIFY_SUPPORTED_VERSION = "0.75.1" UNCRUSTIFY_SUPPORTED_VERSION = "0.75.1"
CONFIG_FILE = ".uncrustify.cfg" CONFIG_FILE = ".uncrustify.cfg"
@ -32,10 +33,33 @@ UNCRUSTIFY_EXE = "uncrustify"
UNCRUSTIFY_ARGS = ["-c", CONFIG_FILE] UNCRUSTIFY_ARGS = ["-c", CONFIG_FILE]
STDOUT_UTF8 = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') STDOUT_UTF8 = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
STDERR_UTF8 = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8') STDERR_UTF8 = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
CHECK_GENERATED_FILES = "tests/scripts/check-generated-files.sh"
def print_err(*args): def print_err(*args):
print("Error: ", *args, file=STDERR_UTF8) print("Error: ", *args, file=STDERR_UTF8)
# Match FILENAME(s) in "check SCRIPT (FILENAME...)"
CHECK_CALL_RE = re.compile(r"\n\s*check\s+[^\s#$&*?;|]+([^\n#$&*?;|]+)",
re.ASCII)
def list_generated_files() -> FrozenSet[str]:
"""Return the names of generated files.
We don't reformat generated files, since the result might be different
from the output of the generator. 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.
"""
# Parse check-generated-files.sh to get an up-to-date list of
# generated files. Read the file rather than calling it so that
# this script only depends on Git, Python and uncrustify, and not other
# tools such as sh or grep which might not be available on Windows.
# This introduces a limitation: check-generated-files.sh must have
# the expected format and must list the files explicitly, not through
# wildcards or command substitution.
content = open(CHECK_GENERATED_FILES, encoding="utf-8").read()
checks = re.findall(CHECK_CALL_RE, content)
return frozenset(word for s in checks for word in s.split())
def get_src_files() -> List[str]: def get_src_files() -> List[str]:
""" """
Use git ls-files to get a list of the source files Use git ls-files to get a list of the source files
@ -52,11 +76,14 @@ def get_src_files() -> List[str]:
print_err("git ls-files returned: " + str(result.returncode)) print_err("git ls-files returned: " + str(result.returncode))
return [] return []
else: else:
generated_files = list_generated_files()
src_files = str(result.stdout, "utf-8").split() src_files = str(result.stdout, "utf-8").split()
# Don't correct style for files in 3rdparty/ # Don't correct style for third-party files (and, for simplicity,
src_files = list(filter( \ # companion files in the same subtree), or for automatically
lambda filename: not filename.startswith("3rdparty/"), \ # generated files (we're correcting the templates instead).
src_files)) src_files = [filename for filename in src_files
if not (filename.startswith("3rdparty/") or
filename in generated_files)]
return src_files return src_files
def get_uncrustify_version() -> str: def get_uncrustify_version() -> str:

View file

@ -116,6 +116,11 @@ check()
fi fi
} }
# Note: if the format of calls to the "check" function changes, update
# scripts/code_style.py accordingly. For generated C source files (*.h or *.c),
# the format must be "check SCRIPT FILENAME...". For other source files,
# any shell syntax is permitted (including e.g. command substitution).
check scripts/generate_errors.pl library/error.c check scripts/generate_errors.pl library/error.c
check scripts/generate_query_config.pl programs/test/query_config.c check scripts/generate_query_config.pl programs/test/query_config.c
check scripts/generate_driver_wrappers.py library/psa_crypto_driver_wrappers.c check scripts/generate_driver_wrappers.py library/psa_crypto_driver_wrappers.c