[clang] [clang-format][NFC] Replace size() with empty() (PR #147164)
llvm-ci wrote: LLVM Buildbot has detected a new failure on builder `sanitizer-x86_64-linux-fast` running on `sanitizer-buildbot3` while building `clang` at step 2 "annotate". Full details are available at: https://lab.llvm.org/buildbot/#/builders/169/builds/12888 Here is the relevant piece of the build log for the reference ``` Step 2 (annotate) failure: 'python ../sanitizer_buildbot/sanitizers/zorg/buildbot/builders/sanitizers/buildbot_selector.py' (failure) ... llvm-lit: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using lld-link: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/lld-link llvm-lit: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using ld64.lld: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/ld64.lld llvm-lit: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using wasm-ld: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/wasm-ld llvm-lit: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using ld.lld: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/ld.lld llvm-lit: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using lld-link: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/lld-link llvm-lit: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using ld64.lld: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/ld64.lld llvm-lit: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using wasm-ld: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/wasm-ld llvm-lit: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/main.py:73: note: The test suite configuration requested an individual test timeout of 0 seconds but a timeout of 900 seconds was requested on the command line. Forcing timeout to be 900 seconds. -- Testing: 91030 tests, 96 workers -- Testing: 0.. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90.. FAIL: LLVM :: ExecutionEngine/JITLink/x86-64/COFF_directive_alternatename_fail.s (74729 of 91030) TEST 'LLVM :: ExecutionEngine/JITLink/x86-64/COFF_directive_alternatename_fail.s' FAILED Exit Code: 1 Command Output (stderr): -- /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/llvm-mc -filetype=obj -triple=x86_64-windows-msvc /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/test/ExecutionEngine/JITLink/x86-64/COFF_directive_alternatename_fail.s -o /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/test/ExecutionEngine/JITLink/x86-64/Output/COFF_directive_alternatename_fail.s.tmp # RUN: at line 1 + /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/llvm-mc -filetype=obj -triple=x86_64-windows-msvc /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/test/ExecutionEngine/JITLink/x86-64/COFF_directive_alternatename_fail.s -o /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/test/ExecutionEngine/JITLink/x86-64/Output/COFF_directive_alternatename_fail.s.tmp not /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/llvm-jitlink -noexec /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/test/ExecutionEngine/JITLink/x86-64/Output/COFF_directive_alternatename_fail.s.tmp 2>&1 | /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/FileCheck /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/test/ExecutionEngine/JITLink/x86-64/COFF_directive_alternatename_fail.s # RUN: at line 2 + not /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/llvm-jitlink -noexec /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/test/ExecutionEngine/JITLink/x86-64/Output/COFF_directive_alternatename_fail.s.tmp + /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/FileCheck /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/test/ExecutionEngine/JITLink/x86-64/COFF_directive_alternatename_fail.s -- Testing: 0.. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90.. Slowest Tests: -- 596.28s: LLVM :: CodeGen/AMDGPU/sched-group-barrier-pipeline-solver.mir 353.55s: Clang :: Driver/fsanitize.c 349.22s: LLVM :: CodeGen/AMDGPU/amdgcn.bitcast.1024bit.ll 285.65s: Clang :: Preprocessor/riscv-target-features.c 226.05s: LLVM :: CodeGen/AMDGPU/memintrinsic-unroll.ll 224.91s: Clang :: Driver/arm-cortex-cpus-2.c 222.43s: Clang :: OpenMP/target_update_codegen.cpp 220.12s: Clang :: OpenMP/target_defaultmap_codegen_01.cpp 215.56s: Clang :: Driver/arm-cortex-cpus-1.c 205.47s: Cl
[clang] [clang-format][NFC] Replace size() with empty() (PR #147164)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/147164 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format][NFC] Replace size() with empty() (PR #147164)
https://github.com/HazardyKnusperkeks approved this pull request. https://github.com/llvm/llvm-project/pull/147164 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format][NFC] Replace size() with empty() (PR #147164)
llvmbot wrote:
@llvm/pr-subscribers-clang-format
Author: Owen Pan (owenca)
Changes
---
Full diff: https://github.com/llvm/llvm-project/pull/147164.diff
5 Files Affected:
- (modified) clang/lib/Format/Format.cpp (+1-1)
- (modified) clang/lib/Format/FormatTokenLexer.cpp (+5-6)
- (modified) clang/lib/Format/QualifierAlignmentFixer.cpp (+1-1)
- (modified) clang/lib/Format/UnwrappedLineFormatter.cpp (+2-3)
- (modified) clang/lib/Format/WhitespaceManager.cpp (+4-4)
``diff
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index bdaf264e9adce..f0412cddc6f19 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3582,7 +3582,7 @@ tooling::Replacements sortJavaImports(const FormatStyle
&Style, StringRef Code,
ImportsInBlock.push_back(
{Identifier, Line, Prev, AssociatedCommentLines, IsStatic});
AssociatedCommentLines.clear();
-} else if (Trimmed.size() > 0 && !ImportsInBlock.empty()) {
+} else if (!Trimmed.empty() && !ImportsInBlock.empty()) {
// Associating comments within the imports with the nearest import below
AssociatedCommentLines.push_back(Line);
}
diff --git a/clang/lib/Format/FormatTokenLexer.cpp
b/clang/lib/Format/FormatTokenLexer.cpp
index 06f68ec8b0fc1..05dee26735cc9 100644
--- a/clang/lib/Format/FormatTokenLexer.cpp
+++ b/clang/lib/Format/FormatTokenLexer.cpp
@@ -1371,8 +1371,7 @@ FormatToken *FormatTokenLexer::getNextToken() {
} else if (FormatTok->TokenText == "``") {
FormatTok->Tok.setIdentifierInfo(nullptr);
FormatTok->Tok.setKind(tok::hashhash);
- } else if (Tokens.size() > 0 &&
- Tokens.back()->is(Keywords.kw_apostrophe) &&
+ } else if (!Tokens.empty() && Tokens.back()->is(Keywords.kw_apostrophe)
&&
NumberBase.match(FormatTok->TokenText, &Matches)) {
// In Verilog in a based number literal like `'b10`, there may be
// whitespace between `'b` and `10`. Therefore we handle the base and
@@ -1420,7 +1419,7 @@ FormatToken *FormatTokenLexer::getNextToken() {
tryParseJavaTextBlock();
}
- if (Style.isVerilog() && Tokens.size() > 0 &&
+ if (Style.isVerilog() && !Tokens.empty() &&
Tokens.back()->is(TT_VerilogNumberBase) &&
FormatTok->Tok.isOneOf(tok::identifier, tok::question)) {
// Mark the number following a base like `'h?a0` as a number.
@@ -1454,9 +1453,9 @@ FormatToken *FormatTokenLexer::getNextToken() {
if (Style.isCpp()) {
auto *Identifier = FormatTok->Tok.getIdentifierInfo();
auto it = Macros.find(Identifier);
-if (!(Tokens.size() > 0 && Tokens.back()->Tok.getIdentifierInfo() &&
- Tokens.back()->Tok.getIdentifierInfo()->getPPKeywordID() ==
- tok::pp_define) &&
+if ((Tokens.empty() || !Tokens.back()->Tok.getIdentifierInfo() ||
+ Tokens.back()->Tok.getIdentifierInfo()->getPPKeywordID() !=
+ tok::pp_define) &&
it != Macros.end()) {
FormatTok->setType(it->second);
if (it->second == TT_IfMacro) {
diff --git a/clang/lib/Format/QualifierAlignmentFixer.cpp
b/clang/lib/Format/QualifierAlignmentFixer.cpp
index b0dda65adfba1..441a37a4902b7 100644
--- a/clang/lib/Format/QualifierAlignmentFixer.cpp
+++ b/clang/lib/Format/QualifierAlignmentFixer.cpp
@@ -640,7 +640,7 @@ bool isPossibleMacro(const FormatToken *Tok) {
return false;
const auto Text = Tok->TokenText;
- assert(Text.size() > 0);
+ assert(!Text.empty());
// T,K,U,V likely could be template arguments
if (Text.size() == 1)
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp
b/clang/lib/Format/UnwrappedLineFormatter.cpp
index ac8c0c8fd0ec0..0adf7ee9ed543 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -223,8 +223,6 @@ class LineJoiner {
tryFitMultipleLinesInOne(LevelIndentTracker &IndentTracker,
ArrayRef::const_iterator I,
ArrayRef::const_iterator E) {
-const unsigned Indent = IndentTracker.getIndent();
-
// Can't join the last line with anything.
if (I + 1 == E)
return 0;
@@ -240,6 +238,7 @@ class LineJoiner {
return 0;
}
+const auto Indent = IndentTracker.getIndent();
if (Style.ColumnLimit > 0 && Indent > Style.ColumnLimit)
return 0;
@@ -1090,7 +1089,7 @@ class LineFormatter {
const FormatToken *LBrace = State.NextToken->getPreviousNonComment();
bool HasLBrace = LBrace && LBrace->is(tok::l_brace) &&
LBrace->is(BK_Block);
FormatToken &Previous = *State.NextToken->Previous;
-if (Previous.Children.size() == 0 || (!HasLBrace && !LBrace->MacroParent))
{
+if (Previous.Children.empty() || (!HasLBrace && !LBrace->MacroParent)) {
// The previous token does not open a block. Nothing to do. We don't
// assert so that we can simply call this function for all tokens.
return true;
diff --git a/clang
[clang] [clang-format][NFC] Replace size() with empty() (PR #147164)
https://github.com/owenca created
https://github.com/llvm/llvm-project/pull/147164
None
>From e6945b8f51409e1ffd71a47b70e06599774d213e Mon Sep 17 00:00:00 2001
From: Owen Pan
Date: Sat, 5 Jul 2025 15:15:05 -0700
Subject: [PATCH] [clang-format][NFC] Replace size() with empty()
---
clang/lib/Format/Format.cpp | 2 +-
clang/lib/Format/FormatTokenLexer.cpp| 11 +--
clang/lib/Format/QualifierAlignmentFixer.cpp | 2 +-
clang/lib/Format/UnwrappedLineFormatter.cpp | 5 ++---
clang/lib/Format/WhitespaceManager.cpp | 8
5 files changed, 13 insertions(+), 15 deletions(-)
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index bdaf264e9adce..f0412cddc6f19 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3582,7 +3582,7 @@ tooling::Replacements sortJavaImports(const FormatStyle
&Style, StringRef Code,
ImportsInBlock.push_back(
{Identifier, Line, Prev, AssociatedCommentLines, IsStatic});
AssociatedCommentLines.clear();
-} else if (Trimmed.size() > 0 && !ImportsInBlock.empty()) {
+} else if (!Trimmed.empty() && !ImportsInBlock.empty()) {
// Associating comments within the imports with the nearest import below
AssociatedCommentLines.push_back(Line);
}
diff --git a/clang/lib/Format/FormatTokenLexer.cpp
b/clang/lib/Format/FormatTokenLexer.cpp
index 06f68ec8b0fc1..05dee26735cc9 100644
--- a/clang/lib/Format/FormatTokenLexer.cpp
+++ b/clang/lib/Format/FormatTokenLexer.cpp
@@ -1371,8 +1371,7 @@ FormatToken *FormatTokenLexer::getNextToken() {
} else if (FormatTok->TokenText == "``") {
FormatTok->Tok.setIdentifierInfo(nullptr);
FormatTok->Tok.setKind(tok::hashhash);
- } else if (Tokens.size() > 0 &&
- Tokens.back()->is(Keywords.kw_apostrophe) &&
+ } else if (!Tokens.empty() && Tokens.back()->is(Keywords.kw_apostrophe)
&&
NumberBase.match(FormatTok->TokenText, &Matches)) {
// In Verilog in a based number literal like `'b10`, there may be
// whitespace between `'b` and `10`. Therefore we handle the base and
@@ -1420,7 +1419,7 @@ FormatToken *FormatTokenLexer::getNextToken() {
tryParseJavaTextBlock();
}
- if (Style.isVerilog() && Tokens.size() > 0 &&
+ if (Style.isVerilog() && !Tokens.empty() &&
Tokens.back()->is(TT_VerilogNumberBase) &&
FormatTok->Tok.isOneOf(tok::identifier, tok::question)) {
// Mark the number following a base like `'h?a0` as a number.
@@ -1454,9 +1453,9 @@ FormatToken *FormatTokenLexer::getNextToken() {
if (Style.isCpp()) {
auto *Identifier = FormatTok->Tok.getIdentifierInfo();
auto it = Macros.find(Identifier);
-if (!(Tokens.size() > 0 && Tokens.back()->Tok.getIdentifierInfo() &&
- Tokens.back()->Tok.getIdentifierInfo()->getPPKeywordID() ==
- tok::pp_define) &&
+if ((Tokens.empty() || !Tokens.back()->Tok.getIdentifierInfo() ||
+ Tokens.back()->Tok.getIdentifierInfo()->getPPKeywordID() !=
+ tok::pp_define) &&
it != Macros.end()) {
FormatTok->setType(it->second);
if (it->second == TT_IfMacro) {
diff --git a/clang/lib/Format/QualifierAlignmentFixer.cpp
b/clang/lib/Format/QualifierAlignmentFixer.cpp
index b0dda65adfba1..441a37a4902b7 100644
--- a/clang/lib/Format/QualifierAlignmentFixer.cpp
+++ b/clang/lib/Format/QualifierAlignmentFixer.cpp
@@ -640,7 +640,7 @@ bool isPossibleMacro(const FormatToken *Tok) {
return false;
const auto Text = Tok->TokenText;
- assert(Text.size() > 0);
+ assert(!Text.empty());
// T,K,U,V likely could be template arguments
if (Text.size() == 1)
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp
b/clang/lib/Format/UnwrappedLineFormatter.cpp
index ac8c0c8fd0ec0..0adf7ee9ed543 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -223,8 +223,6 @@ class LineJoiner {
tryFitMultipleLinesInOne(LevelIndentTracker &IndentTracker,
ArrayRef::const_iterator I,
ArrayRef::const_iterator E) {
-const unsigned Indent = IndentTracker.getIndent();
-
// Can't join the last line with anything.
if (I + 1 == E)
return 0;
@@ -240,6 +238,7 @@ class LineJoiner {
return 0;
}
+const auto Indent = IndentTracker.getIndent();
if (Style.ColumnLimit > 0 && Indent > Style.ColumnLimit)
return 0;
@@ -1090,7 +1089,7 @@ class LineFormatter {
const FormatToken *LBrace = State.NextToken->getPreviousNonComment();
bool HasLBrace = LBrace && LBrace->is(tok::l_brace) &&
LBrace->is(BK_Block);
FormatToken &Previous = *State.NextToken->Previous;
-if (Previous.Children.size() == 0 || (!HasLBrace && !LBrace->MacroParent))
{
+if (Previous.Children.empty() || (!HasLBrace && !LBrace->MacroParent)) {
// The previous token does not open a block. Nothing
