Dealing with code movement when comparing static analysis reports

149 views Asked by At

When I run a static analysis tool over my codebase, and I get results like this:

...
arch/powerpc/kernel/time.c:102:5: warning: symbol 'decrementer_max' was not declared. Should it be static?
arch/powerpc/kernel/time.c:138:1: warning: symbol 'rtc_lock' was not declared. Should it be static?
arch/powerpc/kernel/time.c:361:37: warning: implicit cast to nocast type
...

I want to keep track of the number of warnings and where they are in the code as people make changes.

I could just diff the results of the static analysis runs, but then if someone inserts some code in time.c at line 50, the warnings above will move, and because the line numbers have changed, diff will tell me that they've changed.

How should I go about comparing these in a way that deals with movement of code within a file?

Googling for 'smart diff', etc hasn't been productive: they're mostly smart diffs of code rather than smart diff of logs. Log analysis tools like Greylog or Kibana also seem like a poor fit, designed more for different and more general analysis rather than for this quite specific task.

Is there something obvious that I'm missing? Or is this a problem where I should expect to be writing my own tooling?

2

There are 2 answers

0
dja On BEST ANSWER

I had a go with a slightly simpler setup - as @ajd suggested, parsing the messages, and doing line-number-insensitive matching.

The code is up at https://github.com/daxtens/smart-sparse-diff

1
Michael Dyck On

You could maintain a merge of the code and the errors: insert each error message (minus its line number) after the corresponding line of code. Then if someone inserts code at line 50, the (updated) merge will not have diffs around the later error points. It'll have a diff at line 50, of course, which you may or may not be interested in. If you like, you can ignore diff-chunks that don't involve an error message (for which you'd need some distinctive marker at each inserted error message).