From: Mikko Rasa Date: Tue, 9 Mar 2021 14:00:19 +0000 (+0200) Subject: Validate return types of overloaded functions X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=7d66c70e15b84cbaf6b1973db07629f5bd3e5cdf;p=libs%2Fgl.git Validate return types of overloaded functions --- diff --git a/source/glsl/validate.cpp b/source/glsl/validate.cpp index f8ecc5ee..91d1de99 100644 --- a/source/glsl/validate.cpp +++ b/source/glsl/validate.cpp @@ -173,6 +173,17 @@ void DeclarationValidator::visit(InterfaceBlock &iface) void DeclarationValidator::visit(FunctionDeclaration &func) { + string key = func.name+func.signature; + map::const_iterator i = overloaded_functions.find(key); + if(i==overloaded_functions.end()) + overloaded_functions.insert(make_pair(key, &func)); + else if(func.return_type_declaration && i->second->return_type_declaration!=func.return_type_declaration) + { + error(func, format("Conflicting return type '%s' for function '%s'", func.return_type_declaration->name, func.name)); + if(i->second->return_type_declaration) + add_info(*i->second, format("Previously declared as returning '%s'", i->second->return_type_declaration->name)); + } + if(Statement *previous = find_definition(func.name)) { if(!dynamic_cast(previous)) diff --git a/source/glsl/validate.h b/source/glsl/validate.h index 9477b67d..a8c6f11c 100644 --- a/source/glsl/validate.h +++ b/source/glsl/validate.h @@ -48,6 +48,7 @@ private: std::map declarations; std::map interface_blocks; + std::map overloaded_functions; bool anonymous_block; public: diff --git a/tests/glsl/return_type_conflict.glsl b/tests/glsl/return_type_conflict.glsl new file mode 100644 index 00000000..2de20c35 --- /dev/null +++ b/tests/glsl/return_type_conflict.glsl @@ -0,0 +1,17 @@ +#pragma MSP stage(vertex) +layout(location=0) in vec4 position; +void func(); +vec4 func(); +float func(); +float func(float v); +void main() +{ + gl_Position = position; +} + +/* Expected error: +:4: Conflicting return type 'vec4' for function 'func' +:3: Previously declared as returning 'void' +:5: Conflicting return type 'float' for function 'func' +:3: Previously declared as returning 'void' +*/