[clang] c03e4bd - [clang-format] Set Context.IsExpression for C++ ternary operators (#206300)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Jun 28 09:55:36 PDT 2026
Author: owenca
Date: 2026-06-28T09:55:31-07:00
New Revision: c03e4bd358920eae86370c60d8bb0f2ef478cf0d
URL: https://github.com/llvm/llvm-project/commit/c03e4bd358920eae86370c60d8bb0f2ef478cf0d
DIFF: https://github.com/llvm/llvm-project/commit/c03e4bd358920eae86370c60d8bb0f2ef478cf0d.diff
LOG: [clang-format] Set Context.IsExpression for C++ ternary operators (#206300)
This reverts e99e343dfdb2b1b490789a7bcba6d646a4824f15 (#199112).
Fixes #199027
Added:
Modified:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 997203aeb1526..4a771a4a92a43 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)) &&
@@ -3042,14 +3044,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 78ddb27e61e95..bab36c17aab1e 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 8250af8af6942..25ad923af8f4a 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) {
@@ -4220,10 +4225,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