printer tree BUGFIX implicit case in compiled tree
Fix #1574. The printer tree can now print the implicit case node when
flag LY_CTX_SET_PRIV_PARSED is set.
diff --git a/src/context.h b/src/context.h
index f208c95..e9334c9 100644
--- a/src/context.h
+++ b/src/context.h
@@ -184,6 +184,8 @@
#define LY_CTX_PREFER_SEARCHDIRS 0x20 /**< When searching for schema, prefer searchdirs instead of user callback. */
#define LY_CTX_SET_PRIV_PARSED 0x40 /**< For all compiled nodes, their private objects (::lysc_node.priv) are used
by libyang as a reference to the corresponding parsed node (::lysp_node).
+ The exception are \"case\" statements, which are ommitted (shorthand),
+ in that case the private objects are set to NULL.
So if this option is set, the user must not change private objects.
Setting this option by ::ly_ctx_set_options() may result in context recompilation.
Resetting this option by ::ly_ctx_unset_options() cause that private
diff --git a/src/printer_tree.c b/src/printer_tree.c
index c5468ec..28579af 100644
--- a/src/printer_tree.c
+++ b/src/printer_tree.c
@@ -3136,9 +3136,10 @@
{
(void) ca;
const struct lysc_node *cn;
+ const struct lysc_node *child;
struct trt_node ret;
- assert(tc && tc->cn && tc->cn->priv);
+ assert(tc && tc->cn);
cn = tc->cn;
ret = TRP_EMPTY_NODE;
@@ -3161,11 +3162,25 @@
/* set node's name */
ret.name.str = cn->name;
- /* <type> */
- ret.type = trop_resolve_type(TRP_TREE_CTX_GET_LYSP_NODE(cn));
+ if (tc->cn->priv) {
+ /* <type> */
+ ret.type = trop_resolve_type(TRP_TREE_CTX_GET_LYSP_NODE(cn));
- /* <iffeature> */
- ret.iffeatures = trop_node_has_iffeature(TRP_TREE_CTX_GET_LYSP_NODE(cn));
+ /* <iffeature> */
+ ret.iffeatures = trop_node_has_iffeature(TRP_TREE_CTX_GET_LYSP_NODE(cn));
+ } else {
+ /* only the case node can have the priv pointer set to NULL */
+ assert(tc->cn->nodetype & LYS_CASE);
+
+ /* <type> */
+ ret.type = TRP_EMPTY_TRT_TYPE;
+
+ /* copy <iffeature> from child */
+ child = lysc_node_child(cn);
+ ret.iffeatures = child ?
+ trop_node_has_iffeature(TRP_TREE_CTX_GET_LYSP_NODE(child)) :
+ 0;
+ }
ret.last_one = !tro_next_sibling(cn, tc->lysc_tree);
diff --git a/src/schema_compile_node.c b/src/schema_compile_node.c
index 8a8e6e9..ee44ad5 100644
--- a/src/schema_compile_node.c
+++ b/src/schema_compile_node.c
@@ -3292,6 +3292,7 @@
LY_ERR ret = LY_SUCCESS;
struct lysp_node *child_p_next = child_p->next;
struct lysp_node_case *cs_p;
+ struct lysc_node *compiled_case;
if (child_p->nodetype == LYS_CASE) {
/* standard case under choice */
@@ -3311,7 +3312,16 @@
free_fake_node:
/* free the fake parsed node and correct pointers back */
+
cs_p->child = NULL;
+ if (ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED) {
+ /* compiled node cannot point to the implicit case node */
+ /* get last case node */
+ compiled_case = ((struct lysc_node_choice *)node)->cases->prev;
+ assert(compiled_case->priv == cs_p);
+ /* must be set to NULL */
+ compiled_case->priv = NULL;
+ }
lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
child_p->next = child_p_next;
}
diff --git a/tests/utests/schema/test_printer_tree.c b/tests/utests/schema/test_printer_tree.c
index eb0de68..6b42d84 100644
--- a/tests/utests/schema/test_printer_tree.c
+++ b/tests/utests/schema/test_printer_tree.c
@@ -431,13 +431,43 @@
" choice my_choice {\n"
" case my_case;\n"
" }\n"
+ " choice shorthand {\n"
+ " container cont1;\n"
+ " container cont2 {\n"
+ " container cont3;\n"
+ " }\n"
+ " }\n"
+ " container top {\n"
+ " choice shorthand1 {\n"
+ " container cont1;\n"
+ " }\n"
+ " choice shorthand2 {\n"
+ " container cont2 {\n"
+ " container cont3;\n"
+ " }\n"
+ " }\n"
+ " }\n"
"}\n";
/* from pyang */
expect =
"module: a08\n"
" +--rw (my_choice)?\n"
- " +--:(my_case)\n";
+ " | +--:(my_case)\n"
+ " +--rw (shorthand)?\n"
+ " | +--:(cont1)\n"
+ " | | +--rw cont1\n"
+ " | +--:(cont2)\n"
+ " | +--rw cont2\n"
+ " | +--rw cont3\n"
+ " +--rw top\n"
+ " +--rw (shorthand1)?\n"
+ " | +--:(cont1)\n"
+ " | +--rw cont1\n"
+ " +--rw (shorthand2)?\n"
+ " +--:(cont2)\n"
+ " +--rw cont2\n"
+ " +--rw cont3\n";
UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
TEST_LOCAL_PRINT(mod, 72);