Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang-format: Break before braces for lambda functions and extern blocks

I have a lambda function formatted like this:

auto cb = [](std::string const& _param)
{
    std::cout << _param;
};

I would like to keep the opening brace on a new line, but clang-format always places it at the end of the first line. Is it possible to configure clang-format to follow the style above?

The relevant part of my current configuration looks like this:

BraceWrapping:                         
  AfterClass:      true   
  AfterControlStatement: true
  AfterEnum:       true
  AfterFunction:   true          
  AfterNamespace:  true                
  AfterStruct:     true
  AfterUnion:      true
  BeforeCatch:     true
  BeforeElse:      true   
  IndentBraces:    true
BreakBeforeBraces: Allman

I would also like to do the same for extern blocks:

extern "C"
{
  // ...
}
like image 404
Federico B. Avatar asked May 17 '17 18:05

Federico B.


People also ask

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 I turn off clang format?

Disabling Formatting on a Piece of Code The code between a comment // clang-format off or /* clang-format off */ up to a comment // clang-format on or /* clang-format on */ will not be formatted. The comments themselves will be formatted (aligned) normally.

What is clang tidy?

clang-tidy is a clang-based C++ “linter” tool. Its purpose is to provide an extensible framework for diagnosing and fixing typical programming errors, like style violations, interface misuse, or bugs that can be deduced via static analysis.

Where do I put the clang format?

clang-format is located in clang/tools/clang-format and can be used to format C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C# code.


1 Answers

This appears to be addressed with BeforeLambdaBody introduced in clang-format 11.

like image 195
Jakub Jeziorski Avatar answered Oct 14 '22 08:10

Jakub Jeziorski