Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 326
daj Avatar asked Apr 06 '15 18:04

daj


People also ask

How do you customize your clang-format?

clang-format supports two ways to provide custom style options: directly specify style configuration in the -style= command line option or use -style=file and put style configuration in the . clang-format or _clang-format file in the project directory.

Can clang-format break code?

Short answer: YES. The clang-format tool has a -sort-includes option. Changing the order of #include directives can definitely change the behavior of existing code, and may break existing code.

How do you add a format to clang?

You can install clang-format and git-clang-format via npm install -g clang-format . To automatically format a file according to Electron C++ code style, run clang-format -i path/to/electron/file.cc . It should work on macOS/Linux/Windows.

What is clang-format default style?

clang-format file uses YAML format: key1: value1 key2: value2 # A comment. ... The configuration file can consist of several sections each having different Language: parameter denoting the programming language this section of the configuration is targeted at.


2 Answers

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.

like image 117
Pradhan Avatar answered Sep 29 '22 09:09

Pradhan


Pradhan has an excellent answer, but you may find that you get breaks where you do not want them (as I found).

There is another option, "Custom", in at least clang-format versions 3.8 and 5 (I'm using 3.8 and found BS_Custom in the 5 docs). With this, you can specify in BraceWrapping what you want, including an "AfterFunction" option.

In the following example excerpt, I have listed others as true/false since the OP's question only specifies AfterFunction (i.e. "before opening brace of a function"):

BraceWrapping:   AfterClass:      true   AfterControlStatement: true   AfterEnum:       true/false   AfterFunction:   true   AfterNamespace:  true/false   AfterObjCDeclaration: true/false   AfterStruct:     true/false   AfterUnion:      true/false   BeforeCatch:     true/false   BeforeElse:      true/false   IndentBraces:    true/false BreakBeforeBraces: Custom 

I have tested this with my configuration and it gives finer-grained control of brace breaking.

like image 23
sage Avatar answered Sep 29 '22 08:09

sage