[clang-format] Fix a bug that changes keyword or to an identifier (#128410)

Fixes #105482
This commit is contained in:
Owen Pan
2025-02-26 18:14:26 -08:00
committed by GitHub
parent c8f70d7286
commit 2d585ccecc
4 changed files with 13 additions and 33 deletions

View File

@@ -743,29 +743,6 @@ public:
return isOneOf(tok::star, tok::amp, tok::ampamp);
}
bool isCppAlternativeOperatorKeyword() const {
assert(!TokenText.empty());
if (!isalpha(TokenText[0]))
return false;
switch (Tok.getKind()) {
case tok::ampamp:
case tok::ampequal:
case tok::amp:
case tok::pipe:
case tok::tilde:
case tok::exclaim:
case tok::exclaimequal:
case tok::pipepipe:
case tok::pipeequal:
case tok::caret:
case tok::caretequal:
return true;
default:
return false;
}
}
bool isUnaryOperator() const {
switch (Tok.getKind()) {
case tok::plus:

View File

@@ -1712,12 +1712,6 @@ void UnwrappedLineParser::parseStructuralElement(
OpeningBrace && OpeningBrace->isOneOf(TT_RequiresExpressionLBrace,
TT_CompoundRequirementLBrace);
!eof();) {
if (IsCpp && FormatTok->isCppAlternativeOperatorKeyword()) {
if (auto *Next = Tokens->peekNextToken(/*SkipComment=*/true);
Next && Next->isBinaryOperator()) {
FormatTok->Tok.setKind(tok::identifier);
}
}
const FormatToken *Previous = FormatTok->Previous;
switch (FormatTok->Tok.getKind()) {
case tok::at:

View File

@@ -18075,9 +18075,11 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
verifyFormat("int a = 5;");
verifyFormat("a += 42;");
verifyFormat("a or_eq 8;");
verifyFormat("xor = foo;");
FormatStyle Spaces = getLLVMStyle();
auto Spaces = getLLVMStyle(FormatStyle::LK_C);
verifyFormat("xor = foo;", Spaces);
Spaces.Language = FormatStyle::LK_Cpp;
Spaces.SpaceBeforeAssignmentOperators = false;
verifyFormat("int a= 5;", Spaces);
verifyFormat("a+= 42;", Spaces);

View File

@@ -3677,6 +3677,11 @@ TEST_F(TokenAnnotatorTest, CppAltOperatorKeywords) {
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
EXPECT_TOKEN(Tokens[3], tok::pipepipe, TT_BinaryOperator);
Tokens = annotate("return segment < *this or *this < segment;");
ASSERT_EQ(Tokens.size(), 12u) << Tokens;
EXPECT_TOKEN(Tokens[5], tok::pipepipe, TT_BinaryOperator);
EXPECT_TOKEN(Tokens[6], tok::star, TT_UnaryOperator);
Tokens = annotate("a = b or_eq c;");
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
EXPECT_TOKEN(Tokens[3], tok::pipeequal, TT_BinaryOperator);
@@ -3689,11 +3694,13 @@ TEST_F(TokenAnnotatorTest, CppAltOperatorKeywords) {
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
EXPECT_TOKEN(Tokens[3], tok::caretequal, TT_BinaryOperator);
Tokens = annotate("xor = foo;");
const auto StyleC = getLLVMStyle(FormatStyle::LK_C);
Tokens = annotate("xor = foo;", StyleC);
ASSERT_EQ(Tokens.size(), 5u) << Tokens;
EXPECT_TOKEN(Tokens[0], tok::identifier, TT_Unknown);
Tokens = annotate("int xor = foo;");
Tokens = annotate("int xor = foo;", StyleC);
ASSERT_EQ(Tokens.size(), 6u) << Tokens;
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_StartOfName);
}