Skip to content

Commit ab9083f

Browse files
committed
C++: generate correct null check for special properties _root and _parent
1 parent deec84c commit ab9083f

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

spec/cpp_stl_11/test_nested_types_import.cpp

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/cpp_stl_98/test_nested_types_import.cpp

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

translator/src/main/scala/io/kaitai/struct/testtranslator/specgenerators/CppStlSG.scala

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.kaitai.struct.testtranslator.specgenerators
22

33
import io.kaitai.struct.datatype.{DataType, KSError}
44
import io.kaitai.struct.exprlang.Ast
5+
import io.kaitai.struct.format.Identifier
56
import io.kaitai.struct.languages.CppCompiler
67
import io.kaitai.struct.testtranslator.{Main, TestAssert, TestEquals, TestSpec}
78
import io.kaitai.struct.translators.CppTranslator
@@ -75,9 +76,19 @@ class CppStlSG(spec: TestSpec, provider: ClassTypeProvider, cppConfig: CppRuntim
7576
override def nullAssert(actual: Ast.expr): Unit = {
7677
val nullCheckStr = actual match {
7778
case Ast.expr.Attribute(x, Ast.identifier(attrName)) =>
78-
translateAct(x) + s"->_is_null_$attrName()"
79+
val expr = translateAct(x)
80+
attrName match {
81+
case Identifier.PARENT | Identifier.ROOT =>
82+
config.cppConfig.pointers match {
83+
case CppRuntimeConfig.UniqueAndRawPointers =>
84+
out.puts(s"BOOST_CHECK_EQUAL($expr->$attrName(), nullptr);")
85+
case CppRuntimeConfig.RawPointers =>
86+
out.puts(s"BOOST_CHECK(!$expr->$attrName());")
87+
}
88+
case _ =>
89+
out.puts(s"BOOST_CHECK($expr->_is_null_$attrName());")
90+
}
7991
}
80-
out.puts(s"BOOST_CHECK($nullCheckStr);")
8192
}
8293

8394
override def trueArrayEquality(check: TestEquals, elType: DataType, elts: Seq[Ast.expr]): Unit = {

0 commit comments

Comments
 (0)