Search code examples
c++cformattinguncrustify

Uncrustify Collapse Multiline Function Call


I have function calls that look like this (for no apparent reason):

func
(
    a,
    b,
    c
)

Is there a way to make uncrustify collapse the function into a single line? I have been trying for two days not on and off...

I got it to work for function declarations, but I don't get it to work for function calls.

While we are at it I also have functions that look like so:

func
(
    a, // (IN) the A
    b, // (IN) something b
    c  // (OUT) the resulting value
)

Is there a way to handle that case too, without breaking the code? Since uncrustify keeps comments, I think this is kind of impossible. With function declarations it collapses it to the first comment.


Solution

  • After some LOOONG research I have come to the conclusion, that uncrustify can't do that. For my porposses I hacked a small perl script together:

    $filename = $ARGV[0];
    
    {
        open(FILE, "<", $filename) or die "Cant open $filename for reading\n";
        local $/ = undef;
        $lines = <FILE>;
        close(FILE);
    }
    
    # squash comments in function calls and declarations
    $lines =~ s/,[ \t]*\/\/[^\n\r]*/,/gm;
    # squash last comment in function calls and declarations
    $lines =~ s/[ \t]*\/\/[^\n\r]*\r\n[ \t]*\)/\)/gm;
    # squash newlines at the start of a function call or declaration
    $lines =~ s/\([ \t]*\r\n[ \t]*/\(/gm;
    # squash newlines in function calls and declarations
    $lines =~ s/,[ \t]*\r\n[ \t]*/, /gm;
    # squash the last newline in a function call or declaration
    $lines =~ s/[ \t]*\r\n[ \t]*\)/\)/gm;
    
    {
        open(FILE, ">", $filename) or die "Cant open $filename for writing\n";
        print FILE $lines;
        close(FILE);
    }
    

    I will look into if I can build a patch that integrates that feature into uncustify.