Skip to content
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

[clang-format] Fix a bug in annotating * in #defines #99433

Merged
merged 3 commits into from
Jul 20, 2024
Merged

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Jul 18, 2024

Fixes #99271.

@llvmbot
Copy link
Collaborator

llvmbot commented Jul 18, 2024

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Fixes #99271.


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

2 Files Affected:

  • (modified) clang/lib/Format/TokenAnnotator.cpp (+2-1)
  • (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+4)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index b6d6e52ccb8f8..f1125fd35b427 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -374,7 +374,8 @@ class AnnotatingParser {
       Contexts.back().IsExpression = true;
     } else if (Line.InPPDirective &&
                (!OpeningParen.Previous ||
-                OpeningParen.Previous->isNot(tok::identifier))) {
+                OpeningParen.Previous->isNot(tok::identifier) ||
+                !OpeningParen.Previous->Previous)) {
       Contexts.back().IsExpression = true;
     } else if (Contexts[Contexts.size() - 2].CaretFound) {
       // This is the parameter list of an ObjC block.
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index c5e8aa72cd2cb..82c9be0f4df71 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -75,6 +75,10 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
   EXPECT_TOKEN(Tokens[10], tok::r_paren, TT_TypeDeclarationParen);
   EXPECT_TOKEN(Tokens[11], tok::star, TT_PointerOrReference);
 
+  Tokens = annotate("#define FOO bar(a * b)");
+  ASSERT_EQ(Tokens.size(), 10u) << Tokens;
+  EXPECT_TOKEN(Tokens[6], tok::star, TT_BinaryOperator);
+
   Tokens = annotate("void f() {\n"
                     "  while (p < a && *p == 'a')\n"
                     "    p++;\n"

@hnakamura5
Copy link
Contributor

This change certainly fixes the issue, but also seems specific for the reported code.
For example it does not work well for the case the line contains something else as,

#define FN(N) void fn(void *addr) { a + f(addr, 0, N * two); }

This results the annotations where * is PointerOrReference,

{(hash, "#" , Unknown), (identifier, "define" , Unknown), (identifier, "FN" , Unknown), (l_paren, "(" , Unknown),
(identifier, "N" , Unknown), (r_paren, ")" , Unknown), (void, "void" , Unknown), (identifier, "fn" , FunctionDeclarationName), 
(l_paren, "(" , FunctionDeclarationLParen), (void, "void" , Unknown), (star, "*" , PointerOrReference),
(identifier, "addr" , StartOfName), (r_paren, ")" , Unknown), (l_brace, "{" , FunctionLBrace)👈, (identifier, "a" , Unknown),
(plus, "+" , BinaryOperator), (identifier, "f" , Unknown), (l_paren, "(" , Unknown), (identifier, "addr" , Unknown),
(comma, "," , Unknown), (numeric_constant, "0" , Unknown), (comma, "," , Unknown), (identifier, "N" , Unknown),
(star, "*" , PointerOrReference) 👈, (identifier, "two" , StartOfName), (r_paren, ")" , Unknown), (semi, ";" , Unknown),
(r_brace, "}" , Unknown), (eof, "" , Unknown)}

How about judging whether the FunctionLBrace exists as the opener of the outer context?

@owenca
Copy link
Contributor Author

owenca commented Jul 19, 2024

How about judging whether the FunctionLBrace exists as the opener of the outer context?

That wouldn't work as we must also handle function calls at the top level when dealing with macro definitions.

@owenca owenca merged commit ce1a874 into llvm:main Jul 20, 2024
7 checks passed
@owenca owenca deleted the 99271 branch July 20, 2024 19:22
sgundapa pushed a commit to sgundapa/upstream_effort that referenced this pull request Jul 23, 2024
owenca added a commit that referenced this pull request Jul 25, 2024
llvmbot pushed a commit to llvmbot/llvm-project that referenced this pull request Jul 25, 2024
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
)"

Summary:
This reverts commit ce1a874.

Closes #100304.

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60250538
tru pushed a commit to llvmbot/llvm-project that referenced this pull request Jul 27, 2024
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] Cannot recognize multiplication semantic of *
4 participants