]> git.tdb.fi Git - libs/gl.git/commitdiff
Require conditions to be booleans
authorMikko Rasa <tdb@tdb.fi>
Tue, 6 Apr 2021 21:21:23 +0000 (00:21 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 6 Apr 2021 21:21:44 +0000 (00:21 +0300)
source/glsl/validate.cpp
source/glsl/validate.h
tests/glsl/bad_condition_type.glsl [new file with mode: 0644]

index 67d456a8a2a1d6abb3bc8253bab72a9ab2cac2ab..cb866b4ba215562c10fff9255bd1867b51ff6f00 100644 (file)
@@ -625,6 +625,28 @@ void ExpressionValidator::visit(FunctionDeclaration &func)
        TraversingVisitor::visit(func);
 }
 
+void ExpressionValidator::visit(Conditional &cond)
+{
+       if(cond.condition->type)
+       {
+               BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(cond.condition->type);
+               if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
+                       error(cond, "Condition is not a boolean");
+       }
+       TraversingVisitor::visit(cond);
+}
+
+void ExpressionValidator::visit(Iteration &iter)
+{
+       if(iter.condition->type)
+       {
+               BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(iter.condition->type);
+               if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
+                       error(iter, "Loop condition is not a boolean");
+       }
+       TraversingVisitor::visit(iter);
+}
+
 void ExpressionValidator::visit(Return &ret)
 {
        if(current_function && current_function->return_type_declaration)
index 7f8339f678adf71e17418ee26eff983f9c36fc23..1a05817069282873e27328f03683921b5945499e 100644 (file)
@@ -132,6 +132,8 @@ private:
        virtual void visit(TernaryExpression &);
        virtual void visit(VariableDeclaration &);
        virtual void visit(FunctionDeclaration &);
+       virtual void visit(Conditional &);
+       virtual void visit(Iteration &);
        virtual void visit(Return &);
 };
 
diff --git a/tests/glsl/bad_condition_type.glsl b/tests/glsl/bad_condition_type.glsl
new file mode 100644 (file)
index 0000000..867d04e
--- /dev/null
@@ -0,0 +1,28 @@
+uniform vec3 light_dir[8];
+uniform int light_count;
+
+#pragma MSP stage(vertex)
+layout(location=0) in vec4 position;
+layout(location=1) in vec3 normal;
+void main()
+{
+       out float light_intensity = 0.0;
+       if(light_count)
+       {
+               for(int i=light_count; i--; )
+                       light_intensity += dot(normal, light_dir[i]);
+       }
+       gl_Position = position;
+}
+
+#pragma MSP stage(fragment)
+layout(location=0) out vec4 frag_color;
+void main()
+{
+       frag_color = vec4(vec3(light_intensity), 1.0);
+}
+
+/* Expected error:
+<test>:10: Condition is not a boolean
+<test>:12: Loop condition is not a boolean
+*/