Skip to content

uncrustify: add auto uncrustify with blacklist#8519

Merged
kaspar030 merged 3 commits intoRIOT-OS:masterfrom
jia200x:uncrustify
Apr 29, 2020
Merged

uncrustify: add auto uncrustify with blacklist#8519
kaspar030 merged 3 commits intoRIOT-OS:masterfrom
jia200x:uncrustify

Conversation

@jia200x
Copy link
Copy Markdown
Member

@jia200x jia200x commented Feb 5, 2018

Contribution description

Here is a proposal for the discussion in #4363.

  • This PR adds a tool in dist/tools that applies the current uncrustify-riot.cfg config to all tracked files that don't match an entry in a blacklist.txt file.
  • It also includes a check.sh that could be used from CI. Checks if files in a branch have been uncrustified (close to a automatic style check).
  • Last but not least, provides a blacklist.txt with core and (most) vendor files blacklisted

To use this tool, simply run either uncrustify.sh for applying the configuration or check.sh to check if files have been uncrustified.

Does it make sense? Please feel free to give feedback.

Issues/PRs references

#4363

@jia200x
Copy link
Copy Markdown
Member Author

jia200x commented Feb 5, 2018

we might need to tune again the cfg file though...

@jnohlgard
Copy link
Copy Markdown
Member

Does this have any kind of safety check so that uncrustify does not overwrite local working tree changes?

RIOTBASE=$(git rev-parse --show-toplevel)
CURDIR=$(cd $(dirname $0) && pwd)
BLACKLIST=$CURDIR/blacklist.txt
FILES=$(git ls-tree -r HEAD --name-only --full-tree | grep -E '.*\.(c$|cpp$|h$|hpp$)' | grep -vf $BLACKLIST)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

if possible, please re-use dist/tools/ci/changed_files.sh

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

ah, sure! Didn't know such file existed

@kaspar030
Copy link
Copy Markdown
Contributor

we might need to tune again the cfg file though...

that will be the hard part. ;)

@jia200x
Copy link
Copy Markdown
Member Author

jia200x commented Feb 6, 2018

Does this have any kind of safety check so that uncrustify does not overwrite local working tree changes?

Maybe I could add something that prevents the execution if there are changes (e.g output with git diff)

FILES=$(git ls-tree -r HEAD --name-only --full-tree | grep -E '.*\.(c$|cpp$|h$|hpp$)' | grep -vf $BLACKLIST)
for F in $FILES
do
OUT=$(diff $RIOTBASE/$F <(uncrustify -c $CURDIR/uncrustify-riot.cfg -f $RIOTBASE/$F 2>/dev/null))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

tab ?

FILES=$(git ls-tree -r HEAD --name-only --full-tree | grep -E '.*\.(c$|cpp$|h$|hpp$)' | grep -vf $BLACKLIST)
for F in $FILES
do
uncrustify -c $CURDIR/uncrustify-riot.cfg --no-backup $RIOTBASE/$F
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Indentation should be 4 spaces (here and above)

@jia200x
Copy link
Copy Markdown
Member Author

jia200x commented Feb 6, 2018

@aabadie solved

@bergzand
Copy link
Copy Markdown
Member

bergzand commented Feb 6, 2018

@jia200x Could you please run your shell scripts through shellcheck?
It checks for simple issues such as missing quotes around variables (can cause issues with spaces in paths).

I can enumerate the issues as a review for you if you have trouble using shellcheck.

@jia200x
Copy link
Copy Markdown
Member Author

jia200x commented Feb 6, 2018

@bergzand cool! Ran OK. I'm fixing those issues.

@jia200x
Copy link
Copy Markdown
Member Author

jia200x commented Feb 6, 2018

@bergzand done. Thanks for the tip!

BLACKLIST=$CURDIR/blacklist.txt

. "$RIOTBASE"/dist/tools/ci/changed_files.sh
FILES=$(changed_files | grep -vf "$BLACKLIST")
Copy link
Copy Markdown
Contributor

@kaspar030 kaspar030 Feb 6, 2018

Choose a reason for hiding this comment

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

changed files allows it's own blacklist. Sth like this should be equivalent:

FILES=$(EXCLUDE="(/vendor/|/core/)" changed_files)

edit: fixed regex

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I proposed to provide a separate blacklist.txt file because I think it's easier to add/remove entries from there. Since there might be several blacklisted entries, I thought it was a good idea. What do you think?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What do you think?

Ok! Then I'd suggest removing "vendor", as that should be filtered by changed_files' defaults.
Anyone reading this, next one who needs a blacklist file needs to add generic support for that to changed_files.sh! ;)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ok, perfect! ;)

@jia200x
Copy link
Copy Markdown
Member Author

jia200x commented Feb 6, 2018

@kaspar030 @haukepetersen anything else to blackilist besides core files?

@jia200x
Copy link
Copy Markdown
Member Author

jia200x commented Feb 6, 2018

(If this get merged, the uncrustify process should come in a further PR)

@kaspar030
Copy link
Copy Markdown
Contributor

@kaspar030 @haukepetersen anything else to blackilist besides core files?

Well, as is uncrustify would change ~24k lines, with many of them unwanted changes. That IMO shows that the config file needs some tuning... I think it'll take some time util we can actually force uncrustify, even for new files. Thus whether core is blacklisted or not is pretty much irrelevant...

@jia200x
Copy link
Copy Markdown
Member Author

jia200x commented Feb 6, 2018

Well, as is uncrustify would change ~24k lines, with many of them unwanted changes. That IMO shows that the config file needs some tuning... I think it'll take some time util we can actually force uncrustify, even for new files. Thus whether core is blacklisted or not is pretty much irrelevant...

Could you point to some unwanted changes? ;) I can do my best to address them.

EDIT: It seems there are still tabs being inserted by the cfg file, I will check

@jia200x
Copy link
Copy Markdown
Member Author

jia200x commented Feb 6, 2018

@kaspar030 well, assuming files in upstream are in good shape, I wil try to find the config that minimizes the number of changed files

@kaspar030
Copy link
Copy Markdown
Contributor

This is from the first diff uncrustify.sh does on this PR's branch:

         .name = "Button 1",
-        .pin  = BTN0_PIN,
+        .pin = BTN0_PIN,
         .mode = BTN0_MODE,
         .flags = SAUL_GPIO_INVERTED,
     }

The alignment is removed here.

@jia200x
Copy link
Copy Markdown
Member Author

jia200x commented Feb 6, 2018

The alignment is removed here.

I see.. I will play a little bit with the cfg file

@miri64 miri64 removed their request for review February 27, 2018 17:57
@tcschmidt
Copy link
Copy Markdown
Member

This tool seems a proper way to automate the burden of code style review. Can we advance to make this production ready for

  • sanitizing old code
  • adding this to the automated test system so that new PRs authors get automatic feedback prior to review?

@haukepetersen
Copy link
Copy Markdown
Contributor

good luck :-) All the previous attempts to make this work over the last years have failed - but it would be nice to get somewhere this time!

I'd say, if applying uncrustify to our codebase yields somwhere < 1000 changes, we are on a good path...

@tcschmidt
Copy link
Copy Markdown
Member

I'm sure @jia200x and others will have energy to make this work: efforts most likely will be honored by a huge burden of labor taken off reviewers shoulders.
Btw @haukepetersen : How is the status of your attempt for structuring review work? You started out with a Wiki page a while ago ?

@jia200x
Copy link
Copy Markdown
Member Author

jia200x commented Jun 1, 2018

Check this gist to see the how the whole RIOT repo (without core files) get's uncrustified.

@kaspar030
Copy link
Copy Markdown
Contributor

As discussed offline, let's go with an iterative approach here.

One way would be:

  • add the result of "uncrustify -c /dev/null --update-config > uncrustify-force.cfg" somewhere. Best make a commit of it's own, stating the used uncrustify version

  • try to disable as much as possible in that config, set defaults for the rest, in order to get the minimal amount of lines when applied to the code base

  • those lines left, either accept the changes, or exclude from uncrustify (using uncrustify's macros)

  • merge this PR, with the enforcment enabled

Then, successively, add more options from uncrustify-riot.cfg. Probably best would be one per PR to make the discussions both manageable and parallelizable.

@tcschmidt
Copy link
Copy Markdown
Member

@jia200x ready for the first deployment step as outlined by @kaspar030 ?

@jia200x
Copy link
Copy Markdown
Member Author

jia200x commented Mar 10, 2020

@kaspar030 are you still interested in this PR?

@kaspar030
Copy link
Copy Markdown
Contributor

@kaspar030 are you still interested in this PR?

yes! I want this to enforce uncrustify once #10867 is in.

@kaspar030
Copy link
Copy Markdown
Contributor

#10867 is merged.
Could you rebase, move the check to dist/tools/ci/static_tests.sh and add core/*.c and core/include/*.h?

@jia200x
Copy link
Copy Markdown
Member Author

jia200x commented Apr 2, 2020

Could you rebase, move the check to dist/tools/ci/static_tests.sh and add core/.c and core/include/.h?

Sure!

@kaspar030
Copy link
Copy Markdown
Contributor

ping @jia200x, I have review time tomorrow!

@jia200x
Copy link
Copy Markdown
Member Author

jia200x commented Apr 27, 2020

done!

@jia200x
Copy link
Copy Markdown
Member Author

jia200x commented Apr 27, 2020

Side note: In another open source project I'm working as a volunteer we are using a linter and a code beautifier. Both are for python code (black and pylint) but the experience has been quite nice.

The CI complains if the developer didn't run "black" (beautifier) but also if the linter detect issues (pylint). So I think this can live in harmony with Vera++

@kaspar030
Copy link
Copy Markdown
Contributor

LGTM, please squash!

@jia200x
Copy link
Copy Markdown
Member Author

jia200x commented Apr 27, 2020

squashed!

@kaspar030 kaspar030 added CI: skip compile test If set, CI server will run only non-compile jobs, but no compile jobs or their dependent jobs CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: needs squashing Commits in this PR need to be squashed; If set, CI systems will mark this PR as unmergable CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Apr 28, 2020
@kaspar030
Copy link
Copy Markdown
Contributor

CI passed on compilation but failed on (still set) "needs squashing". re-triggering.

@kaspar030
Copy link
Copy Markdown
Contributor

@jia200x I found an issue: the whitelist contains paths, but should contain regexps. Also, each line should match a whole path, not a substring.
So as is the whitelist entry only matches by chance.
And when the regexp is fixed to e.g., core/.*\.c, it would also match foo/.../tests-core/foo.c.

This patch should fix the issue (adds "-x" for line based patterns to the filter, and fixes the core regexps):

https://gist.github.com/e28bea3fd46a16651ea955f5abf0b2b7

@kaspar030 kaspar030 removed the CI: skip compile test If set, CI server will run only non-compile jobs, but no compile jobs or their dependent jobs label Apr 28, 2020
@jia200x
Copy link
Copy Markdown
Member Author

jia200x commented Apr 28, 2020

fixed, and squashed directly. Thanks for the patch!

Copy link
Copy Markdown
Contributor

@kaspar030 kaspar030 left a comment

Choose a reason for hiding this comment

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

ACK.

@jia200x
Copy link
Copy Markdown
Member Author

jia200x commented Apr 28, 2020

does this require Murdock? Can't we add the CI: skip compile tests label?

@kaspar030 kaspar030 added the CI: skip compile test If set, CI server will run only non-compile jobs, but no compile jobs or their dependent jobs label Apr 28, 2020
@kaspar030 kaspar030 merged commit e531bdb into RIOT-OS:master Apr 29, 2020
@miri64 miri64 added this to the Release 2020.07 milestone Jun 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR CI: skip compile test If set, CI server will run only non-compile jobs, but no compile jobs or their dependent jobs Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants