-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create a clang-format config for PCL style. #2376
Create a clang-format config for PCL style. #2376
Conversation
The clang-format config enforces spacing and line-break guidelines from the PCL style guide, but not guidelines such as all cap constants or parenthesised return values etc. Unfortunately, clang-format has limitations that mean a couple of the style guidelines cannot be enforced, specifically: 1) Members affected by access qualifiers cannot be indented by one more level 2) Return type of functions will go on own separate line, not even the same line as template parameters as in one example from section 2.3 of the style guide resolves PointCloudLibrary#2106
Hi, thanks, I'll give it a try. Which version of clang-format are you using?
So formatting existing code will result in enormous diffs. That's really bad :( |
I used clang-format-5.0. For the guidelines that aren't related to spacing (e.g. parenthesised return values) would the maintainers be interested in using clang-tidy? |
Let me incept this idea: at this point I believe it is more important to have an automated tool to format code and to do formatting checks on the CI than preserving the current code style. If we are happy with something, I would simply reformat all files in a single commit. We do something really bad (hopefully) once to end this issue once and for all. This of course, assuming we exaust all our options with the existing formatting tools.
After reading this I would say yes. |
That's interesting. But then shouldn't we rather drop this guideline? Anyway it wasn't consistently enforced. |
I personally didn't know about it and never enforced it. I'm ok with getting rid of it. I like the clang-tidy/clang-format combo and I would like to adopt it even we have to let go some guidelines. |
There's a list of checks available in clang-tidy here. It's also possible to add new custom checks. How should the checks be chosen? |
Thanks for the reference. I think it's better to only start fiddling with clang-tidy once we've agreed on formatting first. This is something we will need to escalate to our "elders council". I also think Sergey has mixed feelings about my proposal, since the downsides are considerable, which normally indicates its good to bring older maintainers into the discussion. I'll write to them today. Edit: Email sent. |
I've been discussing your findings and I have a couple of news. I believe we're going to drop the two points mentioned that you're unable to implement with clang-format, so ignore those for now. Can you find out if it is possible to only enforce formatting on changes i.e., if a user submits a PR with some changes, is it possible only to enforce formatting on that diff? From our side it is important to provide a way for our users to format their code prior to committing the changes. We can accomplish that by providing them with a method to do so. Optionally we can also provide a git hook to that effect. The second important point is to have the same formatting check running on Travis CI, so that we immediately flag PRs which are not compliant. Can you find out if this is possible? |
I agree we should postpone clang-tidy discussion until after formatting issues are solved. (For the record, I've made an attempt at integrating clang-tidy into CI of one other project that I participate in maintaining, but that came with it's own set of challenges and for the time being I abandoned that idea.)
Yes, it's possible to format diff with respect to the master, see example script here.
This, by the way, means that we have to reformat the whole repository. Because, otherwise, how would we explain users that they need to run clang-format only on the lines that they touched? |
I have an intuition why this might be necessary but I'm sure I'm not fully understanding the reasons behind this requirement. Anyway I'm totally pro reformatting the entire repo so I'm happy with it anyway 😄
We would need to the append an explanation of the process to CONTRIBUTING.md detailing how to generate the diff and applying clang-format-diff on top of it. Ideally, we really supply users with a pre-commit git hook which automatically enforces the formatting changes the before the commit takes place. The job for the users would just be to install clang-format and python/clang-format-diff if he/she wants the automatic formatting feature. Although writing a cross platform script... well python again. I have the impression this should be possible, albeit harder, in case we really want to do it. |
Regarding using git commit hooks for formatting. I've had an experience with that before and I think it's a mess. There are two problems. First, the hook script has to be extremely robust, because if it fails, then the user is not able to commit. And if it does something wrong (without explicitly failing), then it's even worse because some gibberish will be committed. Second, even if you put the hook script inside the repository, it won't be automatically set up in a cloned repo, a manual step on the user's side is needed to install it. Now imagine we get a new pull request that is not formatted. I think it's easy to just say: "please run clang-format over the files, style definition is included in our repository". Compare with: "setup a hook, uncommmit your commits and recommit them again such that the hook will (hopefully) take care of formatting".
Speaking of this, it's not "just". As I pointed out some time ago, different versions of clang-format produce different results! The user has to install exactly the same version as on CI, otherwise there might be situations when locally everything is formated, but CI fails.
Think about it this way. We add official Finally, in the e-mails you invoked the argument that reformatting the whole repo will lead to the need to rebase old PRs. Well, fine, that's another necessary evil. In most cases it won't be a significant effort anyway. And, besides, how many of the old PRs do we expect to merge (realistically)? |
Hmm. So:
Plus I'm also sensing you're sure that global is the way to go. I have doubts but I personally also look forward to adopt the style globally at once. I say let's go with that. I suggest we proceed with CI integration in this PR and then finalize with the commit which reformats the entire repo. @JamesGiller care to continue with the task and start working on the necessary changes on Travis CI to check for the current style? My idea would be to run the format check as the first CI stage. If formatting fails we don't even bother building. Regarding the clang-format version I would suggest adopt to the most recent release. |
@SergioRAgostinho, OK! Just to clarify, by changes to the Travis CI you are referring to something like this script that @taketwo linked above? |
I'm not sure if you closed this intentionally but my idea was to add commits to this PR.
Lines 59 to 86 in f6a443e
You'll need to add a new stage here, which will run before everything. The corresponding shell code will need to be written on travis.sh. It's something like that script Sergey linked but it should be much simpler since you need to run the tool on all C++ source files instead of simply newly committed changes. The idea is to run clang-format with the format file you implemented on the entire repo. clang-format will likely return an error code >0 if it finds format mistakes and that is enough for Travis to identify that the stage failed. |
I think I accidentally clicked the "close" button but didn't realise. I will continue with this task. |
@taketwo How about also giving a try at cmake_format for cmake files? |
I think it will be extremely useful to also print out the formatting mistakes found, maybe even directly in a diff format that can be applied on the users side.
Thanks for the link, I did not know about this tool. Generally, yes, I'd be pro formatting CMake files as well, but I wouldn't tie it with code formatting and this PR. I'd like to get more familiar with the tool first, maybe try to use it with my private projects to get a feeling how stable/reliable it is, is the output formatting good, etc. |
There is a repo run_clang_format that seems to tick all the boxes. If it's fine to just give a yes/no answer to "does the code satisfy the format rules?" then I have something worked out that I will push for you to check out. |
The more I think about it, the more I'm convinced that this won't do. As was established before, different clang versions format slightly differently. Imagine a contributor has Clang 3.x and submits a formatted patch. CI runs with the latest Clang and maybe finds some very subtle formatting difference. And thus yells back that something is mis-formatted. What should we do then? It's likely that the formatting "problem" is too subtle to even notice. So somebody (either a maintainer or the contributor) will have to install the latest Clang and run formatting to find out what's wrong. I don't think this is a good process. |
@taketwo The run_clang_format repo I mentioned seems to be able to produce a diff. The contributor would have to view the Travis build in order to copy the diff into a file or otherwise apply it. Is the preferred way to proceed to check the whole repo for formatting errors, fail the build if there are any and print a diff to apply in order to fix them? |
Regarding the clang formatter python wrapper tool. It looks cool 👍 .
Too my understanding, yes.
It might be, but it also involved having a bogus user with write permission to push the new commit automatically to the user's forks. I'm not comfortable with this. |
The Python wrapper at https://github.com/Sarcasm/run-clang-format will print the formatting diff and return a status (0=Success, 1=Failure). It will also format all desired files in a directory recursively.
Given the new CI, I thought about jump starting this one again. So currently missing:
|
I think we should have a Docker image with the "formatting toolbox" installed. That would include clang-format (of a certain "golden standard" version), the helper script we talked about before. In future this can also include spell checker, linter, clang-tidy and whatnot. |
Separate image for these assistive tools or merge it into the current one you created? I'm fine with both actually. @JamesGiller give us a ping in case you want to carry on this final push. I'm saying this because you seem to be inactive on GitHub for some time now. I might personally take a look at this over the weekend. |
Separate, there is no reason to tie together it with the build environment. We may use a completely different distro as a base, e.g. Alpine Linux which is lightweight.
I can put this on my list as well, though I won't have time this weekend. |
Probably better. I have 0 Docker experience. |
Hi guys, |
Don't worry about it James. Your contribution for this one was more than we could hope for. It's just easier for Sergey to complete this final stretch. Whenever you feel like lending a hand again, please do, and if you need ideas ping us. |
+1 for clang-format, this will make the code so much easier to read! |
This pull request has been automatically marked as stale because it hasn't had Come back whenever you have time. We look forward to your contribution. |
Has there been any development regarding this? Going by the discussions, we need:
|
No updates. From the listing you did, there's still a lot left to do so I would just focus on having a viable image to run clang-format. Small increments so we can at least have some progress. Above all we need to figure how long does it take to run a full check in Azures Pipelines. |
Is there a reason for recommending Alpine as base? It'd be easier to use |
I use Alpine in some of my projects for this purposes. The reason to prefer it is that it's lightweight and generally more up-to-date than Ubuntu.
It is. Here is a Dockerfile that will get you an image with
|
I thought this would add only clang and llvm as it does on other systems, not clang-format. Got it now. Will go ahead |
Assuming we have the style to what we want (via a commit), should I create
We'll anyways need scripts to check git diff post clang-format |
From the users perspective I like the idea of providing a cmake target called format. That way, if we need to provide instructions we have a quick way of telling users what to do.
No additional knowledge needed. I'm just wondering how this will propagate to the IDEs. |
Here is a way to do
|
@ kunaltyagi I forgot to add: don't spend too much time thinking how to implement pre/post commit checks at this stage. Let's just get the foundations in place:
|
Closing this in favor of #3188. @JamesGiller thanks for your work. |
The clang-format config enforces spacing and line-break guidelines from
the PCL style guide, but not guidelines such as all cap constants or
parenthesised return values etc.
Unfortunately, clang-format has limitations that mean a couple of the
style guidelines cannot be enforced, specifically:
same line as template parameters as in one example from
section 2.3 of the style guide
resolves #2106