[clang] 8774128 - [clang-format][NFC] Simplify AlignMacroMatches (#164122)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 20 01:43:59 PDT 2025
Author: Björn Schäpers
Date: 2025-10-20T08:43:53Z
New Revision: 8774128fcbff455baa2ac4b49cc7877f78ec8ff8
URL: https://github.com/llvm/llvm-project/commit/8774128fcbff455baa2ac4b49cc7877f78ec8ff8
DIFF: https://github.com/llvm/llvm-project/commit/8774128fcbff455baa2ac4b49cc7877f78ec8ff8.diff
LOG: [clang-format][NFC] Simplify AlignMacroMatches (#164122)
Just return early based on the SpacedRequiredBefore.
Added:
Modified:
clang/lib/Format/WhitespaceManager.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index aae2f3e6439cc..b004d738588ab 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -663,7 +663,7 @@ void WhitespaceManager::alignConsecutiveMacros() {
auto AlignMacrosMatches = [](const Change &C) {
const FormatToken *Current = C.Tok;
- unsigned SpacesRequiredBefore = 1;
+ assert(Current);
if (Current->SpacesRequiredBefore == 0 || !Current->Previous)
return false;
@@ -672,22 +672,22 @@ 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 (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 || 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
+ // 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->endsSequence(tok::identifier, tok::pp_define);
};
AlignTokens<decltype(AlignMacrosMatches) &, /*SimpleCheck=*/true>(
More information about the cfe-commits
mailing list