0

I'm looking for a Clang configuration that will convert code A into code B (see below).

My preference is to "explode" functions with 3 or more arguments for personal readability, and I haven't found a Clang configuration to do this exactly how I want.

I suspect there isn't one, but I wanted to make sure. There does not appear to be a Clang option to put the first parenthesis of a function call on a newline if the number of args is > x or if the line is over a certain char length.

I would certainly be open to another style tool with the suggested flags/options to do what I want.

Desired output

Code A (input)

class X {
public:
    int function(Arg arg1, Arg arg2);

private:
    int _biggerFunction(Arg1 arg1, Arg2 arg2, bool arg3);
};

int X::function(Arg1 arg1, Arg2 arg2) {
    return arg1 + arg2;
}

int X::_biggerFunction(Arg1 arg1, Arg2 arg2, bool arg3) {
    if (arg3) {
        return arg1 + arg2;
    }
    else {
        return arg1 - arg2;
    }
}

Code B (output)

class X
{
public:
    int function(Arg arg1, Arg arg2);

private:
    int _biggerFunction
    (
        Arg arg1,
        Arg arg2,
        bool arg3
    );
};

int X::function(Arg1 arg1, Arg2 arg2)
{
    return arg1 + arg2;
}

int X::_biggerFunction
(
    Arg1 arg1,
    Arg2 arg2,
    bool arg3
)
{
    if (arg3)
    {
        return arg1 + arg2;
    }
    else
    {
        return arg1 - arg2;
    }
}

Attempted solution

I've used the Clang Style Detector (in Clang Power Tools for Visual Studio) to try and find a matching configuration, but the result wasn't quite what I wanted. Clang Style Detector recommended the following:

# Format Style Options - Created with Clang Power Tools
---
BasedOnStyle: WebKit
BreakBeforeBraces: Allman
ColumnLimit: 50
ExperimentalAutoDetectBinPacking: true
...

However, the result is not a good style match:

Clang Style Detector test

Code A (input)

class X {
public:
    int function(Arg arg1, Arg arg2);

private:
    int _biggerFunction(Arg1 arg1, Arg2 arg2, bool arg3);
};

int X::function(Arg1 arg1, Arg2 arg2) {
    return arg1 + arg2;
}

int X::_biggerFunction(Arg1 arg1, Arg2 arg2, bool arg3) {
    if (arg3) {
        return arg1 + arg2;
    }
    else {
        return arg1 - arg2;
    }
}

Code B (output)

class X
{
public:
    int function(Arg arg1, Arg arg2);

private:
    int _biggerFunction(
        Arg1 arg1, Arg2 arg2, bool arg3);
};

int X::function(Arg1 arg1, Arg2 arg2)
{
    return arg1 + arg2;
}

int X::_biggerFunction(
    Arg1 arg1, Arg2 arg2, bool arg3)
{
    if (arg3)
    {
        return arg1 + arg2;
    }
    else
    {
        return arg1 - arg2;
    }
}
2
  • 1
    I suspect that there's no tool that'd add more line breaks due to width limit than necessary due to width limit. You run into a logic leap where the only reason there are newlines around ( is signature too long, but splitting signature by other rules removes that reason. There's only one sweet spot where the function name ends exactly at the width limit - longer and you need a newline after type, shorter and you don't need a newline before (. Commented Nov 25, 2024 at 12:36
  • 1
    That said, you can try adding it to a format tool by yourself. I've done that for options I wanted in uncrustify - much easier to get into than clang-format and standalone. Focus on file newlines.cpp, function void newline_func_multi_line and chunk newline_iarf(start, - it only adds newline after and you need to change that into before&after for an extra option. Commented Nov 25, 2024 at 12:43

0

Your Answer

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.