Skip to content

[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
merged 1 commit into from
Feb 27, 2025

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Feb 23, 2025

Fixes #105482

@llvmbot
Copy link
Member

llvmbot commented Feb 23, 2025

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Fixes #105482


Full diff: https://github.com/llvm/llvm-project/pull/128410.diff

4 Files Affected:

  • (modified) clang/lib/Format/FormatToken.h (-23)
  • (modified) clang/lib/Format/UnwrappedLineParser.cpp (-6)
  • (modified) clang/unittests/Format/FormatTest.cpp (+4-2)
  • (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+9-2)
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);
 }

@owenca owenca merged commit 2d585cc into llvm:main Feb 27, 2025
13 checks passed
@owenca owenca deleted the 105482 branch February 27, 2025 02:14
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Clang-format (20): Incorrect behavior with BreakBinaryOperations
3 participants