Bug 223229 - clang-format config file generate changes not compatible with expected webkit style.
Summary: clang-format config file generate changes not compatible with expected webkit...
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Tools / Tests (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Cameron McCormack (:heycam)
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2021-03-15 17:56 PDT by Jean-Yves Avenard [:jya]
Modified: 2021-03-24 17:23 PDT (History)
8 users (show)

See Also:


Attachments
Patch (1.04 KB, patch)
2021-03-21 16:39 PDT, Cameron McCormack (:heycam)
no flags Details | Formatted Diff | Diff
Patch (1.92 KB, patch)
2021-03-21 19:31 PDT, Cameron McCormack (:heycam)
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jean-Yves Avenard [:jya] 2021-03-15 17:56:16 PDT
Running clang-tidy will produce a style that is incompatible with what webkit-patch requires.

Examples:
---
    MediaSessionPlaybackState m_playbackState { MediaSessionPlaybackState::None };
becomes:
    MediaSessionPlaybackState m_playbackState{ MediaSessionPlaybackState::None };
---
    template<class Encoder> void encode(Encoder&) const;
becomes:
    template <class Encoder>
    void encode(Encoder&) const;
---
    String src;
    if (!decoder.decode(src))
        return { };
becomes:
    String src;
    if (!decoder.decode(src))
        return {};
Comment 1 Radar WebKit Bug Importer 2021-03-15 17:56:36 PDT
<rdar://problem/75456758>
Comment 2 Alexey Proskuryakov 2021-03-16 08:36:18 PDT
Is there any way for us to fix this? Or does this bug track the need to file a bug against clang?
Comment 3 Cameron McCormack (:heycam) 2021-03-21 16:28:10 PDT
Looking at https://clang.llvm.org/docs/ClangFormatStyleOptions.html, we need to set SpaceBeforeCpp11BracedList to true.  And I guess update upstream clang's WebKit defaults as well.
Comment 4 Cameron McCormack (:heycam) 2021-03-21 16:39:37 PDT
Created attachment 423841 [details]
Patch
Comment 5 Cameron McCormack (:heycam) 2021-03-21 16:54:12 PDT
Well that patch addresses the first issue but not the latter two.

For the last issue, it's not clear to me whether Cpp11BracedListStyle = false implies that a space should be inserted into empty braces.
Comment 6 Cameron McCormack (:heycam) 2021-03-21 17:00:36 PDT
For line breaking after template <...>, I don't see any rules in https://webkit.org/code-style-guidelines/#line-breaking about it.  The .clang-format option we're currently setting that governs this is AlwaysBreakTemplateDeclarations = false, which will break if the line is too long or something.

It looks like there's a real mix in the codebase:

$ rg 'template ?<.*>$' | wc -l
    2076
$ rg 'template ?<.*> [a-z]' | wc -l
    2899

but tending towards not breaking.
Comment 7 Cameron McCormack (:heycam) 2021-03-21 17:04:58 PDT
AlwaysBreakTemplateDeclarations = false will take PenaltyBreakTemplateDeclaration into consideration but even if I make that very high, clang-format wants to break there.  So that might be a clang-format problem.
Comment 8 Cameron McCormack (:heycam) 2021-03-21 17:37:58 PDT
So AlwaysBreakTemplateDeclarations = false is not a valid value, really, according to https://clang.llvm.org/docs/ClangFormatStyleOptions.html.  It should be No, MultiLine, or Yes.  Choosing No gives different behavior from false.  Because .clang-format contains ColumnLimit = 0, this means that existing line breaking choices will be preserved unless they violate the rules.  If we set AlwaysBreakTemplateDeclarations = No, this will cause

  template <typename T> void f();
  template <typename T>
  void g();

to be unchanged.

But if we change ColumnLimit to something very high, then the result will be:

  template <typename T> void f();
  template <typename T> void g();

Changing ColumnLimit will probably cause a bunch of other undesirable changes, so I suggest we set AlwaysBreakTemplateDeclarations = No, and then at least we can write single line template declarations and not have clang-format change that.
Comment 9 Cameron McCormack (:heycam) 2021-03-21 19:31:07 PDT
Created attachment 423845 [details]
Patch
Comment 10 Jean-Yves Avenard [:jya] 2021-03-22 13:30:04 PDT
The fix for:.

    String src;
    if (!decoder.decode(src))
        return { };

Seems to be missibg
Comment 11 Cameron McCormack (:heycam) 2021-03-22 13:36:55 PDT
I don't know that there's a solution for that with the current set of .clang-format options.
Comment 12 EWS 2021-03-22 17:44:04 PDT
heycam@apple.com does not have committer permissions according to https://raw.githubusercontent.com/WebKit/WebKit/main/Tools/Scripts/webkitpy/common/config/contributors.json.

Rejecting attachment 423845 [details] from commit queue.
Comment 13 EWS 2021-03-22 18:16:52 PDT
commit-queue failed to commit attachment 423845 [details] to WebKit repository. To retry, please set cq+ flag again.
Comment 14 EWS 2021-03-23 14:24:04 PDT
Committed r274901: <https://commits.webkit.org/r274901>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 423845 [details].
Comment 15 Simon Fraser (smfr) 2021-03-23 14:54:50 PDT
Comment on attachment 423845 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=423845&action=review

> .clang-format:20
> -AlwaysBreakTemplateDeclarations: false
> +AlwaysBreakTemplateDeclarations: No

Are 'false' and 'No' different? AlwaysBreakBeforeMultilineStrings is false.

> ChangeLog:17
> +        Reviewed by NOBODY (OOPS!).

This should go below the bug URL (with a blank line in between).
Comment 16 Cameron McCormack (:heycam) 2021-03-23 15:01:49 PDT
(In reply to Simon Fraser (smfr) from comment #15)
> > -AlwaysBreakTemplateDeclarations: false
> > +AlwaysBreakTemplateDeclarations: No
> 
> Are 'false' and 'No' different? AlwaysBreakBeforeMultilineStrings is false.

They are definitely different for AlwaysBreakTemplateDeclarations.  And as mentioned above, false isn't documented to be a valid value for that setting anyway.

AlwaysBreakBeforeMultilineStrings is defined to take true/false.
Comment 17 Ross Kirsling 2021-03-24 17:06:43 PDT
(In reply to Cameron McCormack (:heycam) from comment #3)
> Looking at https://clang.llvm.org/docs/ClangFormatStyleOptions.html, we need
> to set SpaceBeforeCpp11BracedList to true.  And I guess update upstream
> clang's WebKit defaults as well.

Upstream clang's WebKit defaults are already correct -- I added SpaceBeforeCpp11BracedList a few years ago for the purpose of supporting WebKit style. :)
https://github.com/llvm/llvm-project/blob/main/clang/lib/Format/Format.cpp#L1314
Comment 18 Cameron McCormack (:heycam) 2021-03-24 17:23:53 PDT
(In reply to Ross Kirsling from comment #17)
> Upstream clang's WebKit defaults are already correct -- I added
> SpaceBeforeCpp11BracedList a few years ago for the purpose of supporting
> WebKit style. :)
> https://github.com/llvm/llvm-project/blob/main/clang/lib/Format/Format.
> cpp#L1314

Oh, thank you!  I should probably go through that to see if there's anything else we're incorrectly overriding.