Skip to content

Commit 2d585cc

Browse files
authored
[clang-format] Fix a bug that changes keyword or to an identifier (llvm#128410)
Fixes llvm#105482
1 parent c8f70d7 commit 2d585cc

File tree

4 files changed

+13
-33
lines changed

4 files changed

+13
-33
lines changed

clang/lib/Format/FormatToken.h

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -743,29 +743,6 @@ struct FormatToken {
743743
return isOneOf(tok::star, tok::amp, tok::ampamp);
744744
}
745745

746-
bool isCppAlternativeOperatorKeyword() const {
747-
assert(!TokenText.empty());
748-
if (!isalpha(TokenText[0]))
749-
return false;
750-
751-
switch (Tok.getKind()) {
752-
case tok::ampamp:
753-
case tok::ampequal:
754-
case tok::amp:
755-
case tok::pipe:
756-
case tok::tilde:
757-
case tok::exclaim:
758-
case tok::exclaimequal:
759-
case tok::pipepipe:
760-
case tok::pipeequal:
761-
case tok::caret:
762-
case tok::caretequal:
763-
return true;
764-
default:
765-
return false;
766-
}
767-
}
768-
769746
bool isUnaryOperator() const {
770747
switch (Tok.getKind()) {
771748
case tok::plus:

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,12 +1712,6 @@ void UnwrappedLineParser::parseStructuralElement(
17121712
OpeningBrace && OpeningBrace->isOneOf(TT_RequiresExpressionLBrace,
17131713
TT_CompoundRequirementLBrace);
17141714
!eof();) {
1715-
if (IsCpp && FormatTok->isCppAlternativeOperatorKeyword()) {
1716-
if (auto *Next = Tokens->peekNextToken(/*SkipComment=*/true);
1717-
Next && Next->isBinaryOperator()) {
1718-
FormatTok->Tok.setKind(tok::identifier);
1719-
}
1720-
}
17211715
const FormatToken *Previous = FormatTok->Previous;
17221716
switch (FormatTok->Tok.getKind()) {
17231717
case tok::at:

clang/unittests/Format/FormatTest.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18075,9 +18075,11 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
1807518075
verifyFormat("int a = 5;");
1807618076
verifyFormat("a += 42;");
1807718077
verifyFormat("a or_eq 8;");
18078-
verifyFormat("xor = foo;");
1807918078

18080-
FormatStyle Spaces = getLLVMStyle();
18079+
auto Spaces = getLLVMStyle(FormatStyle::LK_C);
18080+
verifyFormat("xor = foo;", Spaces);
18081+
18082+
Spaces.Language = FormatStyle::LK_Cpp;
1808118083
Spaces.SpaceBeforeAssignmentOperators = false;
1808218084
verifyFormat("int a= 5;", Spaces);
1808318085
verifyFormat("a+= 42;", Spaces);

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3677,6 +3677,11 @@ TEST_F(TokenAnnotatorTest, CppAltOperatorKeywords) {
36773677
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
36783678
EXPECT_TOKEN(Tokens[3], tok::pipepipe, TT_BinaryOperator);
36793679

3680+
Tokens = annotate("return segment < *this or *this < segment;");
3681+
ASSERT_EQ(Tokens.size(), 12u) << Tokens;
3682+
EXPECT_TOKEN(Tokens[5], tok::pipepipe, TT_BinaryOperator);
3683+
EXPECT_TOKEN(Tokens[6], tok::star, TT_UnaryOperator);
3684+
36803685
Tokens = annotate("a = b or_eq c;");
36813686
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
36823687
EXPECT_TOKEN(Tokens[3], tok::pipeequal, TT_BinaryOperator);
@@ -3689,11 +3694,13 @@ TEST_F(TokenAnnotatorTest, CppAltOperatorKeywords) {
36893694
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
36903695
EXPECT_TOKEN(Tokens[3], tok::caretequal, TT_BinaryOperator);
36913696

3692-
Tokens = annotate("xor = foo;");
3697+
const auto StyleC = getLLVMStyle(FormatStyle::LK_C);
3698+
3699+
Tokens = annotate("xor = foo;", StyleC);
36933700
ASSERT_EQ(Tokens.size(), 5u) << Tokens;
36943701
EXPECT_TOKEN(Tokens[0], tok::identifier, TT_Unknown);
36953702

3696-
Tokens = annotate("int xor = foo;");
3703+
Tokens = annotate("int xor = foo;", StyleC);
36973704
ASSERT_EQ(Tokens.size(), 6u) << Tokens;
36983705
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_StartOfName);
36993706
}

0 commit comments

Comments
 (0)