Darkyenus / glsl4idea

A GLSL language plugin for IntelliJ IDEA
GNU Lesser General Public License v3.0
100 stars 30 forks source link

Incorrect typechecking when preprocessors are used #182

Open Martomate opened 1 year ago

Martomate commented 1 year ago

Describe the bug When a variable is defined multiple times in a file the first definition seems to be used for type checking. This can be a problem if multiple shader stages are written in the same file.

To Reproduce

#if VERTEX_SHADER
#version 330

in vec3 color;
out vec3 fragColor;

void main() {
    fragColor = color;
}

#else
#version 330

in vec3 fragColor;
out vec4 color;

void main() {
    color = vec4(fragColor, 1); // error: Incompatible types as operands of '=': 'vec3' and 'vec4'
}

#endif

Expected behavior Ideally there would be no complaints from Intellij for the above code.

If multiple shader stages in the same file is not supported, then I could of course put in them in separate files instead, or use another name for the variable. It would be a reasonable workaround for now.

Versions

Extra context My real code uses #pragma shader vert instead of #if, but I found that both versions had the same problem.

Darkyenus commented 1 year ago

If multiple shader stages in the same file is not supported

The plugin does not care about shader stages, the problem is that the plugin always processes both branches of the #if. This is not trivial to fix (at least for me) because of the fairly complex interactions between the preprocessor and the lexer. Basically the code is parsed as if the #if-else was not there.

PR's welcome.