[clang] [clang-format] Prevent re-assigning type on finalized tokens (PR #210763)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 30 11:46:50 PDT 2026
https://github.com/johnnyb2543 updated https://github.com/llvm/llvm-project/pull/210763
>From e7d05a6c7a08735ec157ae53cfd5eb3a64a9c5f5 Mon Sep 17 00:00:00 2001
From: John Boncore <johnnyb2543 at gmail.com>
Date: Mon, 20 Jul 2026 13:12:59 -0400
Subject: [PATCH 1/3] [clang-format] Prevent re-assigning type on finalized
tokens
---
clang/lib/Format/TokenAnnotator.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index b2a858859174b..a77af4388adb2 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2310,7 +2310,7 @@ class AnnotatingParser {
break;
if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
Previous->isPointerOrReference() && Previous->Previous &&
- Previous->Previous->isNot(tok::equal)) {
+ Previous->Previous->isNot(tok::equal) && !Previous->isTypeFinalized()) {
Previous->setType(TT_PointerOrReference);
}
}
>From 00efa7ceb03998c0aaa8eb56049016b1535aac5e Mon Sep 17 00:00:00 2001
From: John Boncore <johnnyb2543 at gmail.com>
Date: Mon, 27 Jul 2026 19:44:55 -0400
Subject: [PATCH 2/3] [clang-format] Addresses review feedback for pull request
on #210509 and adds a crash test
---
clang/lib/Format/TokenAnnotator.cpp | 4 ++--
clang/unittests/Format/FormatTest.cpp | 1 +
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index a77af4388adb2..7232a93eaf3f2 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2308,9 +2308,9 @@ class AnnotatingParser {
}
if (Previous->opensScope())
break;
- if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
+ if (!Previous->isTypeFinalized() && Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
Previous->isPointerOrReference() && Previous->Previous &&
- Previous->Previous->isNot(tok::equal) && !Previous->isTypeFinalized()) {
+ Previous->Previous->isNot(tok::equal)) {
Previous->setType(TT_PointerOrReference);
}
}
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index ec9ad612832f5..b72a683ac1fff 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -22562,6 +22562,7 @@ TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
verifyNoCrash("[[ [a] ]]");
verifyNoCrash(
"#xxxx??x<xxxxxxx||??x<xxxxxxx and xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
+ verifyNoCrash("a &alias & =");
}
TEST_F(FormatTest, FormatsTableGenCode) {
>From 34d456f61b7ee5ac3b566073486e4fc63cb1916a Mon Sep 17 00:00:00 2001
From: John Boncore <johnnyb2543 at gmail.com>
Date: Thu, 30 Jul 2026 14:44:53 -0400
Subject: [PATCH 3/3] Format TokenAnnotator.cpp with clang-format
---
clang/lib/Format/TokenAnnotator.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 7232a93eaf3f2..6240db231c1ee 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2308,7 +2308,8 @@ class AnnotatingParser {
}
if (Previous->opensScope())
break;
- if (!Previous->isTypeFinalized() && Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
+ if (!Previous->isTypeFinalized() &&
+ Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
Previous->isPointerOrReference() && Previous->Previous &&
Previous->Previous->isNot(tok::equal)) {
Previous->setType(TT_PointerOrReference);
More information about the cfe-commits
mailing list