Can clang-format force bracing on all control statement bodies?

荒凉一梦 提交于 2020-03-22 09:42:10

问题


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.


回答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.



来源:https://stackoverflow.com/questions/58999076/can-clang-format-force-bracing-on-all-control-statement-bodies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!