[cfe-dev] [clang-format] sort includes and group per category

Roman Lebedev via cfe-dev cfe-dev at lists.llvm.org
Tue Oct 24 04:14:34 PDT 2017


On Tue, Oct 24, 2017 at 2:02 PM, Oleksii Vilchanskyi via cfe-dev
<cfe-dev at lists.llvm.org> wrote:
> As a clang-format user, I'd love to see this kind of sort. This is the
> general way many people structure their includes, so that they don't
> introduce any hidden dependencies. One thing I'd propose is putting the
> respective header of a given file on the top (or introduce a feature for
> it).
> For example, for file `foo.c`:
>
> #include "foo.h"
>
> /* Other "" includes */
>
> /* Other <> includes which are inside the build tree, for example MOC
> headers */
>
> /* less common libs, i.e. home-made ones */
>
> /* boost, Qt, whatever else headers */
>
> /* standard library headers */

Hi.

I feel like mentioning that IncludeCategories parameter already exists,
and already allows to sort the includes according to the config-specified
rules. See https://clang.llvm.org/docs/ClangFormatStyleOptions.html

The one thing it does *not* allow to do, is to separate the include groups
with empty line.

And i **believe** (i can be wrong) clang-format only sorts the includes
within one block. So if you have two groups of #include's, separated
by empty line, only the includes withing each block will be sorted.

So this empty line #include block separation handling enhancements
is the only thing missing, and is the subject of that issue.

Roman.

> On 23.10.2017 21:22, Wim Leflere via cfe-dev wrote:
>> To fix bug 28203 and improve the includes sorting, I would like to add
>> an includes grouping feature.
>>
>> A 'GroupIncludes (bool)' format option could be added to group the
>> includes per category.
>> The includes could be grouped with a empty line between every group.
>>
>> Example priorities: system includes 3, boost 2, other 1
>> before:
>> #include <memory>
>> #include <boost/make_shared.hpp>
>> #include "Device.h"
>>
>> after:
>> #include "Device.h"
>>
>> #include <boost/make_shared.hpp>
>>
>> #include <memory>
>>
>> What do you think of the proposal?
>>
>> Regards,
>>
>> Wim
>>
>>
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>>
>
> --
> Regards,
> Oleksii Vilchanskyi
> PGP:0x8D3A0E046BDE941F2A53867CE3FD952D48C0B338
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



More information about the cfe-dev mailing list