clang-format 22 Regression: Adding empty '---' section to _clang-format resets some settings
Minimal Repro Steps: Step 1) Create a _clang-format file with these contents:
---
AlignAfterOpenBracket: false
# --- # Uncomment these 2 lines to repro
# Language: Cpp
Step 2) Use this to re-format this C++ file:
void MyMethod(int whyDoesThisChangeLines) noexcept {
DoThing(whyDoesThisChangeLines,
"Adding 2nd '---' section in _clang-format moves whyDoesThisChangeLines");
}
Step 3) Observe that the resulting format has DoThing(whyDoesThisChangeLines, all on the same line.
Step 4) Uncomment the 2 lines in the _clang-format file:
---
AlignAfterOpenBracket: false
--- # Uncomment these 2 lines to repro
Language: Cpp
Step 5) Re-format the C++ file
BUG! [EXPECTED]: The resulting file should still look like...
void MyMethod(int whyDoesThisChangeLines) noexcept {
DoThing(whyDoesThisChangeLines,
"Adding 2nd '---' section in _clang-format moves whyDoesThisChangeLines");
}
because our new "Language: Cpp" section doesn't add or overwrite any _clang-format options, so that section should not be producing a different output.
[ACTUAL]: The resulting file looks like this now:
void MyMethod(int whyDoesThisChangeLines) noexcept {
DoThing(
whyDoesThisChangeLines,
"Adding 2nd '---' section in _clang-format moves whyDoesThisChangeLines");
}
Notice that DoThing( and whyDoesThisChangeLines, are now on 2 different lines!
This is a recent regression in clang-format version 22 (It did not repro in version 21, even if you used the previously allowed values for AlignAfterOpenBracket )
@llvm/issue-subscribers-clang-format
Author: None (Psycho900)
--- # Uncomment these 2 lines to repro
Language: Cpp
Step 2) Use this to re-format this C++ file:
void MyMethod(int whyDoesThisChangeLines) noexcept { DoThing(whyDoesThisChangeLines, "Adding 2nd '---' section in _clang-format moves whyDoesThisChangeLines"); }
Step 3) Observe that the resulting format has `DoThing(whyDoesThisChangeLines,` all on the same line.
Step 4) Uncomment the 2 lines in the _clang-format file:
AlignAfterOpenBracket: false
--- # Uncomment these 2 lines to repro Language: Cpp
Step 5) Re-format the C++ file
BUG!
[EXPECTED]: The resulting file should still look like...
void MyMethod(int whyDoesThisChangeLines) noexcept { DoThing(whyDoesThisChangeLines, "Adding 2nd '---' section in _clang-format moves whyDoesThisChangeLines"); }
because our new "Language: Cpp" section doesn't add or overwrite any _clang-format options, so that section should not be producing a different output.
[ACTUAL]: The resulting file looks like this now:
void MyMethod(int whyDoesThisChangeLines) noexcept { DoThing( whyDoesThisChangeLines, "Adding 2nd '---' section in _clang-format moves whyDoesThisChangeLines"); }
Notice that `DoThing(` and `whyDoesThisChangeLines,` are now on 2 different lines!
This is a recent regression in clang-format version 22 (It did not repro in version 21, even if you used the previously allowed values for `AlignAfterOpenBracket` )
</details>
@mydeveloperday Could we get some help fixing this one?
This is a regression from the 'backwards compatibility' part of @gedare 's https://github.com/llvm/llvm-project/pull/108332.
The logic in MappingTraits<FormatStyle>::mapping will always set Style.AlignAfterOpenBracket to a value for every processed block, regardless of whether it is explicitly set. I'm not familiar enough with the style inheritance code, but this seems overly aggressive for 'backwards compatibility' style behavior. Not sure if the right fix is to detect whether we're in a 'top level' style, or just only trigger the backwards compatibility path if an 'AlignAfterOpenBracket' with old enums is present, but avoid it if there is no explicit value for AlignAfterOpenBracket.
I'll try to look at this within the next few weeks.
I also don't know the correct way to fix this. Probably it should try to detect the old enums.
Most of the issues we are seeing are when the derived blocks aren't specifying any AlignAfterOpenBracket behavior, but all the settings get reset regardless because the code does BracketAlignmentStyle LocalBAS = BAS_Align. If LocalBAS was an optional with no default, and we only triggered that code if it was explicitly declared in the particular settings block we're processing, I assume that would be mostly sufficient.
The big thing I wasn't sure about with that approach is what to IsGoogleOrChromium and BasedOnStyle webkit checks. They are probably fine as I assume those probably only appear in the root configuration blocks where we aren't worried about accidentally overwriting some inherited settings.
Most of the issues we are seeing are when the derived blocks aren't specifying any AlignAfterOpenBracket behavior, but all the settings get reset regardless because the code does
BracketAlignmentStyle LocalBAS = BAS_Align. IfLocalBASwas an optional with no default, and we only triggered that code if it was explicitly declared in the particular settings block we're processing, I assume that would be mostly sufficient.The big thing I wasn't sure about with that approach is what to
IsGoogleOrChromiumand BasedOnStyle webkit checks. They are probably fine as I assume those probably only appear in the root configuration blocks where we aren't worried about accidentally overwriting some inherited settings.
Thanks, please see https://github.com/llvm/llvm-project/pull/192283
It fixes the minimal repro at least.