Skip to content

Commit e8f7048

Browse files
committed
[Swift] Cease to tie ASSERT_ENABLED and LOG_DISABLED
https://bugs.webkit.org/show_bug.cgi?id=329066 Reviewed by Abrar Rahman Protyasha. 324710@main gated Swift's debugLog on ASSERT_ENABLED, because Swift could not see LOG_DISABLED, and added a static_assert that the two agree. This turned out to be inconvenient. Move the *_DISABLED defaults derived from ASSERT_ENABLED into PlatformEnable.h, and have generate-platform-args forward LOG_ macros and resolve `!NAME` values, so Swift sees LOG_DISABLED and the static_assert can go. Alternatives considered: preprocessing Assertions.h in generate-platform-args instead. It includes system headers (e.g. syslog.h via sd-journal.h, which defines LOG_ALERT), so the script would have had to hard-code macro names rather than match prefixes. This also corrects a couple of comments. * Source/WTF/Scripts/generate-platform-args: (platform_defines_from_dM): * Source/WTF/wtf/Assertions.h: * Source/WTF/wtf/Logging.h: * Source/WTF/wtf/PlatformEnable.h: * Source/WebKit/Platform/Logging.swift: Canonical link: https://commits.webkit.org/324870@main
1 parent 15cb356 commit e8f7048

5 files changed

Lines changed: 46 additions & 40 deletions

File tree

‎Source/WTF/Scripts/generate-platform-args‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,18 @@ import sys
2727
# define names to --output (or stdout) for the generators' --defines-file.
2828

2929
DEFINE_RE = re.compile(r'^#define (\w+) (.+)', re.MULTILINE)
30-
PLATFORM_PREFIX_RE = re.compile(r'^(HAVE_|USE_|ENABLE_|WTF_PLATFORM|ASSERT_)')
30+
PLATFORM_PREFIX_RE = re.compile(r'^(HAVE_|USE_|ENABLE_|WTF_PLATFORM|ASSERT_|LOG_)')
31+
NEGATION_RE = re.compile(r'^!\s*(\w+)$')
3132
STL_FEATURE_MACRO_RE = re.compile(r'^#define (__cpp_lib_\w+) (.+)', re.MULTILINE)
3233

3334

3435
def platform_defines_from_dM(stdout):
3536
"""Parse `clang -E -dM` output and return the names of the truthy
36-
HAVE_/USE_/ENABLE_/WTF_PLATFORM_/ASSERT_ macros. Resolves simple macro
37-
indirection: clang -dM emits macros alphabetically rather than
38-
topologically, so a macro may reference another we have not yet seen;
39-
iterate to a fixed point with a bound to avoid recursive definitions
40-
looping forever."""
37+
HAVE_/USE_/ENABLE_/WTF_PLATFORM_/ASSERT_/LOG_ macros. Resolves simple
38+
macro indirection and negation (`!NAME`): clang -dM emits macros
39+
alphabetically rather than topologically, so a macro may reference
40+
another we have not yet seen; iterate to a fixed point with a bound to
41+
avoid recursive definitions looping forever."""
4142
definitions = {}
4243
for m in re.finditer(DEFINE_RE, stdout):
4344
name = m.group(1)
@@ -51,9 +52,16 @@ def platform_defines_from_dM(stdout):
5152
for _ in range(10):
5253
changed = False
5354
for name, value in definitions.items():
54-
if type(value) == str and value in definitions:
55+
if type(value) != str:
56+
continue
57+
if value in definitions:
5558
definitions[name] = definitions[value]
5659
changed = True
60+
continue
61+
negation = NEGATION_RE.match(value)
62+
if negation and type(definitions.get(negation.group(1))) == int:
63+
definitions[name] = int(not definitions[negation.group(1)])
64+
changed = True
5765
if not changed:
5866
break
5967
return [

‎Source/WTF/wtf/Assertions.h‎

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -77,31 +77,7 @@
7777
#define _XSTRINGIFY(line) #line
7878
#define _STRINGIFY(line) _XSTRINGIFY(line)
7979

80-
/* ASSERT_ENABLED is defined in PlatformEnable.h. */
81-
82-
#ifndef BACKTRACE_DISABLED
83-
#define BACKTRACE_DISABLED !ASSERT_ENABLED
84-
#endif
85-
86-
#ifndef ASSERT_MSG_DISABLED
87-
#define ASSERT_MSG_DISABLED !ASSERT_ENABLED
88-
#endif
89-
90-
#ifndef ASSERT_ARG_DISABLED
91-
#define ASSERT_ARG_DISABLED !ASSERT_ENABLED
92-
#endif
93-
94-
#ifndef FATAL_DISABLED
95-
#define FATAL_DISABLED !ASSERT_ENABLED
96-
#endif
97-
98-
#ifndef ERROR_DISABLED
99-
#define ERROR_DISABLED !ASSERT_ENABLED
100-
#endif
101-
102-
#ifndef LOG_DISABLED
103-
#define LOG_DISABLED !ASSERT_ENABLED
104-
#endif
80+
/* ASSERT_ENABLED and the *_DISABLED macros derived from it are defined in PlatformEnable.h. */
10581

10682
#if ENABLE(RELEASE_LOG)
10783
#define RELEASE_LOG_DISABLED 0

‎Source/WTF/wtf/Logging.h‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030

3131
namespace WTF {
3232

33-
// Swift cannot see LOG_DISABLED, so it decides whether LOG is compiled in from ASSERT_ENABLED.
34-
static_assert(!LOG_DISABLED == !!ASSERT_ENABLED, "Swift's debugLog assumes that LOG is compiled in exactly when ASSERT_ENABLED is");
35-
3633
#if !LOG_DISABLED || !RELEASE_LOG_DISABLED
3734

3835
#ifndef LOG_CHANNEL_PREFIX

‎Source/WTF/wtf/PlatformEnable.h‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,30 @@
6969
#endif
7070
#endif
7171

72+
#ifndef BACKTRACE_DISABLED
73+
#define BACKTRACE_DISABLED !ASSERT_ENABLED
74+
#endif
75+
76+
#ifndef ASSERT_MSG_DISABLED
77+
#define ASSERT_MSG_DISABLED !ASSERT_ENABLED
78+
#endif
79+
80+
#ifndef ASSERT_ARG_DISABLED
81+
#define ASSERT_ARG_DISABLED !ASSERT_ENABLED
82+
#endif
83+
84+
#ifndef FATAL_DISABLED
85+
#define FATAL_DISABLED !ASSERT_ENABLED
86+
#endif
87+
88+
#ifndef ERROR_DISABLED
89+
#define ERROR_DISABLED !ASSERT_ENABLED
90+
#endif
91+
92+
#ifndef LOG_DISABLED
93+
#define LOG_DISABLED !ASSERT_ENABLED
94+
#endif
95+
7296

7397
/* ==== Platform additions: additions to PlatformEnable.h from outside the main repository ==== */
7498

‎Source/WebKit/Platform/Logging.swift‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ import os
3838
///
3939
/// On Cocoa both are an `os.Logger` for the channel's os_log handle, so call sites get os_log's compile-time
4040
/// handling. `debugLog` discards everything unless the channel is enabled. Where LOG is compiled out, it is a
41-
/// `NullLogger`, so that neither the messages nor the calls remain in the binary. Use only the `privacy:`
42-
/// interpolation option so that code also builds against `PortableLogger` and `NullLogger`.
41+
/// `NullLogger`, which never evaluates its messages. They and the calls stay in the binary until the module is
42+
/// built with whole-module optimization. Use only the `privacy:` interpolation option so that code also builds
43+
/// against `PortableLogger` and `NullLogger`.
4344
extension WTFLogChannel {
44-
// Swift cannot see LOG_DISABLED, so this relies on the static_assert in wtf/Logging.h that it matches ASSERT_ENABLED.
45-
#if ASSERT_ENABLED
45+
#if !LOG_DISABLED
4646
#if canImport(os)
4747
@inline(always)
4848
var debugLog: os.Logger {
@@ -112,7 +112,8 @@ struct PortableLogMessage: ExpressibleByStringInterpolation {
112112

113113
/// What `debugLog` is where LOG is compiled out.
114114
///
115-
/// Unlike `os.Logger(.disabled)`, it is visibly a no-op, so the optimizer removes each call along with its message.
115+
/// Unlike `os.Logger(.disabled)`, it is visibly a no-op, so whole-module optimization removes each call along with
116+
/// its message. Until the module is built that way, both stay in the binary.
116117
struct NullLogger {
117118
@inline(always)
118119
func log(_ message: @autoclosure () -> PortableLogMessage) {}

0 commit comments

Comments
 (0)