6

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.

Tyler Shellberg
  • 1,086
  • 11
  • 28
  • Does this answer your question? [Can clang format add braces to single line if statements etc](https://stackoverflow.com/questions/26111162/can-clang-format-add-braces-to-single-line-if-statements-etc) – cigien Aug 31 '21 at 16:06

3 Answers3

5

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 tells clang-tidy to fix any issues it finds, even if compilation errors were found

See the documentation for more information.

Eric Backus
  • 1,614
  • 1
  • 12
  • 29
2

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)

Bouncner
  • 2,124
  • 1
  • 19
  • 19
1

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.

dovedevic
  • 673
  • 2
  • 14
  • 33
  • For a few reasons, it's not possible to use both Uncrustify and ClangFormat right now, only one. But thank you for at least showing that you researched into it and couldn't find anything either. – Tyler Shellberg Feb 11 '20 at 15:47
  • Not a problem. It's unfortunate your build process cant integrate this, but at least *some* solution exists, which I was in a similar situation to. Perhaps you can find a work around at some point. Have you looked into if `clang-tidy` can integrate into your build workflow? – dovedevic Feb 11 '20 at 19:37
  • I believe it can. Is it possible to have clang-tidy somehow also run clang-format *and* do some additional work on top of that? I've yet to use clang-tidy. It would be ideal if we had to change as little as possible to get it working. And if clang-tidy can be run on saving the file, all the better. – Tyler Shellberg Feb 12 '20 at 21:07
  • Perhaps. What environment are you writing source in? Windows/Mac/Linux/CLI? – dovedevic Feb 12 '20 at 22:35
  • Windows and Linux, with QT currently running the clang-format on file save, though others set up emacs to handle that as well. If those can both run clang-tidy which will then run clang-format on save *and* the bracing stuff, that'd be neat. – Tyler Shellberg Feb 12 '20 at 22:39
  • I'd have to do some research into that, but I do think `clang-tidy` can call / do what `clang-format` can do. So in a sense if you can get `clang-tidy` to work, you are already getting `clang-format` to "work". Another solution, which might not be something you or your development team might want, would be to transfer to a more intelligent IDE such as VS / VS Code and use C++ Resharper. These can enforce syntax rules *as you type*. – dovedevic Feb 12 '20 at 22:42
  • Another thing, if you have QT running `clang-format` at save time, are you not able to reconfigure the executable it is running then? – dovedevic Feb 12 '20 at 22:43