1

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!

asked Jan 24, 2019 at 14:08

1 Answer 1

3

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.

answered Jan 24, 2019 at 14:27
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! As you say, not quite perfect because of where the opening brace ends up, but a lot better than it was!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.