tsoding / musializer

Music Visualizer
MIT License
953 stars 100 forks source link

FIX: genf macro lineno #84

Closed ismagilli closed 8 months ago

ismagilli commented 9 months ago

The correct line number of the source file for the generated code.

See for example following code and it's output:

#include <stdio.h>

#define genf(out, ...)                                     \
    do {                                                   \
        fprintf((out), __VA_ARGS__);                       \
        fprintf((out), " // %s:%d\n", __FILE__, __LINE__); \
    } while (0)

int main(void) {
    genf(stdout, "Hello, world");
}
ismagilli commented 9 months ago

For code

void f(void) {  // line 1
    int x = 10  // line 2
    int y = 20; // line 3
}               // line 4

the compiler says that an error on line 3 because after 10 it try to read something different from int (operator / , / ;), but it read int. Compiler output:

<source>: In function 'f':
<source>:3:5: error: expected ',' or ';' before 'int'
    3 |     int y = 20;
      |     ^~~
Compiler returned: 1

In this case, the real error in line 2. On stream you have situation like following:

genf("int x = 10");  // line N
genf("int y = 20;"); // line N+1

The compiler says that the error in the code is generated by line N+1, but you know that error in the сode is generated by line N. This led to the erroneous assumption that it is necessary to subtract one.

rexim commented 8 months ago

Yeah, I think there was something wrong with my Emacs at the time of writing this. Its go to line command had off by one moment for whatever reason. Now it's fine. Thanks!