[cfe-dev] clang-format chromium ternary operator

Daniel Jasper djasper at google.com
Sun Aug 11 23:58:15 PDT 2013


This was decided to be the best way to go forward with Google's C++ style.
I am reasonably certain that the style guide does not contain anything that
says otherwise. The section you quote:
a) applies to boolean expressions, which a conditional expression is not.
b) explicitly says that you can also wrap them to the new line.

If you find anything else in the style guide, please let me know, I am
happy to get that changed.

As for "This is not done in Chromium":
https://code.google.com/p/chromium/codesearch#search/&q=%5E%5C%20*%5C?&all=1&sq=package:chromium&type=cs
and
https://code.google.com/p/chromium/codesearch#search/&q=(?m:%5C?.*%5Cn%5Cs*%5C:)%20pcre:yes&all=1&sq=package:chromium&type=cs

I am aware that the other way of formatting is ~2:1 more common in Chromium
right now, but there already is quite a bit of diversity.

The basic reasoning for going with the slightly less popular style is this
very basic example:

aaaaa ? bbbbb :
        ccccc;

aaaaa ? bbbbb
      : ccccc;

Here, the second formatting looks more structured and is easier to grasp at
first sight. If the operands get more complex, this gets even more obvious.
And for consistency with that formatting, ? and : should both be wrapped to
the new line where necessary.

Cheers,
Daniel


On Sat, Aug 10, 2013 at 9:06 PM, Thiago Farina <tfransosi at gmail.com> wrote:

> Hi,
>
> Looks like clang-format for Chromium style is incorrectly formatting
> ternary operators (?:)
>
> By Google C++ Style guide, which Chromium code follows:
>
> http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml?showone=Boolean_Expressions#Boolean_Expressions
>
> The operators should be in the end of the expressions.
>
> But clang-format did this:
>
> https://codereview.chromium.org/21696003/diff/35001/chrome/browser/ui/views/location_bar/location_bar_view.cc#newcode1107
>
> According to the style guide and Peter it should have been formatted like
> this:
>
> return (ui::GetDisplayLayout() == ui::LAYOUT_TOUCH) ?
>     GetItemPadding() / 2 : 0;
>
> --
> Thiago
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130812/4c6e6c7e/attachment.html>


More information about the cfe-dev mailing list