alrevuelta/cONNXr

Code formatting

Closed this issue · 4 comments

Since we are adding a lot of autogenerated code and formatting this code is pure pain.
How about we add some kind of code formatter for the whole project?

  • clang-format
    • simple config
      • common styles
      • hate it or love it approach
    • intended for c++
  • uncrustify
    • complex config
      • almost every token transition is configurable
      • pain to configure

currently I'm evaluating both for another project and am leaning towards uncrustify

Great idea. I haven't used neither of them but found that clang-format has a GitHub Action CI built on top that can act as a linter. So I guess the idea here would be to use the code formatter locally, and then on CI enforce that all the code is formatted accordingly (but without auto formatting it).

The action is this one (not widely used though). Since its built on top clang-format, we can share the configuration file for both the formatter and CI linter.

Nothing against uncrustify but couldn't find the linter functionality for it.

Great idea. I haven't used neither of them but found that clang-format has a GitHub Action CI built on top that can act as a linter. So I guess the idea here would be to use the code formatter locally, and then on CI enforce that all the code is formatted accordingly (but without auto formatting it).

that's exactly how it's done

The action is this one (not widely used though). Since its built on top clang-format, we can share the configuration file for both the formatter and CI linter.

Nothing against uncrustify but couldn't find the linter functionality for it.

what do you mean by linting support (except an ide integration)?
A CI check if code conforms can be simply done by formatting it and checking if git reports any differences (git diff --exit-code).
Git can also be used to generate diffs explaining the differences

example:

git ls-files -i -x '**/*.h' -x '**/*.c' | while read file; do echo formatting $file; done
git diff --exit-code

what do you mean by linting support (except an ide integration)?
A CI check if code conforms can be simply done by formatting it and checking if git reports any differences (git diff --exit-code).
Git can also be used to generate diffs explaining the differences

I guess the mentioned action is doing something like that under the hood, format the code and diff to check if the code conforms. Maybe even show the mismatches in some fancy way. Here is an example: https://github.com/tensorflow/addons/runs/422021572?check_suite_focus=true

Maybe even show the mismatches in some fancy way.

does it get more fancy than this?

git ls-files -ico -x '**/*.c' -x '**/*.h' | xargs -I % sh -c "clang-format % | git diff --exit-code --no-index --color --word-diff-regex=. % -"

image