From 59da3addc1ead4aa1b898965bad6a5f006b53ed9 Mon Sep 17 00:00:00 2001 From: Stephane Del Pino <stephane.delpino44@gmail.com> Date: Sat, 4 Sep 2021 13:35:08 +0200 Subject: [PATCH] Fix argument-dependent lookup syntax, thanks to clang (the bad code is incorrectly compiled by g++) --- tests/FixturesForBuiltinT.hpp | 56 +++ ...t_BinaryExpressionProcessor_arithmetic.cpp | 326 ++++++++---------- tests/test_UnaryExpressionProcessor.cpp | 13 +- 3 files changed, 201 insertions(+), 194 deletions(-) create mode 100644 tests/FixturesForBuiltinT.hpp diff --git a/tests/FixturesForBuiltinT.hpp b/tests/FixturesForBuiltinT.hpp new file mode 100644 index 000000000..a51de19d7 --- /dev/null +++ b/tests/FixturesForBuiltinT.hpp @@ -0,0 +1,56 @@ +#ifndef FIXTURES_FOR_BUILTIN_T_HPP +#define FIXTURES_FOR_BUILTIN_T_HPP + +#include <language/utils/ASTNodeDataTypeTraits.hpp> + +#include <memory> +#include <stdexcept> + +template <> +inline ASTNodeDataType ast_node_data_type_from<std::shared_ptr<const double>> = + ASTNodeDataType::build<ASTNodeDataType::type_id_t>("builtin_t"); +const auto builtin_data_type = ast_node_data_type_from<std::shared_ptr<const double>>; + +inline std::shared_ptr<const double> +operator-(const std::shared_ptr<const double>& p_a) +{ + return std::make_shared<double>(-*p_a); +} + +inline std::shared_ptr<const double> +operator+(const std::shared_ptr<const double>& p_a, const std::shared_ptr<const double>& p_b) +{ + return std::make_shared<double>(*p_a + *p_b); +} + +inline std::shared_ptr<const double> +operator+(std::shared_ptr<const double> p_a, double b) +{ + return std::make_shared<double>(*p_a + b); +} + +inline std::shared_ptr<const double> +operator+(double a, const std::shared_ptr<const double>& p_b) +{ + return std::make_shared<double>(a + *p_b); +} + +inline std::shared_ptr<const double> +operator/(const std::shared_ptr<const double>&, const std::shared_ptr<const double>&) +{ + throw std::runtime_error("runtime error both"); +} + +inline std::shared_ptr<const double> +operator/(const std::shared_ptr<const double>&, double) +{ + throw std::runtime_error("runtime error lhs"); +} + +inline std::shared_ptr<const double> +operator/(double, const std::shared_ptr<const double>&) +{ + throw std::runtime_error("runtime error rhs"); +} + +#endif // FIXTURES_FOR_BUILTIN_T_HPP diff --git a/tests/test_BinaryExpressionProcessor_arithmetic.cpp b/tests/test_BinaryExpressionProcessor_arithmetic.cpp index 198b24e70..6ded2c991 100644 --- a/tests/test_BinaryExpressionProcessor_arithmetic.cpp +++ b/tests/test_BinaryExpressionProcessor_arithmetic.cpp @@ -1,7 +1,8 @@ #include <catch2/catch_test_macros.hpp> #include <catch2/matchers/catch_matchers_all.hpp> -#include <language/utils/ASTNodeDataTypeTraits.hpp> +#include <FixturesForBuiltinT.hpp> + #include <language/utils/BasicAffectationRegistrerFor.hpp> #include <language/utils/BinaryOperatorProcessorBuilder.hpp> #include <language/utils/DataHandler.hpp> @@ -12,46 +13,148 @@ // clazy:excludeall=non-pod-global-static -template <> -inline ASTNodeDataType ast_node_data_type_from<std::shared_ptr<const double>> = - ASTNodeDataType::build<ASTNodeDataType::type_id_t>("builtin_t"); -const auto builtin_data_type = ast_node_data_type_from<std::shared_ptr<const double>>; - -inline std::shared_ptr<const double> -operator+(const std::shared_ptr<const double>& p_a, const std::shared_ptr<const double>& p_b) -{ - return std::make_shared<double>(*p_a + *p_b); -} - -inline std::shared_ptr<const double> -operator/(const std::shared_ptr<const double>&, const std::shared_ptr<const double>&) -{ - throw std::runtime_error("runtime error both"); -} - -inline std::shared_ptr<const double> -operator/(const std::shared_ptr<const double>&, double) -{ - throw std::runtime_error("runtime error lhs"); -} - -inline std::shared_ptr<const double> -operator/(double, const std::shared_ptr<const double>&) -{ - throw std::runtime_error("runtime error rhs"); -} - -inline std::shared_ptr<const double> -operator+(const std::shared_ptr<const double>& p_a, double b) -{ - return std::make_shared<double>(*p_a + b); -} +#define CHECK_BUILTIN_BINARY_EXPRESSION_RESULT(data, result) \ + { \ + TAO_PEGTL_NAMESPACE::string_input input{data, "test.pgs"}; \ + auto ast = ASTBuilder::build(input); \ + \ + ASTModulesImporter{*ast}; \ + \ + BasicAffectationRegisterFor<EmbeddedData>{ASTNodeDataType::build<ASTNodeDataType::type_id_t>("builtin_t")}; \ + \ + OperatorRepository& repository = OperatorRepository::instance(); \ + \ + repository.addBinaryOperator<language::plus_op>( \ + std::make_shared< \ + BinaryOperatorProcessorBuilder<language::plus_op, std::shared_ptr<const double>, \ + std::shared_ptr<const double>, std::shared_ptr<const double>>>()); \ + \ + repository.addBinaryOperator<language::plus_op>( \ + std::make_shared<BinaryOperatorProcessorBuilder<language::plus_op, std::shared_ptr<const double>, \ + std::shared_ptr<const double>, double>>()); \ + \ + repository.addBinaryOperator<language::plus_op>( \ + std::make_shared<BinaryOperatorProcessorBuilder<language::plus_op, std::shared_ptr<const double>, double, \ + std::shared_ptr<const double>>>()); \ + \ + SymbolTable& symbol_table = *ast->m_symbol_table; \ + auto [i_symbol, success] = symbol_table.add(builtin_data_type.nameOfTypeId(), ast->begin()); \ + if (not success) { \ + throw UnexpectedError("cannot add '" + builtin_data_type.nameOfTypeId() + "' type for testing"); \ + } \ + \ + i_symbol->attributes().setDataType(ASTNodeDataType::build<ASTNodeDataType::type_name_id_t>()); \ + i_symbol->attributes().setIsInitialized(); \ + i_symbol->attributes().value() = symbol_table.typeEmbedderTable().size(); \ + symbol_table.typeEmbedderTable().add(std::make_shared<TypeDescriptor>(builtin_data_type.nameOfTypeId())); \ + \ + auto [i_symbol_bt_a, success_bt_a] = symbol_table.add("bt_a", ast->begin()); \ + if (not success_bt_a) { \ + throw UnexpectedError("cannot add 'bt_a' of type builtin_t for testing"); \ + } \ + i_symbol_bt_a->attributes().setDataType(ast_node_data_type_from<std::shared_ptr<const double>>); \ + i_symbol_bt_a->attributes().setIsInitialized(); \ + i_symbol_bt_a->attributes().value() = \ + EmbeddedData(std::make_shared<DataHandler<const double>>(std::make_shared<double>(3.2))); \ + \ + auto [i_symbol_bt_b, success_bt_b] = symbol_table.add("bt_b", ast->begin()); \ + if (not success_bt_b) { \ + throw UnexpectedError("cannot add 'bt_b' of type builtin_t for testing"); \ + } \ + i_symbol_bt_b->attributes().setDataType(ast_node_data_type_from<std::shared_ptr<const double>>); \ + i_symbol_bt_b->attributes().setIsInitialized(); \ + i_symbol_bt_b->attributes().value() = \ + EmbeddedData(std::make_shared<DataHandler<const double>>(std::make_shared<double>(5.3))); \ + \ + ASTNodeTypeCleaner<language::import_instruction>{*ast}; \ + \ + ASTSymbolTableBuilder{*ast}; \ + ASTNodeDataTypeBuilder{*ast}; \ + \ + ASTNodeDeclarationToAffectationConverter{*ast}; \ + ASTNodeTypeCleaner<language::var_declaration>{*ast}; \ + \ + ASTNodeExpressionBuilder{*ast}; \ + ExecutionPolicy exec_policy; \ + ast->execute(exec_policy); \ + \ + using namespace TAO_PEGTL_NAMESPACE; \ + position use_position{internal::iterator{"fixture"}, "fixture"}; \ + use_position.byte = 10000; \ + auto [symbol, found] = symbol_table.find("r", use_position); \ + \ + auto attributes = symbol->attributes(); \ + auto embedded_value = std::get<EmbeddedData>(attributes.value()); \ + \ + double value = *dynamic_cast<const DataHandler<const double>&>(embedded_value.get()).data_ptr(); \ + REQUIRE(value == expected); \ + } -inline std::shared_ptr<const double> -operator+(double a, const std::shared_ptr<const double>& p_b) -{ - return std::make_shared<double>(a + *p_b); -} +#define CHECK_BUILTIN_BINARY_EXPRESSION_ERROR(data, error_msg) \ + { \ + TAO_PEGTL_NAMESPACE::string_input input{data, "test.pgs"}; \ + auto ast = ASTBuilder::build(input); \ + \ + ASTModulesImporter{*ast}; \ + \ + BasicAffectationRegisterFor<EmbeddedData>{ASTNodeDataType::build<ASTNodeDataType::type_id_t>("builtin_t")}; \ + \ + OperatorRepository& repository = OperatorRepository::instance(); \ + \ + repository.addBinaryOperator<language::divide_op>( \ + std::make_shared< \ + BinaryOperatorProcessorBuilder<language::divide_op, std::shared_ptr<const double>, \ + std::shared_ptr<const double>, std::shared_ptr<const double>>>()); \ + \ + repository.addBinaryOperator<language::divide_op>( \ + std::make_shared<BinaryOperatorProcessorBuilder<language::divide_op, std::shared_ptr<const double>, \ + std::shared_ptr<const double>, double>>()); \ + \ + repository.addBinaryOperator<language::divide_op>( \ + std::make_shared<BinaryOperatorProcessorBuilder<language::divide_op, std::shared_ptr<const double>, double, \ + std::shared_ptr<const double>>>()); \ + \ + SymbolTable& symbol_table = *ast->m_symbol_table; \ + auto [i_symbol, success] = symbol_table.add(builtin_data_type.nameOfTypeId(), ast->begin()); \ + if (not success) { \ + throw UnexpectedError("cannot add '" + builtin_data_type.nameOfTypeId() + "' type for testing"); \ + } \ + \ + i_symbol->attributes().setDataType(ASTNodeDataType::build<ASTNodeDataType::type_name_id_t>()); \ + i_symbol->attributes().setIsInitialized(); \ + i_symbol->attributes().value() = symbol_table.typeEmbedderTable().size(); \ + symbol_table.typeEmbedderTable().add(std::make_shared<TypeDescriptor>(builtin_data_type.nameOfTypeId())); \ + \ + auto [i_symbol_bt_a, success_bt_a] = symbol_table.add("bt_a", ast->begin()); \ + if (not success_bt_a) { \ + throw UnexpectedError("cannot add 'bt_a' of type builtin_t for testing"); \ + } \ + i_symbol_bt_a->attributes().setDataType(ast_node_data_type_from<std::shared_ptr<const double>>); \ + i_symbol_bt_a->attributes().setIsInitialized(); \ + i_symbol_bt_a->attributes().value() = \ + EmbeddedData(std::make_shared<DataHandler<const double>>(std::make_shared<double>(3.2))); \ + \ + auto [i_symbol_bt_b, success_bt_b] = symbol_table.add("bt_b", ast->begin()); \ + if (not success_bt_b) { \ + throw UnexpectedError("cannot add 'bt_b' of type builtin_t for testing"); \ + } \ + i_symbol_bt_b->attributes().setDataType(ast_node_data_type_from<std::shared_ptr<const double>>); \ + i_symbol_bt_b->attributes().setIsInitialized(); \ + i_symbol_bt_b->attributes().value() = \ + EmbeddedData(std::make_shared<DataHandler<const double>>(std::make_shared<double>(5.3))); \ + \ + ASTNodeTypeCleaner<language::import_instruction>{*ast}; \ + \ + ASTSymbolTableBuilder{*ast}; \ + ASTNodeDataTypeBuilder{*ast}; \ + \ + ASTNodeDeclarationToAffectationConverter{*ast}; \ + ASTNodeTypeCleaner<language::var_declaration>{*ast}; \ + \ + ASTNodeExpressionBuilder{*ast}; \ + ExecutionPolicy exec_policy; \ + REQUIRE_THROWS_WITH(ast->execute(exec_policy), error_msg); \ + } TEST_CASE("BinaryExpressionProcessor arithmetic", "[language]") { @@ -202,83 +305,6 @@ TEST_CASE("BinaryExpressionProcessor arithmetic", "[language]") SECTION("binary operator [builtin]") { -#define CHECK_BUILTIN_BINARY_EXPRESSION_RESULT(data, result) \ - { \ - TAO_PEGTL_NAMESPACE::string_input input{data, "test.pgs"}; \ - auto ast = ASTBuilder::build(input); \ - \ - ASTModulesImporter{*ast}; \ - \ - BasicAffectationRegisterFor<EmbeddedData>{ASTNodeDataType::build<ASTNodeDataType::type_id_t>("builtin_t")}; \ - \ - OperatorRepository& repository = OperatorRepository::instance(); \ - \ - repository.addBinaryOperator<language::plus_op>( \ - std::make_shared< \ - BinaryOperatorProcessorBuilder<language::plus_op, std::shared_ptr<const double>, \ - std::shared_ptr<const double>, std::shared_ptr<const double>>>()); \ - \ - repository.addBinaryOperator<language::plus_op>( \ - std::make_shared<BinaryOperatorProcessorBuilder<language::plus_op, std::shared_ptr<const double>, \ - std::shared_ptr<const double>, double>>()); \ - \ - repository.addBinaryOperator<language::plus_op>( \ - std::make_shared<BinaryOperatorProcessorBuilder<language::plus_op, std::shared_ptr<const double>, double, \ - std::shared_ptr<const double>>>()); \ - \ - SymbolTable& symbol_table = *ast->m_symbol_table; \ - auto [i_symbol, success] = symbol_table.add(builtin_data_type.nameOfTypeId(), ast->begin()); \ - if (not success) { \ - throw UnexpectedError("cannot add '" + builtin_data_type.nameOfTypeId() + "' type for testing"); \ - } \ - \ - i_symbol->attributes().setDataType(ASTNodeDataType::build<ASTNodeDataType::type_name_id_t>()); \ - i_symbol->attributes().setIsInitialized(); \ - i_symbol->attributes().value() = symbol_table.typeEmbedderTable().size(); \ - symbol_table.typeEmbedderTable().add(std::make_shared<TypeDescriptor>(builtin_data_type.nameOfTypeId())); \ - \ - auto [i_symbol_bt_a, success_bt_a] = symbol_table.add("bt_a", ast->begin()); \ - if (not success_bt_a) { \ - throw UnexpectedError("cannot add 'bt_a' of type builtin_t for testing"); \ - } \ - i_symbol_bt_a->attributes().setDataType(ast_node_data_type_from<std::shared_ptr<const double>>); \ - i_symbol_bt_a->attributes().setIsInitialized(); \ - i_symbol_bt_a->attributes().value() = \ - EmbeddedData(std::make_shared<DataHandler<const double>>(std::make_shared<double>(3.2))); \ - \ - auto [i_symbol_bt_b, success_bt_b] = symbol_table.add("bt_b", ast->begin()); \ - if (not success_bt_b) { \ - throw UnexpectedError("cannot add 'bt_b' of type builtin_t for testing"); \ - } \ - i_symbol_bt_b->attributes().setDataType(ast_node_data_type_from<std::shared_ptr<const double>>); \ - i_symbol_bt_b->attributes().setIsInitialized(); \ - i_symbol_bt_b->attributes().value() = \ - EmbeddedData(std::make_shared<DataHandler<const double>>(std::make_shared<double>(5.3))); \ - \ - ASTNodeTypeCleaner<language::import_instruction>{*ast}; \ - \ - ASTSymbolTableBuilder{*ast}; \ - ASTNodeDataTypeBuilder{*ast}; \ - \ - ASTNodeDeclarationToAffectationConverter{*ast}; \ - ASTNodeTypeCleaner<language::var_declaration>{*ast}; \ - \ - ASTNodeExpressionBuilder{*ast}; \ - ExecutionPolicy exec_policy; \ - ast->execute(exec_policy); \ - \ - using namespace TAO_PEGTL_NAMESPACE; \ - position use_position{internal::iterator{"fixture"}, "fixture"}; \ - use_position.byte = 10000; \ - auto [symbol, found] = symbol_table.find("r", use_position); \ - \ - auto attributes = symbol->attributes(); \ - auto embedded_value = std::get<EmbeddedData>(attributes.value()); \ - \ - double value = *dynamic_cast<const DataHandler<const double>&>(embedded_value.get()).data_ptr(); \ - REQUIRE(value == expected); \ - } - SECTION("builtin both side") { std::string_view data = R"(let r:builtin_t, r = bt_a + bt_b;)"; @@ -303,72 +329,6 @@ TEST_CASE("BinaryExpressionProcessor arithmetic", "[language]") CHECK_BUILTIN_BINARY_EXPRESSION_RESULT(data, expected); } -#define CHECK_BUILTIN_BINARY_EXPRESSION_ERROR(data, error_msg) \ - { \ - TAO_PEGTL_NAMESPACE::string_input input{data, "test.pgs"}; \ - auto ast = ASTBuilder::build(input); \ - \ - ASTModulesImporter{*ast}; \ - \ - BasicAffectationRegisterFor<EmbeddedData>{ASTNodeDataType::build<ASTNodeDataType::type_id_t>("builtin_t")}; \ - \ - OperatorRepository& repository = OperatorRepository::instance(); \ - \ - repository.addBinaryOperator<language::divide_op>( \ - std::make_shared< \ - BinaryOperatorProcessorBuilder<language::divide_op, std::shared_ptr<const double>, \ - std::shared_ptr<const double>, std::shared_ptr<const double>>>()); \ - \ - repository.addBinaryOperator<language::divide_op>( \ - std::make_shared<BinaryOperatorProcessorBuilder<language::divide_op, std::shared_ptr<const double>, \ - std::shared_ptr<const double>, double>>()); \ - \ - repository.addBinaryOperator<language::divide_op>( \ - std::make_shared<BinaryOperatorProcessorBuilder<language::divide_op, std::shared_ptr<const double>, double, \ - std::shared_ptr<const double>>>()); \ - \ - SymbolTable& symbol_table = *ast->m_symbol_table; \ - auto [i_symbol, success] = symbol_table.add(builtin_data_type.nameOfTypeId(), ast->begin()); \ - if (not success) { \ - throw UnexpectedError("cannot add '" + builtin_data_type.nameOfTypeId() + "' type for testing"); \ - } \ - \ - i_symbol->attributes().setDataType(ASTNodeDataType::build<ASTNodeDataType::type_name_id_t>()); \ - i_symbol->attributes().setIsInitialized(); \ - i_symbol->attributes().value() = symbol_table.typeEmbedderTable().size(); \ - symbol_table.typeEmbedderTable().add(std::make_shared<TypeDescriptor>(builtin_data_type.nameOfTypeId())); \ - \ - auto [i_symbol_bt_a, success_bt_a] = symbol_table.add("bt_a", ast->begin()); \ - if (not success_bt_a) { \ - throw UnexpectedError("cannot add 'bt_a' of type builtin_t for testing"); \ - } \ - i_symbol_bt_a->attributes().setDataType(ast_node_data_type_from<std::shared_ptr<const double>>); \ - i_symbol_bt_a->attributes().setIsInitialized(); \ - i_symbol_bt_a->attributes().value() = \ - EmbeddedData(std::make_shared<DataHandler<const double>>(std::make_shared<double>(3.2))); \ - \ - auto [i_symbol_bt_b, success_bt_b] = symbol_table.add("bt_b", ast->begin()); \ - if (not success_bt_b) { \ - throw UnexpectedError("cannot add 'bt_b' of type builtin_t for testing"); \ - } \ - i_symbol_bt_b->attributes().setDataType(ast_node_data_type_from<std::shared_ptr<const double>>); \ - i_symbol_bt_b->attributes().setIsInitialized(); \ - i_symbol_bt_b->attributes().value() = \ - EmbeddedData(std::make_shared<DataHandler<const double>>(std::make_shared<double>(5.3))); \ - \ - ASTNodeTypeCleaner<language::import_instruction>{*ast}; \ - \ - ASTSymbolTableBuilder{*ast}; \ - ASTNodeDataTypeBuilder{*ast}; \ - \ - ASTNodeDeclarationToAffectationConverter{*ast}; \ - ASTNodeTypeCleaner<language::var_declaration>{*ast}; \ - \ - ASTNodeExpressionBuilder{*ast}; \ - ExecutionPolicy exec_policy; \ - REQUIRE_THROWS_WITH(ast->execute(exec_policy), error_msg); \ - } - SECTION("runtime error") { std::string_view data_both = R"(let r:builtin_t, r = bt_a / bt_b;)"; diff --git a/tests/test_UnaryExpressionProcessor.cpp b/tests/test_UnaryExpressionProcessor.cpp index bb6b28540..2c3f6acb8 100644 --- a/tests/test_UnaryExpressionProcessor.cpp +++ b/tests/test_UnaryExpressionProcessor.cpp @@ -1,6 +1,8 @@ #include <catch2/catch_test_macros.hpp> #include <catch2/matchers/catch_matchers_all.hpp> +#include <FixturesForBuiltinT.hpp> + #include <language/ast/ASTBuilder.hpp> #include <language/ast/ASTModulesImporter.hpp> #include <language/ast/ASTNodeDataTypeBuilder.hpp> @@ -63,17 +65,6 @@ REQUIRE_THROWS_WITH(ASTNodeDataTypeBuilder{*ast}, error_message); \ } -template <> -inline ASTNodeDataType ast_node_data_type_from<std::shared_ptr<const double>> = - ASTNodeDataType::build<ASTNodeDataType::type_id_t>("builtin_t"); -const auto builtin_data_type = ast_node_data_type_from<std::shared_ptr<const double>>; - -inline std::shared_ptr<const double> -operator-(const std::shared_ptr<const double>& p_a) -{ - return std::make_shared<double>(-*p_a); -} - // clazy:excludeall=non-pod-global-static TEST_CASE("UnaryExpressionProcessor", "[language]") -- GitLab