[cfe-users] Questions about clang-format for specific formatting rules

Seth Cantrell seth.cantrell at gmail.com
Thu Oct 16 21:20:56 PDT 2014


> On Oct 16, 2014, at 5:40 PM, Robert Dailey <rcdailey.lists at gmail.com> wrote:
> 
> I just started taking a look at Clang-Format on Windows using clang
> 3.6 and I have a few questions concerning formatting rules.
> 
> 1. Is there a way to force constructor member initiailizer lists to
> always use 1 line per member? Example:
> 
> Foo::Foo()
>    : m_member1(1)
>    , m_member2(2)
> {}

BreakConstructorInitializersBeforeComma: true should do this.

> 
> 2. The coding standard we use on the project I am involved in requires
> that class indentation be relative to the indentation of the access
> specifiers. For example:
> 
> class Foo
> {
>    public:
>        Foo();
> };
> 
> Basically if an access specifier is provided, it follows the normal
> indent width (4 in this case). Everything *after* the access specifier
> is indented relative to the indentation of the specifier before it (so
> actually it would be indented twice). If there are no access
> specifiers in a class, indentation goes back to the normal 1 indent.

I do not think this is possible. Members get one level of indentation relative to the class scope.

> 
> 3. When the parameters of a function exceed the length of the line,
> I'd like to see a way to make the parameters get their own dedicated
> lines indented 1 level from the start of the function header. For
> example:
> 
> // Priority 1: All fits on one line
> void Foo::SomeFunction(int longparam1, int longparam2, int longparam3);
> 
> // Priority 2: No room, try 1 carriage return:
> void Foo::SomeFunction(
>    int longparam1, int longparam2, int longparam3);
> 
> // Priority 3: No room for priority 2, so each gets its own line
> void Foo::SomeFunction(
>    int longparam1,
>    int longparam2,
>    int longparam3);

You might be able to get close with something like BinPackParameters: false and some value for PenaltyBreakBeforeFirstCallParameter.

When parameters are formatted to one-per-line I think the first parameter will be on the same line as the rest of the declaration and the rest of the parameters are aligned with the first.

> 
> 4. Is there a way to make nested namespaces follow this formatting style?
> 
> namespace Outter {
> namespace Inner {
> 
> class Foo;
> 
> }} // namespace Outter::Inner
> 
> Notice the closing braces are collectively placed on the same line.
> Also none of the namespaces have indentation.

You can do NamespaceIndentationKind: None, but I don't think you can get the closing braces on the same line.

> 
> Thanks in advance!
> _______________________________________________
> cfe-users mailing list
> cfe-users at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-users





More information about the cfe-users mailing list