叮当格式的强制支撑可以对所有控制语句主体进行支撑吗?



IE,这个:

if (x > 5)
  return test;

将永远变成:

if (x > 5)
{
  return test;
}

我不是在谈论大括号样式(Allman,GNU,Whiteman等(,我只是说在那里有大括号。

有一些东西可以防止/启用单行控制语句,例如:

if (x > 5) return test;

这是AllowShortBlocksOnASingleLine,但这不是我在这里寻找的。

如果它适用于 clang 7,那是理想的,但如果不是,请告诉我。

我同意鸽子的观点,clang-format目前不能这样做。要考虑的另一种选择是 clang-tidy .您可以使用以下命令在控制语句两边强制使用大括号:

clang-tidy -checks='-*,readability-braces-around-statements' -fix-errors myfile.cpp

解释:

  • -*禁止显示所有检查
  • 然后readability-braces-around-statements启用该检查
  • 然后-fix-errors告诉clang-tidy修复它发现的任何问题,即使发现编译错误也是如此

有关详细信息,请参阅文档。

即将推出的 clang 15 提供了选项 InsertBraces ,它应该完全符合您的要求。

描述:

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(

你想坚持下去是可以理解的clang-format但是我最近的帖子让我陷入了你所处的类似兔子洞。似乎clang-format主要用作仅空格格式化程序。为了得到你想要的,我建议使用Uncrustify。构建过程非常简单(有关详细信息,请参阅 github 页面(,您的配置如下:

$ 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

您可以使用以下命令对源文件运行 Uncrustify:

$ uncrustify -c /path/to/.uncrustify --no-backup example.c

如果您需要更多格式化选项,他们的在线配置工具提供了大量可配置项的一些示例和描述。

您声明:

我不是在寻找要安装的辅助工具 - 它必须与未更改的 clang 格式安装一起使用。[...]

恐怕从clang-format 6.0(我研究和测试的内容(和 7.0(我研究的内容(来看,这似乎是不可能的。

最新更新