-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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
base: main
Are you sure you want to change the base?
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be 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 If you have received no comments on your PR for a week, you can request a review 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. |
@llvm/pr-subscribers-clang-format Author: Matthew Olsson (mattco98) ChangesFixes #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:
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;
|
@nico pointed out that calling |
There was a problem hiding this 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
Fixes #92661.
Also fixes the same problem applied to function-style macros, which I didn't consider when creating that issue.