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 bad spacing of macro after lambda introducer #92673

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mattco98
Copy link

Fixes #92661.

Also fixes the same problem applied to function-style macros, which I didn't consider when creating that issue.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Collaborator

llvmbot commented May 18, 2024

@llvm/pr-subscribers-clang-format

Author: Matthew Olsson (mattco98)

Changes

Fixes #92661.

Also fixes the same problem applied to function-style macros, which I didn't consider when creating that issue.


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

2 Files Affected:

  • (modified) clang/lib/Format/TokenAnnotator.cpp (+24)
  • (modified) clang/unittests/Format/FormatTest.cpp (+9)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 7c4c76a91f2c5..5f0040ec22d8b 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -76,6 +76,13 @@ static bool isLambdaParameterList(const FormatToken *Left) {
          Left->Previous->MatchingParen->is(TT_LambdaLSquare);
 }
 
+/// Returns \c true if the token is the right square bracket of a lambda
+/// introducer.
+static bool isLambdaRightIntroducerBracket(const FormatToken &Tok) {
+  return Tok.is(tok::r_square) && Tok.MatchingParen &&
+    Tok.MatchingParen->is(TT_LambdaLSquare);
+}
+
 /// Returns \c true if the token is followed by a boolean condition, \c false
 /// otherwise.
 static bool isKeywordWithCondition(const FormatToken &Tok) {
@@ -4646,6 +4653,23 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
         (Left.is(tok::r_square) && Left.is(TT_AttributeSquare))) {
       return true;
     }
+    if (Left.is(tok::identifier) && Left.Previous &&
+        isLambdaRightIntroducerBracket(*Left.Previous)) {
+      // Check if Right is part of a macro call
+      if (Right.MatchingParen && Right.MatchingParen->Next &&
+          Right.MatchingParen->Next->is(tok::l_paren)) {
+        return false;
+      }
+      return true;
+    }
+    if (Left.is(tok::r_paren) && Left.MatchingParen &&
+        Left.MatchingParen->Previous) {
+      auto const *LeftParenPrev = Left.MatchingParen->Previous;
+      if (LeftParenPrev->is(tok::identifier) && LeftParenPrev->Previous &&
+          isLambdaRightIntroducerBracket(*LeftParenPrev->Previous)) {
+        return true;
+      }
+    }
     if (Left.is(TT_ForEachMacro)) {
       return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
              spaceRequiredBeforeParens(Right);
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 6f57f10e12e88..6efcadd1310f1 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -16639,6 +16639,9 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
   verifyFormat("T A::operator()();", NoSpace);
   verifyFormat("X A::operator++(T);", NoSpace);
   verifyFormat("auto lambda = []() { return 0; };", NoSpace);
+  verifyFormat("auto lambda = [] [[attr]] () { return 0; };", NoSpace);
+  verifyFormat("auto lambda = [] MACRO_ATTR () { return 0; };", NoSpace);
+  verifyFormat("auto lambda = [] FN_MACRO_ATTR(a, (b + c)) () { return 0; };", NoSpace);
   verifyFormat("#if (foo || bar) && baz\n"
                "#elif ((a || b) && c) || d\n"
                "#endif",
@@ -16696,6 +16699,9 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
   verifyFormat("T A::operator() ();", Space);
   verifyFormat("X A::operator++ (T);", Space);
   verifyFormat("auto lambda = [] () { return 0; };", Space);
+  verifyFormat("auto lambda = [] [[attr]] () { return 0; };", Space);
+  verifyFormat("auto lambda = [] MACRO_ATTR () { return 0; };", Space);
+  verifyFormat("auto lambda = [] FN_MACRO_ATTR(a, (b + c)) () { return 0; };", Space);
   verifyFormat("int x = int (y);", Space);
   verifyFormat("#define F(...) __VA_OPT__ (__VA_ARGS__)", Space);
   verifyFormat("__builtin_LINE ()", Space);
@@ -16756,6 +16762,9 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
   verifyFormat("X A::operator++ (T);", SomeSpace);
   verifyFormat("int x = int (y);", SomeSpace);
   verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
+  verifyFormat("auto lambda = [] [[attr]] () { return 0; };", SomeSpace);
+  verifyFormat("auto lambda = [] MACRO_ATTR () { return 0; };", SomeSpace);
+  verifyFormat("auto lambda = [] FN_MACRO_ATTR(a, (b + c)) () { return 0; };", SomeSpace);
 
   FormatStyle SpaceControlStatements = getLLVMStyle();
   SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom;

@mattco98
Copy link
Author

@nico pointed out that calling handleAttributes() in tryToParseLambda() might be a better way to fix this, and I might be able to fix his other attribute issue while I'm at it. Going to draft this for now.

@mattco98 mattco98 marked this pull request as draft May 19, 2024 21:12
Copy link
Contributor

@mydeveloperday mydeveloperday left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an annotator test

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] Invalid formatting of attribute applied through macro to lambda call operator with arguments
3 participants