From b2b5a14679a0e65359d4e299776305f8797d7d69 Mon Sep 17 00:00:00 2001 From: Mingun Date: Sat, 6 Apr 2024 19:40:47 +0500 Subject: [PATCH 1/2] C++: generate correct null check for special properties `_root` and `_parent` --- spec/cpp_stl_11/test_nested_types_import.cpp | 2 ++ spec/cpp_stl_98/test_nested_types_import.cpp | 2 ++ .../testtranslator/specgenerators/CppStlSG.scala | 15 +++++++++++++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/spec/cpp_stl_11/test_nested_types_import.cpp b/spec/cpp_stl_11/test_nested_types_import.cpp index 35dbfe1e9..1ba039da6 100644 --- a/spec/cpp_stl_11/test_nested_types_import.cpp +++ b/spec/cpp_stl_11/test_nested_types_import.cpp @@ -1,3 +1,5 @@ +// Autogenerated from KST: please remove this line if doing any edits by hand! + #include #include "nested_types_import.h" #include diff --git a/spec/cpp_stl_98/test_nested_types_import.cpp b/spec/cpp_stl_98/test_nested_types_import.cpp index b0721ad79..5d359b76b 100644 --- a/spec/cpp_stl_98/test_nested_types_import.cpp +++ b/spec/cpp_stl_98/test_nested_types_import.cpp @@ -1,3 +1,5 @@ +// Autogenerated from KST: please remove this line if doing any edits by hand! + #include #include "nested_types_import.h" #include diff --git a/translator/src/main/scala/io/kaitai/struct/testtranslator/specgenerators/CppStlSG.scala b/translator/src/main/scala/io/kaitai/struct/testtranslator/specgenerators/CppStlSG.scala index c2b9fe6b1..1d9aca95d 100644 --- a/translator/src/main/scala/io/kaitai/struct/testtranslator/specgenerators/CppStlSG.scala +++ b/translator/src/main/scala/io/kaitai/struct/testtranslator/specgenerators/CppStlSG.scala @@ -2,6 +2,7 @@ package io.kaitai.struct.testtranslator.specgenerators import io.kaitai.struct.datatype.{DataType, KSError} import io.kaitai.struct.exprlang.Ast +import io.kaitai.struct.format.Identifier import io.kaitai.struct.languages.CppCompiler import io.kaitai.struct.testtranslator.{Main, TestAssert, TestEquals, TestSpec} import io.kaitai.struct.translators.CppTranslator @@ -75,9 +76,19 @@ class CppStlSG(spec: TestSpec, provider: ClassTypeProvider, cppConfig: CppRuntim override def nullAssert(actual: Ast.expr): Unit = { val nullCheckStr = actual match { case Ast.expr.Attribute(x, Ast.identifier(attrName)) => - translateAct(x) + s"->_is_null_$attrName()" + val expr = translateAct(x) + attrName match { + case Identifier.PARENT | Identifier.ROOT => + config.cppConfig.pointers match { + case CppRuntimeConfig.UniqueAndRawPointers => + out.puts(s"BOOST_CHECK_EQUAL($expr->$attrName(), nullptr);") + case CppRuntimeConfig.RawPointers => + out.puts(s"BOOST_CHECK(!$expr->$attrName());") + } + case _ => + out.puts(s"BOOST_CHECK($expr->_is_null_$attrName());") + } } - out.puts(s"BOOST_CHECK($nullCheckStr);") } override def trueArrayEquality(check: TestEquals, elType: DataType, elts: Seq[Ast.expr]): Unit = { From 732b4c276a966a035a5adab92430abe73bbbae76 Mon Sep 17 00:00:00 2001 From: Mingun Date: Sat, 6 Apr 2024 20:43:08 +0500 Subject: [PATCH 2/2] C++: do not cast numerical expected values of enums to the enum type The test was initially introduced with that logic in 3bdfb59642dc60b8abd98bb9455cda94235df3d3, however the similar tests `enum_invalid` and `enum_to_i_invalid` uses the numerical values and passes successfully, which means that such manual edits in tests are not required. Regenerate those tests from KST with command: ./spec_kst_to_all -t cpp_stl_98 -t cpp_stl_11 --all-specs -f --- spec/cpp_stl_11/test_switch_manual_enum_invalid.cpp | 8 ++++---- spec/cpp_stl_11/test_switch_manual_enum_invalid_else.cpp | 8 ++++---- spec/cpp_stl_98/test_switch_manual_enum_invalid.cpp | 8 ++++---- spec/cpp_stl_98/test_switch_manual_enum_invalid_else.cpp | 8 ++++---- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/spec/cpp_stl_11/test_switch_manual_enum_invalid.cpp b/spec/cpp_stl_11/test_switch_manual_enum_invalid.cpp index 9345c016d..31a723355 100644 --- a/spec/cpp_stl_11/test_switch_manual_enum_invalid.cpp +++ b/spec/cpp_stl_11/test_switch_manual_enum_invalid.cpp @@ -1,20 +1,20 @@ +// Autogenerated from KST: please remove this line if doing any edits by hand! + #include #include "switch_manual_enum_invalid.h" #include #include #include - BOOST_AUTO_TEST_CASE(test_switch_manual_enum_invalid) { std::ifstream ifs("src/enum_negative.bin", std::ifstream::binary); kaitai::kstream ks(&ifs); switch_manual_enum_invalid_t* r = new switch_manual_enum_invalid_t(&ks); - BOOST_CHECK_EQUAL(r->opcodes()->size(), 2); - BOOST_CHECK_EQUAL(r->opcodes()->at(0)->code(), static_cast(255)); + BOOST_CHECK_EQUAL(r->opcodes()->at(0)->code(), 255); BOOST_CHECK(r->opcodes()->at(0)->_is_null_body()); - BOOST_CHECK_EQUAL(r->opcodes()->at(1)->code(), static_cast(1)); + BOOST_CHECK_EQUAL(r->opcodes()->at(1)->code(), 1); BOOST_CHECK(r->opcodes()->at(1)->_is_null_body()); delete r; diff --git a/spec/cpp_stl_11/test_switch_manual_enum_invalid_else.cpp b/spec/cpp_stl_11/test_switch_manual_enum_invalid_else.cpp index 2445d0156..df686e170 100644 --- a/spec/cpp_stl_11/test_switch_manual_enum_invalid_else.cpp +++ b/spec/cpp_stl_11/test_switch_manual_enum_invalid_else.cpp @@ -1,20 +1,20 @@ +// Autogenerated from KST: please remove this line if doing any edits by hand! + #include #include "switch_manual_enum_invalid_else.h" #include #include #include - BOOST_AUTO_TEST_CASE(test_switch_manual_enum_invalid_else) { std::ifstream ifs("src/enum_negative.bin", std::ifstream::binary); kaitai::kstream ks(&ifs); switch_manual_enum_invalid_else_t* r = new switch_manual_enum_invalid_else_t(&ks); - BOOST_CHECK_EQUAL(r->opcodes()->size(), 2); - BOOST_CHECK_EQUAL(r->opcodes()->at(0)->code(), static_cast(255)); + BOOST_CHECK_EQUAL(r->opcodes()->at(0)->code(), 255); BOOST_CHECK_EQUAL(static_cast(r->opcodes()->at(0)->body())->value(), 123); - BOOST_CHECK_EQUAL(r->opcodes()->at(1)->code(), static_cast(1)); + BOOST_CHECK_EQUAL(r->opcodes()->at(1)->code(), 1); BOOST_CHECK_EQUAL(static_cast(r->opcodes()->at(1)->body())->value(), 123); delete r; diff --git a/spec/cpp_stl_98/test_switch_manual_enum_invalid.cpp b/spec/cpp_stl_98/test_switch_manual_enum_invalid.cpp index 9345c016d..31a723355 100644 --- a/spec/cpp_stl_98/test_switch_manual_enum_invalid.cpp +++ b/spec/cpp_stl_98/test_switch_manual_enum_invalid.cpp @@ -1,20 +1,20 @@ +// Autogenerated from KST: please remove this line if doing any edits by hand! + #include #include "switch_manual_enum_invalid.h" #include #include #include - BOOST_AUTO_TEST_CASE(test_switch_manual_enum_invalid) { std::ifstream ifs("src/enum_negative.bin", std::ifstream::binary); kaitai::kstream ks(&ifs); switch_manual_enum_invalid_t* r = new switch_manual_enum_invalid_t(&ks); - BOOST_CHECK_EQUAL(r->opcodes()->size(), 2); - BOOST_CHECK_EQUAL(r->opcodes()->at(0)->code(), static_cast(255)); + BOOST_CHECK_EQUAL(r->opcodes()->at(0)->code(), 255); BOOST_CHECK(r->opcodes()->at(0)->_is_null_body()); - BOOST_CHECK_EQUAL(r->opcodes()->at(1)->code(), static_cast(1)); + BOOST_CHECK_EQUAL(r->opcodes()->at(1)->code(), 1); BOOST_CHECK(r->opcodes()->at(1)->_is_null_body()); delete r; diff --git a/spec/cpp_stl_98/test_switch_manual_enum_invalid_else.cpp b/spec/cpp_stl_98/test_switch_manual_enum_invalid_else.cpp index 2445d0156..df686e170 100644 --- a/spec/cpp_stl_98/test_switch_manual_enum_invalid_else.cpp +++ b/spec/cpp_stl_98/test_switch_manual_enum_invalid_else.cpp @@ -1,20 +1,20 @@ +// Autogenerated from KST: please remove this line if doing any edits by hand! + #include #include "switch_manual_enum_invalid_else.h" #include #include #include - BOOST_AUTO_TEST_CASE(test_switch_manual_enum_invalid_else) { std::ifstream ifs("src/enum_negative.bin", std::ifstream::binary); kaitai::kstream ks(&ifs); switch_manual_enum_invalid_else_t* r = new switch_manual_enum_invalid_else_t(&ks); - BOOST_CHECK_EQUAL(r->opcodes()->size(), 2); - BOOST_CHECK_EQUAL(r->opcodes()->at(0)->code(), static_cast(255)); + BOOST_CHECK_EQUAL(r->opcodes()->at(0)->code(), 255); BOOST_CHECK_EQUAL(static_cast(r->opcodes()->at(0)->body())->value(), 123); - BOOST_CHECK_EQUAL(r->opcodes()->at(1)->code(), static_cast(1)); + BOOST_CHECK_EQUAL(r->opcodes()->at(1)->code(), 1); BOOST_CHECK_EQUAL(static_cast(r->opcodes()->at(1)->body())->value(), 123); delete r;