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

Martin Liška mliska at suse.cz
Tue Aug 5 06:22:49 PDT 2014


On 08/05/2014 02:08 PM, Daniel Jasper wrote:
> 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).
Unfortunately, there's no reference to coding conventions. I think there are precedents in e.g. GCC project, where all there struct initializers are indented as described. Moreover, in GNU coding style there are almost none examples of braces that are not placed to a separate line?
>
> 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:
I know that C++11 offers really funny initializers and I don't know how much sensitive clang-format can be?
>
> 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)) {
>
I tried to apply your patch, but still my struct.c sample looks the same?

Thank you,
Martin
>
>
> On Tue, Aug 5, 2014 at 2:58 PM, Martin Liška <mliska at suse.cz <mailto: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> <mailto: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> <mailto:cfe-dev at cs.uiuc.edu <mailto:cfe-dev at cs.uiuc.edu>>
>         http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
>
>




More information about the cfe-dev mailing list