3

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.

Steve Friedl
  • 3,929
  • 1
  • 23
  • 30
rioki
  • 5,988
  • 5
  • 32
  • 55
  • Thanks for telling me about uncrustify. I think I might like it (in addition to)/(over) astyle, which I normally use :) – sehe Sep 05 '12 at 08:38
  • You might want to try UniversalIndentGUI, there you can try a bunch of different formatting tools and see the result immediately. – rioki Sep 05 '12 at 09:21
  • Oh and I took a spin of all the free formatting tools for C++ (I could find): http://www.rioki.org/2012/09/04/cleanup-yerr-code.html – rioki Sep 05 '12 at 09:22

2 Answers2

2

Reading the docs, I came up with this:

# Add or remove newline between a function name and the opening '('
nl_func_paren                            = remove   # ignore/add/remove/force

# Add or remove newline between a function name and the opening '(' in the definition
nl_func_def_paren                        = remove   # ignore/add/remove/force

# Add or remove newline after '(' in a function declaration
nl_func_decl_start                       = remove   # ignore/add/remove/force

# Add or remove newline after '(' in a function definition
nl_func_def_start                        = remove   # ignore/add/remove/force

# Add or remove newline after each ',' in a function declaration
nl_func_decl_args                        = remove   # ignore/add/remove/force

# Add or remove newline after each ',' in a function definition
nl_func_def_args                         = remove   # ignore/add/remove/force

# Add or remove newline before the ')' in a function declaration
nl_func_decl_end                         = remove   # ignore/add/remove/force

# Add or remove newline before the ')' in a function definition
nl_func_def_end                          = remove   # ignore/add/remove/force

As you anticipate, the comments kind-of ruin it, though. There is an option to change single-line comments (//) into block comments (/* ... */) though, which should make it easier for you to join lines manually (e.g. in vim v%J)

# Whether to change cpp-comments into c-comments
cmt_cpp_to_c                             = true    # false/true

I tested it with prototypes, declarations and calls:

The calls are not affected. Note also the following related option:

# Whether to fully split long function protos/calls at commas
ls_func_split_full                       = false    # false/true
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Well I already use the nl_func*** options. They work on the declaration and definition of the functions quite well, but not the call. I know about cmt_cpp_to_c but I don't want to use it for other reasons, I can live with it sort of. I would need a "remove comments in function calls / definitions" option. If the call merging would work, I would just remove the comments with editor fu and rerun uncrustify... – rioki Sep 05 '12 at 08:44
  • Next time, please state this information in the post. It saves you the energy of reading what you already know. Pro tip: To remove the comments, consider using the preprocessor (e.g. in vim): `:'<,'>!cpp | grep -Ev '^\#'` – sehe Sep 05 '12 at 09:02
  • "I got it to work for function declarations, but I don't get it to work for function calls." - rioki Sorry though it was clear, but thanks for trying anyway. – rioki Sep 05 '12 at 09:15
0

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.

rioki
  • 5,988
  • 5
  • 32
  • 55