Skip to content

Commit 5b9d062

Browse files
committed
Optimize out an extra regular expression substitution
Signed-off-by: Brentley Jones <github@brentleyjones.com>
1 parent c08dd99 commit 5b9d062

1 file changed

Lines changed: 19 additions & 18 deletions

File tree

xcodeproj/internal/bazel_integration_files/process_bazel_build_log.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88
import signal
99

1010

11+
STRIP_COLOR_RE = re.compile(r"\x1b\[[0-9;]{1,}[A-Za-z]")
12+
RELATIVE_DIAGNOSTICS_RE = re.compile(
13+
r"""
14+
^
15+
(?P<loc>.+?:\d+(?::\d+)?:\s) # Capture location (e.g. "foo/bar:12:3: ")
16+
(?:fatal\s)? # Dropping "fatal "
17+
(?P<sev>(?:error|warning):\s) # Capture severity
18+
(?P<msg>.*) # Capture the rest of the message
19+
""",
20+
re.VERBOSE,
21+
)
22+
1123
def _main(command: List[str]) -> None:
1224

1325
def _signal_handler(signum, frame):
@@ -18,7 +30,7 @@ def _signal_handler(signum, frame):
1830

1931
# Set up signal handler for SIGINT
2032
signal.signal(signal.SIGINT, _signal_handler)
21-
33+
2234
srcroot = os.getenv("SRCROOT")
2335
if not srcroot:
2436
sys.exit("SRCROOT environment variable must be set")
@@ -43,23 +55,12 @@ def _signal_handler(signum, frame):
4355

4456
should_strip_color = os.getenv("COLOR_DIAGNOSTICS", default="YES") != "YES"
4557

46-
strip_color = re.compile(r"\x1b\[[0-9;]{1,}[A-Za-z]")
47-
relative_diagnostic = re.compile(
48-
r"^.+?:\d+(:\d+)?: (fatal\s)?(error|warning): ."
49-
)
50-
fatal_error_diagnostic = re.compile(
51-
r": fatal error: "
52-
)
5358
has_relative_diagnostic = False
5459

5560
def _replacement(match: re.Match) -> str:
56-
message = match.group(0)
57-
58-
# Uppercase the first letter of the (actual) message
59-
message = message[:-1] + message[-1].upper()
60-
61-
# Replace 'fatal error:' with 'error:' to match Xcode's native build system
62-
message = fatal_error_diagnostic.sub(': error: ', message)
61+
message = f"""\
62+
{match.group("loc")}{match.group("sev")}{match.group("msg").capitalize()}\
63+
"""
6364

6465
if message.startswith(execution_root):
6566
# VFS overlays can make paths absolute, so make them relative again
@@ -87,12 +88,12 @@ def _process_log_line(line: str):
8788
input_line = line.rstrip()
8889

8990
if should_strip_color:
90-
input_line = strip_color.sub("", input_line)
91+
input_line = STRIP_COLOR_RE.sub("", input_line)
9192

9293
if not input_line:
9394
return
9495

95-
output_line = relative_diagnostic.sub(_replacement, input_line)
96+
output_line = RELATIVE_DIAGNOSTICS_RE.sub(_replacement, input_line)
9697
# Record if we have performed a relative diagnostic substitution.
9798
if output_line != input_line:
9899
nonlocal has_relative_diagnostic
@@ -106,7 +107,7 @@ def _process_log_line(line: str):
106107
for line in process.stderr:
107108
_process_log_line(line)
108109

109-
# If the Bazel invocation failed and there was no formatted error found,
110+
# If the Bazel invocation failed and there was no formatted error found,
110111
# print a nicer error message instead of a cryptic in Xcode:
111112
# 'Command PhaseScriptExecution failed with a nonzero exit code'
112113
if process.returncode != 0 and not has_relative_diagnostic:

0 commit comments

Comments
 (0)