简体   繁体   中英

Configuring uncrustify to add braces

I'm using version of 0.66.1 of uncrustify, and am puzzled by the behavior of 'mod_full_brace_if' which I've set to 'force'. Its comment says:

Add or remove braces on single-line 'if' statement. Will not remove the braces if they contain an 'else'.

Given the line:

if (flag) val = 10;

I hoped/expected it to be transformed to

if (flag) { val = 10; }

Instead it remains unchanged.

Is this just my misunderstanding of the behavior of 'mod_full_brace_if'?

Later: I had some time to do a little experimentation at home. I started off by creating a new format file, and modified some of the settings having to do with forcing braces to be on the same line as various keywords, as well as some settings forcing braces to surround a one-line body. I made a test file with the following contents:

void foo() {
bool flag;
int var;
if (flag) var = 10;
if (!flag)
    var = 20; 
}

Running uncrustify with my new config file yielded the same lack of transformation that I saw at work.

I then created another config file and only changed 'mod_full_brace_if' (to 'force'). Using it on my test file resulted in braces surrounding the bodies of the if statements. Clearly there's some weird interaction of multiple settings. I see some change-a-setting-and-test drudgery in my future.

It turns out that the problem was setting "mod_full_brace_if_chain" to "true".

The documentation says:

Make all if/elseif/else statements in a chain be braced or not. Overrides mod_full_brace_if.

If any must be braced, they are all braced. If all can be unbraced, then the braces are removed.

I didn't read the last sentence closely enough when setting the value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM