From b2b5a14679a0e65359d4e299776305f8797d7d69 Mon Sep 17 00:00:00 2001 From: Mingun Date: Sat, 6 Apr 2024 19:40:47 +0500 Subject: [PATCH] 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 = {