llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Clang-format 14] `WhitespaceSensitiveMacros` setting is broken

Open adalisk-emikhaylov opened this issue 2 years ago • 8 comments

WhitespaceSensitiveMacros used to completely disable formatting for the specified macros. Starting from Clang-format 14 it no longer does that. It still does something, but I'm not sure what exactly.

Here's a code sample: (C++)

#define FOO(x)

FOO(1+2)

.clang-format:

WhitespaceSensitiveMacros:
    - FOO

Code after formatting:

#define FOO(x)

FOO(1 + 2)

As you can see, formatting introduced a whitespace change despite the setting.

In our codebase, we also used this setting to disable formatting for long multiline macros calls that are not whitespace-sensitive but are uglified by Clang-format. Now we'd need to wrap all of them in // clang-format off/on.

adalisk-emikhaylov avatar Mar 24 '22 08:03 adalisk-emikhaylov

@llvm/issue-subscribers-clang-format

llvmbot avatar Mar 24 '22 14:03 llvmbot

We are also hitting this bug in clang-format 14 and I managed to identify the commit that introduces this regression via git bisect.

FYI @ksyx

sfc-gh-mheimel avatar Apr 11 '22 22:04 sfc-gh-mheimel

We are also hitting this bug in clang-format 14 and I managed to identify the commit that introduces this regression via git bisect.

FYI @ksyx

Thanks for reporting! It seems the parser had changed T=UntouchableMacroFunc to T=FunctionLikeOrFreestandingMacro in this part of change. Adding a check before setting type (either check T!=UntouchableMacroFunc or T==Unknown, and I only see this type as Untouchable in the type list) do helps, and I am wondering whether it is a good solution.

cc @mkurdej @mydeveloperday

ksyx avatar Apr 11 '22 22:04 ksyx

We are also hitting this bug in clang-format 14 and I managed to identify the commit that introduces this regression via git bisect. FYI @ksyx

Thanks for reporting! It seems the parser had changed T=UntouchableMacroFunc to T=FunctionLikeOrFreestandingMacro in this part of change. Adding a check before setting type (either check T!=UntouchableMacroFunc or T==Unknown, and I only see this type as Untouchable in the type list) do helps, and I am wondering whether it is a good solution.

cc @mkurdej @mydeveloperday

Another observation: If I add a semicolon after the macro call, things work as expected. In other words, the following won't be reformatted with WhiteSpaceSensitiveMacros set to FOO:

#define FOO(x)

FOO(1+2);

Interestingly, all tests for the WhiteSpaceSensitiveFormat setting in clang/unittests/Format/FormatTest.cpp use macro calls with semicolons, which is probably why this wasn't caught.

sfc-gh-mheimel avatar Apr 12 '22 09:04 sfc-gh-mheimel

@llvm/issue-subscribers-bug

llvmbot avatar Apr 13 '22 12:04 llvmbot

Revision URI: https://reviews.llvm.org/D123676

@ksyx, thanks for the analysis!

mkurdej avatar Apr 13 '22 12:04 mkurdej

Reverted by 573a5b5.

owenca avatar Aug 16 '22 19:08 owenca

Related to #57158.

owenca avatar Aug 16 '22 19:08 owenca

The issue still exists in clang-format 15. Would be cool if the fix could get merged.

martin-eder-zeiss avatar Nov 16 '22 15:11 martin-eder-zeiss

Unfortunately we are past the last 15.0.x release.

owenca avatar Nov 16 '22 20:11 owenca

It would also be fine if this was fixed only in 16.0.0. Would that be feasible?

martin-eder-zeiss avatar Nov 17 '22 09:11 martin-eder-zeiss

Yep! It’s in the main branch and will be included by default when the 16.x branch is created.

owenca avatar Nov 17 '22 20:11 owenca