IE, this:
if (x > 5)
return test;
Would always become:
if (x > 5)
{
return test;
}
I'm not talking about the brace style (Allman, GNU, Whiteman, etc) I just mean having the braces there at all.
There is something to prevent/enable single-line control statements like:
if (x > 5) return test;
which is AllowShortBlocksOnASingleLine
, but that's not what I'm looking for here.
If it works on clang 7 that's ideal, but if not let me know.
Standalone Tool. 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.
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.
clang-format file, we need to place it in the project folder or in any parent folder of the file you want to format. clang-format.exe searches for the config file automatically starting with the folder where the file you want to format is located, all the way to the topmost directory.
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.
I agree with dovedevic that clang-format
can't do this currently. Another alternative to consider is clang-tidy
. You can force braces around control statements using this:
clang-tidy -checks='-*,readability-braces-around-statements' -fix-errors myfile.cpp
Explanation:
-*
suppresses all checksreadability-braces-around-statements
enables that one check-fix-errors
tells clang-tidy
to fix any issues it finds, even if compilation errors were foundSee the documentation for more information.
The upcoming clang 15 provides the option InsertBraces
, which should do exactly what you ask for.
Description:
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.
(https://clang.llvm.org/docs/ClangFormatStyleOptions.html)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With