]> git.tdb.fi Git - libs/gl.git/commitdiff
Check that variable references refer do declared variables
authorMikko Rasa <tdb@tdb.fi>
Thu, 4 Mar 2021 23:18:15 +0000 (01:18 +0200)
committerMikko Rasa <tdb@tdb.fi>
Thu, 4 Mar 2021 23:19:59 +0000 (01:19 +0200)
source/glsl/compiler.cpp
source/glsl/validate.cpp
source/glsl/validate.h
tests/glsl/undeclared.glsl [new file with mode: 0644]
tests/glsl/unlinked_interface.glsl [new file with mode: 0644]

index f5540139e3391cffd1afd1b9f2f8e709fa4b99d5..0f608d6d600313682577df96a3c7e2cccd2e5226 100644 (file)
@@ -260,6 +260,7 @@ void Compiler::generate(Stage &stage, Mode mode)
 bool Compiler::validate(Stage &stage)
 {
        DeclarationValidator().apply(stage);
+       ReferenceValidator().apply(stage);
 
        for(vector<Diagnostic>::const_iterator i=stage.diagnostics.begin(); i!=stage.diagnostics.end(); ++i)
                if(i->severity==Diagnostic::ERR)
index 6d0f8e51ac2cb9a95ee94883f8a7449f0c97fa44..6f0869ea141897813c4e4f3190a81082457ceed1 100644 (file)
@@ -112,6 +112,32 @@ void DeclarationValidator::visit(FunctionDeclaration &func)
        TraversingVisitor::visit(func);
 }
 
+
+void ReferenceValidator::visit(VariableReference &var)
+{
+       if(!var.declaration)
+               error(var, format("Use of undeclared variable '%s'", var.name));
+       else if(stage->type!=Stage::VERTEX && var.declaration->interface=="in" && !var.declaration->linked_declaration)
+               error(var, format("Use of unlinked input variable '%s'", var.name));
+}
+
+void ReferenceValidator::visit(InterfaceBlockReference &iface)
+{
+       /* An interface block reference without a declaration should be impossible
+       since references are generated based on existing declarations. */
+       if(!iface.declaration)
+               error(iface, format("Use of undeclared interface block '%s'", iface.name));
+       else if(stage->type!=Stage::VERTEX && iface.declaration->interface=="in" && !iface.declaration->linked_block)
+               error(iface, format("Use of unlinked input block '%s'", iface.name));
+}
+
+void ReferenceValidator::visit(VariableDeclaration &var)
+{
+       if(!var.type_declaration)
+               error(var, format("Use of undeclared type '%s'", var.type));
+       TraversingVisitor::visit(var);
+}
+
 } // namespace SL
 } // namespace GL
 } // namespace Msp
index c723b995567f869330d0c4f393278c824c33cab8..bf2677e8d1cd993f67a5fa1936c4aeb37c0f32f1 100644 (file)
@@ -50,6 +50,17 @@ private:
        virtual void visit(FunctionDeclaration &);
 };
 
+class ReferenceValidator: private Validator
+{
+public:
+       void apply(Stage &s) { stage = &s; s.content.visit(*this); }
+
+private:
+       virtual void visit(VariableReference &);
+       virtual void visit(InterfaceBlockReference &);
+       virtual void visit(VariableDeclaration &);
+};
+
 } // namespace SL
 } // namespace GL
 } // namespace Msp
diff --git a/tests/glsl/undeclared.glsl b/tests/glsl/undeclared.glsl
new file mode 100644 (file)
index 0000000..da888cd
--- /dev/null
@@ -0,0 +1,15 @@
+uniform Transform
+{
+       mat4 mvp;
+} transform;
+
+#pragma MSP stage(vertex)
+void main()
+{
+       gl_Position = transform.model*position;
+}
+
+/* Expected error:
+<test>:9: Use of undeclared member 'model'
+<test>:9: Use of undeclared variable 'position'
+*/
diff --git a/tests/glsl/unlinked_interface.glsl b/tests/glsl/unlinked_interface.glsl
new file mode 100644 (file)
index 0000000..bb90f0d
--- /dev/null
@@ -0,0 +1,25 @@
+uniform sampler2D tex;
+
+#pragma MSP stage(vertex)
+layout(location=0) in vec4 position;
+void main()
+{
+       gl_Position = position;
+}
+
+#pragma MSP stage(fragment)
+in vec2 texcoord;
+in VertexOut
+{
+       vec4 color;
+} vs_out;
+out vec4 frag_color;
+void main()
+{
+       frag_color = texture(tex, texcoord)*vs_out.color;
+}
+
+/* Expected error:
+<test>:19: Use of unlinked input variable 'texcoord'
+<test>:19: Use of unlinked input block 'vs_out'
+*/