Skip to content

Commit 87e378e

Browse files
committed
Fix likely/unlikely attribute placement
https://bugs.webkit.org/show_bug.cgi?id=327728 rdar://190152289 Reviewed by Yusuke Suzuki. It looks like Clang only processes `[[likely]]` and `[[unlikely]]` attributes if they precede the brace in an if statement. Fix the places where we used the attribute after the brace and add a style checker rule to catch this in the future. * Source/JavaScriptCore/runtime/MarkedVector.h: * Source/bmalloc/bmalloc/TZoneHeapInlines.h: * Tools/Scripts/webkitpy/style/checkers/cpp.py: (check_likelihood_attribute_placement): (check_style): * Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py: Canonical link: https://commits.webkit.org/323916@main
1 parent c13166c commit 87e378e

4 files changed

Lines changed: 69 additions & 3 deletions

File tree

‎Source/JavaScriptCore/runtime/MarkedVector.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ class MarkedVector : public OverflowHandler, public MarkedVectorBase {
345345

346346
[[nodiscard]] T& at(size_t i) LIFETIME_BOUND
347347
{
348-
if (i >= size()) { [[unlikely]]
348+
if (i >= size()) [[unlikely]] {
349349
OverflowHandler::overflowed();
350350
if constexpr (!std::is_same_v<OverflowHandler, CrashOnOverflow>) {
351351
T* oobStorage = std::bit_cast<T*>(&m_storageForOutOfBoundsAccess);
@@ -357,7 +357,7 @@ class MarkedVector : public OverflowHandler, public MarkedVectorBase {
357357
}
358358
[[nodiscard]] const T& at(size_t i) const LIFETIME_BOUND
359359
{
360-
if (i >= size()) { [[unlikely]]
360+
if (i >= size()) [[unlikely]] {
361361
const_cast<MarkedVector*>(this)->OverflowHandler::overflowed();
362362
if constexpr (!std::is_same_v<OverflowHandler, CrashOnOverflow>) {
363363
T* oobStorage = std::bit_cast<T*>(&m_storageForOutOfBoundsAccess);

‎Source/bmalloc/bmalloc/TZoneHeapInlines.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public: \
7373
TZONE_SPEC_NAME_ARG(#_type) \
7474
}; \
7575
\
76-
if (!s_heapRef || size != sizeof(_type)) { [[unlikely]] \
76+
if (!s_heapRef || size != sizeof(_type)) [[unlikely]] { \
7777
if constexpr (::bmalloc::api::compactAllocationMode<_type>() == CompactAllocationMode::Compact) \
7878
return ::bmalloc::api::tzoneAllocateCompactSlow(size, s_heapSpec); \
7979
return ::bmalloc::api::tzoneAllocate ## _compactMode ## Slow(size, s_heapSpec); \

‎Tools/Scripts/webkitpy/style/checkers/cpp.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3957,6 +3957,34 @@ def check_adopt_of_dynamic_cast(clean_lines, line_number, file_state, error):
39573957
error(line_number, 'runtime/adopt_dynamic_cast', 4, "Adopt the value before casting, e.g. '%s<>(%s(...))', otherwise the value leaks on type mismatch." % (cast_func, adopt_func))
39583958

39593959

3960+
def check_likelihood_attribute_placement(clean_lines, line_number, file_state, error):
3961+
"""Looks for '[[likely]]' / '[[unlikely]]' placed just after the opening brace of a block.
3962+
3963+
An attribute at the start of a statement applies to that statement. After
3964+
'if (condition) {' it applies to the first statement inside the block, not
3965+
to the branch, and Clang ignores it there. It belongs on the substatement:
3966+
'if (condition) [[unlikely]] {'.
3967+
3968+
Args:
3969+
clean_lines: A CleansedLines instance containing the file.
3970+
line_number: The number of the line to check.
3971+
file_state: A _FileState instance which maintains information about
3972+
the state of things in the file.
3973+
error: The function to call with any errors found.
3974+
"""
3975+
3976+
line = clean_lines.elided[line_number] # Get rid of comments and strings.
3977+
attribute = r'\[\[\s*(likely|unlikely)\s*\]\]'
3978+
3979+
matched = search(r'\{\s*' + attribute, line)
3980+
if not matched and line_number > 0 and match(r'\s*' + attribute, line):
3981+
matched = search(r'\{\s*$', clean_lines.elided[line_number - 1])
3982+
if matched:
3983+
attribute_name = search(attribute, line).group(1)
3984+
error(line_number, 'readability/braces', 4,
3985+
"[[%s]] after '{' applies to the first statement in the block, not to the branch. Put it before the '{', e.g. 'if (condition) [[%s]] {'." % (attribute_name, attribute_name))
3986+
3987+
39603988
def check_lock_guard(clean_lines, line_number, file_state, error):
39613989
"""Looks for use of 'std::lock_guard<>' which should be replaced with 'WTF::Locker'.
39623990
@@ -4842,6 +4870,7 @@ def check_style(clean_lines, line_number, file_extension, class_state, file_stat
48424870
check_printstream_printf(clean_lines, line_number, file_state, error)
48434871
check_auto_with_adopt(clean_lines, line_number, file_state, error)
48444872
check_adopt_of_dynamic_cast(clean_lines, line_number, file_state, error)
4873+
check_likelihood_attribute_placement(clean_lines, line_number, file_state, error)
48454874
check_lock_guard(clean_lines, line_number, file_state, error)
48464875
check_log(clean_lines, line_number, file_state, error)
48474876
check_ctype_functions(clean_lines, line_number, file_state, error)

‎Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6327,6 +6327,43 @@ def test_adopt_of_dynamic_cast(self):
63276327
'',
63286328
'foo.cpp')
63296329

6330+
def test_likelihood_attribute_placement(self):
6331+
message = "[[unlikely]] after '{' applies to the first statement in the block, not to the branch. Put it before the '{', e.g. 'if (condition) [[unlikely]] {'. [readability/braces] [4]"
6332+
# The existing whitespace checks also complain about this form, so only look for the new error.
6333+
self.assertIn(message, self.perform_multi_line_lint(
6334+
'''void f()
6335+
{
6336+
if (x) { [[unlikely]]
6337+
g();
6338+
h();
6339+
}
6340+
}
6341+
''', 'h'))
6342+
self.assert_multi_line_lint(
6343+
'''void f()
6344+
{
6345+
if (x) {
6346+
[[unlikely]]
6347+
g();
6348+
h();
6349+
}
6350+
}
6351+
''',
6352+
message)
6353+
# Inside a macro definition, where every line ends with a backslash.
6354+
self.assertIn(message, self.perform_multi_line_lint(
6355+
'''#define CHECK_THING(x) \\
6356+
do { \\
6357+
if (!(x)) { [[unlikely]] \\
6358+
handleFailure(); \\
6359+
return; \\
6360+
} \\
6361+
} while (0)
6362+
''', 'h'))
6363+
self.assert_lint('if (x) [[unlikely]] {', '', 'foo.cpp')
6364+
self.assert_lint('else [[likely]] {', '', 'foo.cpp')
6365+
self.assert_lint('case 1: [[likely]] {', '', 'foo.cpp')
6366+
63306367
def test_wtf_make_unique(self):
63316368
self.assert_lint(
63326369
'std::unique_ptr<Foo> foo = WTF::makeUnique<Foo>();',

0 commit comments

Comments
 (0)