Error column is wrong when non-ASCII characters come earlier on the line (gcc reports bytes)
Error column is wrong when non-ASCII characters come earlier on the line: gcc reports bytes (on Windows), Sublime counts characters
Version: SublimeLinter-gcc from Package Control, gcc 16.1.0 (MinGW-w64 UCRT), SublimeLinter 4, Sublime Text 4215 on Windows. Reproduced in a clean install.
gcc_probe.c (UTF-8, tab-indented):
int main(void) {
int a = 1; int b = undefined_one;
char *s = "é😀"; int c = undefined_two;
return 0
}
The linter runs gcc -c -Wall -O0 -x c -o <file>.o - with the source on stdin. On line 3 the errors are
<stdin>:3:30: error: 'undefined_two' undeclared (first use in this function)
<stdin>:3:26: warning: unused variable 'c' [-Wunused-variable]
undefined_two starts at character 26 (1-based) as Sublime counts, but gcc says 30, because the é is two bytes and the 😀 four in UTF-8: gcc's column is a byte offset. In Sublime the highlight of the error starts four characters too far to the right (start 29, expected 25, 0-based), on fined_two instead of undefined_two, and the warning for c is highlighted on undefined_two instead of c. Line 2, without non-ASCII characters, is correct (tabs count as one).
Adding -fdiagnostics-column-unit=byte gives the same numbers here, so on this setup byte is what the default gives. The documentation says the default is display (a column in terminal cells, where a wide character such as an emoji is two and a tab expands), which I did not test on Linux or macOS with a UTF-8 locale, so the unit may differ per platform.
Suggested fix: pass -fdiagnostics-column-unit=byte (gcc 11 and later, older gcc rejects the option, so it would have to be conditional) so the unit is always known, and convert the byte offset to a character offset in reposition_match(self, line, col, m, vv) with the source line from vv.select_line(line): encode the line to UTF-8, cut it at the byte offset, decode, and take the length. The same fix was merged for SublimeLinter-xmllint and is proposed for SublimeLinter-cppcheck (byte columns as well). I can prepare a PR if you would like one.
I tested this on Linux as well (Ubuntu in WSL 2, gcc 15.2.0, LANG=C.UTF-8), with the same gcc_probe.c on stdin as the linter does: the default, -fdiagnostics-column-unit=byte and -fdiagnostics-column-unit=display all report
<stdin>:3:30: error: 'undefined_two' undeclared (first use in this function)
<stdin>:3:26: warning: unused variable 'c' [-Wunused-variable]
that is, bytes (character 26 is column 30), so it is not only a Windows effect, and adding -fdiagnostics-column-unit=byte does not change the numbers here (which also means it is not needed for the fix on gcc 15/16; I did not test other versions). Converting the byte offset to a character offset in reposition_match is enough on both platforms I tried.
Ack, you can try a fix. This is only a case for stdin afaik.