Skip to content

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Jan 26, 2024

Fixes #78965.

@llvmbot
Copy link
Member

llvmbot commented Jan 26, 2024

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Fixes #78965.


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

2 Files Affected:

  • (modified) clang/lib/Format/TokenAnnotator.cpp (+2)
  • (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+5)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 25fcceb87864379..df1c5bc19de1e84 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2488,6 +2488,8 @@ class AnnotatingParser {
         (Tok.Next->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
       return false;
     }
+    if (Line.InPPDirective && Tok.Next->is(tok::minus))
+      return false;
     // Search for unexpected tokens.
     for (FormatToken *Prev = Tok.Previous; Prev != Tok.MatchingParen;
          Prev = Prev->Previous) {
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 3dbf504c35ed55e..67678c18963b1f3 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -611,6 +611,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsCasts) {
   EXPECT_TOKEN(Tokens[13], tok::r_paren, TT_Unknown);
   EXPECT_TOKEN(Tokens[14], tok::star, TT_BinaryOperator);
 
+  Tokens = annotate("#define foo(i) ((i) - bar)");
+  ASSERT_EQ(Tokens.size(), 14u) << Tokens;
+  EXPECT_TOKEN(Tokens[9], tok::r_paren, TT_Unknown);
+  EXPECT_TOKEN(Tokens[10], tok::minus, TT_BinaryOperator);
+
   Tokens = annotate("return (Foo) & 10;");
   ASSERT_EQ(Tokens.size(), 8u) << Tokens;
   EXPECT_TOKEN(Tokens[3], tok::r_paren, TT_Unknown);

@owenca owenca merged commit f826f55 into llvm:main Jan 27, 2024
@owenca owenca deleted the 78965 branch January 27, 2024 02:32
@prj-
Copy link

prj- commented Feb 4, 2024

@owenca, @HazardyKnusperkeks, I'm sorry I'm not very familiar with LLVM release cycle, but couldn't this be back-ported for version 18.1.0rc2? I see this fix only in Debian clang-format version 19.0.0 (++20240203111505+d9850fe23cc6-1~exp1~20240203111625.1866), not Debian clang-format version 18.1.0 (++20240203063053+42b7061f1f0a-1~exp1~20240203183208.28)

@owenca owenca added this to the LLVM 18.X Release milestone Feb 4, 2024
@owenca
Copy link
Contributor Author

owenca commented Feb 4, 2024

/cherry-pick f826f55

llvmbot pushed a commit to llvmbot/llvm-project that referenced this pull request Feb 4, 2024
@llvmbot
Copy link
Member

llvmbot commented Feb 4, 2024

/pull-request #80591

@owenca owenca assigned owenca and unassigned owenca Feb 4, 2024
@owenca owenca removed this from the LLVM 18.X Release milestone Feb 4, 2024
llvmbot pushed a commit to llvmbot/llvm-project that referenced this pull request Feb 4, 2024
llvmbot pushed a commit to llvmbot/llvm-project that referenced this pull request Feb 7, 2024
tstellar pushed a commit to tstellar/llvm-project that referenced this pull request Feb 14, 2024
tstellar pushed a commit to tstellar/llvm-project that referenced this pull request Feb 14, 2024
tstellar pushed a commit to tstellar/llvm-project that referenced this pull request Feb 14, 2024
tstellar pushed a commit to tstellar/llvm-project that referenced this pull request Feb 14, 2024
@pointhex pointhex mentioned this pull request May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Needs Triage
Development

Successfully merging this pull request may close these issues.

[clang-format] Incorrectly formatted macro
4 participants