Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can clang format add braces to single line if statements etc

Is there an option for clang-format to add braces to all if()/do/while statements etc?

eg

if( i == 42 )
   std::cout << "You found the meaning of life\n";
else
   std::cout << "Wrong!\n";

to

if( i == 42 )
{
   std::cout << "You found the meaning of life\n";
}
else
{
   std::cout << "Wrong!\n";
}

Using

$ clang-format --version
clang-format version 3.6.0
like image 219
Adrian Cornish Avatar asked Sep 30 '14 01:09

Adrian Cornish


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 run a clang-format from the command line?

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. The workflow to format your changed code: Make codes changes in Electron repository.

Does Google use clang-format?

Clang-Format Style Options are flags that are supported by the ClangFormat tool, which became the de-facto standard to format C++ code. Clang offers the option to use one of the predefined styles (LLVM, Google, Chromium, Mozilla, WebKit, Microsoft) or to create a custom configuration by using the given flags.

Can Python format format clang?

There is an integration for vim which lets you run the clang-format standalone tool on your current buffer, optionally selecting regions to reformat. The integration has the form of a python -file which can be found under clang/tools/clang-format/clang-format.py .


2 Answers

clang-tidy can make syntactic changes to your code using FIXITS

clang-tidy YOUR_FILE.cpp -fix -checks="readability-braces-around-statements" -- COMPILE_OPTIONS

Updated:

clang-tidy is a bit of a heavyweight tool for this as it needs compile options to parse the file, sadly clang-format (as of v3.9) won't add braces.

COMPILE_OPTIONS would be the include paths etc that you use to compile the file with, ie -std=c++14 -stdlib=libc++ -O2 -I.

If you have a compile_options.json file from CMake then you can pass the path of the directory it is contained in to clang-tidy and it will look up the appropriate compile options for the file:

clang-tidy YOUR_FILE.cpp -fix -checks="readability-braces-around-statements" -p COMPILE_OPTIONS_DIR
like image 192
jbcoe Avatar answered Oct 12 '22 08:10

jbcoe


Starting clang-format-15 (currently trunk), the answer is yes - using the new InsertBraces option, which landed just today:
https://github.com/llvm/llvm-project/commit/77e60bc42c48e16d646488d43210b1630cd4db49 https://reviews.llvm.org/D120217

From the clang-format documentation:

InsertBraces (Boolean) clang-format 15

Insert braces after control statements (if, else, for, do, and while) in C++ unless the control statements are inside macro definitions or the braces would enclose preprocessor directives.

Warning:

Setting this option to true could lead to incorrect code formatting due to clang-format’s lack of complete semantic information. As such, extra care should be taken to review code changes made by this option.

false:                                    true:

if (isa<FunctionDecl>(D))        vs.      if (isa<FunctionDecl>(D)) {
  handleFunctionDecl(D);                    handleFunctionDecl(D);
else if (isa<VarDecl>(D))                 } else if (isa<VarDecl>(D)) {
  handleVarDecl(D);                         handleVarDecl(D);
else                                      } else {
  return;                                   return;
                                          }

while (i--)                      vs.      while (i--) {
  for (auto *A : D.attrs())                 for (auto *A : D.attrs()) {
    handleAttr(A);                            handleAttr(A);
                                            }
                                          }

do                               vs.      do {
  --i;                                      --i;
while (i);                                } while (i);
like image 33
valiano Avatar answered Oct 12 '22 09:10

valiano