1

Scenario: I've run a static code analysis tool on my sources which found an issue on lots of my files.

The issue is that the statement

*retval = -1;

needs to be replaced by

if(retval != NULL)
{
    *retval = -1;
}

I already managed to have all relevant places in an errorfile and started with the cycle

This workflow does it but I've got 400 places to touch, so it's a bit boring.

Could I instruct Vim (similar like for example bufdo) to correct the statement on all the places that are present in the errorfile?

And no, simply opening all the files and bufdo then is not an option since the construct *retval = -1; occurs several times but only needs to be fixed on the lines contained in the errorfile.

eckes
  • 64,417
  • 29
  • 168
  • 201
  • 1
    The question & answer here is very relevant: http://stackoverflow.com/q/5686206 – glts Sep 30 '13 at 15:28
  • There's even [a patch pending](https://groups.google.com/d/msg/vim_dev/dfyt-G6SMec/_6h8pDUpeZMJ) for `:cdo` in core Vim, so if you're feeling especially charitable you could add a vote for that there, too. – glts Sep 30 '13 at 15:30
  • if static analysis tools are able to find these issues you should be able to automatically fix it via semantic patching. E.g. coccinelle has a couple of example patches to fix `NULL` dereferences: [link](http://coccinelle.lip6.fr/rules/#null) – jtaylor Oct 11 '13 at 17:17

2 Answers2

4

Use a macro:

  1. Go to the first error
  2. Press qq to record a macro the the q register
  3. Fix that error
  4. Save the buffer
  5. Execute :cn to go to the next error
  6. Press q to finish recording the macro

Now, you can type 100@q - this will execute steps 3-5 100 times. But it won't be 100 times - at some point it will reach the end of the error list and fail. By then, all the errors will be fixed(for now...)

Idan Arye
  • 12,402
  • 5
  • 49
  • 68
2

You could use a macro. Just type qq then do your :cn and fix the code, save, and then type q1000@q.

Conner
  • 30,144
  • 8
  • 52
  • 73