How to debug/reformat C printf calls with lots of arguments in vim?
I have a function call in a program that I'm maintaining has 28 arguments for a printf call. It's printing a lot of data in a CSV file. I have problems following finding where what goes and I have some mismatches in the parameters types. I enabled -Wall in gcc开发者_运维问答 and I get warnings like:
n.c:495: warning: int format, pointer arg (arg 15)
n.c:495: warning: format argument is not a pointer (arg 16)
n.c:495: warning: double format, pointer arg (arg 23)
The function is like this:
fprintf (ConvFilePtr, "\"FORMAT3\"%s%04d%s%04d%s%s%s%d%s%c%s%d%c%s%s%s%s%s%s%s%11.lf%s%11.lf%s%11.lf%s%d\n", some_28_arguments_go_here);
I would like to know if there is a vim plugin that highlights the printf format specifier when i go with the cursor over a variable.
Other solutions? How to better reformat the code to make it more readable?
Not sure I know a good vim trick off the top of my head, but I know a good C macro to make it a little easier:
#define last( f, a, ft, ... ) f ft, a, __VA_ARGS__
#define pair( f, a, ftat ) last( f, a, ftat )
// ...
printf( pair( "%s", "hello",
pair( "%s", "world",
pair( "%c", '\n',
last( "%4x", 0xfeed,
"%f\n", 3.14159 )))));
split the format string and the call into several fprintf
calls
From the warning you know the argument number in question, e.g. 15. In normal mode:
- Go to the start of the line.
- Type '15f%' to find the 15. occurrence of '%' inside the format string.
You can split it up and keeping only one fprintf
call. I do often something like that:
fprintf (ConvFilePtr,"\"FORMAT3"
"%s"
"%04d%s"
"%04d"
"%s%s%s"
"%d"
"%s%c"
"%s%d%d"
"%c%s"
"%s%s%s"
"%s%s"
"%s%11.lf"
"%s%11.lf"
"%s%11.lf"
"%s%d\n", str1,
int1, str2,
int2,
etc...);
You get the point, you still have only one call (which is important as I/O is often order of magnitude slower than pushing varaibles on the stack), and you can arrange your variables so that they are grouped logically, making it easier to spot a problem.
精彩评论