-
Notifications
You must be signed in to change notification settings - Fork 38.7k
Coding style update with clang-format #4498
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
Conversation
|
Even with column limit 0, IMPLEMENT_SERIALIZE doesn't work well. See addrman.h, where it suddenly loses two levels of indentation, starting inside the serialize implementation, up until the end of the file. |
src/addrman.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yuck (as @sipa already commented)
|
Putting curlies inside the parens gets reasonable clang-format results: IMPLEMENT_SERIALIZE({
READWRITE(stuff);
}) |
|
@kazcw Look at addrman.h, where even that construct fails in a very odd way (we lose indentation, even after the IMPLEMENT_SERIALIZE ends). And when setting an explicit line length, it's even worse. |
src/serialize.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it need to break aligned lines? :(
|
@sipa that's not what I'm seeing in addrman.h (with or without ColumnLimit): public:
IMPLEMENT_SERIALIZE({
CAddress* pthis = (CAddress*)(this);
READWRITE(*pthis);
READWRITE(source);
READWRITE(nLastSuccess);
READWRITE(nAttempts);
})
void Init()
{
nLastSuccess = 0;I'm using a clang from yesterday [4a3ff91], so it could be a recent change. I do see behavior that looks like what you're describing when I open with ({ and close with )}. |
|
@kazcw That one works. Look at the larger one in CAddrMan. |
|
Upgrading; let's see. |
|
@sipa: I see, that block makes clang-format just give up. Maybe clang-format has a nesting limit for blocks within apparent function calls or something. I found that it works if you use just ({ and }) rather than the current (({{ and }})), though. |
|
Rebased on #4508, and changed pointer alignment to right. |
|
I retract my previous position. I think pointer* and reference& should cuddle with the type, rather than the name. |
|
It is disappointing that we lose multi-column alignments, e.g. |
|
@jgarzik Agree. it retains it in some places (for comments eg), but not always. |
|
We lose indented cpp |
|
Overall I think it is a net-win. |
|
I also prefer pointer* ptr and reference& ref over pointer *ptr and reference &ref. It is more clear to me since being a pointer is really part of type. But I don't think it is important enough to change it unless you're already touching that line. |
|
I love the intention of this pull, I really love how it feels to have a unified style all over the code! @sipa @laanwj What about the include order policy? This won't be fixed by this, right? If we intent to merge this, am I allowed to fix the includes again or not? |
|
So this is purely about indentation/whitespace. Anything that changes anything but a space or newline is outside of scope here. That doesn't mean we can have a policy about other things too - it just won't be automatically enforceable. |
I'm happy that we lose manual alignments like that. No more code changes that change surrounding lines to 'realign' when the point is just to add one another constant (that happens to be longer than the rest). Or pulls that 'fix' the silly column alignment afterwards. |
|
Added more examples. Ugly things:
|
As this is auto-generated data, it makes sense to move it to an external include file that is not affected by clang-format. Talking about autogenerated files, bitcoinstrings.cpp should also be excluded. |
|
Can I have some ACKs on the general effect of these? If accepted, I'll remove the example change here and just commit the .clang-format file. Next steps: apply the changes one by one to infrequently-changing files. The majority of them will probably need to wait until close to release candidate stage, as to not affect pull requests. |
|
ACK general direction |
|
ACK on general effect, absolutely. The roadmap seems reasonable too. And even if we will need to eventually break some PR, I wouldn't worry too much since the conflicts in the rebase should be trivial to resolve. Now excuse me for my bike shedding... We get less changes in the example files as shown in https://github.com/jtimon/bitcoin/compare/clang?expand=1 |
|
Oh, also added BOOST_REVERSE_FOREACH to ForEachMacros |
|
(untested :p) ACK on general effects |
|
(untested) ACK |
|
@jtimon: thanks for backing up that suggestion with actual numbers (less changes to the code is indeed what I was aiming for mostly at this point). |
|
Added BOOST_REVERSE_FOREACH to ForEachMacros and changed SpacesBeforeTrailingComments to 1. Then tried both PointerAlignment: Left and Right for src/*.{h,cpp}
I'm switching to: PointerAlignment: Left as well. |
|
Ready for review. I've updated coding-style.md as well. Note that I've removed the variable/class naming style as well - it wasn't being followed strictly anymore anyway. |
|
Automatic sanity-testing: PASSED, see http://jenkins.bluematt.me/pull-tester/p4498_2887bffcfdc138f53c60091ab2906790971c9af5/ for binaries and test log. |
|
ACK |
|
ACK. |
|
By the way, I've tried to find an alternative to @kazcw's solution to implement_serialize on http://clang.llvm.org/docs/ClangFormatStyleOptions.html with no luck, but the curlies inside the parenthesis don't look bad in that case in my opinion. |
|
I'm going to try to replace the IMPLEMENT_SERIALIZE macros with just (templated) class methods. |
2887bff Update coding style and add .clang-format (Pieter Wuille)
|
Thanks! |
|
Hi guys, I've proposed the coding style changes in the The rationale for most coding style changes are Stroustrup, Google coding style and common visual and readability sense. By your generous permission, I'd like to discuss the coding style changes here and if they can be made automatic using The changes are... Always add curly braces.Always add curly braces to Example: Example: Example: Always put curly braces on the new line.Surround curly-braced code with blank lines.Example: Example: Example: No one-liners.Example: Example: Maximum one blank line.Example: Could you please help to produce the Thanks, |
|
To change the .clang-format file, you may want to read this: First of all, I wouldn't change anything on it until we have applied the current style to the whole project. -Always add curly braces. I don't think these increase readability. Is python less readable than C for not having curly braces? -Surround curly-braced code with blank lines. -No one-liners. -Maximum one blank line. |
|
I disagree that coding style specifics matter much for readability. It's mostly convention - and we should mostly be aiming at consistency. People will always have different preferences about coding style. If there's enough people who prefer one style to another, I guess we can change things. But the purpose here is mostly having one style that everyone agrees on, which means the things like indentation changes after refactors can be done automatically, with much easier review requirements (just redo the reformatting locally, and check that the result is the same). For most of the choices in the style, the decisions are just based on minimizing changes to the existing code. Because actually reformatting things will take time and won't be trivial. Just doing all changes all at once will break every single pull request in a very painful way. While redoing formatting afterwards is trivial. So we'll likely want to delay reformatting changes to just before release (candidate) when big changes are in. Even then, smaller changes are better as not every PR gets in within one release cycle. |
|
Sipa, it is pity that you think that the coding style is not related to the readability. After all, the code is there to read it, change and write a new one. The C++ source code matters only to humans who mostly read it! Hence, IMHO, more readable code brings more people. Hmmm... exactly like the book - if you can read book easily, you keep reading it. Otherwise, you put it off for a while / for eternity. :-( Regarding the existing |
|
@sipa mentioned a fun thing once: once the entire source code is formatted using clang-format, you could locally format your source after checking out however suits you best, then before checking in re-format it with the 'canonical' style file of the project. Anyhow - different people have different opinions on what coding style is most readable. That's subjective. But it is a fact that a consistent coding style is more efficient. Any time saved not worrying about whether a certain { should be at the end or the beginning, or the else, and the eternal discussions, is worth it. |
|
I fully agree that the source code is there for humans. Humans are however known to have different preferences. |
This pull request just to show what the effect of running a recent clang-format-3.5 (from clang's PPA; the version in the Trusty repository misses some features still) would be on our source code.
For now, I've set the column limit to 0, which means it should respect existing line breaking decisions, but as you can see the result is still fairly arbitrary (it breaks and unbreaks in several place).
I would prefer using the normal mode where an actual column limit is specified (it breaks quite intelligently, with different penalty scores for breaking in several places), but that deals very badly with the IMPLEMENT_SERIALIZE macros.
This is not intended for merging. If we decide to adopt this policy and its effects, I'd rerun it just before release candidates, and then hopefully afterwards more frequently, but now it would obviously break every pull request in existence, with 0 benefit.