我想知道是否可以设置 clang-format 以减少非 compound_statement
进入 { non-compound_statement; }
在 iteration_statement
.
statement
: labeled_statement
| compound_statement
| expression_statement
| selection_statement
| iteration_statement
| jump_statement
;
iteration_statement
: WHILE '(' expression ')' statement
| DO statement WHILE '(' expression ')' ';'
| FOR '(' expression_statement expression_statement ')' statement
| FOR '(' expression_statement expression_statement expression ')' statement
;
示例
输入:
if (exp) foo = 1;
输出:
if (exp) { foo = 1; }
然后美化器会根据需要缩进。
最佳答案
您想要做的超出了 clang-format 试图实现的范围:
- The only lexical elements clang-format should touch are: whitespaces, string-literals and comments. Any other changes ranging from ordering includes to removing superfluous paranthesis are not in the scope of this tool.
来源:http://clang-developers.42468.n3.nabble.com/Design-clang-format-td3980439.html
然而,clang tidy 可以,功能标志被称为
readability-braces-around-statements
.来源:http://clang.llvm.org/extra/clang-tidy/checks/readability-braces-around-statements.html
关于在 if() while() for() 之后的语句周围添加 {},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35047952/