From 6377f8efc81ffe84b7ffe44380f1caa2059f437f Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 7 Apr 2021 00:21:23 +0300 Subject: [PATCH] Require conditions to be booleans --- source/glsl/validate.cpp | 22 ++++++++++++++++++++++ source/glsl/validate.h | 2 ++ tests/glsl/bad_condition_type.glsl | 28 ++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 tests/glsl/bad_condition_type.glsl diff --git a/source/glsl/validate.cpp b/source/glsl/validate.cpp index 67d456a8..cb866b4b 100644 --- a/source/glsl/validate.cpp +++ b/source/glsl/validate.cpp @@ -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(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(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) diff --git a/source/glsl/validate.h b/source/glsl/validate.h index 7f8339f6..1a058170 100644 --- a/source/glsl/validate.h +++ b/source/glsl/validate.h @@ -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 index 00000000..867d04ed --- /dev/null +++ b/tests/glsl/bad_condition_type.glsl @@ -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: +:10: Condition is not a boolean +:12: Loop condition is not a boolean +*/ -- 2.43.0