Clang-format add {} around statement after if () while () for ()

I would like to know if clang-format can be set to reduce non- compound_statement to { non-compound_statement; } { non-compound_statement; } in 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 ; 

Example

Input:

 if (exp) foo = 1; 

Output:

 if (exp) { foo = 1; } 

Then, as necessary, the decorator will back off.

+5
source share
1 answer

What you want to do goes beyond what clang style tries to achieve:

  • The only clang-format lexical elements should concern: spaces, string literals and comments. Any other changes from the order includes the removal of unnecessary parenterase not included in this tool.

Source: http://clang-developers.42468.n3.nabble.com/Design-clang-format-td3980439.html

However, clang tidy may have a function flag called readability-braces-around-statements .

Source: http://clang.llvm.org/extra/clang-tidy/checks/readability-braces-around-statements.html

+1
source

Source: https://habr.com/ru/post/1241661/