[cfe-dev] clang-format support for braces in struct initialization

Daniel Jasper djasper at google.com
Tue Aug 5 06:08:34 PDT 2014


I see. Is that part of some public coding style or just how you'd like
things formatted? I didn't find any reference to this in the GNU Coding
Standards (http://www.gnu.org/prep/standards/standards.html#Formatting).

Generally, these are braced initializers and they are treated very
differently from braces of function/class definitions or blocks. This is
for good reason, especially with regard to the various braced initializers
that C++11 offers.

A crude hack to get the behavior your are desiring is:

diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp
index ed6938f..a13568b 100644
--- a/lib/Format/TokenAnnotator.cpp
+++ b/lib/Format/TokenAnnotator.cpp
@@ -1650,6 +1650,8 @@ static bool isAllmanBrace(const FormatToken &Tok) {
 bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
                                      const FormatToken &Right) {
   const FormatToken &Left = *Right.Previous;
+  if (Right.is(tok::l_brace) && Right.BlockKind == BK_BracedInit)
+    return true;
   if (Right.NewlinesBefore > 1)
     return true;
   if (Right.is(tok::comment)) {



On Tue, Aug 5, 2014 at 2:58 PM, Martin Liška <mliska at suse.cz> wrote:

>
> On 08/05/2014 01:37 PM, Daniel Jasper wrote:
>
>> Could you be more specific about what clang-format currently doesn't do
>> to your liking?
>>
>
> Sure,
>    I attached a test case, where clang-format -style=gnu produces:
>
> struct s
> {
>   int a;
>   int b;
>   int c;
> };
>
> static struct s ss = {
> my interest-----------^
>   22222222, /* First value.  */
>   33333333, /* Second value.  */
>   44444444  /* Third value.  */
> };
>
> int
> main ()
> {
>   return ss.c;
> }
>
> Where I would like to expect:
> s ss =
> {
> ...
>
>
> Thats my motivation,
> thank you,
> Martin
>
>>
>>
>> On Tue, Aug 5, 2014 at 2:27 PM, Martin Liška <mliska at suse.cz <mailto:
>> mliska at suse.cz>> wrote:
>>
>>     Hello,
>>        I would like to implement support for braces for struct
>> inialization like:
>>
>>     |address temp_addres=  {
>>       0,   // street_no
>>       nullptr,   // street_name
>>       "Hamilton",   // city
>>       "Ontario",   // prov
>>       nullptr,   // postal_code
>>     };
>>     |
>>
>>
>>     My interest is to control if the brace after equation should be
>> placed to a separate line or not (controlled by *BreakBeforeBraces). I
>> tried to read UnwrappedLineParser, but don't know how to identify such a
>> context?
>>
>>     Thanks,
>>     Martin
>>     *
>>     _______________________________________________
>>     cfe-dev mailing list
>>     cfe-dev at cs.uiuc.edu <mailto:cfe-dev at cs.uiuc.edu>
>>     http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140805/71381e37/attachment.html>


More information about the cfe-dev mailing list