'Can clang-format force bracing on all control statement bodies?
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.
Solution 1:[1]
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:
- The
-*
suppresses all checks - Then
readability-braces-around-statements
enables that one check - Then
-fix-errors
tellsclang-tidy
to fix any issues it finds, even if compilation errors were found
See the documentation for more information.
Solution 2:[2]
It's understandable you would want to stick with clang-format
however my recent post has led me down a similar rabbit hole you are in. It appears that clang-format
is mostly used as a whitespace-only formatter. To get exactly what you want, I recommend using Uncrustify. The build process is very easy (see the github page for details), and the config for you is as follows:
$ cat .uncrustify
# Uncrustify-0.70.1
nl_if_brace = remove
nl_brace_else = force
nl_elseif_brace = remove
nl_else_brace = remove
nl_else_if = remove
nl_before_if_closing_paren = remove
nl_for_brace = remove
nl_while_brace = remove
nl_do_brace = remove
nl_brace_while = remove
nl_multi_line_sparen_open = remove
nl_multi_line_sparen_close = remove
nl_after_vbrace_close = true
mod_full_brace_do = force
mod_full_brace_for = force
mod_full_brace_function = force
mod_full_brace_if = force
mod_full_brace_while = force
You can run Uncrustify against your source file this using the command:
$ uncrustify -c /path/to/.uncrustify --no-backup example.c
Should you require even more formatting options, their online config tool has some examples and descriptions for a plethora of configurables.
You state:
I'm not looking for a secondary tool to install - it must work with an unaltered install of clang-format. [...]
I'm afraid as of clang-format
6.0 (what I researched and tested on) and 7.0 (what I researched on) it doesn't seem possible.
Solution 3:[3]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Eric Backus |
Solution 2 | dovedevic |
Solution 3 | Bouncner |