[clang] [clang-format][NFC] Simplify AlignMacroMatches (PR #164122)
Björn Schäpers via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 20 01:18:32 PDT 2025
https://github.com/HazardyKnusperkeks updated https://github.com/llvm/llvm-project/pull/164122
>From ff688c7e3829f769cfd20f2482e90b07e54618c8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= <bjoern at hazardy.de>
Date: Sat, 18 Oct 2025 22:11:47 +0200
Subject: [PATCH 1/3] [clang-format][NFC] Simplify AlignMacroMatches
Just return early based on the SpacedRequiredBefore.
---
clang/lib/Format/WhitespaceManager.cpp | 28 ++++++++++++--------------
1 file changed, 13 insertions(+), 15 deletions(-)
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index 7348a3af8cf95..03a1e9a6bd80b 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -656,7 +656,6 @@ void WhitespaceManager::alignConsecutiveMacros() {
auto AlignMacrosMatches = [](const Change &C) {
const FormatToken *Current = C.Tok;
- unsigned SpacesRequiredBefore = 1;
if (Current->SpacesRequiredBefore == 0 || !Current->Previous)
return false;
@@ -665,22 +664,21 @@ void WhitespaceManager::alignConsecutiveMacros() {
// If token is a ")", skip over the parameter list, to the
// token that precedes the "("
- if (Current->is(tok::r_paren) && Current->MatchingParen) {
- Current = Current->MatchingParen->Previous;
- SpacesRequiredBefore = 0;
- }
-
- if (!Current || Current->isNot(tok::identifier))
- return false;
-
- if (!Current->Previous || Current->Previous->isNot(tok::pp_define))
+ if (const auto *MatchingParen = Current->MatchingParen;
+ Current->is(tok::r_paren) && MatchingParen) {
+ // For a macro function, 0 spaces are required between the
+ // identifier and the lparen that opens the parameter list.
+ if (MatchingParen->SpacesRequiredBefore > 0)
+ return false;
+ Current = MatchingParen->Previous;
+ } else if (Current->Next->SpacesRequiredBefore != 1) {
+ // For a simple macro, 1 space is required between the
+ // identifier and the first token of the defined value.
return false;
+ }
- // For a macro function, 0 spaces are required between the
- // identifier and the lparen that opens the parameter list.
- // For a simple macro, 1 space is required between the
- // identifier and the first token of the defined value.
- return Current->Next->SpacesRequiredBefore == SpacesRequiredBefore;
+ return Current && Current->is(tok::identifier) && Current->Previous &&
+ Current->Previous->is(tok::pp_define);
};
unsigned MinColumn = 0;
>From b4c06a4a867871fea45bce340037628d956112ef Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= <bjoern at hazardy.de>
Date: Sun, 19 Oct 2025 22:44:28 +0200
Subject: [PATCH 2/3] Address review
---
clang/lib/Format/WhitespaceManager.cpp | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index 03a1e9a6bd80b..ecc8ec024a8dc 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -664,12 +664,14 @@ void WhitespaceManager::alignConsecutiveMacros() {
// If token is a ")", skip over the parameter list, to the
// token that precedes the "("
- if (const auto *MatchingParen = Current->MatchingParen;
- Current->is(tok::r_paren) && MatchingParen) {
+ if (Current->is(tok::r_paren)) {
+ const auto *MatchingParen = Current->MatchingParen;
// For a macro function, 0 spaces are required between the
// identifier and the lparen that opens the parameter list.
- if (MatchingParen->SpacesRequiredBefore > 0)
+ if (!MatchingParen || MatchingParen->SpacesRequiredBefore > 0 ||
+ !MatchingParen->Previous) {
return false;
+ }
Current = MatchingParen->Previous;
} else if (Current->Next->SpacesRequiredBefore != 1) {
// For a simple macro, 1 space is required between the
@@ -677,8 +679,7 @@ void WhitespaceManager::alignConsecutiveMacros() {
return false;
}
- return Current && Current->is(tok::identifier) && Current->Previous &&
- Current->Previous->is(tok::pp_define);
+ return Current->endsSequence(tok::identifier, tok::pp_define);
};
unsigned MinColumn = 0;
>From d08df2855a5ef6fdf9534c94164f64d7fa7f64ba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= <github at hazardy.de>
Date: Mon, 20 Oct 2025 10:18:24 +0200
Subject: [PATCH 3/3] Update clang/lib/Format/WhitespaceManager.cpp
---
clang/lib/Format/WhitespaceManager.cpp | 1 +
1 file changed, 1 insertion(+)
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index ecc8ec024a8dc..5b241c5e84a6c 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -656,6 +656,7 @@ void WhitespaceManager::alignConsecutiveMacros() {
auto AlignMacrosMatches = [](const Change &C) {
const FormatToken *Current = C.Tok;
+ assert(Current);
if (Current->SpacesRequiredBefore == 0 || !Current->Previous)
return false;
More information about the cfe-commits
mailing list