Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

clang-format

I'd like to know if clang-format could be setup to reduce non-compound_statement into { 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 the beautifier would indent as needed.

like image 923
Phi Avatar asked Jan 27 '16 21:01

Phi


People also ask

How do you add a format to clang?

You can install clang-format and git-clang-format via npm install -g clang-format . To automatically format a file according to Electron C++ code style, run clang-format -i path/to/electron/file.cc . It should work on macOS/Linux/Windows.

Is clang tidy a Linter?

clang-tidy is a clang-based C++ “linter” tool. Its purpose is to provide an extensible framework for diagnosing and fixing typical programming errors, like style violations, interface misuse, or bugs that can be deduced via static analysis.

Can clang format break code?

Short answer: YES. The clang-format tool has a -sort-includes option. Changing the order of #include directives can definitely change the behavior of existing code, and may break existing code.


1 Answers

What you want to do is outside of the scope of what clang-format tries to achieve:

  • 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.

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

However clang tidy can, the feature flag is called readability-braces-around-statements.

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

like image 52
kamikaze Avatar answered Nov 15 '22 06:11

kamikaze