[PATCH] Fixes bug 20587 - Add K&R break before braces style

Roman Kashitsyn romankashicin at gmail.com
Sun Aug 10 23:20:17 PDT 2014


> I think Linux uses K&R style. So we should not need to introduce a new style but fix Linux style instead.
Yep, that's what I wrote in the original bug. I don't mind fixing linux style. But it could break backward compatibility.

> According to all resources I have found, K&R style also puts the "{" on the new line for classes and namespaces.
The only resource I've found is [AStyle Manual](http://astyle.sourceforge.net/astyle.html).
Original K&R book contains no classes nor namespaces, so everyone is free to interpret K&R style for C++ constructs as they like. I wish I could ask Ritchie how would he place braces...
My opinion is that Stroustrup style is the closest one to original K&R style, it just adds a few more breaks for control structures.

> we will need to handle structs and classes differently.
I don't think it's a good idea. From C++ point of view, struct is almost identical to class. If formatter changes the breaks when I replace struct with a class, it will confuse me much. I don't know anyone who wish to format structs and classes differently.

Possibly, it's better to provide fine-grained control over the brace usage (break after struct/class/) like [Uncrucify](https://github.com/bengardner/uncrustify) do. The BreakBeforeBrace could then be high-level interface for fine-grained options.

================
Comment at: include/clang/Format/Format.h:287
@@ +286,3 @@
+    BS_GNU,
+    /// Like \c Attach, but break before braces on named function
+    /// definitions.
----------------
Daniel Jasper wrote:
> "named"?
> 
> Also, maybe "..., but only break ..."?
I used "named" here because I'm not sure how to format lambdas in K&R. I think it's perfectly legal to attach braces to lambda declarations because lambdas could be nested (unlike named functions).
Either way, comments for other styles don't stress this, so I'll remove "named".

http://reviews.llvm.org/D4837






More information about the cfe-commits mailing list