There are always line breaks after an enum declaration - c++

I have the following BraceWrapping options:
BraceWrapping:
AfterEnum: false
AfterStruct: false
SplitEmptyFunction: false
AfterControlStatement: "Never"
AfterFunction: false
AfterNamespace: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
However, clang-format always inserts a new line after an enum:
enum class event_flags : std::uint8_t
{
running = 1 << 0,
executed = 1 << 1,
};
I want it to be like this:
enum class event_flags : std::uint8_t {
running = 1 << 0,
executed = 1 << 1,
};
what am I doing wrong here?

The one option I could find that seems to fix this formatting for you is outside the BraceWrapping section and that is setting
AllowShortEnumsOnASingleLine: true
even though the description of that option doesn't mention it:
true:
enum { A, B } myEnum;
false:
enum {
A,
B
} myEnum;

Related

clang-format: single-line blocks on next line

Is there a way to get clang-format to produce the following formatting:
void foo()
{
if(cond)
{ return; }
}
void f(int x)
{ return 2*x; }
I.e. I want always to see break before {, so I have
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: Always
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
however, I want short statements to be on a single line, i.e.
{ return; }
// vs
{
return;
}
So far, I am noticing that even with
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
those blocks are expanded into 3 lines.
If I set
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
I still get the same thing
I would like a configuration something like "AllowShortBlocksWhateverTheyAre"

How to put curly braces on new line in vscode for c++?

I have this option in settings:
Clang_format_fallback Style : Visual studio
Clang_format_style : file
But when I write
int main(){
}
braces are on the same line, but I want to put them on the new line like this:
int main()
{
}
How to achieve that?
edit: settings like
"javascript.format.placeOpenBraceOnNewLineForFunctions": true,
do not help for c++ (only for js)
EDIT2: .clang-format:
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
#AfterExternBlock: false # Unknown to clang-format-5.0
BeforeCatch: false
BeforeElse: false
IndentBraces: false
BreakBeforeBraces: Custom
Cpp11BracedListStyle: false
which of these settings are responsible for my issue?

Clang linebreak after access modifier

I can't find a configuration for the linebreak behaviour after access modifiers.
(But without setting MaxEmptyLinesToKeep to 0)
(To remove the linebreak after an access modifier change)
I have my current clang configuration like this:
Language: Cpp
CompactNamespaces: false
FixNamespaceComments: false
Cpp11BracedListStyle: true
NamespaceIndentation: All
PointerAlignment: Right
SortIncludes: false
SpacesInParentheses: false
SpaceInEmptyParentheses: false
SpaceAfterTemplateKeyword: false
SortUsingDeclarations: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
IndentWidth: 4
TabWidth: 4
ColumnLimit: 0
AccessModifierOffset: -1
UseTab: true
MaxEmptyLinesToKeep: 2
BreakBeforeBraces: Allman
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AlignEscapedNewlines: Right
KeepEmptyLinesAtTheStartOfBlocks: false
AllowShortFunctionsOnASingleLine: None
BraceWrapping:
AfterFunction: true
AfterClass: true
AfterStruct: true
AfterControlStatement: true
AfterEnum: true
AfterUnion: true
AfterExternBlock: true
AfterNamespace: true
AfterUnion: true
BeforeCatch: true
BeforeElse: true
SplitEmptyFunction: true
SplitEmptyRecord: true
IndentBraces: false
BreakBeforeBraces: Custom
Example:
At the moment clang produces:
class Test
{
public:
int test();
private:
int m_test;
}
But i want it to remove the linebreak so that it becomes:
class Test
{
public:
int test();
private:
int m_test;
}

clang-format weird indentation of array of structs in C++

I'm trying to use clang-format (in VS code) to format my C++ files and configure it to my preferred style. For an array of structs (for getopts) it is adding a load of extra spaces and messing up the brace wrapping:
I'll append my .clang-format at the end of this query
Here's the way I want my array to appear:
int main()
{
const struct option longopts[]=
{
{"log-file", required_argument, 0, LOGFILE},
{"log-level", required_argument, 0, LOGLEVEL},
{nullptr, 0, nullptr, 0}
};
}
Here's how it actually appears:
int main()
{
const struct option longopts[] =
{
{"log-file", required_argument, 0, LOGFILE},
{"log-level", required_argument, 0, LOGLEVEL},
{nullptr, 0, nullptr, 0}};
}
My .clang-format file contains:
BasedOnStyle: LLVM
IndentWidth: 2
AlignAfterOpenBracket: Align
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortFunctionsOnASingleLine: true
AllowShortBlocksOnASingleLine: true
BinPackParameters: true
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakConstructorInitializers: AfterColon
ColumnLimit: 0
ConstructorInitializerAllOnOneLineOrOnePerLine: false
IndentCaseLabels: true
KeepEmptyLinesAtTheStartOfBlocks: true
NamespaceIndentation: All
PointerAlignment: Right
SortIncludes: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: Never
SpaceInEmptyParentheses: false
SpacesInContainerLiterals: false
SpacesInAngles: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
UseTab: Never
Any solutions welcome!
I'm not sure that exactly what you want could be achived, but sometimes easiest way is to slighly modify the source file to help-up the clang-format utility. At first you need to add an ContinuationIndentWidth: 2 option to your format file. Then add a comma after the last item in the array:
{nullptr, 0, nullptr, 0}, // <---
And finnaly move the first curly brace on the same line as array name. The resulting file will be like this:
int main()
{
const struct option longopts[] = {
{"log-file", required_argument, 0, LOGFILE},
{"log-level", required_argument, 0, LOGLEVEL},
{nullptr, 0, nullptr, 0},
};
}
Running the clang-format will leave it as it is. Tested on clang-format from the LLVM snapshot build LLVM-9.0.0-r351376-win64.exe.

How can clang-format don't put if statements into one line?

Just like the code below, I use clang-format to automatic format my codes
if(detectBeats[*beatsCont-2] > detectBeats[*beatsCont-1]
|| fabs(detectBeats[*beatsCont-2] > detectBeats[*beatsCont-1]) < 1.0)
{
*beatsCont -=1;
}
Whatever I set the .clang-formt file, it always formatted like this:
if(detectBeats[*beatsCont-2] > detectBeats[*beatsCont-1] || fabs(detectBeats[*beatsCont-2] > detectBeats[*beatsCont-1]) < 1.0)
{
*beatsCont -=1;
}
How can I set the rules not wrap the if statements into oneline?
My question isn't as that question(Clang format splits if statement body into multiple lines), b/c my if statement wrapped, not the body
Here is my .clang-format file
AccessModifierOffset : -4
AllowAllParametersOfDeclarationOnNextLine : false
AlignEscapedNewlinesLeft : false
AlignOperands: true
AlignTrailingComments : true
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine : true
AllowShortLoopsOnASingleLine: true
BinPackArguments : false
BinPackParameters : false
BreakBeforeBraces : Linux
ColumnLimit: 0
CommentPragmas: '^ *\/\/'
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
IndentWidth : 4
KeepEmptyLinesAtTheStartOfBlocks : false
Language : Cpp
MaxEmptyLinesToKeep : 2
ObjCBlockIndentWidth : 2
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList : false
PointerAlignment: Right
ReflowComments: true
SortIncludes: true
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators : true
SpaceBeforeParens : ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments : 1
SpacesInAngles: false
SpacesInContainerLiterals : false
SpacesInParentheses : false
SpacesInSquareBrackets: false
Standard: Cpp11
UseTab : Never
You need to use ColumnLimit. Doc here. More method here