-
Notifications
You must be signed in to change notification settings - Fork 13.7k
[clang-format] Fix a bug that changes keyword or
to an identifier
#128410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-clang-format Author: Owen Pan (owenca) ChangesFixes #105482 Full diff: https://github.com/llvm/llvm-project/pull/128410.diff 4 Files Affected:
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index 29aba281ae103..02429970599c0 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -743,29 +743,6 @@ struct FormatToken {
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:
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 57ddd80382d88..16f19e955bf55 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -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:
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index fe0e47080a577..0012a456e6cff 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -18072,9 +18072,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);
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 8ada6c3daeaf6..1e1774ba5b3b5 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -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);
}
|
HazardyKnusperkeks
approved these changes
Feb 26, 2025
owenca
added a commit
to owenca/llvm-project
that referenced
this pull request
Feb 27, 2025
joaosaffran
pushed a commit
to joaosaffran/llvm-project
that referenced
this pull request
Mar 3, 2025
swift-ci
pushed a commit
to swiftlang/llvm-project
that referenced
this pull request
Mar 4, 2025
sabitov-kirill
added a commit
to sabitov-kirill/llvm-project
that referenced
this pull request
Apr 2, 2025
* workflows/release-binaries-all: Add missing secret input (llvm#126921) Since d194c6b this workflow was missing the secret input which was causing it to fail. (cherry picked from commit a684e0e) * workflows/release-binaries: Fix macos-14 build (llvm#127157) This was broken when pgo was enabled by 0572580. (cherry picked from commit d595d5a) * [clang-format] Fix a bug that changes keyword `or` to an identifier (llvm#128410) Backports ffc61dc 0968df9 2d585cc Fixes llvm#105482 * [clangd] Add clangd 20 release notes (llvm#127358) * Bump version to 20.1.0 (final) * Add dummy "SystemS" target * Add barebone SystemS backend initislisation, that can succsesfully perform`CodeGenTargetMachineImpl::initAsmInfo`. Commit includes simplest form of registers, instructions, subtarget descrption. Commit includes simple lit test to check how far we can get in performing end-to-end compilation pipeline. * Add functional backend, that can emit basical asm * Add different testing scenarious * Add dag selection to pass config
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #105482