Space after 'if', 'while', 'catch' etc.. with clang-format - c++

Cannot figure out that option adds the space after if, while, catch, etc...
Currently my .clang-format file produce this:
while(true)
{
if(flushedCount == count)
{
break;
}
}

The clang-format configuration option controlling space after if, while, catch and other control statements is called SpaceBeforeParens.
SpaceBeforeParens: ControlStatements
From clang-format 8 documentation:
SpaceBeforeParens (SpaceBeforeParensOptions)
Defines in which cases to put a space before opening parentheses.
Possible values:
[...]
SBPO_ControlStatements (in configuration: ControlStatements) Put a space before opening parentheses only after control statement keywords (for/if/while...).
[...]

Related

Eclipse / CDT auto-indent on new line only incorrect for structs?

Using new Eclipse and CDT versions built into STM32CubeIDE. I have the built in formatter options adjusted and use clang-format as my main beautifier. Everywhere I can see in the Window >> Preferences menus I have indent turned to 2 spaces-only.
Next line indent works correctly with everything but structs which the indentation seems to be doubled.
It doesn't seem to be indenting 2 units twice, because pressing tab moves me 4 spaces while inside a struct block. Clang-Format with CTRL + SHIFT + F does fix the incorrect formatting, but I'd rather a proper solution.
Either it's a bug, or somewhere this is yet another setting for "4 space indent but only while inside a struct block"?
See examples:
typedef struct
{
//New lines created inside the block start here, indented at 4 and not 2
//I get here if I press tab from the start column
//This is where it should intent to, manually pressed spaced twice
} some_new_t;
void foo()
{
//Correct
}
if (something)
{
//Correct
}
while(1)
{
//Correct
}
#ifdef TEST
//Doesn't indent, that's fine
#endif
EDIT: Applies to unions as well
Figured it out. This was not a 2 spaces vs 4 spaces issue, rather double indent when you only wanted one.
For some reason CDT has a lot more references to C++ and still Java than C and this was labeled under something misleading.
Under Window, Preferences, C/C++, Code Style, Formatter >> Edit >> Indentation >> Indent there are two options that can be checked.
'public', 'protected', 'private' within class body
and
Declarations relative to 'public' 'private'
You can check ONLY ONE of these for C structures and unions to indent at your chosen width. For whatever non-C reason, each one counts as including one indent width.

clang-format won't attach brace if there is a newline

I got a problem regarding clang-format:
What I want to enforce is that braces at the start of blocks are always attached to the function head / control sequence ...
This means that instead of
int f()
{
return 1;
}
or
if(o < 1)
{
return -1;
}
clang-format should always attach the opening brace like this:
int f() {
return 1;
}
and
if(o < 1) {
return -1;
}
While this works for the first case (no newline between function head and brace), it does not work if there is a newline between the function head / if etc.
I did not find any option for clang-format that enforces this.
The option closest to what I am looking for is BreakBeforeBraces: Attach, but this did not format the second case properly either. Playing around with this option as well as the options underneath BraceWrapping did not solve the issue as well.
Is there a way to configure clang-format to join lines such as the opening brackets always end up at the end of the last line of code before them?
I played around with online configurators like https://clangformat.com/ or http://cf.monofraps.net/ but could not find an option set that would serve my needs.
I am currently using clang-format version 3.8 .
You could use:
MaxEmptyLinesToKeep: 0
This would cause the empty lines to be deleted, and then the braces would get formatted the way you request.
However, I expect this is not really a good solution for you, because it will delete all empty lines, not just the empty lines before a brace.
Other than this, I don't think this is possible with clang-format 6.0.0. I don't think it has been added in newer versions either - nothing in the documentation seems to relate to this.

How to configure Vim's indentation for closing braces in C and C++ files?

I'm working with some code (C and C++) that's presently formatted as (3 spaces):
void foo() {
bar();
}
I want to modify the code so that it's indented one more space (4 spaces):
void foo() {
bar();
}
In Vim I've set:
set expandtab
set shiftwidth=4
set softtabstop=4
But then when I use == or ='(mark) to autoindent a line or set of lines, it gives me:
void foo() {
bar();
}
Is there a setting which controls how the closing brace is indented? The practice for the code I'm working on presently is that the closing brace is indented the same amount as the contents of the block. Vim, however, does not indent the closing brace.
Let's assume you're using the cindent option. Then you can just set cino=}1s to indent the closing braces by one level of indentation (one shiftwidth). See cinoptions-values in vim help for more information.

Regex pattern to match switch statements C++

I am trying to write a regex pattern to be used in a bash script which checks for the syntax of switch statements (C++).
The syntax for switch statements which I want to follow is the following one.
switch(expression)
{
case constant-expression:
statement(s);
break; // must be present
case constant-expression:
statement(s);
break; // must be present
....
....
default : // must be present
statement(s);
break; // must be present
}
Please note that even though the break and default statements are not a must, I wish to check for their presence.
I have written this regex pattern to match switch blocks.
switch(.*?)\n(\s)*?{(\n(.*?))*?(\n(\s)*case(.*?):?(\n(.*?))*?break;)+(\n(.*?))*?\n(\s)*(default:)?(\n(\s)*)*(break|return(.*?))?;(\n(\s)*(.*?))*}
It successfully matches switch blocks but the problem is that it matches the switch blocks even if the break and default statements are missing. I tried using + operator with the break and default words but they don't seem to work.
EDIT UPDATE:
Is it possible to match switch blocks such as the following one using a parser?
switch (PC_INT[address.port][address.pin])
{
#if defined (__AVR_ATmega2560__) || defined(__AVR_AT90CAN128__)
case EINT_0:
// Mask the interrupt so it doesn't fire anymore, i.e put a zero in the mask register.
EIMSK &= ~(1 << INT0);
break;
case EINT_1:
EIMSK &= ~(1 << INT1);
break;
....
default:
return GPIO_INT_OUT_OF_RANGE;
#elif defined(__AVR_ATmega64M1__) || defined(__AVR_ATmega64C1__)
case EINT_0:
// Mask the interrupt so it doesn't fire anymore, i.e put a zero in the mask register.
EIMSK &= ~(1 << INT0);
break;
case EINT_1:
EIMSK &= ~(1 << INT1);
break;
....
default:
return GPIO_INT_OUT_OF_RANGE;
#else
#error "GPIO interrupts not implemented for this configuration."
#endif
}
Non-greedy patterns (like .*?) are not magic.
You apparently expect the .*? in (\<case:.*?\<break;\s*)+ (a simplified form of your regex) to not match case:. Why wouldn't it? In other words, the text:
case 1:
do_something();
case 2:
do_something_else();
break;
certainly matches case.*?break;; the .*? matches 1: do_something(); case 2: do_something_else();.
.*? isn't a fence, either. case.*?break(more) might not match the first break following the case, if (more) doesn't match the text following the first break but does match the text following the second one.
As for the default: apparently being optional, that's precisely what your regex says:
(default:)?
I don't think the regex is salvageable. You can't parse C or C++ with regexes.
You really need to use a better parsing infrastructure. You could build a simple parser using flex and bison which would work for source code which doesn't play games with the preprocessor, but you might be better off using a real C++ parsing library, like libclang.

Visual Studio 2010 indentation after for loop

Why am I getting this behavior right after the if block? Am I missing something?
for (;;)
if (/*...*/)
{
// statements
}
// statements indented to match the if indentation instead of the for loop;
Visual Studio 2010 appears to be riddled with editor bugs. Indentation is particularly hosed.
Just wait until it starts moving your cursor to the beginning of the line every time you type a ':'.
If you close the file and reopen it that sometimes fixes the issue...for a little while anyway.
About the only way to keep VS doing indentation reasonably is to always use a block to enclose the statement controlled by a for, if, while, etc. In your case that would mean:
for (;;)
{
if (/* ... */)
{
// ...
}
}
// further statements here indented to match for loop.