[all-commits] [llvm/llvm-project] 5daa25: clang-format: support aligned nested conditionals ...

Typz via All-commits all-commits at lists.llvm.org
Wed Apr 22 08:37:57 PDT 2020


  Branch: refs/heads/master
  Home:   https://github.com/llvm/llvm-project
  Commit: 5daa25fd7a184524759b6ad065a8bd7e95aa149a
      https://github.com/llvm/llvm-project/commit/5daa25fd7a184524759b6ad065a8bd7e95aa149a
  Author: Francois Ferrand <thetypz at gmail.com>
  Date:   2020-04-22 (Wed, 22 Apr 2020)

  Changed paths:
    M clang/lib/Format/ContinuationIndenter.cpp
    M clang/lib/Format/ContinuationIndenter.h
    M clang/lib/Format/WhitespaceManager.cpp
    M clang/lib/Format/WhitespaceManager.h
    M clang/unittests/Format/FormatTest.cpp

  Log Message:
  -----------
  clang-format: support aligned nested conditionals formatting

When multiple ternary operators are chained, e.g. like an if/else-if/
else-if/.../else sequence, clang-format will keep aligning the colon
with the question mark, which increases the indent for each
conditionals:

  int a = condition1 ? result1
                     : condition2 ? result2
                                  : condition3 ? result3
                                               : result4;

This patch detects the situation (e.g. conditionals used in false branch
of another conditional), to avoid indenting in that case:

  int a = condition1   ? result1
          : condition2 ? result2
          : condition3 ? result3
                       : result4;

When BreakBeforeTernaryOperators is false, this will format like this:

  int a = condition1 ? result1 :
          condition2 ? result2 :
          conditino3 ? result3 :
                       result4;


  Commit: 3d61b1120e8267aa39f4c9a33d618dbaec4ec6fa
      https://github.com/llvm/llvm-project/commit/3d61b1120e8267aa39f4c9a33d618dbaec4ec6fa
  Author: Francois Ferrand <thetypz at gmail.com>
  Date:   2020-04-22 (Wed, 22 Apr 2020)

  Changed paths:
    M clang/docs/ClangFormatStyleOptions.rst
    M clang/include/clang/Format/Format.h
    M clang/lib/Format/ContinuationIndenter.cpp
    M clang/lib/Format/ContinuationIndenter.h
    M clang/lib/Format/Format.cpp
    M clang/unittests/Format/FormatTest.cpp
    M clang/unittests/Format/FormatTestJS.cpp

  Log Message:
  -----------
  clang-format: Introduce stricter AlignOperands flag

Summary:
Even when BreakBeforeBinaryOperators is set, AlignOperands kept
aligning the beginning of the line, even when it could align the
actual operands (e.g. after an assignment).

With this patch, there is an option to actually align the operands, so
that the operator gets right-aligned with the equal sign or return
operator:

  int aaaaa = bbbbbb
            + cccccc;
  return aaaaaaa
      && bbbbbbb;

This not happen in parentheses, to avoid 'breaking' the indentation:

  if (aaaaa
      && bbbbb)
    return;

Reviewers: krasimir, djasper

Subscribers: cfe-commits, klimek

Differential Revision: https://reviews.llvm.org/D32478


Compare: https://github.com/llvm/llvm-project/compare/d7ab9e7c9b30...3d61b1120e82


More information about the All-commits mailing list