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

Typz via All-commits all-commits at lists.llvm.org
Fri May 15 07:38:51 PDT 2020


  Branch: refs/heads/master
  Home:   https://github.com/llvm/llvm-project
  Commit: 4db94094b469b4715d08ef37f1799bf3ea7ca8ea
      https://github.com/llvm/llvm-project/commit/4db94094b469b4715d08ef37f1799bf3ea7ca8ea
  Author: Francois Ferrand <thetypz at gmail.com>
  Date:   2020-05-15 (Fri, 15 May 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

Summary:
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;

This formatting style is referenced here:
https://www.fluentcpp.com/2018/02/27/replace-else-if-ternary-operator/
and here:
https://marcmutz.wordpress.com/2010/10/14/top-5-reasons-you-should-love-your-ternary-operator/

Reviewers: krasimir, djasper, klimek, MyDeveloperDay

Reviewed By: MyDeveloperDay

Subscribers: hokein, dyung, MyDeveloperDay, acoomans, cfe-commits

Tags: #clang, #clang-format

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




More information about the All-commits mailing list