Ericsson / CodecheckerVSCodePlugin

VSCode plugin that shows bugs detected by the Clang Static Analyzer and Clang Tidy analyzers using CodeChecker as a backend.
Apache License 2.0
24 stars 7 forks source link

Ranges in plist files generated by Clang Tidy with CodeChecker are incomplete #16

Closed csordasmarton closed 2 years ago

csordasmarton commented 3 years ago

Range information in plist files generated by CodeChecker with Clang Tidy analyzer are incomplete and doesn't contain range information.

For example on the following code:

#include <assert.h>

int main() {
    int a = 0; assert(a++ || "line 3");
}

the Clang Tidy output will look like this:

~/main.cpp:4:16: warning: found assert() with side effect [bugprone-assert-side-effect]
    int a = 0; assert(a++ || "line 3");
               ^

It is hard to see the reports in the opened file from the user perspective if I don't open the CodeChecke view but only viewing the red underscores in the code.

Maybe in this case the end range can be the end of the line:

#include <assert.h>

int main() {
    int a = 0; assert(a++ || "line 3");
               ^^^^^^^^^^^^^^^^^^^^^^^^
}
csordasmarton commented 2 years ago

A TextDocument has a method to get word range at a given position called getWordRangeAtPosition. If no range information is available for a report we can use this method to get word position at the given line + column position and if the end position is larger than the current column we can set the end column position of the report to this:

So instead of this:

#include <assert.h>

int main() {
    int a = 0; assert(a++ || "line 3");
               ^
}

we would highlight this:

#include <assert.h>

int main() {
    int a = 0; assert(a++ || "line 3");
               ^^^^^^
}

The code for this would look similar to this:

const editors = window.visibleTextEditors.reduce(
    (prev, curr) => {prev[curr.document.uri.fsPath] = curr; return prev;}, {} as {[key: string]: TextEditor});

let line = report.line - 1;
let column = report.column - 1;

const editor = editors[report.file.original_path]
if (editor) {
    const range = editor.document.getWordRangeAtPosition(new Position(line, column));
    if (range?.isSingleLine && column < range?.end.character) {
        column = range.end.character;
    }
}