llvm / llvm-project

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

`AlignOperands: DontAlign` mostly ignored for `<<` #67897

Open pcolby opened 1 year ago

pcolby commented 1 year ago

Hopefully I'm just misunderstanding something here, but I cannot get clang-format (at least v15, v16 and v17) to obey AlignOperands: DontAlign for << operators.

Probably related (though not exactly the same scenarios) as:

Consider this clang-format style:

---
Language: Cpp
AlignOperands: DontAlign
...

And this example source:

    // Incorrectly ignores `AlignOperands: DontAlign`
    qCWarning(lc).noquotex() << a << b << c << d << e << f << g << h << i << j << k << l;

    // Correctly (?) uses ContinuationIndentWidth.
    qCWarning(lc).noquotexx() << a << b << c << d << e << f << g << h << i << j << k << l;

    // Incorrectly ignores `AlignOperands: DontAlign`
    qCWarning(lc).noquotex() << aa << bb << cc << dd << ee << ff << gg << hh << ii << jj << kk;

    // Correctly (?) uses ContinuationIndentWidth.
    qCWarning(lc).noquotexx() << aa << bb << cc << dd << ee << ff << gg << hh << ii << jj << kk;

    // Incorrectly ignores `AlignOperands: DontAlign`
    qCWarning(lc).noquotex() << aaa << bbb << ccc << ddd << eee << fff << ggg << hhh << iii << jjj;

    // Correctly (?) uses ContinuationIndentWidth.
    qCWarning(lc).noquotexx() << aaa << bbb << ccc << ddd << eee << fff << ggg << hhh << iii << jjj;

    // Incorrectly ignores `AlignOperands: DontAlign`
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx << aaa << bbb << ccc << ddd << eee << fff << ggg << hhh << iii << jjj;

    // Correctly (?) uses ContinuationIndentWidth.
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx << aaa << bbb << ccc << ddd << eee << fff << ggg << hhh << iii << jjj;

    // Correctly (?) uses ContinuationIndentWidth.
    qCWarning(lc) + aaa + bbb + ccc + ddd + eee + fff + ggg + hhh + iii + jjj + kkk + lll;

    // Correctly (?) uses ContinuationIndentWidth.
    qCWarning(lc) >> aaa >> bbb >> ccc >> ddd >> eee >> fff >> ggg >> hhh >> iii >> jjj >> kkk;

The clang-formatted output is:

  // Incorrectly ignores `AlignOperands: DontAlign`
  qCWarning(lc).noquotex() << a << b << c << d << e << f << g << h << i << j
                           << k << l;

  // Correctly (?) uses ContinuationIndentWidth.
  qCWarning(lc).noquotexx()
      << a << b << c << d << e << f << g << h << i << j << k << l;

  // Incorrectly ignores `AlignOperands: DontAlign`
  qCWarning(lc).noquotex() << aa << bb << cc << dd << ee << ff << gg << hh << ii
                           << jj << kk;

  // Correctly (?) uses ContinuationIndentWidth.
  qCWarning(lc).noquotexx()
      << aa << bb << cc << dd << ee << ff << gg << hh << ii << jj << kk;

  // Incorrectly ignores `AlignOperands: DontAlign`
  qCWarning(lc).noquotex() << aaa << bbb << ccc << ddd << eee << fff << ggg
                           << hhh << iii << jjj;

  // Correctly (?) uses ContinuationIndentWidth.
  qCWarning(lc).noquotexx()
      << aaa << bbb << ccc << ddd << eee << fff << ggg << hhh << iii << jjj;

  // Incorrectly ignores `AlignOperands: DontAlign`
  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx << aaa << bbb << ccc << ddd << eee
                                             << fff << ggg << hhh << iii << jjj;

  // Correctly (?) uses ContinuationIndentWidth.
  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
      << aaa << bbb << ccc << ddd << eee << fff << ggg << hhh << iii << jjj;

  // Correctly (?) uses ContinuationIndentWidth.
  qCWarning(lc) + aaa + bbb + ccc + ddd + eee + fff + ggg + hhh + iii + jjj +
      kkk + lll;

  // Correctly (?) uses ContinuationIndentWidth.
  qCWarning(lc) >> aaa >> bbb >> ccc >> ddd >> eee >> fff >> ggg >> hhh >>
      iii >> jjj >> kkk;

As you can see (or as I inerpret it), there are plenty of cases where the << operators' operands are being wrapped, then aligned, despite the AlignOperands: DontAlign, but if the very first operand exceeds some (not consistent) length then the DontAlign is obeyed, and the ContinuationIndentWidth is used.

This appears to only affect << operators. For example, + and >> are not affected, and seems to work as I would expect based on how I read the documentation.

Also, although I use a (real-world) example of qCWarning(lc).noquote(), the same happens if the first terminal is just some plain (not long enough) variable name too (the x{42,43} examples above).

The output above is from clang-format 17.0.2, but I get the same results (or very similar) with v15 and v16 too (haven't tested any other versions).

Thanks 😄

llvmbot commented 1 year ago

@llvm/issue-subscribers-clang-format

Hopefully I'm just misunderstanding something here, but I cannot get clang-format (at least v15, v16 and v17) to obey `AlignOperands: DontAlign` for `<<` operators. Probably related (though not exactly the same scenarios) as: * https://github.com/llvm/llvm-project/issues/44363; and * https://github.com/llvm/llvm-project/issues/59797. Consider this clang-format style: ``` --- Language: Cpp AlignOperands: DontAlign ... ``` And this example source: ```cpp // Incorrectly ignores `AlignOperands: DontAlign` qCWarning(lc).noquotex() << a << b << c << d << e << f << g << h << i << j << k << l; // Correctly (?) uses ContinuationIndentWidth. qCWarning(lc).noquotexx() << a << b << c << d << e << f << g << h << i << j << k << l; // Incorrectly ignores `AlignOperands: DontAlign` qCWarning(lc).noquotex() << aa << bb << cc << dd << ee << ff << gg << hh << ii << jj << kk; // Correctly (?) uses ContinuationIndentWidth. qCWarning(lc).noquotexx() << aa << bb << cc << dd << ee << ff << gg << hh << ii << jj << kk; // Incorrectly ignores `AlignOperands: DontAlign` qCWarning(lc).noquotex() << aaa << bbb << ccc << ddd << eee << fff << ggg << hhh << iii << jjj; // Correctly (?) uses ContinuationIndentWidth. qCWarning(lc).noquotexx() << aaa << bbb << ccc << ddd << eee << fff << ggg << hhh << iii << jjj; // Incorrectly ignores `AlignOperands: DontAlign` xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx << aaa << bbb << ccc << ddd << eee << fff << ggg << hhh << iii << jjj; // Correctly (?) uses ContinuationIndentWidth. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx << aaa << bbb << ccc << ddd << eee << fff << ggg << hhh << iii << jjj; // Correctly (?) uses ContinuationIndentWidth. qCWarning(lc) + aaa + bbb + ccc + ddd + eee + fff + ggg + hhh + iii + jjj + kkk + lll; // Correctly (?) uses ContinuationIndentWidth. qCWarning(lc) >> aaa >> bbb >> ccc >> ddd >> eee >> fff >> ggg >> hhh >> iii >> jjj >> kkk; ``` The clang-formatted output is: ```cpp // Incorrectly ignores `AlignOperands: DontAlign` qCWarning(lc).noquotex() << a << b << c << d << e << f << g << h << i << j << k << l; // Correctly (?) uses ContinuationIndentWidth. qCWarning(lc).noquotexx() << a << b << c << d << e << f << g << h << i << j << k << l; // Incorrectly ignores `AlignOperands: DontAlign` qCWarning(lc).noquotex() << aa << bb << cc << dd << ee << ff << gg << hh << ii << jj << kk; // Correctly (?) uses ContinuationIndentWidth. qCWarning(lc).noquotexx() << aa << bb << cc << dd << ee << ff << gg << hh << ii << jj << kk; // Incorrectly ignores `AlignOperands: DontAlign` qCWarning(lc).noquotex() << aaa << bbb << ccc << ddd << eee << fff << ggg << hhh << iii << jjj; // Correctly (?) uses ContinuationIndentWidth. qCWarning(lc).noquotexx() << aaa << bbb << ccc << ddd << eee << fff << ggg << hhh << iii << jjj; // Incorrectly ignores `AlignOperands: DontAlign` xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx << aaa << bbb << ccc << ddd << eee << fff << ggg << hhh << iii << jjj; // Correctly (?) uses ContinuationIndentWidth. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx << aaa << bbb << ccc << ddd << eee << fff << ggg << hhh << iii << jjj; // Correctly (?) uses ContinuationIndentWidth. qCWarning(lc) + aaa + bbb + ccc + ddd + eee + fff + ggg + hhh + iii + jjj + kkk + lll; // Correctly (?) uses ContinuationIndentWidth. qCWarning(lc) >> aaa >> bbb >> ccc >> ddd >> eee >> fff >> ggg >> hhh >> iii >> jjj >> kkk; ``` As you can see (or as I inerpret it), there are plenty of cases where the `<<` operators' operands are being wrapped, then aligned, despite the `AlignOperands: DontAlign`, but if the very first operand exceeds some (not consistent) length then the `DontAlign` is obeyed, and the `ContinuationIndentWidth` is used. This appears to only affect `<<` operators. For example, `+` and `>>` are not affected, and seems to work as I would expect based on how I read the documentation. Also, although I use a (real-world) example of `qCWarning(lc).noquote()`, the same happens if the first terminal is just some plain (not long enough) variable name too (the `x{42,43}` examples above). The output above is from clang-format 17.0.2, but I get the same results (or very similar) with v15 and v16 too (haven't tested any other versions). Thanks 😄