clang-format support for braces in struct initialization

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

Could you be more specific about what clang-format currently doesn’t do to your liking?

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

struct.c (195 Bytes)

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)) {

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