diff options
author | Daniel Jasper <djasper@google.com> | 2013-07-03 10:34:47 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2013-07-03 10:34:47 +0000 |
commit | 7ae41cdd2264a8785ea53a997cd0bbb5b881e570 (patch) | |
tree | e42ccaa71f023a1df16d24b6dce2d379964332e7 /clang/unittests | |
parent | ed1fab6b5bfb54d63102319a761011e985081cb4 (diff) | |
download | bcm5719-llvm-7ae41cdd2264a8785ea53a997cd0bbb5b881e570.tar.gz bcm5719-llvm-7ae41cdd2264a8785ea53a997cd0bbb5b881e570.zip |
Don't insert confusing line breaks in comparisons.
In general, clang-format breaks after an operator if the LHS spans
multiple lines. Otherwise, this can lead to confusing effects and
effectively hide the operator precendence, e.g. in
if (aaaaaaaaaaaaaa ==
bbbbbbbbbbbbbb && c) { ...
This patch removes this rule for comparisons, if the LHS is not a binary
expression itself as many users were wondering why clang-format inserts
an unnecessary linebreak.
Before:
if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) >
5) { ...
After:
if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) { ...
In the long run, we might:
- Want to do this for other binary expressions as well.
- Do this only if the RHS is short or even only if it is a literal.
llvm-svn: 185530
Diffstat (limited to 'clang/unittests')
-rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index c5a02b614d3..324f797e6da 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -2156,6 +2156,38 @@ TEST_F(FormatTest, LineBreakingInBinaryExpressions) { " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" " cccccc) {\n}"); + // If the LHS of a comparison is not a binary expression itself, the + // additional linebreak confuses many people. + verifyFormat( + "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" + "}"); + verifyFormat( + "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" + "}"); + // Even explicit parentheses stress the precedence enough to make the + // additional break unnecessary. + verifyFormat( + "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" + "}"); + // This cases is borderline, but with the indentation it is still readable. + verifyFormat( + "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" + "}", + getLLVMStyleWithColumns(75)); + + // If the LHS is a binary expression, we should still use the additional break + // as otherwise the formatting hides the operator precedence. + verifyFormat( + "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" + " 5) {\n" + "}"); + FormatStyle OnePerLine = getLLVMStyle(); OnePerLine.BinPackParameters = false; verifyFormat( |