llvm / llvm-project

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

clang-format: Generalize `BAS_AlwaysBreak` to all delimiter pairs #106456

Open dabrahams opened 2 weeks ago

dabrahams commented 2 weeks ago

For the most part clang-format uses endline layout (finding authoritative references for the term on the web is hard; many point to Code Complete by Steve McConnell; see page 16).

For C-like languages, the rule to avoid endline layout is simple:

On a given line, nothing other than a comment may follow an open delimiter ({, (, [, and sometimes <) whose matching close delimiter does not also fit on the same line.

Why does this matter? Kevlin Henney describes many of the problems with endline layout here, although he doesn't call the problematic styles by that name.

From reading the documentation, the BAS_AlwaysBreak option fixes part of the problem, but there should be a way to apply this rule to all opening delimiters.

dabrahams commented 2 weeks ago

Hmm, despite the fact that the documentation says it works for initializer lists, that doesn't seem to be the case. Also, AFAICT there's no way to get it to work for square brackets:

This is a result with ColumnLimit: 50 and AlignAfterOpenBracket: AlwaysBreak:

template <
    class SomethingLong, int x,
    class SomethingElse>
void a_long_function_name(
    int x, int y, std::string z,
    std::vector<SomethingLong> q) {
  int a[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1};
  return one_very_very_very_long_identifier
             [some_very_very_very_long_identifier] +
         1;
}

So I guess this is a feature request for a way to turn this on for square brackets, and a bug report about initializer lists. Let me know if you want me to file new issues.