Skip to content

[C++] [Qt5] Clang format Code#4444

Merged
wing328 merged 13 commits intoOpenAPITools:masterfrom
MartinDelille:clang-format
Nov 27, 2019
Merged

[C++] [Qt5] Clang format Code#4444
wing328 merged 13 commits intoOpenAPITools:masterfrom
MartinDelille:clang-format

Conversation

@MartinDelille
Copy link
Copy Markdown
Contributor

This is an attempt to set several clang-format rules on the generated code.

What do you think about it?

PR checklist

  • Read the contribution guidelines.
  • If contributing template-only or documentation-only changes which will change sample output, build the project before.
  • Run the shell script(s) under ./bin/ (or Windows batch scripts under.\bin\windows) to update Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit, and these must match the expectations made by your contribution. You only need to run ./bin/{LANG}-petstore.sh, ./bin/openapi3/{LANG}-petstore.sh if updating the code or mustache templates for a language ({LANG}) (e.g. php, ruby, python, etc).
  • File the PR against the correct branch: master, 4.3.x, 5.0.x. Default: master.
  • Copy the technical committee to review the pull request if your PR is targeting a particular programming language.

@ravinikam @stkrwork @etherealjoy @muttleyxd

@etherealjoy etherealjoy changed the title Clang format [C++] [Qt5] Clang format Code Nov 10, 2019
@etherealjoy
Copy link
Copy Markdown
Contributor

etherealjoy commented Nov 10, 2019

This is a great PR.
However we need to agree on the style.

https://clang.llvm.org/docs/ClangFormatStyleOptions.html

Possible values:

Dump clang format from one of these.

clang-format -style=llvm -dump-config > .clang-format

cc
@wing328

@jimschubert
Copy link
Copy Markdown
Member

I've had to cancel this PR's CircleCI build because it was blocking our queue for 6 hours. Once the discussion above about style is addressed, please trigger CircleCI again.

@MartinDelille
Copy link
Copy Markdown
Contributor Author

I open to any suggestion concerning the style. I think I used the LLVM style since it seems to be the default. I just needed to apply some tweak to make it possible to use it (see .clang-format comments)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default LLVM is All

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It allowed me to if statement with many condition into several lines.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main issue I was dealing with this setting is that if let to default, the description was splitted into several lines which is not easy to do in the mustache template.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MartinDelille Are we going to restore .clang-foramt to the default?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could remove this file since it is not really used.

I used it to help me to generate a better output but was unable to respect the specific ColumnLimit rule, in particular when writing the header documentation.

The current template generates:

/**
 * OpenAPI Petstore
 * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
 *
 * The version of the OpenAPI document: 1.0.0
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */

Setting ColumnLimit to the default would require the following generation:

/**
 * OpenAPI Petstore
 * This is a sample server Petstore server. For this sample, you can use the api
 * key `special-key` to test the authorization filters.
 *
 * The version of the OpenAPI document: 1.0.0
 *
 * NOTE: This class is auto generated by OpenAPI Generator
 * (https://openapi-generator.tech). https://openapi-generator.tech Do not edit
 * the class manually.
 */

I have no idea how to split the description from the mustache template.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea how to split the description from the mustache template.

My take is to leverage code formatter to take care of the line split.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I got it! This .clang-format will only be used if we want to check the template generate a clang format compatible code.

@MartinDelille
Copy link
Copy Markdown
Contributor Author

@wing328
Copy link
Copy Markdown
Member

wing328 commented Nov 12, 2019

My suggestion is to include a default .clang-format (auto-generated) and let the user customize it to meet their requirements/preferences.

@MartinDelille
Copy link
Copy Markdown
Contributor Author

@wing328 This could make the templating easier to write but wouldn't it require to have clang-format as a dependency?

@wing328
Copy link
Copy Markdown
Member

wing328 commented Nov 13, 2019

@wing328 This could make the templating easier to write but wouldn't it require to have clang-format as a dependency?

To be clear, we don't need to update the template. We simply leverage code formatter to do the job for us.

Users can use file post-processing (as part of openapi-generator) or use the code formatter on the output after the code generation completes.

Using Ruby as an example, the generator outputs the .rubocop.yml file as a starting point. Users can of course customize it with any rules they want.

@MartinDelille
Copy link
Copy Markdown
Contributor Author

@wing328 I totally agree with you about not modifying the template: tweaking it to respect the rules I used for the example was not an easy task.

I will look into the ruby generator to mimic the behavior.

@MartinDelille
Copy link
Copy Markdown
Contributor Author

@etherealjoy Regarding the style, we should maybe have llvm as a default style (since it's clang-format default style) and allow the user to specify it's own?

@wing328
Copy link
Copy Markdown
Member

wing328 commented Nov 14, 2019

maybe have llvm as a default style (since it's clang-format default style)

Agreed with using clang-format's default style as a starting point.

@MartinDelille
Copy link
Copy Markdown
Contributor Author

After investigating I realised that the processing already work and is even more generic than clang-format and can work with other tools like uncrustify for example.

We just need to set CPP_POST_PROCESS_FILE to /usr/local/bin/clang-format -i for example or uncrustify --replace --no-backup and pass --enable-post-process-file to the command line.

The question is: do we want to enable post processing for the sample?

@wing328
Copy link
Copy Markdown
Member

wing328 commented Nov 18, 2019

The question is: do we want to enable post processing for the sample?

We'd a discussion on that before (we also tried) but the problem is not all contributors have the code formatter installed.

@MartinDelille
Copy link
Copy Markdown
Contributor Author

Yes that is what I was thinking.

Do you think improving the style of the generated code is still relevant?

@wing328
Copy link
Copy Markdown
Member

wing328 commented Nov 20, 2019

@MartinDelille I definitely like your change.

A little background on --enable-post-process-file. We found it beneficial to post-process the file (e.g. formatting using different formatter, etc) and it's not easy to get every space, code alignment correct with the mustache templates.

We still try to make the default output looks good in terms of code format so we definitely welcome your PR to update the template with better code format.

Please resolve the merge conflicts and I'll merge this PR.

@MartinDelille
Copy link
Copy Markdown
Contributor Author

Ok I rebased my PR against #4526 which is more important to me (having -Werror enabled it break my compilation)

@MartinDelille
Copy link
Copy Markdown
Contributor Author

Please consider merging this PR (and #4526) before other cpp-qt5 template modification because rebasing and merging conflict can be quite long! 😅

Comment thread samples/client/petstore/cpp-qt5/client/PFXHttpRequest.cpp
Comment thread modules/openapi-generator/src/main/resources/cpp-qt5-client/api-body.mustache Outdated
Comment thread modules/openapi-generator/src/main/resources/cpp-qt5-client/model-body.mustache Outdated
Comment thread samples/client/petstore/cpp-qt5/client/PFXStoreApi.cpp Outdated
@etherealjoy
Copy link
Copy Markdown
Contributor

Just one additional comment, rest looks fine.

@MartinDelille
Copy link
Copy Markdown
Contributor Author

I used git commit --fixup. Tell me if you want me to clean the history.

@etherealjoy
Copy link
Copy Markdown
Contributor

It will be squashed on merge so not necessary.

Copy link
Copy Markdown
Contributor

@etherealjoy etherealjoy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@MartinDelille
Copy link
Copy Markdown
Contributor Author

Is something blocking this PR to be merged?

If someone else is working on the qt5 client template, there is a risk of ruining the job done here.

Copy link
Copy Markdown
Member

@wing328 wing328 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@wing328 wing328 merged commit 8991168 into OpenAPITools:master Nov 27, 2019
@wing328 wing328 added this to the 4.2.2 milestone Dec 2, 2019
@MartinDelille MartinDelille deleted the clang-format branch September 3, 2021 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants