[Development] clang-format

Morten Sørvig Morten.Sorvig at qt.io
Wed Jun 20 13:23:49 CEST 2018



> On 19 Jun 2018, at 17:33, Allan Sandfeld Jensen <kde at carewolf.com> wrote:
> 
> Btw. Just for your information.
> 
> I have attached a few random examples of what we can look forward too after 
> running an auto-"beautifying" tool over our hand-formated Qt code. And these 
> changes are NOT something we can configure out ouf of in clang-format, it is 
> just cases where it can't do any better.<clang-beautified.txt>_______________________________________________
> Development mailing list
> Development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/development

Thanks, this should really be the first thing we do: run a pre-project
so that we can have an informed discussion.

The point of machine formatting is perhaps not to make code beautiful, rather
it is to have automated formatting in order to offload work from the humans.


Looking at qConvertRgb16To32:

Manual formatting:
    return 0xff000000
        | ((((c) << 3) & 0xf8) | (((c) >> 2) & 0x7))
        | ((((c) << 5) & 0xfc00) | (((c) >> 1) & 0x300))
        | ((((c) << 8) & 0xf80000) | (((c) << 3) & 0x70000));

Clang-format:
    return 0xff000000 | ((((c) << 3) & 0xf8) | (((c) >> 2) & 0x7)) | ((((c) << 5) & 0xfc00) | (((c) >> 1) & 0x300))
           | ((((c) << 8) & 0xf80000) | (((c) << 3) & 0x70000));

The manually formatted version clearly has something going for it.


The benefit of reformatting the entire code base and enforcing clang-format
is that there is now a single correct way to format code (At least until
we change the .clang-format file; do we re-format everything again then?). 
This makes it possible to enforce the format via bots.

But this change may be too disruptive for an established C++ project. If the
goal is to offload the humans we could get away with a small code style policy
addition:

  * Clang-formatted code is acceptable from a code style perspective.

Which enables the workflow where patch authors can run clang-format and
be done with code formatting. At the same time we keep the door open for
manual formatting where needed. We may then have a bot which +1 stamps
correctly clang-formatted patches, but I don’t think it should nitpick
manually formatted code.

Morten







More information about the Development mailing list