llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.39k stars 11.73k forks source link

[clang-format] Invalid formatting of attribute applied through macro to lambda call operator with arguments #92661

Open mattco98 opened 4 months ago

mattco98 commented 4 months ago

I would expect the following code to be untouched:

#define ATTR [[clang::annotate("attr")]]
void foo()
{
    auto lambda = [] ATTR (int &arg) {};
}

However, the file gets formatted as:

#define ATTR [[clang::annotate("attr")]]
void foo() {
  auto lambda = [] ATTR(int &arg) {};
}

Lambdas without parameter lists seems to be formatted correctly, though that's not surprising as that was a valid position for attributes before C++23.

Tested on the latest commit (e90126e0dd6f058e1602bb6c769dfa4c52d7ad47)

nico commented 4 months ago

Almost certainly a dupe of #92657.

mattco98 commented 4 months ago

Almost certainly a dupe of https://github.com/llvm/llvm-project/issues/92657.

Maybe, but if you replace the macros in the example with __attribute__((whatever)), it formats as expected.

e: I might have been unclear; this is specifically in regards to macros. The example is also formatted correctly if you replace the macros with [[...]]

nico commented 4 months ago

clang-format works on a lexer level. It doesn't do preprocessing. It does not know about macros. If you put a -> after the __attribute__ (as in the other issue), that doesn't get formatted either. I'm guessing that it looks like the attribute works because it treats that as the name of the lambda and then parens of the attribute as the parameter list.

I'm guessing that this needs a call to handleAttributes() (or similar) somewhere in tryToParseLambda() when it's done parsing the capture.

nico commented 4 months ago

Oh sorry, didn't see that you have a patch out for this already!