[clang] [clang-format] Set Context.IsExpression for C++ ternary operators (PR #206300)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Jun 27 19:53:49 PDT 2026
https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/206300
>From 6f1f1b05a87e80cc2b8a8fb4b1a35a68e787dd3e Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Thu, 25 Jun 2026 03:49:04 -0700
Subject: [PATCH] [clang-format] Set Context.IsExpression for C++ ternary
operators
This reverts e99e343dfdb2b1b490789a7bcba6d646a4824f15 (#199112).
Fixes #199027
---
clang/lib/Format/TokenAnnotator.cpp | 10 ++--------
clang/unittests/Format/FormatTest.cpp | 2 +-
clang/unittests/Format/TokenAnnotatorTest.cpp | 9 +++++----
3 files changed, 8 insertions(+), 13 deletions(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 634143d843c3b..550beb96396a9 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2500,6 +2500,8 @@ class AnnotatingParser {
Current.setType(TT_Unknown);
} else {
Current.setType(TT_ConditionalExpr);
+ if (IsCpp)
+ Contexts.back().IsExpression = true;
}
} else if (Current.isBinaryOperator() &&
(!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
@@ -3040,14 +3042,6 @@ class AnnotatingParser {
if (Style.isCSharp() && Tok.is(tok::ampamp))
return TT_BinaryOperator;
- // The keyword `and` (tok::ampamp) is always binary, never a declarator.
- // Not extended to `bitand` (tok::amp), which can be a reference.
- if (Tok.is(tok::ampamp)) {
- const auto *Info = Tok.Tok.getIdentifierInfo();
- if (Info && Info->isCPlusPlusOperatorKeyword())
- return TT_BinaryOperator;
- }
-
if (Style.isVerilog()) {
// In Verilog, `*` can only be a binary operator. `&` can be either unary
// or binary. `*` also includes `*>` in module path declarations in
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index c42cc147cf21e..896d71181e2b3 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -22558,7 +22558,7 @@ TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
"LY52: ! [internal]");
verifyNoCrash("operator foo *;");
verifyNoCrash(
- " #xxxx??x<xxxxxxx||??x<xxxxxxx and xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
+ "#xxxx??x<xxxxxxx||??x<xxxxxxx and xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
}
TEST_F(FormatTest, FormatsTableGenCode) {
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index b6300e2ca78ba..64d481a3569ab 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -445,6 +445,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
ASSERT_EQ(Tokens.size(), 30u) << Tokens;
EXPECT_TOKEN(Tokens[18], tok::star, TT_PointerOrReference);
EXPECT_TOKEN(Tokens[19], tok::identifier, TT_StartOfName);
+
+ Tokens = annotate("foo ? a < b && c : bar");
+ ASSERT_EQ(Tokens.size(), 10u) << Tokens;
+ EXPECT_TOKEN(Tokens[5], tok::ampamp, TT_BinaryOperator);
+ EXPECT_TOKEN(Tokens[6], tok::identifier, TT_Unknown); // Not TT_StartOfName
}
TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {
@@ -4198,10 +4203,6 @@ TEST_F(TokenAnnotatorTest, CppAltOperatorKeywords) {
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
EXPECT_TOKEN(Tokens[3], tok::caretequal, TT_BinaryOperator);
- Tokens = annotate("if (a and b) {}");
- ASSERT_EQ(Tokens.size(), 9u) << Tokens;
- EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_BinaryOperator);
-
const auto StyleC = getLLVMStyle(FormatStyle::LK_C);
Tokens = annotate("xor = foo;", StyleC);
More information about the cfe-commits
mailing list