[cfe-dev] clang-format: Nested initialiser lists - using continuation indent?

John Beard via cfe-dev cfe-dev at lists.llvm.org
Fri Jan 11 02:26:09 PST 2019


Hi,

[This is a cross-post from Stack Overflow [1], since I was advised to
bring this to the mailing list.]

I am using clang-format (generally rather happily and effectively with
git-clang-format) with rules including the following (only relevant
rules included):

BasedOnStyle: LLVM
BreakBeforeBraces: Allman
ContinuationIndentWidth: 8
Cpp11BracedListStyle: false
IndentWidth: 4

The continuation indent is set to 8, so that continued function
signatures and calls wrap deeper than a "normal" indent. However, I
also have data in tables, often manually formatted like this (usually
the inner data is some kind of struct, not int, but the principle is
similar, and there are often comments at line ends):

static std::vector<std::vector<int>> data{
    {
        42, // Answer
    },
};

The above is how I expect it to look, considering that
`Cpp11BracedListStyle` is set to `false`, so it should use the block
indent (4), not the continuation indent (8). From the clang-format
docs [2], if this is `true`:

> "Important differences: - No spaces inside the braced list. - No line
> break before the closing brace. - Indentation with the continuation
> indent, not with the block indent."

So, I expected to see the "block indent" (4) used. However, what I
*actually* get is:

static std::vector<std::vector<int>> data{
    { // indent 4 - OK
           42, // Indented by 4 + 8 (expected 4 + 4)
    },
};

As you can see, the "outer" initialiser list is indented by 4 (as
expected), but the "inner" list's elements are indented by a further
8, not 4. If I change `Cpp11BracedListStyle` to `true`, all levels are
indented by 8 (this appears consistent with the documentation):

static std::vector<std::vector<int>> data{
        { // Indent by 8
                42, //Indent by 16
        },
};

Is there a way to achieve the first formatting (4+4) using
clang-format? Having the inner lists indent by 8 uses a lot of
horizontal area for the lists, especially when I have nested structs,
which are common, especially in lists of test cases.

Thanks,

John

  [1]: https://stackoverflow.com/questions/54127689/clang-format-indents-inner-initialisers-with-continuation-indent
  [2]: https://clang.llvm.org/docs/ClangFormatStyleOptions.html



More information about the cfe-dev mailing list