Skip to content
Snippets Groups Projects
Commit 59da3add authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Fix argument-dependent lookup syntax, thanks to clang

(the bad code is incorrectly compiled by g++)
parent 246d8a38
No related branches found
No related tags found
1 merge request!116Add tests for EmbeddedIDiscreteFunctionUtils
#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
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_all.hpp> #include <catch2/matchers/catch_matchers_all.hpp>
#include <language/utils/ASTNodeDataTypeTraits.hpp> #include <FixturesForBuiltinT.hpp>
#include <language/utils/BasicAffectationRegistrerFor.hpp> #include <language/utils/BasicAffectationRegistrerFor.hpp>
#include <language/utils/BinaryOperatorProcessorBuilder.hpp> #include <language/utils/BinaryOperatorProcessorBuilder.hpp>
#include <language/utils/DataHandler.hpp> #include <language/utils/DataHandler.hpp>
...@@ -12,45 +13,147 @@ ...@@ -12,45 +13,147 @@
// clazy:excludeall=non-pod-global-static // clazy:excludeall=non-pod-global-static
template <> #define CHECK_BUILTIN_BINARY_EXPRESSION_RESULT(data, result) \
inline ASTNodeDataType ast_node_data_type_from<std::shared_ptr<const double>> = { \
ASTNodeDataType::build<ASTNodeDataType::type_id_t>("builtin_t"); TAO_PEGTL_NAMESPACE::string_input input{data, "test.pgs"}; \
const auto builtin_data_type = ast_node_data_type_from<std::shared_ptr<const double>>; auto ast = ASTBuilder::build(input); \
\
inline std::shared_ptr<const double> ASTModulesImporter{*ast}; \
operator+(const std::shared_ptr<const double>& p_a, const std::shared_ptr<const double>& p_b) \
{ BasicAffectationRegisterFor<EmbeddedData>{ASTNodeDataType::build<ASTNodeDataType::type_id_t>("builtin_t")}; \
return std::make_shared<double>(*p_a + *p_b); \
} OperatorRepository& repository = OperatorRepository::instance(); \
\
inline std::shared_ptr<const double> repository.addBinaryOperator<language::plus_op>( \
operator/(const std::shared_ptr<const double>&, const std::shared_ptr<const double>&) std::make_shared< \
{ BinaryOperatorProcessorBuilder<language::plus_op, std::shared_ptr<const double>, \
throw std::runtime_error("runtime error both"); std::shared_ptr<const double>, std::shared_ptr<const double>>>()); \
} \
repository.addBinaryOperator<language::plus_op>( \
inline std::shared_ptr<const double> std::make_shared<BinaryOperatorProcessorBuilder<language::plus_op, std::shared_ptr<const double>, \
operator/(const std::shared_ptr<const double>&, double) std::shared_ptr<const double>, double>>()); \
{ \
throw std::runtime_error("runtime error lhs"); repository.addBinaryOperator<language::plus_op>( \
} std::make_shared<BinaryOperatorProcessorBuilder<language::plus_op, std::shared_ptr<const double>, double, \
std::shared_ptr<const double>>>()); \
inline std::shared_ptr<const double> \
operator/(double, const 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()); \
throw std::runtime_error("runtime error rhs"); if (not success) { \
} throw UnexpectedError("cannot add '" + builtin_data_type.nameOfTypeId() + "' type for testing"); \
} \
inline std::shared_ptr<const double> \
operator+(const std::shared_ptr<const double>& p_a, double b) i_symbol->attributes().setDataType(ASTNodeDataType::build<ASTNodeDataType::type_name_id_t>()); \
{ i_symbol->attributes().setIsInitialized(); \
return std::make_shared<double>(*p_a + b); 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> #define CHECK_BUILTIN_BINARY_EXPRESSION_ERROR(data, error_msg) \
operator+(double a, const std::shared_ptr<const double>& p_b) { \
{ TAO_PEGTL_NAMESPACE::string_input input{data, "test.pgs"}; \
return std::make_shared<double>(a + *p_b); 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]") TEST_CASE("BinaryExpressionProcessor arithmetic", "[language]")
...@@ -202,83 +305,6 @@ TEST_CASE("BinaryExpressionProcessor arithmetic", "[language]") ...@@ -202,83 +305,6 @@ TEST_CASE("BinaryExpressionProcessor arithmetic", "[language]")
SECTION("binary operator [builtin]") 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") SECTION("builtin both side")
{ {
std::string_view data = R"(let r:builtin_t, r = bt_a + bt_b;)"; std::string_view data = R"(let r:builtin_t, r = bt_a + bt_b;)";
...@@ -303,72 +329,6 @@ TEST_CASE("BinaryExpressionProcessor arithmetic", "[language]") ...@@ -303,72 +329,6 @@ TEST_CASE("BinaryExpressionProcessor arithmetic", "[language]")
CHECK_BUILTIN_BINARY_EXPRESSION_RESULT(data, expected); 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") SECTION("runtime error")
{ {
std::string_view data_both = R"(let r:builtin_t, r = bt_a / bt_b;)"; std::string_view data_both = R"(let r:builtin_t, r = bt_a / bt_b;)";
......
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_all.hpp> #include <catch2/matchers/catch_matchers_all.hpp>
#include <FixturesForBuiltinT.hpp>
#include <language/ast/ASTBuilder.hpp> #include <language/ast/ASTBuilder.hpp>
#include <language/ast/ASTModulesImporter.hpp> #include <language/ast/ASTModulesImporter.hpp>
#include <language/ast/ASTNodeDataTypeBuilder.hpp> #include <language/ast/ASTNodeDataTypeBuilder.hpp>
...@@ -63,17 +65,6 @@ ...@@ -63,17 +65,6 @@
REQUIRE_THROWS_WITH(ASTNodeDataTypeBuilder{*ast}, error_message); \ 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 // clazy:excludeall=non-pod-global-static
TEST_CASE("UnaryExpressionProcessor", "[language]") TEST_CASE("UnaryExpressionProcessor", "[language]")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment