-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Using Clang-Format for Automatic Source Code Formatting #71
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
Comments
I think something like this would be very helpful. Sometimes I spend more time trying to figure out how to make my code follow the unclear conventions set by inconsistent code formatting than I do writing the actual code. A CI check for code style compliance should be done on every PR and commit to enforce consistent formatting from the start, rather than having to periodically go back and clean up whatever mess has accumulated since the last run of the formatter. The person writing the code should be responsible for writing compliant code and the code shouldn't get merged until it passes the check. That should be quite easy to set up. I prefer AStyle to Clang-format. The reason is that the Arduino IDE already uses AStyle for the Tools > Auto Format feature. There is an AStyle configuration file used for formatting of the example sketches which should be consistent with the Auto Format configuration, but with additional style requirements. That file provides the closest thing to a code style guide that Arduino has: I would like to see a consistent code style used across all Arduino code that runs on microcontrollers, whether that is example sketches or library/core code. Ideally some of the same conventions would be extended further into the Arduino code base. |
AStyle will work well as well. Rather than making it difficult for the person, I'd say we pass an AStyle or a Clang Format file as part of this repo in the main folder which every person has to run before committing, no excuses and then have a web based CI handling the verification. I am supportive of standardisation as well. While I too would love to see a consistent coding style across all Arduino repos, I believe we have to at least start somewhere first. Also doesn't the Arduino IDE use Java? In which case they would for obvious reasons prefer another coding style which might not really suit C++. As such, I believe here we should maybe discuss here about a One True Standard for this Repo? Should I update the Title to mention AStyle rather than Clang Format?? |
I'd love if all the code was coherently formatted but this would break some (most) of the PRs. I know that this can be solved by merging (or closing) existing PRs but we are having an hard time doing this (mostly because the current "core" focus is on validating ArduinoAPI and porting the various cores to it). Said that, a huge PR with all the code reformatted and a companion script that can be added as a git precommit hook would not hurt anyone 🙂 |
Yeah, I wasn't suggesting that a single code style would be used across all the repositories, though I do think it would be cool to have consistency where possible. However, that is expanding the scope to the point where probably no consensus could be reached. I guess we should restrict the scope to C and C++ code. My earlier comment was more thinking about eventually applying it to C/C++ code used by Arduino for other purposes. The only such project I know of is arduino-preprocessor. Even if we restrict this to microcontroller code, that will be great. I think just establishing an official code style and making a good effort to follow it on all new commits would be a very good step towards the goal. The CI check I proposed would be difficult to implement until the existing code is all compliant. I have an AStyle configuration for a strict enforcement of what I've been able to decipher as the "Arduino Style" which I put a reasonable amount of thought and research into a while back for a personal project. I want to look at it a little more closely before presenting it as my proposal. It's quite difficult to interpret a style from code that is not consistent. I think of the examples bundled with the Arduino IDE as the primary source, and these are reasonably consistent since they've been through the auto-format. The problem is that they don't contain any precedents for certain things. Once you start diving into the core and library code looking for a specific style, things get much more difficult because there is a lot of inconsistency. In that case, some arbitrary decisions might need to be made. If we can stipulate that the existing auto format settings are already set in stone, that will greatly decrease the things that are left for people to disagree on. Otherwise, we'll probably never get past the tedious spaces vs tabs and if spaces, how many? arguments. I only desire consistency. I leave my own formatting preferences behind when working on a collaborative project. @facchinm do you think this is the correct place to have the discussion of the official code style? Since this will eventually have a larger scope than Arduino AVR Boards, it seems more appropriate for the arduino/Arduino issue tracker. Surprisingly, I don't find an existing issue about this. The closest would be arduino/Arduino#6503. I remember the subject came up at least a couple times in the Developers Mailing list, but without any sort of resolution: |
Here's my proposed AStyle configuration. I have commented it to indicate the origin of each option (formatter.conf is used for the Arduino IDE's Tools > Auto Format, examples_formatter.conf is used by a script in the arduino/Arduino repository which does bulk formatting of example sketches) and you can get information on all the options in the AStyle documentation.
Although they are in my configuration, I recommend that the options There are two options that I have omitted from my configuration. The reason is that they are not valid options in the current version of AStyle. I have added five options which are not present in formatter.conf or examples_formatter.conf. These are at the bottom of the file. I'd also like to see a style guide for things that can not be enforced via AStyle. Here's just a quick list of things off the top of my head. Popular style guides can serve as a reference for further ideas:
FYI, this is me wearing my volunteer hat |
:+100: (The other points are also important, but this one stands out, as it affects the code and isn't just about readability.) One thing that bothered me last time I used AStyle was its handling of C++11 initializers. Don't know if it's ready for that yet. Every code formatter has its "challenges". One which can be adjusted to circumvent most of them is uncrustify. But even then there might be places where well intended formatting gets destroyed, so manual inspection is always necessary. Just my 2¢, |
The topic of automatic code formatting comes up on a regular basis and is usually intensely debated. The major concern is breaking As of right now I can only suggest to use |
In many portions of the Library we find that an inconsistent C++ Code formatting style is followed.
For example, certain places have
We could use an Automatic Source Formatter to rectify such issues.
While I would recommend Clang-Format, AStyle also exists
We could create a configuration file and define the properties.
For a sample Clang-Format configuration, see this
We could decide on what the properties would be here.
To simplify our tasks and reduce the conflicts that come with the question, Clang-Format also provides a few default styles like:-
The text was updated successfully, but these errors were encountered: