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

clang-format 22 Regression: Adding empty '---' section to _clang-format resets some settings

Open Psycho900 opened this issue 9 months ago • 7 comments

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 )

Psycho900 avatar Feb 27 '26 21:02 Psycho900

@llvm/issue-subscribers-clang-format

Author: None (Psycho900)

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` )
</details>

llvmbot avatar Feb 27 '26 21:02 llvmbot

@mydeveloperday Could we get some help fixing this one?

tiagomacarios avatar Feb 28 '26 10:02 tiagomacarios

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.

galenelias avatar Apr 02 '26 18:04 galenelias

I'll try to look at this within the next few weeks.

gedare avatar Apr 08 '26 19:04 gedare

I also don't know the correct way to fix this. Probably it should try to detect the old enums.

gedare avatar Apr 15 '26 04:04 gedare

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.

galenelias avatar Apr 15 '26 15:04 galenelias

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.

Thanks, please see https://github.com/llvm/llvm-project/pull/192283

It fixes the minimal repro at least.

gedare avatar Apr 15 '26 16:04 gedare