[LLVMdev] [bikeshed] Anyone have strong feelings about always putting `template <...>` on its own line?

David Blaikie dblaikie at gmail.com
Mon Jul 1 15:55:09 PDT 2013


On Mon, Jul 1, 2013 at 3:41 PM, Sean Silva <silvas at purdue.edu> wrote:
>
>
>
> On Mon, Jul 1, 2013 at 2:40 PM, David Blaikie <dblaikie at gmail.com> wrote:
>>
>> On Mon, Jul 1, 2013 at 2:38 PM, Sean Silva <silvas at purdue.edu> wrote:
>> >
>> >
>> >
>> > On Mon, Jul 1, 2013 at 2:31 PM, David Blaikie <dblaikie at gmail.com>
>> > wrote:
>> >>
>> >>
>> >> Have you got any statistics for the current state of LLVM with respect
>> >> to this formatting issue? If something is already the overwhelmingly
>> >> common style (& it's not a case where it used to be the style, the
>> >> style has been updated, and nothing has been migrated yet) then just
>> >> make clang-format agree with reality - this doesn't require a
>> >> discussion or bikeshed.
>> >
>> >
>> > It's not overwhelming, but the preponderance seems to be towards putting
>> > it
>> > on its own line (the exceptions are usually small trait specializations
>> > like
>> > isPodLike). I give some rough numbers here
>> > <http://thread.gmane.org/gmane.comp.compilers.llvm.devel/63378>
>>
>> Fair enough - could we draw any further stylistic conclusions that
>> could motivate clang-format? If the entire definition of the template
>> fits on one line is it pretty consistent that it's defined on the one
>> line rather than split? What about template declarations, if any?
>>
>
> As a rough count, there are at least "hundreds" of cases where it changes
> previously existing template definitions onto one line (i.e., they would fit
> on one line but they weren't put on one line); this is more than the total
> number of one-line definitions.

No doubt - I'm just curious whether there's some internal
logic/consistency that we've not discussed. If you're claiming that
they are essentially "random" in the choice of wrap or no-wrap (in the
cases that would be affected by clang-format if code were reformatted
with it & clang-format chose one or the other as the default) then,
sure, flip a coin & go with it.

> To obtain a lower bound on the cited
> "hundreds", I clang-format'd everything and then looked for just diff chunks
> similar to:
>
> -template <typename T>
> -class ImmutableList {
> +template <typename T> class ImmutableList {
>
> $ cd llvm/
> $ clang-format -i **/*.cpp **/*.h
> $ git diff | grep -B2 '^+template' | egrep -B1 '^-(struct|class)' | grep
> '^-template' | wc -l
> 287
> # For comparison
> $ git grep '^\s*template' -- '*.cpp' '*.h' | wc -l
> 2011
>
> $ cd clang/
> $ clang-format -i **/*.cpp **/*.h
> $ git diff | grep -B2 '^+template' | egrep -B1 '^-(struct|class)' | grep
> '^-template' | wc -l
> 396
> # For comparison
> $ git grep '^\s*template' -- '*.cpp' '*.h' | wc -l
> 6713
>
> Outside of clang's test/ directory, there are a really tiny number of
> one-line template definitions in clang:
> $ cd clang/
> $ git grep -E '^\s*template.*(class|struct).*{' -- lib include | wc -l
> 60

Yeah, none of the style choices should include evidence from test code
- we write it completely differently.

> My general feel is that template declarations are usually one-lined in
> existing code, but it seems that it is about half and half:
> $ git grep -E '^ *template *<[^>]*> *(class|struct) [A-Za-z0-9_]+;' | wc -l
> 78
> $ git grep -A1 -E '^ *template' | egrep -- '- *(struct|class)
> [A-Za-z0-9_]+;' | wc -l
> 72

Again, precluding test code, are there any discernable differences
between cases that are one line versus multiline? (some consistent
choice being made that could be enshrined in clang-format (or even a
consistent choice that is beyond the understanding of clang-format -
that's still helpful to know))



More information about the llvm-dev mailing list