4

I would like to use clang-format to product C functions like:

void cfg_InitConfig
(
    cfg_Config_t* cfg,
    char*         name
)
{
    // TODO function
}

After reading the manual I don't think clang-format can do it. Is it possible?

Julien Vermillard
  • 2,945
  • 1
  • 18
  • 18
  • 1
    looks like it's not possible from the answers, thanks for trying! I'm going to test http://uncrustify.sourceforge.net/ apparently it's more flexible. – Julien Vermillard Dec 02 '19 at 08:42

3 Answers3

2

This is my current best solution to break function parameters to have code fences instead of long lines. I hope you can find something useful here.

In order to become fenced the code in question has to be longer than a limit, so here is how it formats the longer example:

void cfg_InitConfig(cfg_Config_t *cfgxxxxxxxxxxxxxx,
                    char *namexxxxxxxxxxxxxxxxxxx) {
  // TODO function
}

The ! symbol means that the lines have to do with code fences.

---
Language:      Cpp
BasedOnStyle:  Google

AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignOperands: false
AlignTrailingComments: false

# !
AllowAllParametersOfDeclarationOnNextLine: false

AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None

AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: Yes

# !
BinPackArguments: false
BinPackParameters: false

ColumnLimit: 80
ConstructorInitializerIndentWidth: 2
ContinuationIndentWidth: 2
CompactNamespaces: false

IncludeBlocks: Preserve
IndentWidth: 2

DerivePointerAlignment: false
PointerAlignment: Right

SortIncludes: true
SortUsingDeclarations: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: true
Stanislav Pankevich
  • 11,044
  • 8
  • 69
  • 129
1

You can almost achieve the requested format with those settings:

echo 'void cfg_InitConfig(cfg_Config_t*cfg,char*name){return 0;}' \
    | \
    clang-format-14 \
        --Werror \
        --style="{\
            AlignAfterOpenBracket: BlockIndent, \
            AlignConsecutiveDeclarations: true, \
            AllowAllParametersOfDeclarationOnNextLine: false, \
            BinPackParameters: false, \
            ColumnLimit: 30, \
            IndentWidth: 4, \
            PointerAlignment: Left \
            }"

Output:

void cfg_InitConfig(
    cfg_Config_t* cfg,
    char*         name
) {
    return 0;
}
Yogev Neumann
  • 2,099
  • 2
  • 13
  • 24
0

I have studied lot of lot of documents according to your problem. Unfortunately there is no way to break your code like this

void cfg_InitConfig
(
    cfg_Config_t* cfg,
    char*         name
)

but someone can be create patch for break that kind of alignment it could be possible. Creating that kind of patch not so much easy.

but if you use this, you can get close answer(80% correct) but not actually you want. here is the code

BinPackArguments : false
BinPackParameters: false
AlignConsecutiveAssignments : true
AlignConsecutiveDeclarations: true

using this outcome will be

void cfg_InitConfig(cfg_Config_t* cfgxxxxxxxxxx,
                    char*         namexxxxxxxx)
{
    // TODO function
}

you have to extend your variable names of the function unless they could fit on one line

Reference

Kalana
  • 5,631
  • 7
  • 30
  • 51