Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
882 views
in Technique[技术] by (71.8m points)

coding style - how to make clang-format add new line before opening brace of a function?

I'm interested in putting an opening brace for functions (but not if statements and other contexts). For example

void foo()
{
   ...
}

Flamewars aside, is there a good rationale for not doing this? Although I use same-line open-brackets for if/else and smaller blocks, I think in this case visual organization of larger units of code (functions/methods/classes/structs) can trump perfect consistency.

Moreover, how do I get clang-format to follow this style?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As the documentation says, invoke clang-format with -style=file, and use a .clang-format file placed in an enclosing directory to customize style options. The format style option specifying brace placement is called BreakBeforeBraces. From the docs,

BreakBeforeBraces (BraceBreakingStyle)

The brace breaking style to use.

Possible values:

  • BS_Attach (in configuration: Attach) Always attach braces to surrounding context.
  • BS_Linux (in configuration: Linux) Like Attach, but break before braces on function, namespace and class definitions.
  • BS_Stroustrup (in configuration: Stroustrup) Like Attach, but break before function definitions, and ‘else’.
  • BS_Allman (in configuration: Allman) Always break before braces.
  • BS_GNU (in configuration: GNU) Always break before braces and add an extra level of indentation to braces of control statements, not to those of class, function or other definitions.

The style that matches your description is BS_Stroustrup. Add the following entry to your .clang-format

BreakBeforeBraces: Stroustrup

In addition to the docs, clangformat.com lists all the options and illustrates many of them with examples.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...