libyang REFACTOR use freeing context

Fixes compiled extension memory leaks.
Refactoring included.
diff --git a/src/context.c b/src/context.c
index 593dff0..3a3c10b 100644
--- a/src/context.c
+++ b/src/context.c
@@ -45,6 +45,7 @@
 #include "tree_data.h"
 #include "tree_data_internal.h"
 #include "tree_schema.h"
+#include "tree_schema_free.h"
 #include "tree_schema_internal.h"
 
 #include "../models/ietf-datastores@2018-02-14.h"
@@ -1237,7 +1238,7 @@
 LIBYANG_API_DEF void
 ly_ctx_destroy(struct ly_ctx *ctx)
 {
-    struct lys_module *mod;
+    struct lysf_ctx fctx = {.ctx = ctx};
 
     if (!ctx) {
         return;
@@ -1245,18 +1246,21 @@
 
     /* models list */
     for ( ; ctx->list.count; ctx->list.count--) {
-        mod = ctx->list.objs[ctx->list.count - 1];
+        fctx.mod = ctx->list.objs[ctx->list.count - 1];
 
         /* remove the module */
-        if (mod->implemented) {
-            mod->implemented = 0;
-            lysc_module_free(mod->compiled);
-            mod->compiled = NULL;
+        if (fctx.mod->implemented) {
+            fctx.mod->implemented = 0;
+            lysc_module_free(&fctx, fctx.mod->compiled);
+            fctx.mod->compiled = NULL;
         }
-        lys_module_free(ctx->list.objs[ctx->list.count - 1], 0);
+        lys_module_free(&fctx, fctx.mod, 0);
     }
     free(ctx->list.objs);
 
+    /* free extensions */
+    lysf_ctx_erase(&fctx);
+
     /* search paths list */
     ly_set_erase(&ctx->search_paths, free);
 
diff --git a/src/parser_xml.c b/src/parser_xml.c
index 49b20fb..a1fd82c 100644
--- a/src/parser_xml.c
+++ b/src/parser_xml.c
@@ -1034,7 +1034,7 @@
 {
     LY_ERR rc = LY_SUCCESS;
     struct ly_ctx *ctx = (struct ly_ctx *)LYD_CTX(node);
-    struct lysc_ctx cctx = {.ctx = ctx};
+    struct lysc_ctx cctx;
     const struct lys_module *mod;
     LY_ARRAY_COUNT_TYPE u;
     struct ly_err_item *err = NULL;
@@ -1042,6 +1042,8 @@
     struct lysc_pattern **patterns = NULL;
     const char *value;
 
+    LYSC_CTX_INIT_CTX(cctx, ctx);
+
     /* get date-and-time parsed type */
     mod = ly_ctx_get_module_latest(ctx, "ietf-yang-types");
     assert(mod);
@@ -1062,7 +1064,7 @@
     rc = lyplg_type_validate_patterns(patterns, value, strlen(value), &err);
 
 cleanup:
-    FREE_ARRAY(ctx, patterns, lysc_pattern_free);
+    FREE_ARRAY(&cctx.free_ctx, patterns, lysc_pattern_free);
     if (rc && err) {
         LOGVAL_ERRITEM(ctx, err);
         ly_err_free(err);
diff --git a/src/parser_yang.c b/src/parser_yang.c
index cb0ff61..d0b2ea1 100644
--- a/src/parser_yang.c
+++ b/src/parser_yang.c
@@ -3,7 +3,7 @@
  * @author Michal Vasko <mvasko@cesnet.cz>
  * @brief YANG parser
  *
- * Copyright (c) 2018 CESNET, z.s.p.o.
+ * Copyright (c) 2018 - 2022 CESNET, z.s.p.o.
  *
  * This source code is licensed under BSD 3-Clause License (the "License").
  * You may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
 #include "tree.h"
 #include "tree_edit.h"
 #include "tree_schema.h"
+#include "tree_schema_free.h"
 #include "tree_schema_internal.h"
 
 struct lys_glob_unres;
@@ -1635,6 +1636,7 @@
     size_t word_len;
     enum ly_stmt kw;
     struct lysp_when *when;
+    struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
 
     if (*when_p) {
         LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
@@ -1669,7 +1671,7 @@
 
 cleanup:
     if (ret) {
-        lysp_when_free(PARSER_CTX(ctx), when);
+        lysp_when_free(&fctx, when);
         free(when);
     } else {
         *when_p = when;
@@ -3809,6 +3811,7 @@
     char *buf = NULL, *word;
     size_t word_len, dev_mod;
     enum ly_stmt kw;
+    struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
     struct lysp_deviate *d = NULL;
     struct lysp_deviate_add *d_add = NULL;
     struct lysp_deviate_rpl *d_rpl = NULL;
@@ -4013,7 +4016,7 @@
 cleanup:
     free(buf);
     if (ret) {
-        lysp_deviate_free(PARSER_CTX(ctx), d);
+        lysp_deviate_free(&fctx, d);
         free(d);
     } else {
         /* insert into siblings */
@@ -4038,6 +4041,7 @@
     size_t word_len;
     enum ly_stmt kw;
     struct lysp_deviation *dev;
+    struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
 
     LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
 
@@ -4078,7 +4082,7 @@
 
 cleanup:
     if (ret) {
-        lysp_deviation_free(PARSER_CTX(ctx), dev);
+        lysp_deviation_free(&fctx, dev);
         LY_ARRAY_DECREMENT_FREE(*deviations);
     }
     return ret;
@@ -4653,6 +4657,7 @@
     size_t word_len;
     enum ly_stmt kw;
     struct lysp_submodule *mod_p = NULL;
+    struct lysf_ctx fctx = {.ctx = ly_ctx};
 
     assert(context && ly_ctx && main_ctx && in && submod);
 
@@ -4711,7 +4716,7 @@
 cleanup:
     LOG_LOCBACK(0, 0, 0, 1);
     if (ret) {
-        lysp_module_free((struct lysp_module *)mod_p);
+        lysp_module_free(&fctx, (struct lysp_module *)mod_p);
         yang_parser_ctx_free(*context);
         *context = NULL;
     }
@@ -4727,6 +4732,7 @@
     size_t word_len;
     enum ly_stmt kw;
     struct lysp_module *mod_p = NULL;
+    struct lysf_ctx fctx = {.ctx = mod->ctx};
 
     /* create context */
     *context = calloc(1, sizeof **context);
@@ -4779,7 +4785,7 @@
 cleanup:
     LOG_LOCBACK(0, 0, 0, 1);
     if (ret) {
-        lysp_module_free(mod_p);
+        lysp_module_free(&fctx, mod_p);
         yang_parser_ctx_free(*context);
         *context = NULL;
     }
diff --git a/src/parser_yin.c b/src/parser_yin.c
index 6dd7d6e..1e201b0 100644
--- a/src/parser_yin.c
+++ b/src/parser_yin.c
@@ -1,9 +1,10 @@
 /**
  * @file parser_yin.c
  * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz>
+ * @author Michal Vasko <mvasko@cesnet.cz>
  * @brief YIN parser.
  *
- * Copyright (c) 2015 - 2019 CESNET, z.s.p.o.
+ * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
  *
  * This source code is licensed under BSD 3-Clause License (the "License").
  * You may not use this file except in compliance with the License.
@@ -2770,7 +2771,6 @@
  *
  * @param[in,out] ctx YIN parser context for logging and to store current state.
  * @param[in] deviations Deviations to add to.
- *
  * @return LY_ERR values.
  */
 static LY_ERR
@@ -2778,6 +2778,7 @@
 {
     LY_ERR ret = LY_SUCCESS;
     struct lysp_deviation *dev = NULL;
+    struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
 
     /* create new deviation */
     LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *deviations, dev, LY_EMEM);
@@ -2797,7 +2798,7 @@
 
 cleanup:
     if (ret) {
-        lysp_deviation_free(PARSER_CTX(ctx), dev);
+        lysp_deviation_free(&fctx, dev);
         LY_ARRAY_DECREMENT_FREE(*deviations);
     }
     return ret;
@@ -2809,7 +2810,6 @@
  * @param[in] ctx YIN parser context used for logging.
  * @param[in] kw Keyword that is child of module or submodule.
  * @param[out] group Group of keyword.
- *
  * @return LY_SUCCESS on success LY_EINT if kw can't be mapped to kw_group, should not happen if called correctly.
  */
 static LY_ERR
@@ -2877,7 +2877,6 @@
  * @param[in] next_kw Next keyword.
  * @param[in] parrent Identification of parrent element, can be se to to LY_STMT_MODULE of LY_STMT_SUBMODULE,
  *            because relative order is required only in module and submodule sub-elements, used for logging.
- *
  * @return LY_SUCCESS on success and LY_EVALID if relative order is invalid.
  */
 static LY_ERR
@@ -2908,7 +2907,6 @@
  * @param[in,out] ctx Yin parser context for logging and to store current state.
  * @param[in] elem_type Type of element that is currently being parsed.
  * @param[out] arg Value to write to.
- *
  * @return LY_ERR values.
  */
 static LY_ERR
@@ -3062,7 +3060,6 @@
  * @param[in,out] ctx Yin parser context for XML context, logging, and to store current state.
  * @param[in] parent Identification of parent element.
  * @param[out] element Where the element structure should be stored.
- *
  * @return LY_ERR values.
  */
 LY_ERR
@@ -3183,7 +3180,6 @@
  * @param[in] subelem The statement this extension instance is a subelement of.
  * @param[in] subelem_index Index of the keyword instance this extension instance is a subelement of
  * @param[in,out] exts Extension instance to add to.
- *
  * @return LY_ERR values.
  */
 LY_ERR
@@ -3291,7 +3287,6 @@
  * @param[in] current_element Type of current element.
  * @param[out] text_content Where the text content of element should be stored if any. Text content is ignored if set to NULL.
  * @param[in,out] exts Extension instance to add to. Can be set to null if element cannot have extension as subelements.
- *
  * @return LY_ERR values.
  */
 LY_ERR
@@ -3595,7 +3590,6 @@
  *
  * @param[in,out] ctx Yin parser context for logging and to store current state.
  * @param[out] mod Parsed module structure.
- *
  * @return LY_ERR values.
  */
 LY_ERR
@@ -3661,7 +3655,6 @@
  * @param[in,out] ctx Yin parser context for logging and to store current state.
  * @param[in] mod_attrs Attributes of submodule element.
  * @param[out] submod Parsed submodule structure.
- *
  * @return LY_ERR values.
  */
 LY_ERR
@@ -3727,6 +3720,7 @@
     enum ly_stmt kw = LY_STMT_NONE;
     LY_ERR ret = LY_SUCCESS;
     struct lysp_submodule *mod_p = NULL;
+    struct lysf_ctx fctx = {.ctx = ctx};
 
     assert(yin_ctx && ctx && main_ctx && in && submod);
 
@@ -3781,7 +3775,7 @@
 
 cleanup:
     if (ret) {
-        lysp_module_free((struct lysp_module *)mod_p);
+        lysp_module_free(&fctx, (struct lysp_module *)mod_p);
         yin_parser_ctx_free(*yin_ctx);
         *yin_ctx = NULL;
     }
@@ -3794,6 +3788,7 @@
     LY_ERR ret = LY_SUCCESS;
     enum ly_stmt kw = LY_STMT_NONE;
     struct lysp_module *mod_p = NULL;
+    struct lysf_ctx fctx = {.ctx = mod->ctx};
 
     /* create context */
     *yin_ctx = calloc(1, sizeof **yin_ctx);
@@ -3843,7 +3838,7 @@
 
 cleanup:
     if (ret != LY_SUCCESS) {
-        lysp_module_free(mod_p);
+        lysp_module_free(&fctx, mod_p);
         yin_parser_ctx_free(*yin_ctx);
         *yin_ctx = NULL;
     }
diff --git a/src/path.c b/src/path.c
index 523a509..b6dc04a 100644
--- a/src/path.c
+++ b/src/path.c
@@ -1148,6 +1148,7 @@
 ly_path_predicates_free(const struct ly_ctx *ctx, enum ly_path_pred_type pred_type, struct ly_path_predicate *predicates)
 {
     LY_ARRAY_COUNT_TYPE u;
+    struct lysf_ctx fctx = {.ctx = (struct ly_ctx *)ctx};
 
     if (!predicates) {
         return;
@@ -1163,7 +1164,7 @@
         case LY_PATH_PREDTYPE_LEAFLIST:
             if (predicates[u].value.realtype) {
                 predicates[u].value.realtype->plugin->free(ctx, &predicates[u].value);
-                lysc_type_free((struct ly_ctx *)ctx, (struct lysc_type *)predicates[u].value.realtype);
+                lysc_type_free(&fctx, (struct lysc_type *)predicates[u].value.realtype);
             }
             break;
         }
diff --git a/src/schema_compile.c b/src/schema_compile.c
index f4aae9e..c02e628 100644
--- a/src/schema_compile.c
+++ b/src/schema_compile.c
@@ -44,6 +44,7 @@
 #include "tree.h"
 #include "tree_data.h"
 #include "tree_schema.h"
+#include "tree_schema_free.h"
 #include "tree_schema_internal.h"
 #include "xpath.h"
 
@@ -55,39 +56,30 @@
 {
     LY_ERR ret = LY_SUCCESS;
 
-    if (ext_p->compiled && (ext_p->compiled->refcount == 1)) {
-        /* context recompilation - all the extension instances were previously freed (the last link to the compiled extension
-         * remains from the parsed extension definition) and now we are recompiling them again, to have the up-to-date
-         * extension definition, we have to recompile it as well now */
-        lysc_extension_free(ctx->ctx, &ext_p->compiled);
-    }
-
     if (!ext_p->compiled) {
         lysc_update_path(ctx, NULL, "{extension}");
         lysc_update_path(ctx, NULL, ext_p->name);
 
         /* compile the extension definition */
-        ext_p->compiled = calloc(1, sizeof **ext);
-        ext_p->compiled->refcount = 2;
-        DUP_STRING_GOTO(ctx->ctx, ext_p->name, ext_p->compiled->name, ret, done);
-        DUP_STRING_GOTO(ctx->ctx, ext_p->argname, ext_p->compiled->argname, ret, done);
-        ext_p->compiled->module = (struct lys_module *)ext_mod;
-        COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, ret, done);
+        *ext = ext_p->compiled = calloc(1, sizeof **ext);
+        (*ext)->refcount = 1;
+        DUP_STRING_GOTO(ctx->ctx, ext_p->name, (*ext)->name, ret, done);
+        DUP_STRING_GOTO(ctx->ctx, ext_p->argname, (*ext)->argname, ret, done);
+        (*ext)->module = (struct lys_module *)ext_mod;
+
+        /* compile nested extensions */
+        COMPILE_EXTS_GOTO(ctx, ext_p->exts, (*ext)->exts, *ext, ret, done);
 
         lysc_update_path(ctx, NULL, NULL);
         lysc_update_path(ctx, NULL, NULL);
 
         /* find extension definition plugin */
-        ext_p->compiled->plugin = lyplg_find(LYPLG_EXTENSION, ext_p->compiled->module->name,
-                ext_p->compiled->module->revision, ext_p->compiled->name);
-
-        /* refcount 2 already */
-        *ext = ext_p->compiled;
-    } else {
-        /* increase refcount */
-        *ext = lysc_ext_dup(ext_p->compiled);
+        (*ext)->plugin = lyplg_find(LYPLG_EXTENSION, (*ext)->module->name,
+                (*ext)->module->revision, (*ext)->name);
     }
 
+    *ext = ext_p->compiled;
+
 done:
     if (ret) {
         lysc_update_path(ctx, NULL, NULL);
@@ -126,7 +118,7 @@
         }
         ret = ext->def->plugin->compile(ctx, ext_p, ext);
         if (ret == LY_ENOT) {
-            lysc_ext_instance_free(ctx->ctx, ext);
+            lysc_ext_instance_free(&ctx->free_ctx, ext);
         }
         if (ext->argument) {
             lysc_update_path(ctx, NULL, NULL);
@@ -137,7 +129,6 @@
 cleanup:
     lysc_update_path(ctx, NULL, NULL);
     lysc_update_path(ctx, NULL, NULL);
-
     return ret;
 }
 
@@ -286,19 +277,19 @@
         const struct lysp_ident *identities_p, struct lysc_ident **identities)
 {
     LY_ARRAY_COUNT_TYPE u;
-    struct lysc_ctx context = {0};
+    struct lysc_ctx cctx;
     struct lysc_ident *ident;
     LY_ERR ret = LY_SUCCESS;
 
     assert(ctx_sc || ctx);
 
     if (!ctx_sc) {
-        context.ctx = ctx;
-        context.cur_mod = parsed_mod ? parsed_mod->mod : NULL;
-        context.pmod = parsed_mod;
-        context.path_len = 1;
-        context.path[0] = '/';
-        ctx_sc = &context;
+        if (parsed_mod) {
+            LYSC_CTX_INIT_PMOD(cctx, parsed_mod);
+        } else {
+            LYSC_CTX_INIT_CTX(cctx, ctx);
+        }
+        ctx_sc = &cctx;
     }
 
     if (!identities_p) {
@@ -657,7 +648,7 @@
                     LY_CHECK_GOTO(rc = lysp_stmt_parse(ctx, stmt, &parsed, NULL), cleanup);
                     r = lys_compile_type(ctx, NULL, flags ? *flags : 0, ext_p->name, parsed, (struct lysc_type **)compiled,
                             (units && !*units) ? units : NULL, NULL);
-                    lysp_type_free(ctx->ctx, parsed);
+                    lysp_type_free(&ctx->free_ctx, parsed);
                     free(parsed);
                     LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
                     break;
@@ -1033,7 +1024,7 @@
  * @param[in] items Sized array of bits/enums.
  */
 static void
-lys_compile_unres_disabled_bitenum_remove(struct ly_ctx *ctx, struct lysc_type_bitenum_item *items)
+lys_compile_unres_disabled_bitenum_remove(struct lysf_ctx *ctx, struct lysc_type_bitenum_item *items)
 {
     LY_ARRAY_COUNT_TYPE u = 0, last_u;
 
@@ -1082,7 +1073,7 @@
         if ((t[u]->basetype == LY_TYPE_BITS) || (t[u]->basetype == LY_TYPE_ENUM)) {
             /* remove all disabled items */
             ent = (struct lysc_type_enum *)(t[u]);
-            lys_compile_unres_disabled_bitenum_remove(ctx->ctx, ent->enums);
+            lys_compile_unres_disabled_bitenum_remove(&ctx->free_ctx, ent->enums);
 
             if (!LY_ARRAY_COUNT(ent->enums)) {
                 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "%s type of node \"%s\" without any (or all disabled) valid values.",
@@ -1384,22 +1375,16 @@
     struct lysc_node *node;
     struct lysc_type *typeiter;
     struct lysc_type_leafref *lref;
-    struct lysc_ctx cctx;
-    struct lys_depset_unres *ds_unres;
+    struct lysc_ctx cctx = {0};
+    struct lys_depset_unres *ds_unres = &unres->ds_unres;
     struct ly_path *path;
     LY_ARRAY_COUNT_TYPE v;
     struct lysc_unres_leafref *l;
+    struct lysc_unres_must *m;
+    struct lysc_unres_dflt *r;
     uint32_t i, processed_leafrefs = 0;
 
-    ds_unres = &unres->ds_unres;
-
-    /* fake compile context */
 resolve_all:
-    memset(&cctx, 0, sizeof cctx);
-    cctx.ctx = ctx;
-    cctx.path_len = 1;
-    cctx.path[0] = '/';
-
     /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
      * can be also leafref, in case it is already resolved, go through the chain and check that it does not
      * point to the starting leafref type). The second round stores the first non-leafref type for later data validation.
@@ -1408,8 +1393,7 @@
         /* remember index, it can change before we get to free this item */
         i = ds_unres->disabled_leafrefs.count - 1;
         l = ds_unres->disabled_leafrefs.objs[i];
-        cctx.cur_mod = l->node->module;
-        cctx.pmod = l->node->module->parsed;
+        LYSC_CTX_INIT_PMOD(cctx, l->node->module->parsed);
 
         LOG_LOCSET(l->node, NULL, NULL, NULL);
         v = 0;
@@ -1417,24 +1401,22 @@
             ret = lys_compile_unres_leafref(&cctx, l->node, lref, l->local_mod, unres);
         }
         LOG_LOCBACK(1, 0, 0, 0);
-        LY_CHECK_RET(ret);
+        LY_CHECK_GOTO(ret, cleanup);
 
         ly_set_rm_index(&ds_unres->disabled_leafrefs, i, free);
     }
 
     for (i = processed_leafrefs; i < ds_unres->leafrefs.count; ++i) {
         l = ds_unres->leafrefs.objs[i];
-        cctx.cur_mod = l->node->module;
-        cctx.pmod = l->node->module->parsed;
-        LOG_LOCSET(l->node, NULL, NULL, NULL);
+        LYSC_CTX_INIT_PMOD(cctx, l->node->module->parsed);
 
+        LOG_LOCSET(l->node, NULL, NULL, NULL);
         v = 0;
         while ((ret == LY_SUCCESS) && (lref = lys_type_leafref_next(l->node, &v))) {
             ret = lys_compile_unres_leafref(&cctx, l->node, lref, l->local_mod, unres);
         }
-
         LOG_LOCBACK(1, 0, 0, 0);
-        LY_CHECK_RET(ret);
+        LY_CHECK_GOTO(ret, cleanup);
     }
     for (i = processed_leafrefs; i < ds_unres->leafrefs.count; ++i) {
         l = ds_unres->leafrefs.objs[i];
@@ -1446,7 +1428,7 @@
                     typeiter->basetype == LY_TYPE_LEAFREF;
                     typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
 
-            lysc_type_free(ctx, lref->realtype);
+            lysc_type_free(&cctx.free_ctx, lref->realtype);
             lref->realtype = typeiter;
             ++lref->realtype->refcount;
         }
@@ -1461,13 +1443,12 @@
     while (ds_unres->whens.count) {
         i = ds_unres->whens.count - 1;
         node = ds_unres->whens.objs[i];
-        cctx.cur_mod = node->module;
-        cctx.pmod = node->module->parsed;
+        LYSC_CTX_INIT_PMOD(cctx, node->module->parsed);
 
         LOG_LOCSET(node, NULL, NULL, NULL);
         ret = lys_compile_unres_when(&cctx, node, unres);
         LOG_LOCBACK(1, 0, 0, 0);
-        LY_CHECK_RET(ret);
+        LY_CHECK_GOTO(ret, cleanup);
 
         ly_set_rm_index(&ds_unres->whens, i, NULL);
     }
@@ -1475,15 +1456,13 @@
     /* check must */
     while (ds_unres->musts.count) {
         i = ds_unres->musts.count - 1;
-        struct lysc_unres_must *m = ds_unres->musts.objs[i];
-
-        cctx.cur_mod = m->node->module;
-        cctx.pmod = m->node->module->parsed;
+        m = ds_unres->musts.objs[i];
+        LYSC_CTX_INIT_PMOD(cctx, m->node->module->parsed);
 
         LOG_LOCSET(m->node, NULL, NULL, NULL);
         ret = lys_compile_unres_must(&cctx, m->node, m->local_mods, unres);
         LOG_LOCBACK(1, 0, 0, 0);
-        LY_CHECK_RET(ret);
+        LY_CHECK_GOTO(ret, cleanup);
 
         lysc_unres_must_free(m);
         ly_set_rm_index(&ds_unres->musts, i, NULL);
@@ -1493,13 +1472,12 @@
     while (ds_unres->disabled_bitenums.count) {
         i = ds_unres->disabled_bitenums.count - 1;
         node = ds_unres->disabled_bitenums.objs[i];
-        cctx.cur_mod = node->module;
-        cctx.pmod = node->module->parsed;
+        LYSC_CTX_INIT_PMOD(cctx, node->module->parsed);
 
         LOG_LOCSET(node, NULL, NULL, NULL);
         ret = lys_compile_unres_disabled_bitenum(&cctx, (struct lysc_node_leaf *)node);
         LOG_LOCBACK(1, 0, 0, 0);
-        LY_CHECK_RET(ret);
+        LY_CHECK_GOTO(ret, cleanup);
 
         ly_set_rm_index(&ds_unres->disabled_bitenums, i, NULL);
     }
@@ -1507,10 +1485,8 @@
     /* finish incomplete default values compilation */
     while (ds_unres->dflts.count) {
         i = ds_unres->dflts.count - 1;
-        struct lysc_unres_dflt *r = ds_unres->dflts.objs[i];
-
-        cctx.cur_mod = r->leaf->module;
-        cctx.pmod = r->leaf->module->parsed;
+        r = ds_unres->dflts.objs[i];
+        LYSC_CTX_INIT_PMOD(cctx, r->leaf->module->parsed);
 
         LOG_LOCSET(&r->leaf->node, NULL, NULL, NULL);
         if (r->leaf->nodetype == LYS_LEAF) {
@@ -1519,7 +1495,7 @@
             ret = lys_compile_unres_llist_dflts(&cctx, r->llist, r->dflt, r->dflts, unres);
         }
         LOG_LOCBACK(1, 0, 0, 0);
-        LY_CHECK_RET(ret);
+        LY_CHECK_GOTO(ret, cleanup);
 
         lysc_unres_dflt_free(ctx, r);
         ly_set_rm_index(&ds_unres->dflts, i, NULL);
@@ -1538,17 +1514,18 @@
             LOG_LOCSET(node, NULL, NULL, NULL);
             LOGVAL(ctx, LYVE_REFERENCE, "Key \"%s\" is disabled.", node->name);
             LOG_LOCBACK(1, 0, 0, 0);
-            return LY_EVALID;
+            ret = LY_EVALID;
+            goto cleanup;
         }
+        LYSC_CTX_INIT_PMOD(cctx, node->module->parsed);
 
-        lysc_node_free(ctx, node, 1);
+        lysc_node_free(&cctx.free_ctx, node, 1);
     }
 
     /* also check if the leafref target has not been disabled */
     for (i = 0; i < ds_unres->leafrefs.count; ++i) {
         l = ds_unres->leafrefs.objs[i];
-        cctx.cur_mod = l->node->module;
-        cctx.pmod = l->node->module->parsed;
+        LYSC_CTX_INIT_PMOD(cctx, l->node->module->parsed);
 
         v = 0;
         while ((lref = lys_type_leafref_next(l->node, &v))) {
@@ -1563,12 +1540,15 @@
                 LOGVAL(ctx, LYVE_REFERENCE, "Target of leafref \"%s\" cannot be referenced because it is disabled.",
                         l->node->name);
                 LOG_LOCBACK(1, 0, 0, 0);
-                return LY_EVALID;
+                ret = LY_EVALID;
+                goto cleanup;
             }
         }
     }
 
-    return LY_SUCCESS;
+cleanup:
+    lysf_ctx_erase(&cctx.free_ctx);
+    return ret;
 }
 
 /**
@@ -1609,6 +1589,7 @@
 lys_compile_depset_r(struct ly_ctx *ctx, struct ly_set *dep_set, struct lys_glob_unres *unres)
 {
     LY_ERR ret = LY_SUCCESS;
+    struct lysf_ctx fctx = {.ctx = ctx};
     struct lys_module *mod;
     uint32_t i;
 
@@ -1621,7 +1602,7 @@
         assert(mod->implemented);
 
         /* free the compiled module, if any */
-        lysc_module_free(mod->compiled);
+        lysc_module_free(&fctx, mod->compiled);
         mod->compiled = NULL;
 
         /* (re)compile the module */
@@ -1647,6 +1628,7 @@
 
 cleanup:
     lys_compile_unres_depset_erase(ctx, unres);
+    lysf_ctx_erase(&fctx);
     return ret;
 }
 
@@ -1782,7 +1764,7 @@
 LY_ERR
 lys_compile(struct lys_module *mod, struct lys_depset_unres *unres)
 {
-    struct lysc_ctx ctx = {0};
+    struct lysc_ctx ctx;
     struct lysc_module *mod_c = NULL;
     struct lysp_module *sp;
     struct lysp_submodule *submod;
@@ -1796,12 +1778,7 @@
     assert(mod->implemented && mod->to_compile);
 
     sp = mod->parsed;
-
-    ctx.ctx = mod->ctx;
-    ctx.cur_mod = mod;
-    ctx.pmod = sp;
-    ctx.path_len = 1;
-    ctx.path[0] = '/';
+    LYSC_CTX_INIT_PMOD(ctx, sp);
     ctx.unres = unres;
 
     ++mod->ctx->change_count;
@@ -1898,7 +1875,7 @@
     LOG_LOCBACK(0, 0, 1, 0);
     lys_compile_unres_mod_erase(&ctx, ret);
     if (ret) {
-        lysc_module_free(mod_c);
+        lysc_module_free(&ctx.free_ctx, mod_c);
         mod->compiled = NULL;
     }
     return ret;
@@ -1908,7 +1885,7 @@
 lys_compile_identities(struct lys_module *mod)
 {
     LY_ERR rc = LY_SUCCESS;
-    struct lysc_ctx ctx = {0};
+    struct lysc_ctx ctx;
     struct lysp_submodule *submod;
     LY_ARRAY_COUNT_TYPE u;
 
@@ -1922,11 +1899,7 @@
     }
 
     /* prepare context */
-    ctx.ctx = mod->ctx;
-    ctx.cur_mod = mod;
-    ctx.pmod = mod->parsed;
-    ctx.path_len = 1;
-    ctx.path[0] = '/';
+    LYSC_CTX_INIT_PMOD(ctx, mod->parsed);
 
     if (mod->parsed->identities) {
         rc = lys_compile_identities_derived(&ctx, mod->parsed->identities, &mod->identities);
diff --git a/src/schema_compile.h b/src/schema_compile.h
index ef51185..c68fd43 100644
--- a/src/schema_compile.h
+++ b/src/schema_compile.h
@@ -4,7 +4,7 @@
  * @author Michal Vasko <mvasko@cesnet.cz>
  * @brief Header for schema compilation.
  *
- * Copyright (c) 2015 - 2021 CESNET, z.s.p.o.
+ * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
  *
  * This source code is licensed under BSD 3-Clause License (the "License").
  * You may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
 #include "set.h"
 #include "tree.h"
 #include "tree_schema.h"
+#include "tree_schema_free.h"
 
 struct lyxp_expr;
 
@@ -52,12 +53,41 @@
     struct lys_depset_unres *unres; /**< dependency set unres sets */
     uint32_t path_len;          /**< number of path bytes used */
     uint32_t compile_opts;      /**< various @ref scflags. */
+    struct lysf_ctx free_ctx;   /**< freeing context for errors/recompilation */
 
 #define LYSC_CTX_BUFSIZE 4078
     char path[LYSC_CTX_BUFSIZE];/**< Path identifying the schema node currently being processed */
 };
 
 /**
+ * @brief Initalize local compilation context using libyang context.
+ *
+ * @param[out] CCTX Compile context.
+ * @param[in] CTX libyang context.
+ */
+#define LYSC_CTX_INIT_CTX(CCTX, CTX) \
+    memset(&(CCTX), 0, sizeof (CCTX)); \
+    (CCTX).ctx = (CTX); \
+    (CCTX).path_len = 1; \
+    (CCTX).path[0] = '/'; \
+    (CCTX).free_ctx.ctx = (CTX)
+
+/**
+ * @brief Initalize local compilation context using a parsed module.
+ *
+ * @param[out] CCTX Compile context.
+ * @param[in] PMOD Parsed module.
+ */
+#define LYSC_CTX_INIT_PMOD(CCTX, PMOD) \
+    memset(&(CCTX), 0, sizeof (CCTX)); \
+    (CCTX).ctx = (PMOD)->mod->ctx; \
+    (CCTX).cur_mod = (PMOD)->mod; \
+    (CCTX).pmod = (PMOD); \
+    (CCTX).path_len = 1; \
+    (CCTX).path[0] = '/'; \
+    (CCTX).free_ctx.ctx = (PMOD)->mod->ctx
+
+/**
  * @brief Structure for unresolved items that may depend on any implemented module data in the dependency set
  * so their resolution can only be performed after the whole dep set compilation is done.
  */
diff --git a/src/schema_compile_amend.c b/src/schema_compile_amend.c
index 37fb351..bab8ba3 100644
--- a/src/schema_compile_amend.c
+++ b/src/schema_compile_amend.c
@@ -1099,7 +1099,7 @@
     LY_ARRAY_COUNT_TYPE u, v;
     struct lysp_qname **uniques, **dflts;
 
-#define DEV_DEL_ARRAY(DEV_ARRAY, ORIG_ARRAY, DEV_MEMBER, ORIG_MEMBER, FREE_FUNC, PROPERTY) \
+#define DEV_DEL_ARRAY(DEV_ARRAY, ORIG_ARRAY, DEV_MEMBER, ORIG_MEMBER, FREE_FUNC, FREE_CTX, PROPERTY) \
     LY_ARRAY_FOR(d->DEV_ARRAY, u) { \
         int found = 0; \
         LY_ARRAY_FOR(ORIG_ARRAY, v) { \
@@ -1116,7 +1116,7 @@
             goto cleanup; \
         } \
         LY_ARRAY_DECREMENT(ORIG_ARRAY); \
-        FREE_FUNC(ctx->ctx, &(ORIG_ARRAY)[v]); \
+        FREE_FUNC(FREE_CTX, &(ORIG_ARRAY)[v]); \
         if (v < LY_ARRAY_COUNT(ORIG_ARRAY)) { \
             memmove(&(ORIG_ARRAY)[v], &(ORIG_ARRAY)[v + 1], (LY_ARRAY_COUNT(ORIG_ARRAY) - v) * sizeof *(ORIG_ARRAY)); \
         } \
@@ -1161,7 +1161,7 @@
             AMEND_WRONG_NODETYPE("deviation", "delete", "must");
         }
 
-        DEV_DEL_ARRAY(musts, *musts, .arg.str, .arg.str, lysp_restr_free, "must");
+        DEV_DEL_ARRAY(musts, *musts, .arg.str, .arg.str, lysp_restr_free, &ctx->free_ctx, "must");
     }
 
     /* *unique-stmt */
@@ -1174,7 +1174,7 @@
         }
 
         uniques = &((struct lysp_node_list *)target)->uniques;
-        DEV_DEL_ARRAY(uniques, *uniques, .str, .str, lysp_qname_free, "unique");
+        DEV_DEL_ARRAY(uniques, *uniques, .str, .str, lysp_qname_free, ctx->ctx, "unique");
     }
 
     /* *default-stmt */
@@ -1189,7 +1189,7 @@
             break;
         case LYS_LEAFLIST:
             dflts = &((struct lysp_node_leaflist *)target)->dflts;
-            DEV_DEL_ARRAY(dflts, *dflts, .str, .str, lysp_qname_free, "default");
+            DEV_DEL_ARRAY(dflts, *dflts, .str, .str, lysp_qname_free, ctx->ctx, "default");
             break;
         case LYS_CHOICE:
             AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
@@ -1238,7 +1238,7 @@
             AMEND_WRONG_NODETYPE("deviation", "replace", "type");
         }
 
-        lysp_type_free(ctx->ctx, &((struct lysp_node_leaf *)target)->type);
+        lysp_type_free(&ctx->free_ctx, &((struct lysp_node_leaf *)target)->type);
         lysp_type_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->type, d->type);
     }
 
@@ -1551,7 +1551,7 @@
 }
 
 void
-lysp_dev_node_free(const struct ly_ctx *ctx, struct lysp_node *dev_pnode)
+lysp_dev_node_free(struct lysc_ctx *cctx, struct lysp_node *dev_pnode)
 {
     if (!dev_pnode) {
         return;
@@ -1591,15 +1591,15 @@
     case LYS_INPUT:
     case LYS_OUTPUT:
         ((struct lysp_node_action_inout *)dev_pnode)->child = NULL;
-        lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
+        lysp_node_free(&cctx->free_ctx, dev_pnode);
         free(dev_pnode);
         return;
     default:
-        LOGINT(ctx);
+        LOGINT(cctx->ctx);
         return;
     }
 
-    lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
+    lysp_node_free(&cctx->free_ctx, dev_pnode);
 }
 
 LY_ERR
@@ -1725,7 +1725,7 @@
     ctx->cur_mod = orig_mod;
     ctx->pmod = orig_pmod;
     if (ret) {
-        lysp_dev_node_free(ctx->ctx, *dev_pnode);
+        lysp_dev_node_free(ctx, *dev_pnode);
         *dev_pnode = NULL;
         *not_supported = 0;
     }
@@ -2212,7 +2212,7 @@
 {
     LY_ERR ret = LY_SUCCESS, r;
     LY_ARRAY_COUNT_TYPE u, v;
-    struct lysc_ctx ctx = {0};
+    struct lysc_ctx ctx;
     struct lysp_module *mod_p;
     struct lys_module *m;
     struct lysp_submodule *submod;
@@ -2222,13 +2222,7 @@
     struct ly_set mod_set = {0}, set = {0};
 
     mod_p = mod->parsed;
-
-    /* prepare context */
-    ctx.ctx = mod->ctx;
-    ctx.cur_mod = mod;
-    ctx.pmod = mod_p;
-    ctx.path_len = 1;
-    ctx.path[0] = '/';
+    LYSC_CTX_INIT_PMOD(ctx, mod_p);
 
     LY_LIST_FOR(mod_p->augments, aug) {
         /* get target module */
diff --git a/src/schema_compile_amend.h b/src/schema_compile_amend.h
index 9fe5c65..581dfae 100644
--- a/src/schema_compile_amend.h
+++ b/src/schema_compile_amend.h
@@ -115,7 +115,7 @@
  * @param[in] ctx libyang context.
  * @param[in] dev_pnode Parsed node to free.
  */
-void lysp_dev_node_free(const struct ly_ctx *ctx, struct lysp_node *dev_pnode);
+void lysp_dev_node_free(struct lysc_ctx *cctx, struct lysp_node *dev_pnode);
 
 /**
  * @brief Compile and apply any precompiled deviations and refines targeting a node.
diff --git a/src/schema_compile_node.c b/src/schema_compile_node.c
index 86621c9..881759e 100644
--- a/src/schema_compile_node.c
+++ b/src/schema_compile_node.c
@@ -1595,7 +1595,7 @@
             --additional;
 
             /* free the replaced union subtype */
-            lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
+            lysc_type_free(&ctx->free_ctx, (struct lysc_type *)un_aux);
             un_aux = NULL;
         } else {
             LY_ARRAY_INCREMENT(utypes);
@@ -1607,7 +1607,7 @@
 
 error:
     if (un_aux) {
-        lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
+        lysc_type_free(&ctx->free_ctx, (struct lysc_type *)un_aux);
     }
     *utypes_p = utypes;
     return ret;
@@ -2002,7 +2002,7 @@
         if (tctx->tpdf->type.compiled && (tctx->tpdf->type.compiled->refcount == 1)) {
             /* context recompilation - everything was freed previously (the only reference is from the parsed type itself)
              * and we need now recompile the type again in the updated context. */
-            lysc_type_free(ctx->ctx, tctx->tpdf->type.compiled);
+            lysc_type_free(&ctx->free_ctx, tctx->tpdf->type.compiled);
             ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = NULL;
         }
 
@@ -2627,13 +2627,14 @@
     goto cleanup;
 
 error:
-    lysc_node_free(ctx->ctx, node, 0);
+    lysc_node_free(&ctx->free_ctx, node, 0);
+
 cleanup:
     if (ret && dev_pnode) {
         LOGVAL(ctx->ctx, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
     }
     ctx->compile_opts = prev_opts;
-    lysp_dev_node_free(ctx->ctx, dev_pnode);
+    lysp_dev_node_free(ctx, dev_pnode);
     return ret;
 }
 
@@ -3340,13 +3341,12 @@
         }
 
         /* check status */
-        LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
-                key->flags, key->module, key->name));
+        LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, key->flags, key->module, key->name));
 
         /* ignore default values of the key */
         if (key->dflt) {
             key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
-            lysc_type_free(ctx->ctx, (struct lysc_type *)key->dflt->realtype);
+            lysc_type_free(&ctx->free_ctx, (struct lysc_type *)key->dflt->realtype);
             free(key->dflt);
             key->dflt = NULL;
         }
@@ -3540,7 +3540,7 @@
 revert_sh_case:
         /* free the parsed shorthand case and correct pointers back */
         cs_p->child = NULL;
-        lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
+        lysp_node_free(&ctx->free_ctx, (struct lysp_node *)cs_p);
         child_p->next = child_p_next;
     }
 
@@ -4080,7 +4080,7 @@
     ctx->path[1] = '\0';
 
     /* cleanup */
-    lysc_node_container_free(ctx->ctx, &fake_container);
+    lysc_node_container_free(&ctx->free_ctx, &fake_container);
 
     return ret;
 }
diff --git a/src/schema_features.c b/src/schema_features.c
index e770cd7..f70968f 100644
--- a/src/schema_features.c
+++ b/src/schema_features.c
@@ -506,6 +506,7 @@
     LY_ERR ret;
     LY_ARRAY_COUNT_TYPE u;
     struct lysc_iffeature iff;
+    struct lysf_ctx fctx = {.ctx = (struct ly_ctx *)ctx};
 
     /* enabled by default */
     *enabled = 1;
@@ -520,7 +521,7 @@
         LY_CHECK_RET(lys_compile_iffeature(ctx, &iffeatures[u], &iff));
 
         ret = lysc_iffeature_value(&iff);
-        lysc_iffeature_free((struct ly_ctx *)ctx, &iff);
+        lysc_iffeature_free(&fctx, &iff);
         if (ret == LY_ENOT) {
             *enabled = 0;
             break;
diff --git a/src/tree_schema.c b/src/tree_schema.c
index 4a247cb..000c115 100644
--- a/src/tree_schema.c
+++ b/src/tree_schema.c
@@ -43,6 +43,7 @@
 #include "set.h"
 #include "tree.h"
 #include "tree_edit.h"
+#include "tree_schema_free.h"
 #include "tree_schema_internal.h"
 #include "xpath.h"
 
@@ -1146,43 +1147,46 @@
 lys_unres_glob_revert(struct ly_ctx *ctx, struct lys_glob_unres *unres)
 {
     uint32_t i, j, idx, prev_lo;
+    struct lysf_ctx fctx = {.ctx = ctx};
     struct ly_set *dep_set;
-    struct lys_module *m;
     LY_ERR ret;
 
     for (i = 0; i < unres->implementing.count; ++i) {
-        m = unres->implementing.objs[i];
-        assert(m->implemented);
+        fctx.mod = unres->implementing.objs[i];
+        assert(fctx.mod->implemented);
 
         /* make the module correctly non-implemented again */
-        m->implemented = 0;
-        lys_precompile_augments_deviations_revert(ctx, m);
-        lysc_module_free(m->compiled);
-        m->compiled = NULL;
+        fctx.mod->implemented = 0;
+        lys_precompile_augments_deviations_revert(ctx, fctx.mod);
+        lysc_module_free(&fctx, fctx.mod->compiled);
+        fctx.mod->compiled = NULL;
 
         /* should not be made implemented */
-        m->to_compile = 0;
+        fctx.mod->to_compile = 0;
     }
 
     for (i = 0; i < unres->creating.count; ++i) {
-        m = unres->creating.objs[i];
+        fctx.mod = unres->creating.objs[i];
 
         /* remove the module from the context */
-        ly_set_rm(&ctx->list, m, NULL);
+        ly_set_rm(&ctx->list, fctx.mod, NULL);
 
         /* remove it also from dep sets */
         for (j = 0; j < unres->dep_sets.count; ++j) {
             dep_set = unres->dep_sets.objs[j];
-            if (ly_set_contains(dep_set, m, &idx)) {
+            if (ly_set_contains(dep_set, fctx.mod, &idx)) {
                 ly_set_rm_index(dep_set, idx, NULL);
                 break;
             }
         }
 
         /* free the module */
-        lys_module_free(m, 1);
+        lys_module_free(&fctx, fctx.mod, 1);
     }
 
+    /* remove the extensions as well */
+    lysf_ctx_erase(&fctx);
+
     if (unres->implementing.count) {
         /* recompile previous context because some implemented modules are no longer implemented,
          * we can reuse the current to_compile flags */
@@ -1290,6 +1294,7 @@
     struct lys_yang_parser_ctx *yangctx = NULL;
     struct lys_yin_parser_ctx *yinctx = NULL;
     struct lys_parser_ctx *pctx;
+    struct lysf_ctx fctx = {.ctx = ctx};
 
     LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
 
@@ -1361,7 +1366,7 @@
     } else {
         LOGERR(ctx, ret, "Parsing submodule \"%s\" failed.", submod->name);
     }
-    lysp_module_free((struct lysp_module *)submod);
+    lysp_module_free(&fctx, (struct lysp_module *)submod);
     if (format == LYS_IN_YANG) {
         yang_parser_ctx_free(yangctx);
     } else {
@@ -1593,6 +1598,7 @@
     struct lys_yang_parser_ctx *yangctx = NULL;
     struct lys_yin_parser_ctx *yinctx = NULL;
     struct lys_parser_ctx *pctx = NULL;
+    struct lysf_ctx fctx = {.ctx = ctx};
     char *filename, *rev, *dot;
     size_t len;
     ly_bool module_created = 0;
@@ -1762,7 +1768,10 @@
         }
     }
     if (!module_created) {
-        lys_module_free(mod, 0);
+        fctx.mod = mod;
+        lys_module_free(&fctx, mod, 0);
+        lysf_ctx_erase(&fctx);
+
         mod = mod_dup;
     }
 
diff --git a/src/tree_schema.h b/src/tree_schema.h
index 8dfd965..32044a8 100644
--- a/src/tree_schema.h
+++ b/src/tree_schema.h
@@ -1429,7 +1429,7 @@
     struct lysc_ext_instance *exts;  /**< list of the extension instances ([sized array](@ref sizedarrays)) */
     struct lyplg_ext *plugin;        /**< Plugin implementing the specific extension */
     struct lys_module *module;       /**< module structure */
-    uint32_t refcount;               /**< reference counter since extension definition is shared among all its instances */
+    uint32_t refcount;               /**< unused, always 1 */
     uint16_t flags;                  /**< LYS_STATUS_* value (@ref snodeflags) */
 };
 
diff --git a/src/tree_schema_free.c b/src/tree_schema_free.c
index 409a56a..71536d3 100644
--- a/src/tree_schema_free.c
+++ b/src/tree_schema_free.c
@@ -1,9 +1,10 @@
 /**
  * @file tree_schema_free.c
  * @author Radek Krejci <rkrejci@cesnet.cz>
+ * @author Michal Vasko <mvasko@cesnet.cz>
  * @brief Freeing functions for schema tree structures.
  *
- * Copyright (c) 2019 CESNET, z.s.p.o.
+ * Copyright (c) 2019 - 2022 CESNET, z.s.p.o.
  *
  * This source code is licensed under BSD 3-Clause License (the "License").
  * You may not use this file except in compliance with the License.
@@ -12,6 +13,8 @@
  *     https://opensource.org/licenses/BSD-3-Clause
  */
 
+#include "tree_schema_free.h"
+
 #include <assert.h>
 #include <stdlib.h>
 
@@ -30,9 +33,23 @@
 #include "xml.h"
 #include "xpath.h"
 
-static void lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp);
-static void lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node);
+static void lysc_extension_free(struct lysf_ctx *ctx, struct lysc_ext **ext);
+static void lysc_node_free_(struct lysf_ctx *ctx, struct lysc_node *node);
 
+void
+lysp_qname_free(struct ly_ctx *ctx, struct lysp_qname *qname)
+{
+    if (qname) {
+        lydict_remove(ctx, qname->str);
+    }
+}
+
+/**
+ * @brief Free the parsed generic statement structure.
+ *
+ * @param[in] ctx libyang context.
+ * @param[in] grp Parsed schema statement structure to free. Note that the structure itself is not freed.
+ */
 static void
 lysp_stmt_free(struct ly_ctx *ctx, struct lysp_stmt *stmt)
 {
@@ -49,32 +66,38 @@
     free(stmt);
 }
 
-static void
-lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext)
+void
+lysp_ext_instance_free(struct lysf_ctx *ctx, struct lysp_ext_instance *ext)
 {
     struct lysp_stmt *stmt, *next;
     struct lysp_node *node, *next_node;
 
-    lydict_remove(ctx, ext->name);
-    lydict_remove(ctx, ext->argument);
+    lydict_remove(ctx->ctx, ext->name);
+    lydict_remove(ctx->ctx, ext->argument);
     ly_free_prefix_data(ext->format, ext->prefix_data);
     LY_LIST_FOR_SAFE(ext->parsed, next_node, node) {
         lysp_node_free(ctx, node);
     }
 
     LY_LIST_FOR_SAFE(ext->child, next, stmt) {
-        lysp_stmt_free(ctx, stmt);
+        lysp_stmt_free(ctx->ctx, stmt);
     }
 }
 
+/**
+ * @brief Free the parsed import structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in] import Parsed schema import structure to free. Note that the structure itself is not freed.
+ */
 static void
-lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import)
+lysp_import_free(struct lysf_ctx *ctx, struct lysp_import *import)
 {
     /* imported module is freed directly from the context's list */
-    lydict_remove(ctx, import->name);
-    lydict_remove(ctx, import->prefix);
-    lydict_remove(ctx, import->dsc);
-    lydict_remove(ctx, import->ref);
+    lydict_remove(ctx->ctx, import->name);
+    lydict_remove(ctx->ctx, import->prefix);
+    lydict_remove(ctx->ctx, import->dsc);
+    lydict_remove(ctx->ctx, import->ref);
     FREE_ARRAY(ctx, import->exts, lysp_ext_instance_free);
 }
 
@@ -91,105 +114,147 @@
  * is supposed to be freed.
  */
 static void
-lysp_include_free_(struct ly_ctx *ctx, struct lysp_include *include, ly_bool main_module)
+lysp_include_free_(struct lysf_ctx *ctx, struct lysp_include *include, ly_bool main_module)
 {
     if (main_module && include->submodule) {
-        lysp_module_free((struct lysp_module *)include->submodule);
+        lysp_module_free(ctx, (struct lysp_module *)include->submodule);
     }
-    lydict_remove(ctx, include->name);
-    lydict_remove(ctx, include->dsc);
-    lydict_remove(ctx, include->ref);
+    lydict_remove(ctx->ctx, include->name);
+    lydict_remove(ctx->ctx, include->dsc);
+    lydict_remove(ctx->ctx, include->ref);
     FREE_ARRAY(ctx, include->exts, lysp_ext_instance_free);
 }
 
+/**
+ * @brief Free the parsed include structure of a submodule.
+ *
+ * @param[in] ctx Free context.
+ * @param[in] include Parsed schema include structure to free. Note that the structure itself is not freed.
+ */
 static void
-lysp_include_free_submodule(struct ly_ctx *ctx, struct lysp_include *include)
+lysp_include_free_submodule(struct lysf_ctx *ctx, struct lysp_include *include)
 {
     lysp_include_free_(ctx, include, 0);
 }
 
+/**
+ * @brief Free the parsed include structure of a module.
+ *
+ * @param[in] ctx Free context.
+ * @param[in] include Parsed schema include structure to free. Note that the structure itself is not freed.
+ */
 static void
-lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include)
+lysp_include_free(struct lysf_ctx *ctx, struct lysp_include *include)
 {
     lysp_include_free_(ctx, include, 1);
 }
 
+/**
+ * @brief Free the parsed revision structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in] rev Parsed schema revision structure to free. Note that the structure itself is not freed.
+ */
 static void
-lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev)
+lysp_revision_free(struct lysf_ctx *ctx, struct lysp_revision *rev)
 {
-    lydict_remove(ctx, rev->dsc);
-    lydict_remove(ctx, rev->ref);
+    lydict_remove(ctx->ctx, rev->dsc);
+    lydict_remove(ctx->ctx, rev->ref);
     FREE_ARRAY(ctx, rev->exts, lysp_ext_instance_free);
 }
 
+/**
+ * @brief Free the parsed ext structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in] ext Parsed schema ext structure to free. Note that the structure itself is not freed.
+ */
 static void
-lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext)
+lysp_ext_free(struct lysf_ctx *ctx, struct lysp_ext *ext)
 {
-    lydict_remove(ctx, ext->name);
-    lydict_remove(ctx, ext->argname);
-    lydict_remove(ctx, ext->dsc);
-    lydict_remove(ctx, ext->ref);
+    lydict_remove(ctx->ctx, ext->name);
+    lydict_remove(ctx->ctx, ext->argname);
+    lydict_remove(ctx->ctx, ext->dsc);
+    lydict_remove(ctx->ctx, ext->ref);
     FREE_ARRAY(ctx, ext->exts, lysp_ext_instance_free);
     if (ext->compiled) {
         lysc_extension_free(ctx, &ext->compiled);
     }
 }
 
+/**
+ * @brief Free the parsed feature structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in] feat Parsed schema feature structure to free. Note that the structure itself is not freed.
+ */
 static void
-lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat)
+lysp_feature_free(struct lysf_ctx *ctx, struct lysp_feature *feat)
 {
-    lydict_remove(ctx, feat->name);
-    FREE_ARRAY(ctx, feat->iffeatures, lysp_qname_free);
+    lydict_remove(ctx->ctx, feat->name);
+    FREE_ARRAY(ctx->ctx, feat->iffeatures, lysp_qname_free);
     FREE_ARRAY(ctx, feat->iffeatures_c, lysc_iffeature_free);
     LY_ARRAY_FREE(feat->depfeatures);
-    lydict_remove(ctx, feat->dsc);
-    lydict_remove(ctx, feat->ref);
+    lydict_remove(ctx->ctx, feat->dsc);
+    lydict_remove(ctx->ctx, feat->ref);
     FREE_ARRAY(ctx, feat->exts, lysp_ext_instance_free);
 }
 
+/**
+ * @brief Free the parsed identity structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in] ident Parsed schema identity structure to free. Note that the structure itself is not freed.
+ */
 static void
-lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident)
+lysp_ident_free(struct lysf_ctx *ctx, struct lysp_ident *ident)
 {
-    lydict_remove(ctx, ident->name);
-    FREE_ARRAY(ctx, ident->iffeatures, lysp_qname_free);
-    FREE_STRINGS(ctx, ident->bases);
-    lydict_remove(ctx, ident->dsc);
-    lydict_remove(ctx, ident->ref);
+    lydict_remove(ctx->ctx, ident->name);
+    FREE_ARRAY(ctx->ctx, ident->iffeatures, lysp_qname_free);
+    FREE_STRINGS(ctx->ctx, ident->bases);
+    lydict_remove(ctx->ctx, ident->dsc);
+    lydict_remove(ctx->ctx, ident->ref);
     FREE_ARRAY(ctx, ident->exts, lysp_ext_instance_free);
 }
 
 void
-lysp_restr_free(struct ly_ctx *ctx, struct lysp_restr *restr)
+lysp_restr_free(struct lysf_ctx *ctx, struct lysp_restr *restr)
 {
-    lydict_remove(ctx, restr->arg.str);
-    lydict_remove(ctx, restr->emsg);
-    lydict_remove(ctx, restr->eapptag);
-    lydict_remove(ctx, restr->dsc);
-    lydict_remove(ctx, restr->ref);
+    lydict_remove(ctx->ctx, restr->arg.str);
+    lydict_remove(ctx->ctx, restr->emsg);
+    lydict_remove(ctx->ctx, restr->eapptag);
+    lydict_remove(ctx->ctx, restr->dsc);
+    lydict_remove(ctx->ctx, restr->ref);
     FREE_ARRAY(ctx, restr->exts, lysp_ext_instance_free);
 }
 
+/**
+ * @brief Free the parsed type enum item.
+ *
+ * @param[in] ctx Free context.
+ * @param[in] item Parsed schema type enum item to free. Note that the structure itself is not freed.
+ */
 static void
-lysp_type_enum_free(struct ly_ctx *ctx, struct lysp_type_enum *item)
+lysp_type_enum_free(struct lysf_ctx *ctx, struct lysp_type_enum *item)
 {
-    lydict_remove(ctx, item->name);
-    lydict_remove(ctx, item->dsc);
-    lydict_remove(ctx, item->ref);
-    FREE_ARRAY(ctx, item->iffeatures, lysp_qname_free);
+    lydict_remove(ctx->ctx, item->name);
+    lydict_remove(ctx->ctx, item->dsc);
+    lydict_remove(ctx->ctx, item->ref);
+    FREE_ARRAY(ctx->ctx, item->iffeatures, lysp_qname_free);
     FREE_ARRAY(ctx, item->exts, lysp_ext_instance_free);
 }
 
 void
-lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type)
+lysp_type_free(struct lysf_ctx *ctx, struct lysp_type *type)
 {
-    lydict_remove(ctx, type->name);
+    lydict_remove(ctx->ctx, type->name);
     FREE_MEMBER(ctx, type->range, lysp_restr_free);
     FREE_MEMBER(ctx, type->length, lysp_restr_free);
     FREE_ARRAY(ctx, type->patterns, lysp_restr_free);
     FREE_ARRAY(ctx, type->enums, lysp_type_enum_free);
     FREE_ARRAY(ctx, type->bits, lysp_type_enum_free);
-    lyxp_expr_free(ctx, type->path);
-    FREE_STRINGS(ctx, type->bases);
+    lyxp_expr_free(ctx->ctx, type->path);
+    FREE_STRINGS(ctx->ctx, type->bases);
     FREE_ARRAY(ctx, type->types, lysp_type_free);
     FREE_ARRAY(ctx, type->exts, lysp_ext_instance_free);
     if (type->compiled) {
@@ -197,22 +262,34 @@
     }
 }
 
+/**
+ * @brief Free the parsed typedef structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in] tpdf Parsed schema typedef structure to free. Note that the structure itself is not freed.
+ */
 static void
-lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf)
+lysp_tpdf_free(struct lysf_ctx *ctx, struct lysp_tpdf *tpdf)
 {
-    lydict_remove(ctx, tpdf->name);
-    lydict_remove(ctx, tpdf->units);
-    lydict_remove(ctx, tpdf->dflt.str);
-    lydict_remove(ctx, tpdf->dsc);
-    lydict_remove(ctx, tpdf->ref);
+    lydict_remove(ctx->ctx, tpdf->name);
+    lydict_remove(ctx->ctx, tpdf->units);
+    lydict_remove(ctx->ctx, tpdf->dflt.str);
+    lydict_remove(ctx->ctx, tpdf->dsc);
+    lydict_remove(ctx->ctx, tpdf->ref);
     FREE_ARRAY(ctx, tpdf->exts, lysp_ext_instance_free);
 
     lysp_type_free(ctx, &tpdf->type);
 
 }
 
+/**
+ * @brief Free the parsed grouping structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in] grp Parsed schema grouping structure to free. Note that the structure itself is not freed.
+ */
 static void
-lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp)
+lysp_grp_free(struct lysf_ctx *ctx, struct lysp_node_grp *grp)
 {
     struct lysp_node *node, *next;
 
@@ -232,40 +309,38 @@
 }
 
 void
-lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when)
+lysp_when_free(struct lysf_ctx *ctx, struct lysp_when *when)
 {
-    lydict_remove(ctx, when->cond);
-    lydict_remove(ctx, when->dsc);
-    lydict_remove(ctx, when->ref);
+    lydict_remove(ctx->ctx, when->cond);
+    lydict_remove(ctx->ctx, when->dsc);
+    lydict_remove(ctx->ctx, when->ref);
     FREE_ARRAY(ctx, when->exts, lysp_ext_instance_free);
 }
 
+/**
+ * @brief Free the parsed augment structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in] aug Parsed schema augment structure to free. Note that the structure itself is not freed.
+ */
 static void
-lysp_augment_free(struct ly_ctx *ctx, struct lysp_node_augment *augment)
+lysp_augment_free(struct lysf_ctx *ctx, struct lysp_node_augment *aug)
 {
     struct lysp_node *node, *next;
 
-    LY_LIST_FOR_SAFE(augment->child, next, node) {
+    LY_LIST_FOR_SAFE(aug->child, next, node) {
         lysp_node_free(ctx, node);
     }
-    LY_LIST_FOR_SAFE((struct lysp_node *)augment->actions, next, node) {
+    LY_LIST_FOR_SAFE((struct lysp_node *)aug->actions, next, node) {
         lysp_node_free(ctx, node);
     }
-    LY_LIST_FOR_SAFE((struct lysp_node *)augment->notifs, next, node) {
+    LY_LIST_FOR_SAFE((struct lysp_node *)aug->notifs, next, node) {
         lysp_node_free(ctx, node);
     }
 }
 
 void
-lysp_qname_free(struct ly_ctx *ctx, struct lysp_qname *qname)
-{
-    if (qname) {
-        lydict_remove(ctx, qname->str);
-    }
-}
-
-void
-lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d)
+lysp_deviate_free(struct lysf_ctx *ctx, struct lysp_deviate *d)
 {
     struct lysp_deviate_add *add = (struct lysp_deviate_add *)d;
     struct lysp_deviate_rpl *rpl = (struct lysp_deviate_rpl *)d;
@@ -281,30 +356,30 @@
         break;
     case LYS_DEV_ADD:
     case LYS_DEV_DELETE: /* compatible for dynamically allocated data */
-        lydict_remove(ctx, add->units);
+        lydict_remove(ctx->ctx, add->units);
         FREE_ARRAY(ctx, add->musts, lysp_restr_free);
-        FREE_ARRAY(ctx, add->uniques, lysp_qname_free);
-        FREE_ARRAY(ctx, add->dflts, lysp_qname_free);
+        FREE_ARRAY(ctx->ctx, add->uniques, lysp_qname_free);
+        FREE_ARRAY(ctx->ctx, add->dflts, lysp_qname_free);
         break;
     case LYS_DEV_REPLACE:
         FREE_MEMBER(ctx, rpl->type, lysp_type_free);
-        lydict_remove(ctx, rpl->units);
-        lysp_qname_free(ctx, &rpl->dflt);
+        lydict_remove(ctx->ctx, rpl->units);
+        lysp_qname_free(ctx->ctx, &rpl->dflt);
         break;
     default:
-        LOGINT(ctx);
+        LOGINT(ctx->ctx);
         break;
     }
 }
 
 void
-lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev)
+lysp_deviation_free(struct lysf_ctx *ctx, struct lysp_deviation *dev)
 {
     struct lysp_deviate *next, *iter;
 
-    lydict_remove(ctx, dev->nodeid);
-    lydict_remove(ctx, dev->dsc);
-    lydict_remove(ctx, dev->ref);
+    lydict_remove(ctx->ctx, dev->nodeid);
+    lydict_remove(ctx->ctx, dev->dsc);
+    lydict_remove(ctx->ctx, dev->ref);
     LY_LIST_FOR_SAFE(dev->deviates, next, iter) {
         lysp_deviate_free(ctx, iter);
         free(iter);
@@ -312,21 +387,27 @@
     FREE_ARRAY(ctx, dev->exts, lysp_ext_instance_free);
 }
 
+/**
+ * @brief Free the parsed refine structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in] ref Parsed schema refine structure to free. Note that the structure itself is not freed.
+ */
 static void
-lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref)
+lysp_refine_free(struct lysf_ctx *ctx, struct lysp_refine *ref)
 {
-    lydict_remove(ctx, ref->nodeid);
-    lydict_remove(ctx, ref->dsc);
-    lydict_remove(ctx, ref->ref);
-    FREE_ARRAY(ctx, ref->iffeatures, lysp_qname_free);
+    lydict_remove(ctx->ctx, ref->nodeid);
+    lydict_remove(ctx->ctx, ref->dsc);
+    lydict_remove(ctx->ctx, ref->ref);
+    FREE_ARRAY(ctx->ctx, ref->iffeatures, lysp_qname_free);
     FREE_ARRAY(ctx, ref->musts, lysp_restr_free);
-    lydict_remove(ctx, ref->presence);
-    FREE_ARRAY(ctx, ref->dflts, lysp_qname_free);
+    lydict_remove(ctx->ctx, ref->presence);
+    FREE_ARRAY(ctx->ctx, ref->dflts, lysp_qname_free);
     FREE_ARRAY(ctx, ref->exts, lysp_ext_instance_free);
 }
 
 void
-lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node)
+lysp_node_free(struct lysf_ctx *ctx, struct lysp_node *node)
 {
     struct lysp_node *child, *next;
     struct lysp_node_container *cont;
@@ -342,10 +423,10 @@
     struct lysp_restr *musts = lysp_node_musts(node);
     struct lysp_when *when = lysp_node_when(node);
 
-    lydict_remove(ctx, node->name);
-    lydict_remove(ctx, node->dsc);
-    lydict_remove(ctx, node->ref);
-    FREE_ARRAY(ctx, node->iffeatures, lysp_qname_free);
+    lydict_remove(ctx->ctx, node->name);
+    lydict_remove(ctx->ctx, node->dsc);
+    lydict_remove(ctx->ctx, node->ref);
+    FREE_ARRAY(ctx->ctx, node->iffeatures, lysp_qname_free);
     FREE_ARRAY(ctx, node->exts, lysp_ext_instance_free);
 
     FREE_MEMBER(ctx, when, lysp_when_free);
@@ -355,7 +436,7 @@
     case LYS_CONTAINER:
         cont = (struct lysp_node_container *)node;
 
-        lydict_remove(ctx, cont->presence);
+        lydict_remove(ctx->ctx, cont->presence);
         FREE_ARRAY(ctx, cont->typedefs, lysp_tpdf_free);
         if (cont->groupings) {
             LY_LIST_FOR_SAFE(&cont->groupings->node, next, child) {
@@ -380,20 +461,20 @@
         leaf = (struct lysp_node_leaf *)node;
 
         lysp_type_free(ctx, &leaf->type);
-        lydict_remove(ctx, leaf->units);
-        lydict_remove(ctx, leaf->dflt.str);
+        lydict_remove(ctx->ctx, leaf->units);
+        lydict_remove(ctx->ctx, leaf->dflt.str);
         break;
     case LYS_LEAFLIST:
         llist = (struct lysp_node_leaflist *)node;
 
         lysp_type_free(ctx, &llist->type);
-        lydict_remove(ctx, llist->units);
-        FREE_ARRAY(ctx, llist->dflts, lysp_qname_free);
+        lydict_remove(ctx->ctx, llist->units);
+        FREE_ARRAY(ctx->ctx, llist->dflts, lysp_qname_free);
         break;
     case LYS_LIST:
         list = (struct lysp_node_list *)node;
 
-        lydict_remove(ctx, list->key);
+        lydict_remove(ctx->ctx, list->key);
         FREE_ARRAY(ctx, list->typedefs, lysp_tpdf_free);
         if (list->groupings) {
             LY_LIST_FOR_SAFE(&list->groupings->node, next, child) {
@@ -413,7 +494,7 @@
                 lysp_node_free(ctx, child);
             }
         }
-        FREE_ARRAY(ctx, list->uniques, lysp_qname_free);
+        FREE_ARRAY(ctx->ctx, list->uniques, lysp_qname_free);
         break;
     case LYS_CHOICE:
         choice = (struct lysp_node_choice *)node;
@@ -421,7 +502,7 @@
         LY_LIST_FOR_SAFE(choice->child, next, child) {
             lysp_node_free(ctx, child);
         }
-        lydict_remove(ctx, choice->dflt.str);
+        lydict_remove(ctx->ctx, choice->dflt.str);
         break;
     case LYS_CASE:
         cas = (struct lysp_node_case *)node;
@@ -496,22 +577,20 @@
         lysp_augment_free(ctx, ((struct lysp_node_augment *)node));
         break;
     default:
-        LOGINT(ctx);
+        LOGINT(ctx->ctx);
     }
 
     free(node);
 }
 
 void
-lysp_module_free(struct lysp_module *module)
+lysp_module_free(struct lysf_ctx *ctx, struct lysp_module *module)
 {
-    struct ly_ctx *ctx;
     struct lysp_node *node, *next;
 
     if (!module) {
         return;
     }
-    ctx = module->mod->ctx;
 
     FREE_ARRAY(ctx, module->imports, lysp_import_free);
     FREE_ARRAY(ctx, module->includes, module->is_submod ? lysp_include_free_submodule : lysp_include_free);
@@ -542,77 +621,102 @@
     if (module->is_submod) {
         struct lysp_submodule *submod = (struct lysp_submodule *)module;
 
-        lydict_remove(ctx, submod->name);
-        lydict_remove(ctx, submod->filepath);
-        lydict_remove(ctx, submod->prefix);
-        lydict_remove(ctx, submod->org);
-        lydict_remove(ctx, submod->contact);
-        lydict_remove(ctx, submod->dsc);
-        lydict_remove(ctx, submod->ref);
+        lydict_remove(ctx->ctx, submod->name);
+        lydict_remove(ctx->ctx, submod->filepath);
+        lydict_remove(ctx->ctx, submod->prefix);
+        lydict_remove(ctx->ctx, submod->org);
+        lydict_remove(ctx->ctx, submod->contact);
+        lydict_remove(ctx->ctx, submod->dsc);
+        lydict_remove(ctx->ctx, submod->ref);
     }
 
     free(module);
 }
 
-void
-lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext)
+/**
+ * @brief Free the compiled extension definition and NULL the provided pointer.
+ *
+ * @param[in] ctx Free context.
+ * @param[in,out] ext Compiled extension definition to be freed.
+ */
+static void
+lysc_extension_free(struct lysf_ctx *ctx, struct lysc_ext **ext)
 {
-    if (--(*ext)->refcount) {
+    if (ly_set_contains(&ctx->ext_set, *ext, NULL)) {
+        /* already freed and only referenced again in this module */
         return;
     }
-    lydict_remove(ctx, (*ext)->name);
-    lydict_remove(ctx, (*ext)->argname);
+
+    /* remember this extension to be freed */
+    ly_set_add(&ctx->ext_set, *ext, 0, NULL);
+
+    /* recursive exts free */
     FREE_ARRAY(ctx, (*ext)->exts, lysc_ext_instance_free);
-    free(*ext);
+
     *ext = NULL;
 }
 
 void
-lysc_ext_instance_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
+lysc_ext_instance_free(struct lysf_ctx *ctx, struct lysc_ext_instance *ext)
 {
     if (ext->def && ext->def->plugin && ext->def->plugin->free) {
-        ext->def->plugin->free(ctx, ext);
+        ext->def->plugin->free(ctx->ctx, ext);
     }
-    if (ext->def) {
-        lysc_extension_free(ctx, &ext->def);
-    }
-    lydict_remove(ctx, ext->argument);
+    lydict_remove(ctx->ctx, ext->argument);
     FREE_ARRAY(ctx, ext->exts, lysc_ext_instance_free);
 }
 
 void
-lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff)
+lysc_iffeature_free(struct lysf_ctx *UNUSED(ctx), struct lysc_iffeature *iff)
 {
     LY_ARRAY_FREE(iff->features);
     free(iff->expr);
 }
 
+/**
+ * @brief Free the compiled when structure (decrease refcount) and NULL the provided pointer.
+ *
+ * @param[in] ctx Free context.
+ * @param[in] grp Parsed schema grouping structure to free. Note that the structure itself is not freed.
+ */
 static void
-lysc_when_free(struct ly_ctx *ctx, struct lysc_when **w)
+lysc_when_free(struct lysf_ctx *ctx, struct lysc_when **w)
 {
     if (--(*w)->refcount) {
         return;
     }
-    lyxp_expr_free(ctx, (*w)->cond);
+    lyxp_expr_free(ctx->ctx, (*w)->cond);
     ly_free_prefix_data(LY_VALUE_SCHEMA_RESOLVED, (*w)->prefixes);
-    lydict_remove(ctx, (*w)->dsc);
-    lydict_remove(ctx, (*w)->ref);
+    lydict_remove(ctx->ctx, (*w)->dsc);
+    lydict_remove(ctx->ctx, (*w)->ref);
     FREE_ARRAY(ctx, (*w)->exts, lysc_ext_instance_free);
     free(*w);
 }
 
+/**
+ * @brief Free the compiled must structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in,out] must Compiled must structure to be freed.
+ * Since the structure is typically part of the sized array, the structure itself is not freed.
+ */
 static void
-lysc_must_free(struct ly_ctx *ctx, struct lysc_must *must)
+lysc_must_free(struct lysf_ctx *ctx, struct lysc_must *must)
 {
-    lyxp_expr_free(ctx, must->cond);
+    lyxp_expr_free(ctx->ctx, must->cond);
     ly_free_prefix_data(LY_VALUE_SCHEMA_RESOLVED, must->prefixes);
-    lydict_remove(ctx, must->emsg);
-    lydict_remove(ctx, must->eapptag);
-    lydict_remove(ctx, must->dsc);
-    lydict_remove(ctx, must->ref);
+    lydict_remove(ctx->ctx, must->emsg);
+    lydict_remove(ctx->ctx, must->eapptag);
+    lydict_remove(ctx->ctx, must->dsc);
+    lydict_remove(ctx->ctx, must->ref);
     FREE_ARRAY(ctx, must->exts, lysc_ext_instance_free);
 }
 
+/**
+ * @brief Unlink the identity from all derived identity arrays.
+ *
+ * @param[in] ident Identity to unlink.
+ */
 static void
 lysc_ident_derived_unlink(const struct lysc_ident *ident)
 {
@@ -696,60 +800,81 @@
     }
 }
 
+/**
+ * @brief Free the compiled identity structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in,out] ident Compiled identity structure to be freed.
+ * Since the structure is typically part of the sized array, the structure itself is not freed.
+ */
 static void
-lysc_ident_free(struct ly_ctx *ctx, struct lysc_ident *ident)
+lysc_ident_free(struct lysf_ctx *ctx, struct lysc_ident *ident)
 {
-    lydict_remove(ctx, ident->name);
-    lydict_remove(ctx, ident->dsc);
-    lydict_remove(ctx, ident->ref);
+    lydict_remove(ctx->ctx, ident->name);
+    lydict_remove(ctx->ctx, ident->dsc);
+    lydict_remove(ctx->ctx, ident->ref);
     LY_ARRAY_FREE(ident->derived);
     FREE_ARRAY(ctx, ident->exts, lysc_ext_instance_free);
 }
 
+/**
+ * @brief Free the compiled range structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in,out] range Compiled range structure to be freed.
+ * Since the structure is typically part of the sized array, the structure itself is not freed.
+ */
 static void
-lysc_range_free(struct ly_ctx *ctx, struct lysc_range *range)
+lysc_range_free(struct lysf_ctx *ctx, struct lysc_range *range)
 {
     LY_ARRAY_FREE(range->parts);
-    lydict_remove(ctx, range->eapptag);
-    lydict_remove(ctx, range->emsg);
-    lydict_remove(ctx, range->dsc);
-    lydict_remove(ctx, range->ref);
+    lydict_remove(ctx->ctx, range->eapptag);
+    lydict_remove(ctx->ctx, range->emsg);
+    lydict_remove(ctx->ctx, range->dsc);
+    lydict_remove(ctx->ctx, range->ref);
     FREE_ARRAY(ctx, range->exts, lysc_ext_instance_free);
 }
 
 void
-lysc_pattern_free(struct ly_ctx *ctx, struct lysc_pattern **pattern)
+lysc_pattern_free(struct lysf_ctx *ctx, struct lysc_pattern **pattern)
 {
     if (--(*pattern)->refcount) {
         return;
     }
     pcre2_code_free((*pattern)->code);
-    lydict_remove(ctx, (*pattern)->expr);
-    lydict_remove(ctx, (*pattern)->eapptag);
-    lydict_remove(ctx, (*pattern)->emsg);
-    lydict_remove(ctx, (*pattern)->dsc);
-    lydict_remove(ctx, (*pattern)->ref);
+    lydict_remove(ctx->ctx, (*pattern)->expr);
+    lydict_remove(ctx->ctx, (*pattern)->eapptag);
+    lydict_remove(ctx->ctx, (*pattern)->emsg);
+    lydict_remove(ctx->ctx, (*pattern)->dsc);
+    lydict_remove(ctx->ctx, (*pattern)->ref);
     FREE_ARRAY(ctx, (*pattern)->exts, lysc_ext_instance_free);
     free(*pattern);
 }
 
 void
-lysc_enum_item_free(struct ly_ctx *ctx, struct lysc_type_bitenum_item *item)
+lysc_enum_item_free(struct lysf_ctx *ctx, struct lysc_type_bitenum_item *item)
 {
-    lydict_remove(ctx, item->name);
-    lydict_remove(ctx, item->dsc);
-    lydict_remove(ctx, item->ref);
+    lydict_remove(ctx->ctx, item->name);
+    lydict_remove(ctx->ctx, item->dsc);
+    lydict_remove(ctx->ctx, item->ref);
     FREE_ARRAY(ctx, item->exts, lysc_ext_instance_free);
 }
 
+/**
+ * @brief Free the compiled type structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in,out] type Pointer to compiled type structure to be freed.
+ * Since the structure is typically part of the sized array, the structure itself is not freed.
+ */
 static void
-lysc_type2_free(struct ly_ctx *ctx, struct lysc_type **type)
+lysc_type2_free(struct lysf_ctx *ctx, struct lysc_type **type)
 {
     lysc_type_free(ctx, *type);
 }
 
 void
-lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type)
+lysc_type_free(struct lysf_ctx *ctx, struct lysc_type *type)
 {
     if (!type || (LY_ATOMIC_DEC_BARRIER(type->refcount) > 1)) {
         return;
@@ -789,7 +914,7 @@
         FREE_ARRAY(ctx, ((struct lysc_type_union *)type)->types, lysc_type2_free);
         break;
     case LY_TYPE_LEAFREF:
-        lyxp_expr_free(ctx, ((struct lysc_type_leafref *)type)->path);
+        lyxp_expr_free(ctx->ctx, ((struct lysc_type_leafref *)type)->path);
         ly_free_prefix_data(LY_VALUE_SCHEMA_RESOLVED, ((struct lysc_type_leafref *)type)->prefixes);
         lysc_type_free(ctx, ((struct lysc_type_leafref *)type)->realtype);
         break;
@@ -805,8 +930,15 @@
     free(type);
 }
 
+/**
+ * @brief Free the compiled input/output structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in,out] inout Compiled inout structure to be freed.
+ * Since the structure is part of the RPC/action structure, it is not freed itself.
+ */
 static void
-lysc_node_action_inout_free(struct ly_ctx *ctx, struct lysc_node_action_inout *inout)
+lysc_node_action_inout_free(struct lysf_ctx *ctx, struct lysc_node_action_inout *inout)
 {
     struct lysc_node *child, *child_next;
 
@@ -816,8 +948,15 @@
     }
 }
 
+/**
+ * @brief Free the compiled RPC/action structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in,out] action Compiled RPC/action structure to be freed.
+ * Since the structure is typically part of the sized array, the structure itself is not freed.
+ */
 static void
-lysc_node_action_free(struct ly_ctx *ctx, struct lysc_node_action *action)
+lysc_node_action_free(struct lysf_ctx *ctx, struct lysc_node_action *action)
 {
     FREE_ARRAY(ctx, action->when, lysc_when_free);
     if (action->input.nodetype) {
@@ -828,8 +967,15 @@
     }
 }
 
+/**
+ * @brief Free the compiled notification structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in,out] notif Compiled notification structure to be freed.
+ * Since the structure is typically part of the sized array, the structure itself is not freed.
+ */
 static void
-lysc_node_notif_free(struct ly_ctx *ctx, struct lysc_node_notif *notif)
+lysc_node_notif_free(struct lysf_ctx *ctx, struct lysc_node_notif *notif)
 {
     struct lysc_node *child, *child_next;
 
@@ -841,7 +987,7 @@
 }
 
 void
-lysc_node_container_free(struct ly_ctx *ctx, struct lysc_node_container *node)
+lysc_node_container_free(struct lysf_ctx *ctx, struct lysc_node_container *node)
 {
     struct lysc_node *child, *child_next;
 
@@ -858,24 +1004,38 @@
     FREE_ARRAY(ctx, node->musts, lysc_must_free);
 }
 
+/**
+ * @brief Free the compiled leaf structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in,out] node Compiled leaf structure to be freed.
+ * Since the structure is typically part of the sized array, the structure itself is not freed.
+ */
 static void
-lysc_node_leaf_free(struct ly_ctx *ctx, struct lysc_node_leaf *node)
+lysc_node_leaf_free(struct lysf_ctx *ctx, struct lysc_node_leaf *node)
 {
     FREE_ARRAY(ctx, node->when, lysc_when_free);
     FREE_ARRAY(ctx, node->musts, lysc_must_free);
     if (node->type) {
         lysc_type_free(ctx, node->type);
     }
-    lydict_remove(ctx, node->units);
+    lydict_remove(ctx->ctx, node->units);
     if (node->dflt) {
-        node->dflt->realtype->plugin->free(ctx, node->dflt);
+        node->dflt->realtype->plugin->free(ctx->ctx, node->dflt);
         lysc_type_free(ctx, (struct lysc_type *)node->dflt->realtype);
         free(node->dflt);
     }
 }
 
+/**
+ * @brief Free the compiled leaflist structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in,out] node Compiled leaflist structure to be freed.
+ * Since the structure is typically part of the sized array, the structure itself is not freed.
+ */
 static void
-lysc_node_leaflist_free(struct ly_ctx *ctx, struct lysc_node_leaflist *node)
+lysc_node_leaflist_free(struct lysf_ctx *ctx, struct lysc_node_leaflist *node)
 {
     LY_ARRAY_COUNT_TYPE u;
 
@@ -884,17 +1044,24 @@
     if (node->type) {
         lysc_type_free(ctx, node->type);
     }
-    lydict_remove(ctx, node->units);
+    lydict_remove(ctx->ctx, node->units);
     LY_ARRAY_FOR(node->dflts, u) {
-        node->dflts[u]->realtype->plugin->free(ctx, node->dflts[u]);
+        node->dflts[u]->realtype->plugin->free(ctx->ctx, node->dflts[u]);
         lysc_type_free(ctx, (struct lysc_type *)node->dflts[u]->realtype);
         free(node->dflts[u]);
     }
     LY_ARRAY_FREE(node->dflts);
 }
 
+/**
+ * @brief Free the compiled list structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in,out] node Compiled list structure to be freed.
+ * Since the structure is typically part of the sized array, the structure itself is not freed.
+ */
 static void
-lysc_node_list_free(struct ly_ctx *ctx, struct lysc_node_list *node)
+lysc_node_list_free(struct lysf_ctx *ctx, struct lysc_node_list *node)
 {
     LY_ARRAY_COUNT_TYPE u;
     struct lysc_node *child, *child_next;
@@ -918,8 +1085,15 @@
     }
 }
 
+/**
+ * @brief Free the compiled choice structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in,out] node Compiled choice structure to be freed.
+ * Since the structure is typically part of the sized array, the structure itself is not freed.
+ */
 static void
-lysc_node_choice_free(struct ly_ctx *ctx, struct lysc_node_choice *node)
+lysc_node_choice_free(struct lysf_ctx *ctx, struct lysc_node_choice *node)
 {
     struct lysc_node *child, *child_next;
 
@@ -929,8 +1103,15 @@
     }
 }
 
+/**
+ * @brief Free the compiled case structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in,out] node Compiled case structure to be freed.
+ * Since the structure is typically part of the sized array, the structure itself is not freed.
+ */
 static void
-lysc_node_case_free(struct ly_ctx *ctx, struct lysc_node_case *node)
+lysc_node_case_free(struct lysf_ctx *ctx, struct lysc_node_case *node)
 {
     struct lysc_node *child, *child_next;
 
@@ -940,22 +1121,36 @@
     }
 }
 
+/**
+ * @brief Free the compiled anyxml/anydata structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in,out] node Compiled anyxml/anydata structure to be freed.
+ * Since the structure is typically part of the sized array, the structure itself is not freed.
+ */
 static void
-lysc_node_anydata_free(struct ly_ctx *ctx, struct lysc_node_anydata *node)
+lysc_node_anydata_free(struct lysf_ctx *ctx, struct lysc_node_anydata *node)
 {
     FREE_ARRAY(ctx, node->when, lysc_when_free);
     FREE_ARRAY(ctx, node->musts, lysc_must_free);
 }
 
+/**
+ * @brief Free the compiled node structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in,out] node Compiled node structure to be freed.
+ * Since the structure is typically part of the sized array, the structure itself is not freed.
+ */
 static void
-lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node)
+lysc_node_free_(struct lysf_ctx *ctx, struct lysc_node *node)
 {
     ly_bool inout = 0;
 
     /* common part */
-    lydict_remove(ctx, node->name);
-    lydict_remove(ctx, node->dsc);
-    lydict_remove(ctx, node->ref);
+    lydict_remove(ctx->ctx, node->name);
+    lydict_remove(ctx->ctx, node->dsc);
+    lydict_remove(ctx->ctx, node->ref);
 
     /* nodetype-specific part */
     switch (node->nodetype) {
@@ -994,7 +1189,7 @@
         lysc_node_notif_free(ctx, (struct lysc_node_notif *)node);
         break;
     default:
-        LOGINT(ctx);
+        LOGINT(ctx->ctx);
     }
 
     FREE_ARRAY(ctx, node->exts, lysc_ext_instance_free);
@@ -1005,7 +1200,7 @@
 }
 
 void
-lysc_node_free(struct ly_ctx *ctx, struct lysc_node *node, ly_bool unlink)
+lysc_node_free(struct lysf_ctx *ctx, struct lysc_node *node, ly_bool unlink)
 {
     struct lysc_node *next, *iter, **child_p;
 
@@ -1037,7 +1232,7 @@
                 } else {
                     iter = (struct lysc_node *)lysc_node_child(node->parent);
                 }
-                LY_CHECK_ERR_RET(!iter, LOGINT(ctx), );
+                LY_CHECK_ERR_RET(!iter, LOGINT(ctx->ctx), );
             } else if (node->nodetype == LYS_RPC) {
                 iter = (struct lysc_node *)node->module->compiled->rpcs;
             } else if (node->nodetype == LYS_NOTIF) {
@@ -1075,17 +1270,14 @@
 }
 
 void
-lysc_module_free(struct lysc_module *module)
+lysc_module_free(struct lysf_ctx *ctx, struct lysc_module *module)
 {
-    struct ly_ctx *ctx;
     struct lysc_node *node, *node_next;
 
     if (!module) {
         return;
     }
 
-    ctx = module->mod->ctx;
-
     LY_LIST_FOR_SAFE(module->data, node_next, node) {
         lysc_node_free_(ctx, node);
     }
@@ -1101,7 +1293,7 @@
 }
 
 void
-lys_module_free(struct lys_module *module, ly_bool remove_links)
+lys_module_free(struct lysf_ctx *ctx, struct lys_module *module, ly_bool remove_links)
 {
     LY_ARRAY_COUNT_TYPE u;
 
@@ -1118,29 +1310,46 @@
             lysc_ident_derived_unlink(&module->identities[u]);
         }
     }
-    FREE_ARRAY(module->ctx, module->identities, lysc_ident_free);
-    lysp_module_free(module->parsed);
+    FREE_ARRAY(ctx, module->identities, lysc_ident_free);
+    lysp_module_free(ctx, module->parsed);
 
     LY_ARRAY_FREE(module->augmented_by);
     LY_ARRAY_FREE(module->deviated_by);
 
-    lydict_remove(module->ctx, module->name);
-    lydict_remove(module->ctx, module->revision);
-    lydict_remove(module->ctx, module->ns);
-    lydict_remove(module->ctx, module->prefix);
-    lydict_remove(module->ctx, module->filepath);
-    lydict_remove(module->ctx, module->org);
-    lydict_remove(module->ctx, module->contact);
-    lydict_remove(module->ctx, module->dsc);
-    lydict_remove(module->ctx, module->ref);
+    lydict_remove(ctx->ctx, module->name);
+    lydict_remove(ctx->ctx, module->revision);
+    lydict_remove(ctx->ctx, module->ns);
+    lydict_remove(ctx->ctx, module->prefix);
+    lydict_remove(ctx->ctx, module->filepath);
+    lydict_remove(ctx->ctx, module->org);
+    lydict_remove(ctx->ctx, module->contact);
+    lydict_remove(ctx->ctx, module->dsc);
+    lydict_remove(ctx->ctx, module->ref);
 
     free(module);
 }
 
+void
+lysf_ctx_erase(struct lysf_ctx *ctx)
+{
+    struct lysc_ext *ext;
+    uint32_t i;
+
+    for (i = 0; i < ctx->ext_set.count; ++i) {
+        ext = ctx->ext_set.objs[i];
+
+        lydict_remove(ctx->ctx, ext->name);
+        lydict_remove(ctx->ctx, ext->argname);
+        free(ext);
+    }
+    ly_set_erase(&ctx->ext_set, NULL);
+}
+
 LIBYANG_API_DEF void
 lyplg_ext_instance_substatements_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts)
 {
     LY_ARRAY_COUNT_TYPE u;
+    struct lysf_ctx fctx = {.ctx = ctx};
 
     LY_ARRAY_FOR(substmts, u) {
         if (!substmts[u].storage) {
@@ -1162,7 +1371,7 @@
             struct lysc_node *child, *child_next;
 
             LY_LIST_FOR_SAFE(*((struct lysc_node **)substmts[u].storage), child_next, child) {
-                lysc_node_free_(ctx, child);
+                lysc_node_free_(&fctx, child);
             }
             break;
         }
@@ -1206,11 +1415,11 @@
             }
             if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
                 /* single item */
-                lysc_iffeature_free(ctx, iff);
+                lysc_iffeature_free(&fctx, iff);
                 free(iff);
             } else {
                 /* multiple items */
-                FREE_ARRAY(ctx, iff, lysc_iffeature_free);
+                FREE_ARRAY(&fctx, iff, lysc_iffeature_free);
             }
             break;
         }
@@ -1222,7 +1431,7 @@
                 if (!type) {
                     break;
                 }
-                lysc_type_free(ctx, type);
+                lysc_type_free(&fctx, type);
             } else {
                 /* multiple items */
                 struct lysc_type **types = *((struct lysc_type ***)substmts[u].storage);
@@ -1230,7 +1439,7 @@
                 if (!types) {
                     break;
                 }
-                FREE_ARRAY(ctx, types, lysc_type2_free);
+                FREE_ARRAY(&fctx, types, lysc_type2_free);
             }
             break;
 
diff --git a/src/tree_schema_free.h b/src/tree_schema_free.h
new file mode 100644
index 0000000..ba5167f
--- /dev/null
+++ b/src/tree_schema_free.h
@@ -0,0 +1,221 @@
+/**
+ * @file tree_schema_free.h
+ * @author Michal Vasko <mvasko@cesnet.cz>
+ * @brief internal freeing functions for YANG schema trees.
+ *
+ * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
+ *
+ * This source code is licensed under BSD 3-Clause License (the "License").
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     https://opensource.org/licenses/BSD-3-Clause
+ */
+
+#ifndef LY_TREE_SCHEMA_FREE_H_
+#define LY_TREE_SCHEMA_FREE_H_
+
+#include "set.h"
+#include "tree_schema.h"
+
+struct lys_yang_parser_ctx;
+struct lys_yin_parser_ctx;
+
+struct lysf_ctx {
+    struct ly_ctx *ctx;
+    struct lys_module *mod;
+    struct ly_set ext_set;
+};
+
+/**
+ * @brief Macro to free [sized array](@ref sizedarrays) of items using the provided free function. The ARRAY itself is also freed,
+ * but the memory is not sanitized.
+ */
+#define FREE_ARRAY(CTX, ARRAY, FUNC) {LY_ARRAY_COUNT_TYPE c__; LY_ARRAY_FOR(ARRAY, c__){(FUNC)(CTX, &(ARRAY)[c__]);}LY_ARRAY_FREE(ARRAY);}
+
+/**
+ * @brief Macro to free the specified MEMBER of a structure using the provided free function. The memory is not sanitized.
+ */
+#define FREE_MEMBER(CTX, MEMBER, FUNC) if (MEMBER) {(FUNC)(CTX, MEMBER);free(MEMBER);}
+
+/**
+ * @brief Macro to free [sized array](@ref sizedarrays) of strings stored in the context's dictionary. The ARRAY itself is also freed,
+ * but the memory is not sanitized.
+ */
+#define FREE_STRINGS(CTX, ARRAY) {LY_ARRAY_COUNT_TYPE c__; LY_ARRAY_FOR(ARRAY, c__){lydict_remove(CTX, ARRAY[c__]);}LY_ARRAY_FREE(ARRAY);}
+
+/**
+ * @brief Free a parsed qualified name.
+ *
+ * @param[in] ctx libyang context.
+ * @param[in] qname Qualified name to free.
+ */
+void lysp_qname_free(struct ly_ctx *ctx, struct lysp_qname *qname);
+
+/**
+ * @brief Free the parsed extension instance structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in] ext Parsed extension instance structure to free. Note that the instance itself is not freed.
+ */
+void lysp_ext_instance_free(struct lysf_ctx *ctx, struct lysp_ext_instance *ext);
+
+/**
+ * @brief Free a parsed restriction.
+ *
+ * @param[in] ctx Free context.
+ * @param[in] restr Restriction to free.
+ */
+void lysp_restr_free(struct lysf_ctx *ctx, struct lysp_restr *restr);
+
+/**
+ * @brief Free the parsed type structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in] type Parsed schema type structure to free. Note that the type itself is not freed.
+ */
+void lysp_type_free(struct lysf_ctx *ctx, struct lysp_type *type);
+
+/**
+ * @brief Free the parsed when structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in] when Parsed schema when structure to free. Note that the structure itself is not freed.
+ */
+void lysp_when_free(struct lysf_ctx *ctx, struct lysp_when *when);
+
+/**
+ * @brief Free the parsed deviate structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in] d Parsed schema deviate structure to free. Note that the structure itself is not freed.
+ */
+void lysp_deviate_free(struct lysf_ctx *ctx, struct lysp_deviate *d);
+
+/**
+ * @brief Free the parsed deviation structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in] dev Parsed schema deviation structure to free. Note that the structure itself is not freed.
+ */
+void lysp_deviation_free(struct lysf_ctx *ctx, struct lysp_deviation *dev);
+
+/**
+ * @brief Free a parsed node.
+ *
+ * @param[in] ctx Free context.
+ * @param[in] node Node to free.
+ */
+void lysp_node_free(struct lysf_ctx *ctx, struct lysp_node *node);
+
+/**
+ * @brief Free the parsed YANG schema tree structure. Works for both modules and submodules.
+ *
+ * @param[in] ctx Free context.
+ * @param[in] module Parsed YANG schema tree structure to free.
+ */
+void lysp_module_free(struct lysf_ctx *ctx, struct lysp_module *module);
+
+/**
+ * @brief Free the compiled extension instance structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in,out] ext Compiled extension instance structure to be cleaned.
+ * Since the structure is typically part of the sized array, the structure itself is not freed.
+ */
+void lysc_ext_instance_free(struct lysf_ctx *ctx, struct lysc_ext_instance *ext);
+
+/**
+ * @brief Free the compiled if-feature structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in,out] iff Compiled if-feature structure to be cleaned.
+ * Since the structure is typically part of the sized array, the structure itself is not freed.
+ */
+void lysc_iffeature_free(struct lysf_ctx *ctx, struct lysc_iffeature *iff);
+
+/**
+ * @brief Free a compiled pattern.
+ *
+ * @param[in] ctx Free context.
+ * @param[in] pattern Pointer to the pattern to free.
+ */
+void lysc_pattern_free(struct lysf_ctx *ctx, struct lysc_pattern **pattern);
+
+/**
+ * @brief Free a bit/enum item.
+ *
+ * @param[in] ctx Free context.
+ * @param[in] item Bit/enum item to free.
+ */
+void lysc_enum_item_free(struct lysf_ctx *ctx, struct lysc_type_bitenum_item *item);
+
+/**
+ * @brief Free the compiled type structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in,out] type Compiled type structure to be freed. The structure has refcount, so it is freed only in case
+ * the value is decreased to 0.
+ */
+void lysc_type_free(struct lysf_ctx *ctx, struct lysc_type *type);
+
+/**
+ * @brief Free the compiled container node structure.
+ *
+ * Only the container-specific members are freed, for generic node free function,
+ * use ::lysc_node_free().
+ *
+ * @param[in] ctx Free context.
+ * @param[in,out] node Compiled container node structure to be freed.
+ */
+void lysc_node_container_free(struct lysf_ctx *ctx, struct lysc_node_container *node);
+
+/**
+ * @brief Free the compiled node structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in] node Compiled node structure to be freed.
+ * @param[in] unlink Whether to first unlink the node before freeing.
+ */
+void lysc_node_free(struct lysf_ctx *ctx, struct lysc_node *node, ly_bool unlink);
+
+/**
+ * @brief Free the compiled schema structure.
+ *
+ * @param[in] ctx Free context.
+ * @param[in,out] module Compiled schema module structure to free.
+ */
+void lysc_module_free(struct lysf_ctx *ctx, struct lysc_module *module);
+
+/**
+ * @brief Free the schema structure. It just frees, it does not remove the schema from its context.
+ *
+ * @param[in] ctx Free context.
+ * @param[in,out] module Schema module structure to free.
+ * @param[in] remove_links Whether to remove links in other modules to structures in this module. Not needed if
+ * the whole context is being freed.
+ */
+void lys_module_free(struct lysf_ctx *ctx, struct lys_module *module, ly_bool remove_links);
+
+/**
+ * @brief Erase free context.
+ *
+ * @param[in] ctx Free context to erase.
+ */
+void lysf_ctx_erase(struct lysf_ctx *ctx);
+
+/**
+ * @brief Free lys parser context.
+ *
+ * @param[in] ctx Context to free.
+ */
+void yang_parser_ctx_free(struct lys_yang_parser_ctx *ctx);
+
+/**
+ * @brief Free yin parser context
+ *
+ * @param[in] ctx Context to free.
+ */
+void yin_parser_ctx_free(struct lys_yin_parser_ctx *ctx);
+
+#endif /* LY_TREE_SCHEMA_FREE_H_ */
diff --git a/src/tree_schema_internal.h b/src/tree_schema_internal.h
index 18a0b90..fcbdbe9 100644
--- a/src/tree_schema_internal.h
+++ b/src/tree_schema_internal.h
@@ -174,11 +174,6 @@
 };
 
 /**
- * @brief free lys parser context.
- */
-void yang_parser_ctx_free(struct lys_yang_parser_ctx *ctx);
-
-/**
  * @brief Internal context for yin schema parser.
  */
 struct lys_yin_parser_ctx {
@@ -195,13 +190,6 @@
 };
 
 /**
- * @brief free yin parser context
- *
- * @param[in] ctx Context to free.
- */
-void yin_parser_ctx_free(struct lys_yin_parser_ctx *ctx);
-
-/**
  * @brief Check that \p c is valid UTF8 code point for YANG string.
  *
  * @param[in] ctx parser context for logging.
@@ -342,24 +330,6 @@
 LY_ERR lysp_load_submodules(struct lys_parser_ctx *pctx, struct lysp_module *pmod, struct ly_set *new_mods);
 
 /**
- * @brief Free a parsed restriction.
- *
- * @param[in] ctx libyang context.
- * @param[in] restr Restriction to free.
- */
-void lysp_restr_free(struct ly_ctx *ctx, struct lysp_restr *restr);
-
-void lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when);
-
-/**
- * @brief Free a parsed qualified name.
- *
- * @param[in] ctx libyang context.
- * @param[in] qname Qualified name to free.
- */
-void lysp_qname_free(struct ly_ctx *ctx, struct lysp_qname *qname);
-
-/**
  * @brief Get address of a node's actions list if any.
  * Decides the node's type and in case it has an actions list, returns its address.
  *
@@ -632,37 +602,6 @@
 uint8_t lysc_iff_getop(uint8_t *list, size_t pos);
 
 /**
- * @brief Macro to free [sized array](@ref sizedarrays) of items using the provided free function. The ARRAY itself is also freed,
- * but the memory is not sanitized.
- */
-#define FREE_ARRAY(CTX, ARRAY, FUNC) {LY_ARRAY_COUNT_TYPE c__; LY_ARRAY_FOR(ARRAY, c__){(FUNC)(CTX, &(ARRAY)[c__]);}LY_ARRAY_FREE(ARRAY);}
-
-/**
- * @brief Macro to free the specified MEMBER of a structure using the provided free function. The memory is not sanitized.
- */
-#define FREE_MEMBER(CTX, MEMBER, FUNC) if (MEMBER) {(FUNC)(CTX, MEMBER);free(MEMBER);}
-
-/**
- * @brief Macro to free [sized array](@ref sizedarrays) of strings stored in the context's dictionary. The ARRAY itself is also freed,
- * but the memory is not sanitized.
- */
-#define FREE_STRINGS(CTX, ARRAY) {LY_ARRAY_COUNT_TYPE c__; LY_ARRAY_FOR(ARRAY, c__){lydict_remove(CTX, ARRAY[c__]);}LY_ARRAY_FREE(ARRAY);}
-
-/**
- * @brief Free the printable YANG schema tree structure. Works for both modules and submodules.
- *
- * @param[in] module Printable YANG schema tree structure to free.
- */
-void lysp_module_free(struct lysp_module *module);
-
-/**
- * @brief Free the parsed type structure.
- * @param[in] ctx libyang context where the string data resides in a dictionary.
- * @param[in] type Parsed schema type structure to free. Note that the type itself is not freed.
- */
-void lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type);
-
-/**
  * @brief Parse generic statement structure into a specific parsed-schema structure.
  *
  * @param[in] ctx The compilation context of the @p stmt being processed
@@ -673,104 +612,6 @@
  */
 LY_ERR lysp_stmt_parse(struct lysc_ctx *ctx, const struct lysp_stmt *stmt, void **result, struct lysp_ext_instance **exts);
 
-void lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d);
-
-void lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev);
-
-/**
- * @brief Free a parsed node.
- *
- * @param[in] ctx libyang context.
- * @param[in] node Node to free.
- */
-void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
-
-/**
- * @brief Free a compiled pattern.
- *
- * @param[in] ctx libyang context.
- * @param[in] pattern Pointer to the pattern to free.
- */
-void lysc_pattern_free(struct ly_ctx *ctx, struct lysc_pattern **pattern);
-
-/**
- * @brief Free a bit/enum item.
- *
- * @param[in] ctx libyang context.
- * @param[in] item Bit/enum item to free.
- */
-void lysc_enum_item_free(struct ly_ctx *ctx, struct lysc_type_bitenum_item *item);
-
-/**
- * @brief Free the compiled type structure.
- *
- * @param[in] ctx libyang context where the string data resides in a dictionary.
- * @param[in,out] type Compiled type structure to be freed. The structure has refcount, so it is freed only in case the value is decreased to 0.
- */
-void lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type);
-
-/**
- * @brief Free the compiled if-feature structure.
- *
- * @param[in] ctx libyang context where the string data resides in a dictionary.
- * @param[in,out] iff Compiled if-feature structure to be cleaned.
- * Since the structure is typically part of the sized array, the structure itself is not freed.
- */
-void lysc_iffeature_free(struct ly_ctx *ctx, struct lysc_iffeature *iff);
-
-/**
- * @brief Free the compiled extension definition and NULL the provided pointer.
- *
- * @param[in] ctx libyang context where the string data resides in a dictionary.
- * @param[in,out] ext Compiled extension definition to be freed.
- */
-void lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext);
-
-/**
- * @brief Free the compiled extension instance structure.
- *
- * @param[in] ctx libyang context where the string data resides in a dictionary.
- * @param[in,out] ext Compiled extension instance structure to be cleaned.
- * Since the structure is typically part of the sized array, the structure itself is not freed.
- */
-void lysc_ext_instance_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext);
-
-/**
- * @brief Free the compiled node structure.
- *
- * @param[in] ctx libyang context where the string data resides in a dictionary.
- * @param[in] node Compiled node structure to be freed.
- * @param[in] unlink Whether to first unlink the node before freeing.
- */
-void lysc_node_free(struct ly_ctx *ctx, struct lysc_node *node, ly_bool unlink);
-
-/**
- * @brief Free the compiled container node structure.
- *
- * Only the container-specific members are freed, for generic node free function,
- * use ::lysc_node_free().
- *
- * @param[in] ctx libyang context where the string data resides in a dictionary.
- * @param[in,out] node Compiled container node structure to be freed.
- */
-void lysc_node_container_free(struct ly_ctx *ctx, struct lysc_node_container *node);
-
-/**
- * @brief Free the compiled schema structure.
- *
- * @param[in,out] module Compiled schema module structure to free.
- */
-void lysc_module_free(struct lysc_module *module);
-
-/**
- * @brief Free the schema structure. It just frees, it does not remove the schema from its context.
- *
- * @param[in,out] module Schema module structure to free.
- * @param[in] remove_links Whether to remove links in other modules to structures in this module. Not needed if
- * the whole context is being freed.
- */
-void lys_module_free(struct lys_module *module, ly_bool remove_links);
-
 /**
  * @brief match yang keyword
  *
diff --git a/tests/utests/CMakeLists.txt b/tests/utests/CMakeLists.txt
index 0f41a8a..5bdbd0c 100644
--- a/tests/utests/CMakeLists.txt
+++ b/tests/utests/CMakeLists.txt
@@ -8,7 +8,6 @@
     ${CMAKE_CURRENT_SOURCE_DIR}/data/*.c
     ${CMAKE_CURRENT_SOURCE_DIR}/extensions/*.c
     ${CMAKE_CURRENT_SOURCE_DIR}/schema/*.c
-    ${CMAKE_CURRENT_SOURCE_DIR}/schema/*.h
     ${CMAKE_CURRENT_SOURCE_DIR}/types/*.c
     ${CMAKE_CURRENT_SOURCE_DIR}/restriction/*.c
     ${CMAKE_CURRENT_SOURCE_DIR}/node/*.c
@@ -55,7 +54,7 @@
 ly_add_utest(NAME xpath SOURCES basic/test_xpath.c)
 ly_add_utest(NAME yanglib SOURCES basic/test_yanglib.c)
 
-ly_add_utest(NAME schema SOURCES schema/test_schema.c schema/test_schema_common.c schema/test_schema_stmts.c schema/test_schema_extensions.c)
+ly_add_utest(NAME schema SOURCES schema/test_schema.c)
 ly_add_utest(NAME yang SOURCES schema/test_yang.c)
 ly_add_utest(NAME yin SOURCES schema/test_yin.c)
 ly_add_utest(NAME tree_schema_compile SOURCES schema/test_tree_schema_compile.c)
diff --git a/tests/utests/schema/test_schema.c b/tests/utests/schema/test_schema.c
index 13e516c..adbe085 100644
--- a/tests/utests/schema/test_schema.c
+++ b/tests/utests/schema/test_schema.c
@@ -1,9 +1,10 @@
-/*
+/**
  * @file test_schema.c
- * @author: Radek Krejci <rkrejci@cesnet.cz>
+ * @author Radek Krejci <rkrejci@cesnet.cz>
+ * @author Michal Vasko <mvasko@cesnet.cz>
  * @brief unit tests for schema related functions
  *
- * Copyright (c) 2018-2019 CESNET, z.s.p.o.
+ * Copyright (c) 2018 - 2022 CESNET, z.s.p.o.
  *
  * This source code is licensed under BSD 3-Clause License (the "License").
  * You may not use this file except in compliance with the License.
@@ -12,16 +13,21 @@
  *     https://opensource.org/licenses/BSD-3-Clause
  */
 #define _UTEST_MAIN_
-#include "test_schema.h"
+#include "utests.h"
 
 #include <string.h>
 
+#include "compat.h"
+#include "context.h"
 #include "log.h"
 #include "parser_schema.h"
+#include "plugins_exts.h"
 #include "set.h"
+#include "tree_edit.h"
 #include "tree_schema.h"
+#include "tree_schema_internal.h"
 
-LY_ERR
+static LY_ERR
 test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
         const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
         const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
@@ -36,37 +42,1816 @@
     return LY_SUCCESS;
 }
 
-/**
- * DECLARE OTHER SCHEMA TESTS
- */
-/* test_schema_common.c */
-void test_getnext(void **state);
-void test_date(void **state);
-void test_revisions(void **state);
-void test_collision_typedef(void **state);
-void test_collision_grouping(void **state);
-void test_collision_identity(void **state);
-void test_collision_feature(void **state);
-void test_accessible_tree(void **state);
-void test_includes(void **state);
-void test_key_order(void **state);
-void test_disabled_enum(void **state);
+#define TEST_YANG_MODULE_10(MOD_NAME, MOD_PREFIX, MOD_NS, CONTENT) \
+    "module "MOD_NAME" { namespace "MOD_NS"; prefix "MOD_PREFIX"; "CONTENT"}"
 
-/* test_schema_stmts.c */
-void test_identity(void **state);
-void test_feature(void **state);
+#define TEST_YANG_MODULE_11(MOD_NAME, MOD_PREFIX, MOD_NS, CONTENT) \
+    "module "MOD_NAME" {yang-version 1.1; namespace "MOD_NS"; prefix "MOD_PREFIX"; "CONTENT"}"
 
-/* test_schema_extensions.c */
-void test_extension_argument(void **state);
-void test_extension_argument_element(void **state);
-void test_extension_compile(void **state);
-void test_ext_recursive(void **state);
+#define TEST_YIN_MODULE_10(MOD_NAME, MOD_PREFIX, MOD_NS, CONTENT) \
+    "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\""MOD_NAME"\">" \
+    "<namespace uri=\""MOD_NS"\"/><prefix value=\""MOD_PREFIX"\"/>"CONTENT"</module>"
+
+#define TEST_YIN_MODULE_11(MOD_NAME, MOD_PREFIX, MOD_NS, CONTENT) \
+    "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\""MOD_NAME"\"><yang-version value=\"1.1\"/>" \
+    "<namespace uri=\""MOD_NS"\"/><prefix value=\""MOD_PREFIX"\"/>"CONTENT"</module>"
+
+#define TEST_SCHEMA_STR(RFC7950, YIN, MOD_NAME, CONTENT, STR) \
+    if (YIN) { \
+        if (RFC7950) { \
+            STR = TEST_YIN_MODULE_11(MOD_NAME, MOD_NAME, "urn:libyang:test:"MOD_NAME, CONTENT); \
+        } else { \
+            STR = TEST_YIN_MODULE_10(MOD_NAME, MOD_NAME, "urn:libyang:test:"MOD_NAME, CONTENT); \
+        } \
+    } else { /* YANG */ \
+        if (RFC7950) { \
+            STR = TEST_YANG_MODULE_11(MOD_NAME, MOD_NAME, "urn:libyang:test:"MOD_NAME, CONTENT); \
+        } else { \
+            STR = TEST_YANG_MODULE_10(MOD_NAME, MOD_NAME, "urn:libyang:test:"MOD_NAME, CONTENT); \
+        } \
+    }
+
+#define TEST_SCHEMA_OK(RFC7950, YIN, MOD_NAME, CONTENT, RESULT) \
+    { \
+    const char *test_str__; \
+    TEST_SCHEMA_STR(RFC7950, YIN, MOD_NAME, CONTENT, test_str__) \
+    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, test_str__, YIN ? LYS_IN_YIN : LYS_IN_YANG, &(RESULT))); \
+    }
+
+#define TEST_SCHEMA_ERR(RFC7950, YIN, MOD_NAME, CONTENT, ERRMSG, ERRPATH) \
+    { \
+    const char *test_str__; \
+    TEST_SCHEMA_STR(RFC7950, YIN, MOD_NAME, CONTENT, test_str__) \
+    assert_int_not_equal(lys_parse_mem(UTEST_LYCTX, test_str__, YIN ? LYS_IN_YIN : LYS_IN_YANG, NULL), LY_SUCCESS); \
+    CHECK_LOG_CTX(ERRMSG, ERRPATH); \
+    }
+
+#define TEST_SCHEMA_PARSE_ERR(RFC7950, YIN, MOD_NAME, CONTENT, ERRMSG, ERRPATH) \
+    { \
+    const char *test_str__; \
+    TEST_SCHEMA_STR(RFC7950, YIN, MOD_NAME, CONTENT, test_str__) \
+    assert_int_not_equal(lys_parse_mem(UTEST_LYCTX, test_str__, YIN ? LYS_IN_YIN : LYS_IN_YANG, NULL), LY_SUCCESS); \
+    CHECK_LOG_CTX("Parsing module \""MOD_NAME"\" failed.", NULL, ERRMSG, ERRPATH); \
+    }
+
+#define TEST_STMT_DUP(RFC7950, YIN, STMT, MEMBER, VALUE1, VALUE2, LINE) \
+    if (YIN) { \
+        TEST_SCHEMA_PARSE_ERR(RFC7950, YIN, "dup", "", "Duplicate keyword \""MEMBER"\".", "Line number "LINE"."); \
+    } else { \
+        TEST_SCHEMA_PARSE_ERR(RFC7950, YIN, "dup", STMT"{"MEMBER" "VALUE1";"MEMBER" "VALUE2";}", \
+                        "Duplicate keyword \""MEMBER"\".", "Line number "LINE"."); \
+    }
+
+#define TEST_STMT_SUBSTM_ERR(RFC7950, STMT, SUBSTMT, VALUE) ;\
+        TEST_SCHEMA_PARSE_ERR(RFC7950, 0, "inv", STMT" test {"SUBSTMT" "VALUE";}", \
+                        "Invalid keyword \""SUBSTMT"\" as a child of \""STMT"\".", "Line number 1.");
+
+struct module_clb_list {
+    const char *name;
+    const char *data;
+};
+
+static LY_ERR
+module_clb(const char *mod_name, const char *UNUSED(mod_rev), const char *submod_name,
+        const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
+        const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
+{
+    struct module_clb_list *list = (struct module_clb_list *)user_data;
+
+    for (unsigned int i = 0; list[i].data; i++) {
+
+        if ((submod_name && !strcmp(list[i].name, submod_name)) ||
+                (!submod_name && mod_name && !strcmp(list[i].name, mod_name))) {
+            *module_data = list[i].data;
+            *format = LYS_IN_YANG;
+            *free_module_data = NULL;
+            return LY_SUCCESS;
+        }
+    }
+    return LY_EINVAL;
+}
+
+static void
+test_getnext(void **state)
+{
+    struct lys_module *mod;
+    const struct lysc_node *node = NULL, *four;
+    const struct lysc_node_container *cont;
+    const struct lysc_action *rpc;
+
+    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {yang-version 1.1; namespace urn:a;prefix a;"
+            "container a { container one {presence test;} leaf two {type string;} leaf-list three {type string;}"
+            "  list four {config false;} choice x { leaf five {type string;} case y {leaf six {type string;}}}"
+            "  anyxml seven; action eight {input {leaf eight-input {type string;}} output {leaf eight-output {type string;}}}"
+            "  notification nine {leaf nine-data {type string;}}}"
+            "leaf b {type string;} leaf-list c {type string;} list d {config false;}"
+            "choice x { case empty-x { choice empty-xc { case nothing;}} leaf e {type string;} case y {leaf f {type string;}}} anyxml g;"
+            "rpc h {input {leaf h-input {type string;}} output {leaf h-output {type string;}}}"
+            "rpc i;"
+            "notification j {leaf i-data {type string;}}"
+            "notification k;}", LYS_IN_YANG, &mod));
+    assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
+    assert_string_equal("a", node->name);
+    cont = (const struct lysc_node_container *)node;
+    assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
+    assert_string_equal("b", node->name);
+    assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
+    assert_string_equal("c", node->name);
+    assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
+    assert_string_equal("d", node->name);
+    assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
+    assert_string_equal("e", node->name);
+    assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
+    assert_string_equal("f", node->name);
+    assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
+    assert_string_equal("g", node->name);
+    assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
+    assert_string_equal("h", node->name);
+    rpc = (const struct lysc_action *)node;
+    assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
+    assert_string_equal("i", node->name);
+    assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
+    assert_string_equal("j", node->name);
+    assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
+    assert_string_equal("k", node->name);
+    assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
+    /* Inside container */
+    assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
+    assert_string_equal("one", node->name);
+    assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
+    assert_string_equal("two", node->name);
+    assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
+    assert_string_equal("three", node->name);
+    assert_non_null(node = four = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
+    assert_string_equal("four", node->name);
+    assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
+    assert_string_equal("five", node->name);
+    assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
+    assert_string_equal("six", node->name);
+    assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
+    assert_string_equal("seven", node->name);
+    assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
+    assert_string_equal("eight", node->name);
+    assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
+    assert_string_equal("nine", node->name);
+    assert_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
+    /* Inside RPC */
+    assert_non_null(node = lys_getnext(node, (const struct lysc_node *)rpc, mod->compiled, 0));
+    assert_string_equal("h-input", node->name);
+    assert_null(node = lys_getnext(node, (const struct lysc_node *)rpc, mod->compiled, 0));
+
+    /* options */
+    assert_non_null(node = lys_getnext(four, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE));
+    assert_string_equal("x", node->name);
+    assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE));
+    assert_string_equal("seven", node->name);
+
+    assert_non_null(node = lys_getnext(four, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_NOCHOICE));
+    assert_string_equal("seven", node->name);
+
+    assert_non_null(node = lys_getnext(four, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
+    assert_string_equal("five", node->name);
+    assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
+    assert_string_equal("y", node->name);
+    assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
+    assert_string_equal("seven", node->name);
+
+    assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT));
+    assert_string_equal("one", node->name);
+
+    assert_non_null(node = lys_getnext(NULL, (const struct lysc_node *)rpc, mod->compiled, LYS_GETNEXT_OUTPUT));
+    assert_string_equal("h-output", node->name);
+    assert_null(node = lys_getnext(node, (const struct lysc_node *)rpc, mod->compiled, LYS_GETNEXT_OUTPUT));
+
+    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module c {namespace urn:c;prefix c; rpc c;}", LYS_IN_YANG, &mod));
+    assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
+    assert_string_equal("c", node->name);
+    assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
+
+    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module d {namespace urn:d;prefix d; notification d;}", LYS_IN_YANG, &mod));
+    assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
+    assert_string_equal("d", node->name);
+    assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
+
+    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module e {namespace urn:e;prefix e; container c {container cc;} leaf a {type string;}}", LYS_IN_YANG, &mod));
+    assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
+    assert_string_equal("c", node->name);
+    assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT));
+    assert_string_equal("a", node->name);
+}
+
+static void
+test_date(void **state)
+{
+    assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date"));
+    CHECK_LOG("Invalid argument date (lysp_check_date()).", NULL);
+    assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date"));
+    CHECK_LOG("Invalid argument date_len (lysp_check_date()).", NULL);
+    assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date"));
+    CHECK_LOG("Invalid value \"nonsencexx\" of \"date\".", NULL);
+    assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date"));
+    CHECK_LOG("Invalid value \"123x-11-11\" of \"date\".", NULL);
+    assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date"));
+    CHECK_LOG("Invalid value \"2018-13-11\" of \"date\".", NULL);
+    assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date"));
+    CHECK_LOG("Invalid value \"2018-11-41\" of \"date\".", NULL);
+    assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date"));
+    CHECK_LOG("Invalid value \"2018-02-29\" of \"date\".", NULL);
+    assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date"));
+    CHECK_LOG("Invalid value \"2018.02-28\" of \"date\".", NULL);
+    assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02.28", 10, "date"));
+    CHECK_LOG("Invalid value \"2018-02.28\" of \"date\".", NULL);
+
+    assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date"));
+    assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date"));
+    assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date"));
+}
+
+static void
+test_revisions(void **state)
+{
+    struct lysp_revision *revs = NULL, *rev;
+
+    /* no error, it just does nothing */
+    lysp_sort_revisions(NULL);
+    CHECK_LOG(NULL, NULL);
+
+    /* revisions are stored in wrong order - the newest is the last */
+    LY_ARRAY_NEW_RET(NULL, revs, rev, );
+    strcpy(rev->date, "2018-01-01");
+    LY_ARRAY_NEW_RET(NULL, revs, rev, );
+    strcpy(rev->date, "2018-12-31");
+
+    assert_int_equal(2, LY_ARRAY_COUNT(revs));
+    assert_string_equal("2018-01-01", &revs[0]);
+    assert_string_equal("2018-12-31", &revs[1]);
+    /* the order should be fixed, so the newest revision will be the first in the array */
+    lysp_sort_revisions(revs);
+    assert_string_equal("2018-12-31", &revs[0]);
+    assert_string_equal("2018-01-01", &revs[1]);
+
+    LY_ARRAY_FREE(revs);
+}
+
+static void
+test_collision_typedef(void **state)
+{
+    const char *str;
+    char *submod;
+    struct module_clb_list list[3] = {0};
+
+    list[0].name = "asub";
+    list[1].name = "bsub";
+
+    /* collision with a built-in type */
+    str = "module a {namespace urn:a; prefix a; typedef binary {type string;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"binary\" of typedef statement - name collision with a built-in type.", NULL);
+    str = "module a {namespace urn:a; prefix a; typedef bits {type string;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"bits\" of typedef statement - name collision with a built-in type.", NULL);
+    str = "module a {namespace urn:a; prefix a; typedef boolean {type string;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"boolean\" of typedef statement - name collision with a built-in type.", NULL);
+    str = "module a {namespace urn:a; prefix a; typedef decimal64 {type string;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"decimal64\" of typedef statement - name collision with a built-in type.", NULL);
+    str = "module a {namespace urn:a; prefix a; typedef empty {type string;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"empty\" of typedef statement - name collision with a built-in type.", NULL);
+    str = "module a {namespace urn:a; prefix a; typedef enumeration {type string;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"enumeration\" of typedef statement - name collision with a built-in type.", NULL);
+    str = "module a {namespace urn:a; prefix a; typedef int8 {type string;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"int8\" of typedef statement - name collision with a built-in type.", NULL);
+    str = "module a {namespace urn:a; prefix a; typedef int16 {type string;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"int16\" of typedef statement - name collision with a built-in type.", NULL);
+    str = "module a {namespace urn:a; prefix a; typedef int32 {type string;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"int32\" of typedef statement - name collision with a built-in type.", NULL);
+    str = "module a {namespace urn:a; prefix a; typedef int64 {type string;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"int64\" of typedef statement - name collision with a built-in type.", NULL);
+    str = "module a {namespace urn:a; prefix a; typedef instance-identifier {type string;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"instance-identifier\" of typedef statement - name collision with a built-in type.", NULL);
+    str = "module a {namespace urn:a; prefix a; typedef identityref {type string;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"identityref\" of typedef statement - name collision with a built-in type.", NULL);
+    str = "module a {namespace urn:a; prefix a; typedef leafref {type string;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"leafref\" of typedef statement - name collision with a built-in type.", NULL);
+    str = "module a {namespace urn:a; prefix a; typedef string {type int8;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"string\" of typedef statement - name collision with a built-in type.", NULL);
+    str = "module a {namespace urn:a; prefix a; typedef union {type string;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"union\" of typedef statement - name collision with a built-in type.", NULL);
+    str = "module a {namespace urn:a; prefix a; typedef uint8 {type string;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"uint8\" of typedef statement - name collision with a built-in type.", NULL);
+    str = "module a {namespace urn:a; prefix a; typedef uint16 {type string;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"uint16\" of typedef statement - name collision with a built-in type.", NULL);
+    str = "module a {namespace urn:a; prefix a; typedef uint32 {type string;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"uint32\" of typedef statement - name collision with a built-in type.", NULL);
+    str = "module a {namespace urn:a; prefix a; typedef uint64 {type string;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"uint64\" of typedef statement - name collision with a built-in type.", NULL);
+
+    str = "module mytypes {namespace urn:types; prefix t; typedef binary_ {type string;} typedef bits_ {type string;} typedef boolean_ {type string;} "
+            "typedef decimal64_ {type string;} typedef empty_ {type string;} typedef enumeration_ {type string;} typedef int8_ {type string;} typedef int16_ {type string;}"
+            "typedef int32_ {type string;} typedef int64_ {type string;} typedef instance-identifier_ {type string;} typedef identityref_ {type string;}"
+            "typedef leafref_ {type string;} typedef string_ {type int8;} typedef union_ {type string;} typedef uint8_ {type string;} typedef uint16_ {type string;}"
+            "typedef uint32_ {type string;} typedef uint64_ {type string;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
+
+    /* collision in node's scope */
+    str = "module a {namespace urn:a; prefix a; container c {typedef y {type int8;} typedef y {type string;}}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"y\" of typedef statement - name collision with sibling type.", NULL);
+
+    /* collision with parent node */
+    str = "module a {namespace urn:a; prefix a; container c {container d {typedef y {type int8;}} typedef y {type string;}}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"y\" of typedef statement - name collision with another scoped type.", NULL);
+
+    /* collision with module's top-level */
+    str = "module a {namespace urn:a; prefix a; typedef x {type string;} container c {typedef x {type int8;}}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"x\" of typedef statement - scoped type collide with a top-level type.", NULL);
+
+    /* collision of submodule's node with module's top-level */
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} container c {typedef x {type string;}}}");
+    str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"x\" of typedef statement - scoped type collide with a top-level type.", NULL);
+
+    /* collision of module's node with submodule's top-level */
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type int8;}}");
+    str = "module a {namespace urn:a; prefix a; include b; container c {typedef x {type string;}}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"x\" of typedef statement - scoped type collide with a top-level type.", NULL);
+
+    /* collision of submodule's node with another submodule's top-level */
+    str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}";
+    list[0].data = "submodule asub {belongs-to a {prefix a;} typedef g {type int;}}";
+    list[1].data = "submodule bsub {belongs-to a {prefix a;} container c {typedef g {type int;}}}";
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
+    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"g\" of typedef statement - scoped type collide with a top-level type.", NULL);
+
+    /* collision of module's top-levels */
+    str = "module a {namespace urn:a; prefix a; typedef test {type string;} typedef test {type int8;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"test\" of typedef statement - name collision with another top-level type.", NULL);
+
+    /* collision of submodule's top-levels */
+    submod = "submodule asub {belongs-to a {prefix a;} typedef g {type int;} typedef g {type int;}}";
+    str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub;}";
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod);
+    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"g\" of typedef statement - name collision with another top-level type.", NULL);
+
+    /* collision of module's top-level with submodule's top-levels */
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type string;}}");
+    str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"x\" of typedef statement - name collision with another top-level type.", NULL);
+
+    /* collision of submodule's top-level with another submodule's top-levels */
+    str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}";
+    list[0].data = "submodule asub {belongs-to a {prefix a;} typedef g {type int;}}";
+    list[1].data = "submodule bsub {belongs-to a {prefix a;} typedef g {type int;}}";
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
+    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"g\" of typedef statement - name collision with another top-level type.", NULL);
+
+    /* error in type-stmt */
+    str = "module a {namespace urn:a; prefix a; container c {typedef x {type t{}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Unexpected end-of-input.", "Line number 1.");
+    UTEST_LOG_CLEAN;
+
+    /* no collision if the same names are in different scope */
+    str = "module a {yang-version 1.1; namespace urn:a; prefix a;"
+            "container c {typedef g {type int;}} container d {typedef g {type int;}}}";
+    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
+}
+
+static void
+test_collision_grouping(void **state)
+{
+    const char *str;
+    char *submod;
+    struct module_clb_list list[3] = {0};
+
+    list[0].name = "asub";
+    list[1].name = "bsub";
+
+    /* collision in node's scope */
+    str = "module a {namespace urn:a; prefix a; container c {grouping y; grouping y;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"y\" of grouping statement - name collision with sibling grouping.", NULL);
+
+    /* collision with parent node */
+    str = "module a {namespace urn:a; prefix a; container c {container d {grouping y;} grouping y;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"y\" of grouping statement - name collision with another scoped grouping.", NULL);
+
+    /* collision with module's top-level */
+    str = "module a {namespace urn:a; prefix a; grouping x; container c {grouping x;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"x\" of grouping statement - scoped grouping collide with a top-level grouping.", NULL);
+
+    /* collision of submodule's node with module's top-level */
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} container c {grouping x;}}");
+    str = "module a {namespace urn:a; prefix a; include b; grouping x;}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"x\" of grouping statement - scoped grouping collide with a top-level grouping.", NULL);
+
+    /* collision of module's node with submodule's top-level */
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} grouping x;}");
+    str = "module a {namespace urn:a; prefix a; include b; container c {grouping x;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"x\" of grouping statement - scoped grouping collide with a top-level grouping.", NULL);
+
+    /* collision of submodule's node with another submodule's top-level */
+    str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}";
+    list[0].data = "submodule asub {belongs-to a {prefix a;} grouping g;}";
+    list[1].data = "submodule bsub {belongs-to a {prefix a;} container c {grouping g;}}";
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
+    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"g\" of grouping statement - scoped grouping collide with a top-level grouping.", NULL);
+
+    /* collision of module's top-levels */
+    str = "module a {namespace urn:a; prefix a; grouping test; grouping test;}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"test\" of grouping statement - name collision with another top-level grouping.", NULL);
+
+    /* collision of submodule's top-levels */
+    submod = "submodule asub {belongs-to a {prefix a;} grouping g; grouping g;}";
+    str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub;}";
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod);
+    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"g\" of grouping statement - name collision with another top-level grouping.", NULL);
+
+    /* collision of module's top-level with submodule's top-levels */
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} grouping x;}");
+    str = "module a {namespace urn:a; prefix a; include b; grouping x;}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"x\" of grouping statement - name collision with another top-level grouping.", NULL);
+
+    /* collision of submodule's top-level with another submodule's top-levels */
+    str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}";
+    list[0].data = "submodule asub {belongs-to a {prefix a;} grouping g;}";
+    list[1].data = "submodule bsub {belongs-to a {prefix a;} grouping g;}";
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
+    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"g\" of grouping statement - name collision with another top-level grouping.", NULL);
+
+    /* collision in nested groupings, top-level */
+    str = "module a {namespace urn:a; prefix a; grouping g {grouping g;}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"g\" of grouping statement - scoped grouping collide with a top-level grouping.", NULL);
+
+    /* collision in nested groupings, in node */
+    str = "module a {namespace urn:a; prefix a; container c {grouping g {grouping g;}}}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"g\" of grouping statement - name collision with another scoped grouping.", NULL);
+
+    /* no collision if the same names are in different scope */
+    str = "module a {yang-version 1.1; namespace urn:a; prefix a;"
+            "container c {grouping g;} container d {grouping g;}}";
+    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
+}
+
+static void
+test_collision_identity(void **state)
+{
+    const char *str;
+    char *submod;
+    struct module_clb_list list[3] = {0};
+
+    list[0].name = "asub";
+    list[1].name = "bsub";
+
+    /* collision of module's top-levels */
+    str = "module a {yang-version 1.1; namespace urn:a; prefix a; identity g; identity g;}";
+    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"g\" of identity statement - name collision with another top-level identity.", NULL);
+
+    /* collision of submodule's top-levels */
+    submod = "submodule asub {belongs-to a {prefix a;} identity g; identity g;}";
+    str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub;}";
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod);
+    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"g\" of identity statement - name collision with another top-level identity.", NULL);
+
+    /* collision of module's top-level with submodule's top-levels */
+    submod = "submodule asub {belongs-to a {prefix a;} identity g;}";
+    str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; identity g;}";
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod);
+    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"g\" of identity statement - name collision with another top-level identity.", NULL);
+
+    /* collision of submodule's top-level with another submodule's top-levels */
+    str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}";
+    list[0].data = "submodule asub {belongs-to a {prefix a;} identity g;}";
+    list[1].data = "submodule bsub {belongs-to a {prefix a;} identity g;}";
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
+    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"g\" of identity statement - name collision with another top-level identity.", NULL);
+}
+
+static void
+test_collision_feature(void **state)
+{
+    const char *str;
+    char *submod;
+    struct module_clb_list list[3] = {0};
+
+    list[0].name = "asub";
+    list[1].name = "bsub";
+
+    /* collision of module's top-levels */
+    str = "module a {yang-version 1.1; namespace urn:a; prefix a; feature g; feature g;}";
+    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"g\" of feature statement - name collision with another top-level feature.", NULL);
+
+    /* collision of submodule's top-levels */
+    submod = "submodule asub {belongs-to a {prefix a;} feature g; feature g;}";
+    str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub;}";
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod);
+    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"g\" of feature statement - name collision with another top-level feature.", NULL);
+
+    /* collision of module's top-level with submodule's top-levels */
+    submod = "submodule asub {belongs-to a {prefix a;} feature g;}";
+    str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; feature g;}";
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod);
+    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"g\" of feature statement - name collision with another top-level feature.", NULL);
+
+    /* collision of submodule's top-level with another submodule's top-levels */
+    str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}";
+    list[0].data = "submodule asub {belongs-to a {prefix a;} feature g;}";
+    list[1].data = "submodule bsub {belongs-to a {prefix a;} feature g;}";
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
+    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
+    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
+            "Duplicate identifier \"g\" of feature statement - name collision with another top-level feature.", NULL);
+}
+
+static void
+test_accessible_tree(void **state)
+{
+    const char *str;
+
+    /* config -> config */
+    str = "module a {\n"
+            "    namespace urn:a;\n"
+            "    prefix a;\n"
+            "    container cont {\n"
+            "        leaf l {\n"
+            "            type empty;\n"
+            "        }\n"
+            "    }\n"
+            "    container cont2 {\n"
+            "        leaf l2 {\n"
+            "            must ../../cont/l;\n"
+            "            type leafref {\n"
+            "                path /cont/l;\n"
+            "            }\n"
+            "        }\n"
+            "    }\n"
+            "}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
+    CHECK_LOG_CTX(NULL, NULL);
+
+    /* config -> state leafref */
+    str = "module b {\n"
+            "    namespace urn:b;\n"
+            "    prefix b;\n"
+            "    container cont {\n"
+            "        config false;\n"
+            "        leaf l {\n"
+            "            type empty;\n"
+            "        }\n"
+            "    }\n"
+            "    container cont2 {\n"
+            "        leaf l2 {\n"
+            "            type leafref {\n"
+            "                path /cont/l;\n"
+            "            }\n"
+            "        }\n"
+            "    }\n"
+            "}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Invalid leafref path \"/cont/l\" - target is supposed to represent configuration data"
+            " (as the leafref does), but it does not.", "Schema location \"/b:cont2/l2\".");
+
+    /* config -> state must */
+    str = "module b {\n"
+            "    namespace urn:b;\n"
+            "    prefix b;\n"
+            "    container cont {\n"
+            "        config false;\n"
+            "        leaf l {\n"
+            "            type empty;\n"
+            "        }\n"
+            "    }\n"
+            "    container cont2 {\n"
+            "        leaf l2 {\n"
+            "            must ../../cont/l;\n"
+            "            type empty;\n"
+            "        }\n"
+            "    }\n"
+            "}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
+    CHECK_LOG_CTX("Schema node \"cont\" for parent \"<config-root>\" not found; in expr \"../../cont\" "
+            "with context node \"/b:cont2/l2\".", NULL);
+
+    /* state -> config */
+    str = "module c {\n"
+            "    namespace urn:c;\n"
+            "    prefix c;\n"
+            "    container cont {\n"
+            "        leaf l {\n"
+            "            type empty;\n"
+            "        }\n"
+            "    }\n"
+            "    container cont2 {\n"
+            "        config false;\n"
+            "        leaf l2 {\n"
+            "            must ../../cont/l;\n"
+            "            type leafref {\n"
+            "                path /cont/l;\n"
+            "            }\n"
+            "        }\n"
+            "    }\n"
+            "}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
+    CHECK_LOG_CTX(NULL, NULL);
+
+    /* notif -> state */
+    str = "module d {\n"
+            "    namespace urn:d;\n"
+            "    prefix d;\n"
+            "    container cont {\n"
+            "        config false;\n"
+            "        leaf l {\n"
+            "            type empty;\n"
+            "        }\n"
+            "    }\n"
+            "    notification notif {\n"
+            "        leaf l2 {\n"
+            "            must ../../cont/l;\n"
+            "            type leafref {\n"
+            "                path /cont/l;\n"
+            "            }\n"
+            "        }\n"
+            "    }\n"
+            "}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
+    CHECK_LOG_CTX(NULL, NULL);
+
+    /* notif -> notif */
+    str = "module e {\n"
+            "    namespace urn:e;\n"
+            "    prefix e;\n"
+            "    notification notif {\n"
+            "        leaf l {\n"
+            "            type empty;\n"
+            "        }\n"
+            "        leaf l2 {\n"
+            "            must ../../notif/l;\n"
+            "            type leafref {\n"
+            "                path /notif/l;\n"
+            "            }\n"
+            "        }\n"
+            "    }\n"
+            "}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
+    CHECK_LOG_CTX(NULL, NULL);
+
+    /* rpc input -> state */
+    str = "module f {\n"
+            "    namespace urn:f;\n"
+            "    prefix f;\n"
+            "    container cont {\n"
+            "        config false;\n"
+            "        leaf l {\n"
+            "            type empty;\n"
+            "        }\n"
+            "    }\n"
+            "    rpc rp {\n"
+            "        input {\n"
+            "            leaf l2 {\n"
+            "                must ../../cont/l;\n"
+            "                type leafref {\n"
+            "                    path /cont/l;\n"
+            "                }\n"
+            "            }\n"
+            "        }\n"
+            "    }\n"
+            "}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
+    CHECK_LOG_CTX(NULL, NULL);
+
+    /* rpc input -> rpc input */
+    str = "module g {\n"
+            "    namespace urn:g;\n"
+            "    prefix g;\n"
+            "    rpc rp {\n"
+            "        input {\n"
+            "            leaf l {\n"
+            "                type empty;\n"
+            "            }\n"
+            "            leaf l2 {\n"
+            "                must ../l;\n"
+            "                type leafref {\n"
+            "                    path /rp/l;\n"
+            "                }\n"
+            "            }\n"
+            "        }\n"
+            "    }\n"
+            "}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
+    CHECK_LOG_CTX(NULL, NULL);
+
+    /* rpc input -> rpc output leafref */
+    str = "module h {\n"
+            "    namespace urn:h;\n"
+            "    prefix h;\n"
+            "    rpc rp {\n"
+            "        input {\n"
+            "            leaf l2 {\n"
+            "                type leafref {\n"
+            "                    path /rp/l;\n"
+            "                }\n"
+            "            }\n"
+            "        }\n"
+            "        output {\n"
+            "            leaf l {\n"
+            "                type empty;\n"
+            "            }\n"
+            "        }\n"
+            "    }\n"
+            "}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Not found node \"l\" in path.", "Schema location \"/h:rp/input/l2\".");
+
+    /* rpc input -> rpc output must */
+    str = "module h {\n"
+            "    namespace urn:h;\n"
+            "    prefix h;\n"
+            "    rpc rp {\n"
+            "        input {\n"
+            "            leaf l2 {\n"
+            "                must ../l;\n"
+            "                type empty;\n"
+            "            }\n"
+            "        }\n"
+            "        output {\n"
+            "            leaf l {\n"
+            "                type empty;\n"
+            "            }\n"
+            "        }\n"
+            "    }\n"
+            "}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
+    CHECK_LOG_CTX("Schema node \"l\" for parent \"/h:rp\" not found; in expr \"../l\" with context node \"/h:rp/input/l2\".", NULL);
+
+    /* rpc input -> notif leafref */
+    str = "module i {\n"
+            "    namespace urn:i;\n"
+            "    prefix i;\n"
+            "    rpc rp {\n"
+            "        input {\n"
+            "            leaf l2 {\n"
+            "                type leafref {\n"
+            "                    path ../../notif/l;\n"
+            "                }\n"
+            "            }\n"
+            "        }\n"
+            "    }\n"
+            "    notification notif {\n"
+            "        leaf l {\n"
+            "            type empty;\n"
+            "        }\n"
+            "    }\n"
+            "}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Not found node \"notif\" in path.", "Schema location \"/i:rp/input/l2\".");
+
+    /* rpc input -> notif must */
+    str = "module i {\n"
+            "    namespace urn:i;\n"
+            "    prefix i;\n"
+            "    rpc rp {\n"
+            "        input {\n"
+            "            leaf l2 {\n"
+            "                must /notif/l;\n"
+            "                type empty;\n"
+            "            }\n"
+            "        }\n"
+            "    }\n"
+            "    notification notif {\n"
+            "        leaf l {\n"
+            "            type empty;\n"
+            "        }\n"
+            "    }\n"
+            "}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
+    CHECK_LOG_CTX("Schema node \"notif\" for parent \"<root>\" not found; in expr \"/notif\" "
+            "with context node \"/i:rp/input/l2\".", NULL);
+
+    /* action output -> state */
+    str = "module j {\n"
+            "    yang-version 1.1;\n"
+            "    namespace urn:j;\n"
+            "    prefix j;\n"
+            "    container cont {\n"
+            "        list ll {\n"
+            "            key k;\n"
+            "            leaf k {\n"
+            "                type string;\n"
+            "            }\n"
+            "            action act {\n"
+            "                output {\n"
+            "                    leaf l2 {\n"
+            "                        must /cont/l;\n"
+            "                        type leafref {\n"
+            "                            path ../../../l;\n"
+            "                        }\n"
+            "                    }\n"
+            "                }\n"
+            "            }\n"
+            "        }\n"
+            "        leaf l {\n"
+            "            config false;\n"
+            "            type empty;\n"
+            "        }\n"
+            "    }\n"
+            "}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
+    CHECK_LOG_CTX(NULL, NULL);
+
+    /* action output -> action input leafref */
+    str = "module k {\n"
+            "    yang-version 1.1;\n"
+            "    namespace urn:k;\n"
+            "    prefix k;\n"
+            "    container cont {\n"
+            "        list ll {\n"
+            "            key k;\n"
+            "            leaf k {\n"
+            "                type string;\n"
+            "            }\n"
+            "            action act {\n"
+            "                input {\n"
+            "                    leaf l {\n"
+            "                        type empty;\n"
+            "                    }\n"
+            "                }\n"
+            "                output {\n"
+            "                    leaf l2 {\n"
+            "                        type leafref {\n"
+            "                            path ../l;\n"
+            "                        }\n"
+            "                    }\n"
+            "                }\n"
+            "            }\n"
+            "        }\n"
+            "    }\n"
+            "}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Not found node \"l\" in path.", "Schema location \"/k:cont/ll/act/output/l2\".");
+
+    /* action output -> action input must */
+    str = "module k {\n"
+            "    yang-version 1.1;\n"
+            "    namespace urn:k;\n"
+            "    prefix k;\n"
+            "    container cont {\n"
+            "        list ll {\n"
+            "            key k;\n"
+            "            leaf k {\n"
+            "                type string;\n"
+            "            }\n"
+            "            action act {\n"
+            "                input {\n"
+            "                    leaf l {\n"
+            "                        type empty;\n"
+            "                    }\n"
+            "                }\n"
+            "                output {\n"
+            "                    leaf l2 {\n"
+            "                        must /cont/ll/act/l;\n"
+            "                        type empty;\n"
+            "                    }\n"
+            "                }\n"
+            "            }\n"
+            "        }\n"
+            "    }\n"
+            "}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
+    CHECK_LOG_CTX("Schema node \"l\" for parent \"/k:cont/ll/act\" not found; in expr \"/cont/ll/act/l\" "
+            "with context node \"/k:cont/ll/act/output/l2\".", NULL);
+}
+
+static void
+test_includes(void **state)
+{
+    struct lys_module *mod;
+
+    {
+        /* YANG 1.0 - the missing include sub_a_two in main_a will be injected from sub_a_one */
+        struct module_clb_list list[] = {
+            {"main_a", "module main_a { namespace urn:test:main_a; prefix ma; include sub_a_one;}"},
+            {"sub_a_one", "submodule sub_a_one { belongs-to main_a { prefix ma; } include sub_a_two;}"},
+            {"sub_a_two", "submodule sub_a_two { belongs-to main_a { prefix ma; } }"},
+            {NULL, NULL}
+        };
+
+        ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
+        mod = ly_ctx_load_module(UTEST_LYCTX, "main_a", NULL, NULL);
+        assert_non_null(mod);
+        assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->includes));
+        assert_true(mod->parsed->includes[1].injected);
+    }
+
+    {
+        /* YANG 1.1 - the missing include sub_b_two in main_b is error */
+        struct module_clb_list list[] = {
+            {"main_b", "module main_b { yang-version 1.1; namespace urn:test:main_b; prefix mb; include sub_b_one;}"},
+            {"sub_b_one", "submodule sub_b_one { yang-version 1.1; belongs-to main_b { prefix mb; } include sub_b_two;}"},
+            {"sub_b_two", "submodule sub_b_two { yang-version 1.1; belongs-to main_b { prefix mb; } }"},
+            {NULL, NULL}
+        };
+
+        ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
+        mod = ly_ctx_load_module(UTEST_LYCTX, "main_b", NULL, NULL);
+        assert_null(mod);
+        CHECK_LOG_CTX("Loading \"main_b\" module failed.", NULL,
+                "Data model \"main_b\" not found in local searchdirs.", NULL,
+                "Parsing module \"main_b\" failed.", NULL,
+                "Including \"sub_b_one\" submodule into \"main_b\" failed.", NULL,
+                "Data model \"sub_b_one\" not found in local searchdirs.", NULL,
+                "Parsing submodule \"sub_b_one\" failed.", NULL,
+                "YANG 1.1 requires all submodules to be included from main module. But submodule \"sub_b_one\" includes "
+                "submodule \"sub_b_two\" which is not included by main module \"main_b\".", NULL,
+                "YANG version 1.1 expects all includes in main module, includes in submodules (sub_b_one) are not necessary.", NULL);
+    }
+
+    {
+        /* YANG 1.1 - all includes are in main_c, includes in submodules are not necessary, so expect warning */
+        struct module_clb_list list[] = {
+            {"main_c", "module main_c { yang-version 1.1; namespace urn:test:main_c; prefix mc; include sub_c_one; include sub_c_two;}"},
+            {"sub_c_one", "submodule sub_c_one { yang-version 1.1; belongs-to main_c { prefix mc; } include sub_c_two;}"},
+            {"sub_c_two", "submodule sub_c_two { yang-version 1.1; belongs-to main_c { prefix mc; } include sub_c_one;}"},
+            {NULL, NULL}
+        };
+
+        ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
+        mod = ly_ctx_load_module(UTEST_LYCTX, "main_c", NULL, NULL);
+        assert_non_null(mod);
+        assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->includes));
+        assert_false(mod->parsed->includes[1].injected);
+        /* result is ok, but log includes the warning */
+        CHECK_LOG_CTX("YANG version 1.1 expects all includes in main module, includes in submodules (sub_c_two) are not necessary.", NULL);
+    }
+}
+
+static void
+test_key_order(void **state)
+{
+    struct lys_module *mod;
+    const struct lysc_node *node;
+
+    struct module_clb_list list1[] = {
+        {"a", "module a {"
+            "yang-version 1.1;"
+            "namespace urn:test:a;"
+            "prefix a;"
+            "list l {"
+            "  key \"k1 k2\";"
+            "  leaf k2 {type string;}"
+            "  leaf k1 {type string;}"
+            "}"
+            "}"},
+        {NULL, NULL}
+    };
+
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list1);
+    mod = ly_ctx_load_module(UTEST_LYCTX, "a", NULL, NULL);
+    assert_non_null(mod);
+
+    node = lysc_node_child(mod->compiled->data);
+    assert_string_equal("k1", node->name);
+    node = node->next;
+    assert_string_equal("k2", node->name);
+
+    struct module_clb_list list2[] = {
+        {"b", "module b {"
+            "yang-version 1.1;"
+            "namespace urn:test:b;"
+            "prefix b;"
+            "list l {"
+            "  key \"k1 k2 k3 k4\";"
+            "  leaf k4 {type string;}"
+            "  container c {"
+            "    leaf l1 {type string;}"
+            "  }"
+            "  leaf k2 {type string;}"
+            "  leaf l2 {type string;}"
+            "  leaf k1 {type string;}"
+            "  leaf k3 {type string;}"
+            "}"
+            "}"},
+        {NULL, NULL}
+    };
+
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list2);
+    mod = ly_ctx_load_module(UTEST_LYCTX, "b", NULL, NULL);
+    assert_non_null(mod);
+
+    node = lysc_node_child(mod->compiled->data);
+    assert_string_equal("k1", node->name);
+    node = node->next;
+    assert_string_equal("k2", node->name);
+    node = node->next;
+    assert_string_equal("k3", node->name);
+    node = node->next;
+    assert_string_equal("k4", node->name);
+}
+
+static void
+test_disabled_enum(void **state)
+{
+    const char *str;
+
+    /* no enabled enum */
+    str = "module a {"
+            "yang-version 1.1;"
+            "namespace urn:test:a;"
+            "prefix a;"
+            "feature f;"
+            "leaf l {type enumeration {"
+            "  enum e1 {if-feature f;}"
+            "  enum e2 {if-feature f;}"
+            "}}"
+            "}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Enumeration type of node \"l\" without any (or all disabled) valid values.", "Schema location \"/a:l\".");
+
+    /* disabled default value */
+    str = "module a {"
+            "yang-version 1.1;"
+            "namespace urn:test:a;"
+            "prefix a;"
+            "feature f;"
+            "leaf l {"
+            "  type enumeration {"
+            "    enum e1 {if-feature f;}"
+            "    enum e2;"
+            "  }"
+            "  default e1;"
+            "}"
+            "}";
+    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
+    CHECK_LOG_CTX("Invalid default - value does not fit the type (Invalid enumeration value \"e1\".).",
+            "Schema location \"/a:l\".");
+}
+
+static void
+test_identity(void **state)
+{
+    struct lys_module *mod, *mod_imp;
+
+    /*
+     * parsing YANG
+     */
+    TEST_STMT_DUP(1, 0, "identity id", "description", "a", "b", "1");
+    TEST_STMT_DUP(1, 0, "identity id", "reference", "a", "b", "1");
+    TEST_STMT_DUP(1, 0, "identity id", "status", "current", "obsolete", "1");
+
+    /* full content */
+    TEST_SCHEMA_OK(1, 0, "identityone",
+            "identity test {base \"a\";base b; description text;reference \'another text\';status current; if-feature x;if-feature y; identityone:ext;}"
+            "identity a; identity b; extension ext; feature x; feature y;", mod);
+    assert_non_null(mod->parsed->identities);
+    assert_int_equal(3, LY_ARRAY_COUNT(mod->parsed->identities));
+
+    /* invalid substatement */
+    TEST_STMT_SUBSTM_ERR(0, "identity", "organization", "XXX");
+
+    /*
+     * parsing YIN
+     */
+    /* max subelems */
+    TEST_SCHEMA_OK(1, 1, "identityone-yin", "<identity name=\"ident-name\">"
+            "<if-feature name=\"iff\"/>"
+            "<base name=\"base-name\"/>"
+            "<status value=\"deprecated\"/>"
+            "<description><text>desc</text></description>"
+            "<reference><text>ref</text></reference>"
+            /* TODO yin-extension-prefix-compilation-bug "<myext:ext xmlns:myext=\"urn:libyang:test:identityone-yin\"/>" */
+            "</identity><extension name=\"ext\"/><identity name=\"base-name\"/><feature name=\"iff\"/>", mod);
+    assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->identities));
+    assert_string_equal(mod->parsed->identities[0].name, "ident-name");
+    assert_string_equal(mod->parsed->identities[0].bases[0], "base-name");
+    assert_string_equal(mod->parsed->identities[0].iffeatures[0].str, "iff");
+    assert_string_equal(mod->parsed->identities[0].dsc, "desc");
+    assert_string_equal(mod->parsed->identities[0].ref, "ref");
+    assert_true(mod->parsed->identities[0].flags & LYS_STATUS_DEPRC);
+    /*assert_string_equal(mod->parsed->identities[0].exts[0].name, "ext");
+    assert_non_null(mod->parsed->identities[0].exts[0].compiled);
+    assert_int_equal(mod->parsed->identities[0].exts[0].yin, 1);
+    assert_int_equal(mod->parsed->identities[0].exts[0].insubstmt_index, 0);
+    assert_int_equal(mod->parsed->identities[0].exts[0].insubstmt, LYEXT_SUBSTMT_SELF);*/
+
+    /* min subelems */
+    TEST_SCHEMA_OK(1, 1, "identitytwo-yin", "<identity name=\"ident-name\" />", mod);
+    assert_int_equal(1, LY_ARRAY_COUNT(mod->parsed->identities));
+    assert_string_equal(mod->parsed->identities[0].name, "ident-name");
+
+    /* invalid substatement */
+    TEST_SCHEMA_PARSE_ERR(0, 1, "inv", "<identity name=\"ident-name\"><if-feature name=\"iff\"/></identity>",
+            "Invalid sub-elemnt \"if-feature\" of \"identity\" element - "
+            "this sub-element is allowed only in modules with version 1.1 or newer.", "Line number 1.");
+
+    /*
+     * compiling
+     */
+    TEST_SCHEMA_OK(0, 0, "a", "identity a1;", mod_imp);
+    TEST_SCHEMA_OK(1, 0, "b", "import a {prefix a;}"
+            "identity b1; identity b2; identity b3 {base b1; base b:b2; base a:a1;}"
+            "identity b4 {base b:b1; base b3;}", mod);
+    assert_non_null(mod_imp->compiled);
+    assert_non_null(mod_imp->identities);
+    assert_non_null(mod->identities);
+    assert_non_null(mod_imp->identities[0].derived);
+    assert_int_equal(1, LY_ARRAY_COUNT(mod_imp->identities[0].derived));
+    assert_ptr_equal(mod_imp->identities[0].derived[0], &mod->identities[2]);
+    assert_non_null(mod->identities[0].derived);
+    assert_int_equal(2, LY_ARRAY_COUNT(mod->identities[0].derived));
+    assert_ptr_equal(mod->identities[0].derived[0], &mod->identities[2]);
+    assert_ptr_equal(mod->identities[0].derived[1], &mod->identities[3]);
+    assert_non_null(mod->identities[1].derived);
+    assert_int_equal(1, LY_ARRAY_COUNT(mod->identities[1].derived));
+    assert_ptr_equal(mod->identities[1].derived[0], &mod->identities[2]);
+    assert_non_null(mod->identities[2].derived);
+    assert_int_equal(1, LY_ARRAY_COUNT(mod->identities[2].derived));
+    assert_ptr_equal(mod->identities[2].derived[0], &mod->identities[3]);
+
+    TEST_SCHEMA_OK(1, 0, "c", "identity c2 {base c1;} identity c1;", mod);
+    assert_int_equal(1, LY_ARRAY_COUNT(mod->identities[1].derived));
+    assert_ptr_equal(mod->identities[1].derived[0], &mod->identities[0]);
+
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule inv_sub {belongs-to inv {prefix inv;} identity i1;}");
+    TEST_SCHEMA_ERR(0, 0, "inv", "identity i1 {base i2;}", "Unable to find base (i2) of identity \"i1\".", "/inv:{identity='i1'}");
+    TEST_SCHEMA_ERR(0, 0, "inv", "identity i1 {base i1;}", "Identity \"i1\" is derived from itself.", "/inv:{identity='i1'}");
+    TEST_SCHEMA_ERR(0, 0, "inv", "identity i1 {base i2;}identity i2 {base i3;}identity i3 {base i1;}",
+            "Identity \"i1\" is indirectly derived from itself.", "/inv:{identity='i3'}");
+
+    /* base in non-implemented module */
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb,
+            "module base {namespace \"urn\"; prefix b; identity i1; identity i2 {base i1;}}");
+    TEST_SCHEMA_OK(0, 0, "ident", "import base {prefix b;} identity ii {base b:i1;}", mod);
+
+    /* default value from non-implemented module */
+    TEST_SCHEMA_ERR(0, 0, "ident2", "import base {prefix b;} leaf l {type identityref {base b:i1;} default b:i2;}",
+            "Invalid default - value does not fit the type (Invalid identityref \"b:i2\" value"
+            " - identity found in non-implemented module \"base\".).", "Schema location \"/ident2:l\".");
+
+    /* default value in typedef from non-implemented module */
+    TEST_SCHEMA_ERR(0, 0, "ident2", "import base {prefix b;} typedef t1 {type identityref {base b:i1;} default b:i2;}"
+            "leaf l {type t1;}", "Invalid default - value does not fit the type (Invalid"
+            " identityref \"b:i2\" value - identity found in non-implemented module \"base\".).", "Schema location \"/ident2:l\".");
+
+    /*
+     * printing
+     */
+
+    /*
+     * cleanup
+     */
+}
+
+static void
+test_feature(void **state)
+{
+    struct lys_module *mod;
+    const struct lysp_feature *f;
+
+    /*
+     * parsing YANG
+     */
+
+    TEST_STMT_DUP(1, 0, "feature f", "description", "a", "b", "1");
+    TEST_STMT_DUP(1, 0, "feature f", "reference", "a", "b", "1");
+    TEST_STMT_DUP(1, 0, "feature f", "status", "current", "obsolete", "1");
+
+    /* full content */
+    TEST_SCHEMA_OK(1, 0, "featureone",
+            "feature test {description text;reference \'another text\';status current; if-feature x; if-feature y; featureone:ext;}"
+            "extension ext; feature x; feature y;", mod);
+    assert_non_null(mod->parsed->features);
+    assert_int_equal(3, LY_ARRAY_COUNT(mod->parsed->features));
+
+    /* invalid substatement */
+    TEST_STMT_SUBSTM_ERR(0, "feature", "organization", "XXX");
+
+    /*
+     * parsing YIN
+     */
+    /* max subelems */
+    TEST_SCHEMA_OK(0, 1, "featureone-yin", "<feature name=\"feature-name\">"
+            "<if-feature name=\"iff\"/>"
+            "<status value=\"deprecated\"/>"
+            "<description><text>desc</text></description>"
+            "<reference><text>ref</text></reference>"
+            /* TODO yin-extension-prefix-compilation-bug "<myext:ext xmlns:myext=\"urn:libyang:test:featureone-yin\"/>" */
+            "</feature><extension name=\"ext\"/><feature name=\"iff\"/>", mod);
+    assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->features));
+    assert_string_equal(mod->parsed->features[0].name, "feature-name");
+    assert_string_equal(mod->parsed->features[0].dsc, "desc");
+    assert_true(mod->parsed->features[0].flags & LYS_STATUS_DEPRC);
+    assert_string_equal(mod->parsed->features[0].iffeatures[0].str, "iff");
+    assert_string_equal(mod->parsed->features[0].ref, "ref");
+    /*assert_string_equal(mod->parsed->features[0].exts[0].name, "ext");
+    assert_int_equal(mod->parsed->features[0].exts[0].insubstmt_index, 0);
+    assert_int_equal(mod->parsed->features[0].exts[0].insubstmt, LYEXT_SUBSTMT_SELF);*/
+
+    /* min subelems */
+    TEST_SCHEMA_OK(0, 1, "featuretwo-yin", "<feature name=\"feature-name\"/>", mod)
+    assert_int_equal(1, LY_ARRAY_COUNT(mod->parsed->features));
+    assert_string_equal(mod->parsed->features[0].name, "feature-name");
+
+    /* invalid substatement */
+    TEST_SCHEMA_PARSE_ERR(0, 1, "inv", "<feature name=\"feature-name\"><organization><text>org</text></organization></feature>",
+            "Unexpected sub-element \"organization\" of \"feature\" element.", "Line number 1.");
+
+    /*
+     * compiling
+     */
+
+    TEST_SCHEMA_OK(1, 0, "a", "feature f1 {description test1;reference test2;status current;} feature f2; feature f3;\n"
+            "feature orfeature {if-feature \"f1 or f2\";}\n"
+            "feature andfeature {if-feature \"f1 and f2\";}\n"
+            "feature f6 {if-feature \"not f1\";}\n"
+            "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n"
+            "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n"
+            "feature f9 {if-feature \"not not f1\";}", mod);
+    assert_non_null(mod->parsed->features);
+    assert_int_equal(9, LY_ARRAY_COUNT(mod->parsed->features));
+
+    /* all features are disabled by default */
+    LY_ARRAY_FOR(mod->parsed->features, struct lysp_feature, f) {
+        assert_false(f->flags & LYS_FENABLED);
+    }
+
+    /* some invalid expressions */
+    TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f{if-feature f1;}",
+            "Invalid value \"f1\" of if-feature - unable to find feature \"f1\".", NULL);
+    TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2{if-feature 'f and';}",
+            "Invalid value \"f and\" of if-feature - unexpected end of expression.", NULL);
+    TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f{if-feature 'or';}",
+            "Invalid value \"or\" of if-feature - unexpected end of expression.", NULL);
+    TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2{if-feature '(f1';}",
+            "Invalid value \"(f1\" of if-feature - non-matching opening and closing parentheses.", NULL);
+    TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2{if-feature 'f1)';}",
+            "Invalid value \"f1)\" of if-feature - non-matching opening and closing parentheses.", NULL);
+    TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2{if-feature ---;}",
+            "Invalid value \"---\" of if-feature - unable to find feature \"---\".", NULL);
+    TEST_SCHEMA_PARSE_ERR(0, 0, "inv", "feature f1; feature f2{if-feature 'not f1';}",
+            "Invalid value \"not f1\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", NULL);
+
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule inv_sub {belongs-to inv {prefix inv;} feature f1;}");
+    TEST_SCHEMA_PARSE_ERR(0, 0, "inv", "feature f1 {if-feature f2;} feature f2 {if-feature f1;}",
+            "Feature \"f1\" is indirectly referenced from itself.", NULL);
+    TEST_SCHEMA_PARSE_ERR(0, 0, "inv", "feature f1 {if-feature f1;}",
+            "Feature \"f1\" is referenced from itself.", NULL);
+    TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f {if-feature ();}",
+            "Invalid value \"()\" of if-feature - number of features in expression does not match the required number of operands for the operations.", NULL);
+    TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f {if-feature 'f1(';}",
+            "Invalid value \"f1(\" of if-feature - non-matching opening and closing parentheses.", NULL);
+    TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f {if-feature 'and f1';}",
+            "Invalid value \"and f1\" of if-feature - missing feature/expression before \"and\" operation.", NULL);
+    TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f {if-feature 'f1 not ';}",
+            "Invalid value \"f1 not \" of if-feature - unexpected end of expression.", NULL);
+    TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f {if-feature 'f1 not not ';}",
+            "Invalid value \"f1 not not \" of if-feature - unexpected end of expression.", NULL);
+    TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2; feature f {if-feature 'or f1 f2';}",
+            "Invalid value \"or f1 f2\" of if-feature - missing feature/expression before \"or\" operation.", NULL);
+
+    /*
+     * printing
+     */
+
+    /*
+     * cleanup
+     */
+}
+
+static void
+test_extension_argument(void **state)
+{
+    struct lys_module *mod;
+    const char *mod_def_yang = "module a {\n"
+            "  namespace \"urn:a\";\n"
+            "  prefix a;\n\n"
+            "  extension e {\n"
+            "    argument name;\n"
+            "  }\n\n"
+            "  a:e \"aaa\";\n"
+            "}\n";
+    const char *mod_def_yin =
+            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+            "<module name=\"a\"\n"
+            "        xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
+            "        xmlns:a=\"urn:a\">\n"
+            "  <namespace uri=\"urn:a\"/>\n"
+            "  <prefix value=\"a\"/>\n"
+            "  <extension name=\"e\">\n"
+            "    <argument name=\"name\"/>\n"
+            "  </extension>\n"
+            "  <a:e name=\"aaa\"/>\n"
+            "</module>\n";
+    const char *mod_test_yin, *mod_test_yang;
+    char *printed;
+
+    mod_test_yang = "module b {\n"
+            "  namespace \"urn:b\";\n"
+            "  prefix b;\n\n"
+            "  import a {\n"
+            "    prefix a;\n"
+            "  }\n\n"
+            "  a:e \"xxx\";\n"
+            "}\n";
+    mod_test_yin =
+            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+            "<module name=\"b\"\n"
+            "        xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
+            "        xmlns:b=\"urn:b\"\n"
+            "        xmlns:a=\"urn:a\">\n"
+            "  <namespace uri=\"urn:b\"/>\n"
+            "  <prefix value=\"b\"/>\n"
+            "  <import module=\"a\">\n"
+            "    <prefix value=\"a\"/>\n"
+            "  </import>\n"
+            "  <a:e name=\"xxx\"/>\n"
+            "</module>\n";
+
+    /* from YANG */
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_def_yang);
+    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_test_yang, LYS_IN_YANG, &mod));
+    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
+    assert_string_equal(printed, mod_test_yang);
+    free(printed);
+
+    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
+    assert_string_equal(printed, mod_test_yin);
+    free(printed);
+
+    assert_non_null(mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
+    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
+    assert_string_equal(printed, mod_def_yang);
+    free(printed);
+
+    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
+    assert_string_equal(printed, mod_def_yin);
+    free(printed);
+
+    /* context reset */
+    ly_ctx_destroy(UTEST_LYCTX);
+    ly_ctx_new(NULL, 0, &UTEST_LYCTX);
+
+    /* from YIN */
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_def_yin);
+    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, &mod));
+    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
+    assert_string_equal(printed, mod_test_yang);
+    free(printed);
+
+    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
+    assert_string_equal(printed, mod_test_yin);
+    free(printed);
+
+    assert_non_null(mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
+    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
+    assert_string_equal(printed, mod_def_yang);
+    free(printed);
+
+    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
+    assert_string_equal(printed, mod_def_yin);
+    free(printed);
+}
+
+static void
+test_extension_argument_element(void **state)
+{
+    struct lys_module *mod;
+    const char *mod_def_yang = "module a {\n"
+            "  namespace \"urn:a\";\n"
+            "  prefix a;\n\n"
+            "  extension e {\n"
+            "    argument name {\n"
+            "      yin-element true;\n"
+            "    }\n"
+            "  }\n\n"
+            "  a:e \"aaa\";\n"
+            "}\n";
+    const char *mod_def_yin =
+            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+            "<module name=\"a\"\n"
+            "        xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
+            "        xmlns:a=\"urn:a\">\n"
+            "  <namespace uri=\"urn:a\"/>\n"
+            "  <prefix value=\"a\"/>\n"
+            "  <extension name=\"e\">\n"
+            "    <argument name=\"name\">\n"
+            "      <yin-element value=\"true\"/>\n"
+            "    </argument>\n"
+            "  </extension>\n"
+            "  <a:e>\n"
+            "    <a:name>aaa</a:name>\n"
+            "  </a:e>\n"
+            "</module>\n";
+    const char *mod_test_yin, *mod_test_yang;
+    char *printed;
+
+    mod_test_yang = "module b {\n"
+            "  namespace \"urn:b\";\n"
+            "  prefix b;\n\n"
+            "  import a {\n"
+            "    prefix a;\n"
+            "  }\n\n"
+            "  a:e \"xxx\";\n"
+            "}\n";
+    mod_test_yin =
+            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+            "<module name=\"b\"\n"
+            "        xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
+            "        xmlns:b=\"urn:b\"\n"
+            "        xmlns:a=\"urn:a\">\n"
+            "  <namespace uri=\"urn:b\"/>\n"
+            "  <prefix value=\"b\"/>\n"
+            "  <import module=\"a\">\n"
+            "    <prefix value=\"a\"/>\n"
+            "  </import>\n"
+            "  <a:e>\n"
+            "    <a:name>xxx</a:name>\n"
+            "  </a:e>\n"
+            "</module>\n";
+
+    /* from YANG */
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_def_yang);
+    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_test_yang, LYS_IN_YANG, &mod));
+    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
+    assert_string_equal(printed, mod_test_yang);
+    free(printed);
+
+    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
+    assert_string_equal(printed, mod_test_yin);
+    free(printed);
+
+    assert_non_null(mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
+    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
+    assert_string_equal(printed, mod_def_yang);
+    free(printed);
+
+    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
+    assert_string_equal(printed, mod_def_yin);
+    free(printed);
+
+    /* context reset */
+    ly_ctx_destroy(UTEST_LYCTX);
+    ly_ctx_new(NULL, 0, &UTEST_LYCTX);
+
+    /* from YIN */
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_def_yin);
+    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, &mod));
+    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
+    assert_string_equal(printed, mod_test_yang);
+    free(printed);
+
+    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
+    assert_string_equal(printed, mod_test_yin);
+    free(printed);
+
+    assert_non_null(mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
+    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
+    assert_string_equal(printed, mod_def_yang);
+    free(printed);
+
+    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
+    assert_string_equal(printed, mod_def_yin);
+    free(printed);
+
+    /* invalid */
+    mod_test_yang = "module x { namespace \"urn:x\"; prefix x; import a { prefix a; } a:e; }";
+    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yang, LYS_IN_YANG, NULL));
+    CHECK_LOG_CTX("Extension instance \"a:e\" misses argument element \"name\".", "/x:{extension='a:e'}");
+
+    mod_test_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+            "<module name=\"x\"\n"
+            "        xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
+            "        xmlns:x=\"urn:x\"\n"
+            "        xmlns:a=\"urn:a\">\n"
+            "  <namespace uri=\"urn:x\"/>\n"
+            "  <prefix value=\"x\"/>\n"
+            "  <import module=\"a\">\n"
+            "    <prefix value=\"a\"/>\n"
+            "  </import>\n\n"
+            "  <a:e/>\n"
+            "</module>\n";
+    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, NULL));
+    CHECK_LOG_CTX("Extension instance \"a:e\" misses argument element \"name\".", "/x:{extension='a:e'}");
+
+    mod_test_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+            "<module name=\"x\"\n"
+            "        xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
+            "        xmlns:x=\"urn:x\"\n"
+            "        xmlns:a=\"urn:a\">\n"
+            "  <namespace uri=\"urn:x\"/>\n"
+            "  <prefix value=\"x\"/>\n"
+            "  <import module=\"a\">\n"
+            "    <prefix value=\"a\"/>\n"
+            "  </import>\n\n"
+            "  <a:e name=\"xxx\"/>\n"
+            "</module>\n";
+    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, NULL));
+    CHECK_LOG_CTX("Extension instance \"a:e\" misses argument element \"name\".", "/x:{extension='a:e'}");
+
+    mod_test_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+            "<module name=\"x\"\n"
+            "        xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
+            "        xmlns:x=\"urn:x\"\n"
+            "        xmlns:a=\"urn:a\">\n"
+            "  <namespace uri=\"urn:x\"/>\n"
+            "  <prefix value=\"x\"/>\n"
+            "  <import module=\"a\">\n"
+            "    <prefix value=\"a\"/>\n"
+            "  </import>\n\n"
+            "  <a:e>\n"
+            "    <x:name>xxx</x:name>\n"
+            "  </a:e>\n"
+            "</module>\n";
+    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, NULL));
+    CHECK_LOG_CTX("Extension instance \"a:e\" element and its argument element \"name\" are expected in the same namespace, but they differ.",
+            "/x:{extension='a:e'}");
+
+    mod_test_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+            "<module name=\"x\"\n"
+            "        xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
+            "        xmlns:x=\"urn:x\"\n"
+            "        xmlns:a=\"urn:a\">\n"
+            "  <namespace uri=\"urn:x\"/>\n"
+            "  <prefix value=\"x\"/>\n"
+            "  <import module=\"a\">\n"
+            "    <prefix value=\"a\"/>\n"
+            "  </import>\n\n"
+            "  <a:e>\n"
+            "    <a:value>xxx</a:value>\n"
+            "  </a:e>\n"
+            "</module>\n";
+    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, NULL));
+    CHECK_LOG_CTX("Extension instance \"a:e\" expects argument element \"name\" as its first XML child, but \"value\" element found.",
+            "/x:{extension='a:e'}");
+
+}
+
+static void
+test_extension_compile(void **state)
+{
+    struct lys_module *mod;
+    struct lysc_ctx cctx = {0};
+    struct lysp_ext_instance ext_p = {0};
+    struct lysp_stmt child = {0};
+    struct lysc_ext_instance ext_c = {0};
+    struct lysc_ext_substmt *substmt;
+    LY_ERR rc = LY_SUCCESS;
+
+    /* current module, whatever */
+    mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang");
+    assert_true(mod);
+
+    /* compile context */
+    cctx.ctx = UTEST_LYCTX;
+    cctx.cur_mod = mod;
+    cctx.pmod = mod->parsed;
+    cctx.path_len = 1;
+    cctx.path[0] = '/';
+
+    /* parsed ext instance */
+    lydict_insert(UTEST_LYCTX, "pref:my-ext", 0, &ext_p.name);
+    ext_p.format = LY_VALUE_JSON;
+    ext_p.parent_stmt = LY_STMT_MODULE;
+
+    /* compiled ext instance */
+    ext_c.parent_stmt = ext_p.parent_stmt;
+    // ext_c.parent =
+    LY_ARRAY_NEW_GOTO(UTEST_LYCTX, ext_c.substmts, substmt, rc, cleanup);
+
+    /*
+     * error-message
+     */
+    ext_p.child = &child;
+    lydict_insert(UTEST_LYCTX, "error-message", 0, &child.stmt);
+    lydict_insert(UTEST_LYCTX, "my error", 0, &child.arg);
+    child.format = LY_VALUE_JSON;
+    child.kw = LY_STMT_ERROR_MESSAGE;
+
+    substmt->stmt = LY_STMT_ERROR_MESSAGE;
+    substmt->cardinality = LY_STMT_CARD_OPT;
+    substmt->storage = &ext_c.data;
+
+    /* compile */
+    assert_int_equal(LY_SUCCESS, lys_compile_extension_instance(&cctx, &ext_p, &ext_c));
+
+    /* check */
+    assert_string_equal(ext_c.data, "my error");
+
+cleanup:
+    lydict_remove(UTEST_LYCTX, ext_p.name);
+    lydict_remove(UTEST_LYCTX, child.stmt);
+    lydict_remove(UTEST_LYCTX, child.arg);
+    LY_ARRAY_FREE(ext_c.substmts);
+
+    lydict_remove(UTEST_LYCTX, ext_c.data);
+    if (rc) {
+        fail();
+    }
+}
+
+static void
+test_ext_recursive(void **state)
+{
+    const char *mod_base_yang, *mod_imp_yang, *mod_base_yin, *mod_imp_yin;
+
+    mod_imp_yang = "module b {\n"
+            "  namespace \"urn:b\";\n"
+            "  prefix b;\n\n"
+            "  extension use-in {\n"
+            "    argument name {\n"
+            "      b:arg-type {\n"
+            "        type string;\n"
+            "      }\n"
+            "    }\n"
+            "    b:use-in \"extension\";\n"
+            "    b:occurence \"*\";\n"
+            "  }\n"
+            "\n"
+            "  extension substatement {\n"
+            "    argument name {\n"
+            "      b:arg-type {\n"
+            "        type string;\n"
+            "      }\n"
+            "    }\n"
+            "    b:use-in \"extension\";\n"
+            "    b:occurence \"*\";\n"
+            "    b:substatement \"b:occurence\";\n"
+            "  }\n"
+            "\n"
+            "  extension arg-type {\n"
+            "    b:use-in \"argument\";\n"
+            "    b:substatement \"type\" {\n"
+            "      b:occurence \"1\";\n"
+            "    }\n"
+            "    b:substatement \"default\";\n"
+            "  }\n"
+            "\n"
+            "  extension occurence {\n"
+            "    argument value {\n"
+            "      b:arg-type {\n"
+            "        type enumeration {\n"
+            "          enum \"?\";\n"
+            "          enum \"*\";\n"
+            "          enum \"+\";\n"
+            "          enum \"1\";\n"
+            "        }\n"
+            "      }\n"
+            "    }\n"
+            "    b:use-in \"extension\";\n"
+            "  }\n"
+            "}\n";
+
+    mod_imp_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+            "<module name=\"b\"\n"
+            "        xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
+            "        xmlns:b=\"urn:b\"\n"
+            "        xmlns:a=\"urn:a\">\n"
+            "  <namespace uri=\"urn:b\"/>\n"
+            "  <prefix value=\"b\"/>\n"
+            "  <import module=\"a\">\n"
+            "    <prefix value=\"a\"/>\n"
+            "  </import>\n\n"
+            "  <a:e name=\"xxx\"/>\n"
+            "</module>\n";
+
+    mod_base_yang = "module a {\n"
+            "  namespace \"urn:a\";\n"
+            "  prefix a;\n\n"
+            "  import b {\n"
+            "    prefix b;\n"
+            "  }\n"
+            "\n"
+            "  extension abstract {\n"
+            "    b:use-in \"identity\";\n"
+            "  }\n"
+            "\n"
+            "  identity mount-id;\n"
+            "\n"
+            "  identity yang-lib-id {\n"
+            "    base mount-id;\n"
+            "    a:abstract;\n"
+            "  }\n"
+            "}\n";
+
+    mod_base_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+            "<module name=\"a\"\n"
+            "        xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
+            "        xmlns:a=\"urn:a\">\n"
+            "  <namespace uri=\"urn:a\"/>\n"
+            "  <prefix value=\"a\"/>\n\n"
+            "  <extension name=\"e\">\n"
+            "    <argument name=\"name\"/>\n"
+            "  </extension>\n\n"
+            "  <a:e name=\"aaa\"/>\n"
+            "</module>\n";
+
+    /* from YANG */
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_imp_yang);
+    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_base_yang, LYS_IN_YANG, NULL));
+
+    /* context reset */
+    ly_ctx_destroy(UTEST_LYCTX);
+    ly_ctx_new(NULL, 0, &UTEST_LYCTX);
+
+    /* from YIN */
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_imp_yin);
+    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_base_yin, LYS_IN_YIN, NULL));
+}
 
 int
 main(void)
 {
     const struct CMUnitTest tests[] = {
-        /** test_schema_common.c */
         UTEST(test_getnext),
         UTEST(test_date),
         UTEST(test_revisions),
@@ -78,12 +1863,8 @@
         UTEST(test_includes),
         UTEST(test_key_order),
         UTEST(test_disabled_enum),
-
-        /** test_schema_stmts.c */
         UTEST(test_identity),
         UTEST(test_feature),
-
-        /** test_schema_extensions.c */
         UTEST(test_extension_argument),
         UTEST(test_extension_argument_element),
         UTEST(test_extension_compile),
diff --git a/tests/utests/schema/test_schema.h b/tests/utests/schema/test_schema.h
deleted file mode 100644
index b7588dc..0000000
--- a/tests/utests/schema/test_schema.h
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
- * @file test_schema.h
- * @author: Radek Krejci <rkrejci@cesnet.cz>
- * @brief macros for schema tests
- *
- * Copyright (c) 2018-2020 CESNET, z.s.p.o.
- *
- * This source code is licensed under BSD 3-Clause License (the "License").
- * You may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     https://opensource.org/licenses/BSD-3-Clause
- */
-
-#ifndef TESTS_UTESTS_SCHEMA_TEST_SCHEMA_H_
-#define TESTS_UTESTS_SCHEMA_TEST_SCHEMA_H_
-
-#include "utests.h"
-
-#include "log.h"
-#include "parser_schema.h"
-#include "set.h"
-#include "tests_config.h"
-
-LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
-        const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT * format,
-        const char **module_data, void (**free_module_data)(void *model_data, void *user_data));
-
-#define TEST_YANG_MODULE_10(MOD_NAME, MOD_PREFIX, MOD_NS, CONTENT) \
-    "module "MOD_NAME" { namespace "MOD_NS"; prefix "MOD_PREFIX"; "CONTENT"}"
-
-#define TEST_YANG_MODULE_11(MOD_NAME, MOD_PREFIX, MOD_NS, CONTENT) \
-    "module "MOD_NAME" {yang-version 1.1; namespace "MOD_NS"; prefix "MOD_PREFIX"; "CONTENT"}"
-
-#define TEST_YIN_MODULE_10(MOD_NAME, MOD_PREFIX, MOD_NS, CONTENT) \
-    "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\""MOD_NAME"\">" \
-    "<namespace uri=\""MOD_NS"\"/><prefix value=\""MOD_PREFIX"\"/>"CONTENT"</module>"
-
-#define TEST_YIN_MODULE_11(MOD_NAME, MOD_PREFIX, MOD_NS, CONTENT) \
-    "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\""MOD_NAME"\"><yang-version value=\"1.1\"/>" \
-    "<namespace uri=\""MOD_NS"\"/><prefix value=\""MOD_PREFIX"\"/>"CONTENT"</module>"
-
-#define TEST_SCHEMA_STR(RFC7950, YIN, MOD_NAME, CONTENT, STR) \
-    if (YIN) { \
-        if (RFC7950) { \
-            STR = TEST_YIN_MODULE_11(MOD_NAME, MOD_NAME, "urn:libyang:test:"MOD_NAME, CONTENT); \
-        } else { \
-            STR = TEST_YIN_MODULE_10(MOD_NAME, MOD_NAME, "urn:libyang:test:"MOD_NAME, CONTENT); \
-        } \
-    } else { /* YANG */ \
-        if (RFC7950) { \
-            STR = TEST_YANG_MODULE_11(MOD_NAME, MOD_NAME, "urn:libyang:test:"MOD_NAME, CONTENT); \
-        } else { \
-            STR = TEST_YANG_MODULE_10(MOD_NAME, MOD_NAME, "urn:libyang:test:"MOD_NAME, CONTENT); \
-        } \
-    }
-
-#define TEST_SCHEMA_OK(RFC7950, YIN, MOD_NAME, CONTENT, RESULT) \
-    { \
-    const char *test_str__; \
-    TEST_SCHEMA_STR(RFC7950, YIN, MOD_NAME, CONTENT, test_str__) \
-    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, test_str__, YIN ? LYS_IN_YIN : LYS_IN_YANG, &(RESULT))); \
-    }
-
-#define TEST_SCHEMA_ERR(RFC7950, YIN, MOD_NAME, CONTENT, ERRMSG, ERRPATH) \
-    { \
-    const char *test_str__; \
-    TEST_SCHEMA_STR(RFC7950, YIN, MOD_NAME, CONTENT, test_str__) \
-    assert_int_not_equal(lys_parse_mem(UTEST_LYCTX, test_str__, YIN ? LYS_IN_YIN : LYS_IN_YANG, NULL), LY_SUCCESS); \
-    CHECK_LOG_CTX(ERRMSG, ERRPATH); \
-    }
-
-#define TEST_SCHEMA_PARSE_ERR(RFC7950, YIN, MOD_NAME, CONTENT, ERRMSG, ERRPATH) \
-    { \
-    const char *test_str__; \
-    TEST_SCHEMA_STR(RFC7950, YIN, MOD_NAME, CONTENT, test_str__) \
-    assert_int_not_equal(lys_parse_mem(UTEST_LYCTX, test_str__, YIN ? LYS_IN_YIN : LYS_IN_YANG, NULL), LY_SUCCESS); \
-    CHECK_LOG_CTX("Parsing module \""MOD_NAME"\" failed.", NULL, ERRMSG, ERRPATH); \
-    }
-
-#define TEST_STMT_DUP(RFC7950, YIN, STMT, MEMBER, VALUE1, VALUE2, LINE) \
-    if (YIN) { \
-        TEST_SCHEMA_PARSE_ERR(RFC7950, YIN, "dup", "", "Duplicate keyword \""MEMBER"\".", "Line number "LINE"."); \
-    } else { \
-        TEST_SCHEMA_PARSE_ERR(RFC7950, YIN, "dup", STMT"{"MEMBER" "VALUE1";"MEMBER" "VALUE2";}", \
-                        "Duplicate keyword \""MEMBER"\".", "Line number "LINE"."); \
-    }
-
-#define TEST_STMT_SUBSTM_ERR(RFC7950, STMT, SUBSTMT, VALUE) ;\
-        TEST_SCHEMA_PARSE_ERR(RFC7950, 0, "inv", STMT" test {"SUBSTMT" "VALUE";}", \
-                        "Invalid keyword \""SUBSTMT"\" as a child of \""STMT"\".", "Line number 1.");
-
-#endif /* TESTS_UTESTS_SCHEMA_TEST_SCHEMA_H_ */
diff --git a/tests/utests/schema/test_schema_common.c b/tests/utests/schema/test_schema_common.c
deleted file mode 100644
index 42ec7c0..0000000
--- a/tests/utests/schema/test_schema_common.c
+++ /dev/null
@@ -1,1094 +0,0 @@
-/*
- * @file test_schema_common.c
- * @author: Radek Krejci <rkrejci@cesnet.cz>
- * @brief unit tests for functions from common.c
- *
- * Copyright (c) 2018 CESNET, z.s.p.o.
- *
- * This source code is licensed under BSD 3-Clause License (the "License").
- * You may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     https://opensource.org/licenses/BSD-3-Clause
- */
-#include "test_schema.h"
-
-#include <string.h>
-
-#include "compat.h"
-#include "context.h"
-#include "log.h"
-#include "tree_edit.h"
-#include "tree_schema.h"
-#include "tree_schema_internal.h"
-
-struct module_clb_list {
-    const char *name;
-    const char *data;
-};
-
-static LY_ERR
-module_clb(const char *mod_name, const char *UNUSED(mod_rev), const char *submod_name,
-        const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
-        const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
-{
-    struct module_clb_list *list = (struct module_clb_list *)user_data;
-
-    for (unsigned int i = 0; list[i].data; i++) {
-
-        if ((submod_name && !strcmp(list[i].name, submod_name)) ||
-                (!submod_name && mod_name && !strcmp(list[i].name, mod_name))) {
-            *module_data = list[i].data;
-            *format = LYS_IN_YANG;
-            *free_module_data = NULL;
-            return LY_SUCCESS;
-        }
-    }
-    return LY_EINVAL;
-}
-
-void
-test_getnext(void **state)
-{
-    struct lys_module *mod;
-    const struct lysc_node *node = NULL, *four;
-    const struct lysc_node_container *cont;
-    const struct lysc_action *rpc;
-
-    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {yang-version 1.1; namespace urn:a;prefix a;"
-            "container a { container one {presence test;} leaf two {type string;} leaf-list three {type string;}"
-            "  list four {config false;} choice x { leaf five {type string;} case y {leaf six {type string;}}}"
-            "  anyxml seven; action eight {input {leaf eight-input {type string;}} output {leaf eight-output {type string;}}}"
-            "  notification nine {leaf nine-data {type string;}}}"
-            "leaf b {type string;} leaf-list c {type string;} list d {config false;}"
-            "choice x { case empty-x { choice empty-xc { case nothing;}} leaf e {type string;} case y {leaf f {type string;}}} anyxml g;"
-            "rpc h {input {leaf h-input {type string;}} output {leaf h-output {type string;}}}"
-            "rpc i;"
-            "notification j {leaf i-data {type string;}}"
-            "notification k;}", LYS_IN_YANG, &mod));
-    assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
-    assert_string_equal("a", node->name);
-    cont = (const struct lysc_node_container *)node;
-    assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
-    assert_string_equal("b", node->name);
-    assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
-    assert_string_equal("c", node->name);
-    assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
-    assert_string_equal("d", node->name);
-    assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
-    assert_string_equal("e", node->name);
-    assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
-    assert_string_equal("f", node->name);
-    assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
-    assert_string_equal("g", node->name);
-    assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
-    assert_string_equal("h", node->name);
-    rpc = (const struct lysc_action *)node;
-    assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
-    assert_string_equal("i", node->name);
-    assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
-    assert_string_equal("j", node->name);
-    assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
-    assert_string_equal("k", node->name);
-    assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
-    /* Inside container */
-    assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
-    assert_string_equal("one", node->name);
-    assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
-    assert_string_equal("two", node->name);
-    assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
-    assert_string_equal("three", node->name);
-    assert_non_null(node = four = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
-    assert_string_equal("four", node->name);
-    assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
-    assert_string_equal("five", node->name);
-    assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
-    assert_string_equal("six", node->name);
-    assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
-    assert_string_equal("seven", node->name);
-    assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
-    assert_string_equal("eight", node->name);
-    assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
-    assert_string_equal("nine", node->name);
-    assert_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
-    /* Inside RPC */
-    assert_non_null(node = lys_getnext(node, (const struct lysc_node *)rpc, mod->compiled, 0));
-    assert_string_equal("h-input", node->name);
-    assert_null(node = lys_getnext(node, (const struct lysc_node *)rpc, mod->compiled, 0));
-
-    /* options */
-    assert_non_null(node = lys_getnext(four, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE));
-    assert_string_equal("x", node->name);
-    assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE));
-    assert_string_equal("seven", node->name);
-
-    assert_non_null(node = lys_getnext(four, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_NOCHOICE));
-    assert_string_equal("seven", node->name);
-
-    assert_non_null(node = lys_getnext(four, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
-    assert_string_equal("five", node->name);
-    assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
-    assert_string_equal("y", node->name);
-    assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
-    assert_string_equal("seven", node->name);
-
-    assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT));
-    assert_string_equal("one", node->name);
-
-    assert_non_null(node = lys_getnext(NULL, (const struct lysc_node *)rpc, mod->compiled, LYS_GETNEXT_OUTPUT));
-    assert_string_equal("h-output", node->name);
-    assert_null(node = lys_getnext(node, (const struct lysc_node *)rpc, mod->compiled, LYS_GETNEXT_OUTPUT));
-
-    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module c {namespace urn:c;prefix c; rpc c;}", LYS_IN_YANG, &mod));
-    assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
-    assert_string_equal("c", node->name);
-    assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
-
-    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module d {namespace urn:d;prefix d; notification d;}", LYS_IN_YANG, &mod));
-    assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
-    assert_string_equal("d", node->name);
-    assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
-
-    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module e {namespace urn:e;prefix e; container c {container cc;} leaf a {type string;}}", LYS_IN_YANG, &mod));
-    assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
-    assert_string_equal("c", node->name);
-    assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT));
-    assert_string_equal("a", node->name);
-}
-
-void
-test_date(void **state)
-{
-    assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date"));
-    CHECK_LOG("Invalid argument date (lysp_check_date()).", NULL);
-    assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date"));
-    CHECK_LOG("Invalid argument date_len (lysp_check_date()).", NULL);
-    assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date"));
-    CHECK_LOG("Invalid value \"nonsencexx\" of \"date\".", NULL);
-    assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date"));
-    CHECK_LOG("Invalid value \"123x-11-11\" of \"date\".", NULL);
-    assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date"));
-    CHECK_LOG("Invalid value \"2018-13-11\" of \"date\".", NULL);
-    assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date"));
-    CHECK_LOG("Invalid value \"2018-11-41\" of \"date\".", NULL);
-    assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date"));
-    CHECK_LOG("Invalid value \"2018-02-29\" of \"date\".", NULL);
-    assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date"));
-    CHECK_LOG("Invalid value \"2018.02-28\" of \"date\".", NULL);
-    assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02.28", 10, "date"));
-    CHECK_LOG("Invalid value \"2018-02.28\" of \"date\".", NULL);
-
-    assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date"));
-    assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date"));
-    assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date"));
-}
-
-void
-test_revisions(void **state)
-{
-    struct lysp_revision *revs = NULL, *rev;
-
-    /* no error, it just does nothing */
-    lysp_sort_revisions(NULL);
-    CHECK_LOG(NULL, NULL);
-
-    /* revisions are stored in wrong order - the newest is the last */
-    LY_ARRAY_NEW_RET(NULL, revs, rev, );
-    strcpy(rev->date, "2018-01-01");
-    LY_ARRAY_NEW_RET(NULL, revs, rev, );
-    strcpy(rev->date, "2018-12-31");
-
-    assert_int_equal(2, LY_ARRAY_COUNT(revs));
-    assert_string_equal("2018-01-01", &revs[0]);
-    assert_string_equal("2018-12-31", &revs[1]);
-    /* the order should be fixed, so the newest revision will be the first in the array */
-    lysp_sort_revisions(revs);
-    assert_string_equal("2018-12-31", &revs[0]);
-    assert_string_equal("2018-01-01", &revs[1]);
-
-    LY_ARRAY_FREE(revs);
-}
-
-void
-test_collision_typedef(void **state)
-{
-    const char *str;
-    char *submod;
-    struct module_clb_list list[3] = {0};
-
-    list[0].name = "asub";
-    list[1].name = "bsub";
-
-    /* collision with a built-in type */
-    str = "module a {namespace urn:a; prefix a; typedef binary {type string;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"binary\" of typedef statement - name collision with a built-in type.", NULL);
-    str = "module a {namespace urn:a; prefix a; typedef bits {type string;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"bits\" of typedef statement - name collision with a built-in type.", NULL);
-    str = "module a {namespace urn:a; prefix a; typedef boolean {type string;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"boolean\" of typedef statement - name collision with a built-in type.", NULL);
-    str = "module a {namespace urn:a; prefix a; typedef decimal64 {type string;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"decimal64\" of typedef statement - name collision with a built-in type.", NULL);
-    str = "module a {namespace urn:a; prefix a; typedef empty {type string;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"empty\" of typedef statement - name collision with a built-in type.", NULL);
-    str = "module a {namespace urn:a; prefix a; typedef enumeration {type string;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"enumeration\" of typedef statement - name collision with a built-in type.", NULL);
-    str = "module a {namespace urn:a; prefix a; typedef int8 {type string;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"int8\" of typedef statement - name collision with a built-in type.", NULL);
-    str = "module a {namespace urn:a; prefix a; typedef int16 {type string;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"int16\" of typedef statement - name collision with a built-in type.", NULL);
-    str = "module a {namespace urn:a; prefix a; typedef int32 {type string;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"int32\" of typedef statement - name collision with a built-in type.", NULL);
-    str = "module a {namespace urn:a; prefix a; typedef int64 {type string;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"int64\" of typedef statement - name collision with a built-in type.", NULL);
-    str = "module a {namespace urn:a; prefix a; typedef instance-identifier {type string;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"instance-identifier\" of typedef statement - name collision with a built-in type.", NULL);
-    str = "module a {namespace urn:a; prefix a; typedef identityref {type string;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"identityref\" of typedef statement - name collision with a built-in type.", NULL);
-    str = "module a {namespace urn:a; prefix a; typedef leafref {type string;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"leafref\" of typedef statement - name collision with a built-in type.", NULL);
-    str = "module a {namespace urn:a; prefix a; typedef string {type int8;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"string\" of typedef statement - name collision with a built-in type.", NULL);
-    str = "module a {namespace urn:a; prefix a; typedef union {type string;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"union\" of typedef statement - name collision with a built-in type.", NULL);
-    str = "module a {namespace urn:a; prefix a; typedef uint8 {type string;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"uint8\" of typedef statement - name collision with a built-in type.", NULL);
-    str = "module a {namespace urn:a; prefix a; typedef uint16 {type string;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"uint16\" of typedef statement - name collision with a built-in type.", NULL);
-    str = "module a {namespace urn:a; prefix a; typedef uint32 {type string;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"uint32\" of typedef statement - name collision with a built-in type.", NULL);
-    str = "module a {namespace urn:a; prefix a; typedef uint64 {type string;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"uint64\" of typedef statement - name collision with a built-in type.", NULL);
-
-    str = "module mytypes {namespace urn:types; prefix t; typedef binary_ {type string;} typedef bits_ {type string;} typedef boolean_ {type string;} "
-            "typedef decimal64_ {type string;} typedef empty_ {type string;} typedef enumeration_ {type string;} typedef int8_ {type string;} typedef int16_ {type string;}"
-            "typedef int32_ {type string;} typedef int64_ {type string;} typedef instance-identifier_ {type string;} typedef identityref_ {type string;}"
-            "typedef leafref_ {type string;} typedef string_ {type int8;} typedef union_ {type string;} typedef uint8_ {type string;} typedef uint16_ {type string;}"
-            "typedef uint32_ {type string;} typedef uint64_ {type string;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
-
-    /* collision in node's scope */
-    str = "module a {namespace urn:a; prefix a; container c {typedef y {type int8;} typedef y {type string;}}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"y\" of typedef statement - name collision with sibling type.", NULL);
-
-    /* collision with parent node */
-    str = "module a {namespace urn:a; prefix a; container c {container d {typedef y {type int8;}} typedef y {type string;}}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"y\" of typedef statement - name collision with another scoped type.", NULL);
-
-    /* collision with module's top-level */
-    str = "module a {namespace urn:a; prefix a; typedef x {type string;} container c {typedef x {type int8;}}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"x\" of typedef statement - scoped type collide with a top-level type.", NULL);
-
-    /* collision of submodule's node with module's top-level */
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} container c {typedef x {type string;}}}");
-    str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"x\" of typedef statement - scoped type collide with a top-level type.", NULL);
-
-    /* collision of module's node with submodule's top-level */
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type int8;}}");
-    str = "module a {namespace urn:a; prefix a; include b; container c {typedef x {type string;}}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"x\" of typedef statement - scoped type collide with a top-level type.", NULL);
-
-    /* collision of submodule's node with another submodule's top-level */
-    str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}";
-    list[0].data = "submodule asub {belongs-to a {prefix a;} typedef g {type int;}}";
-    list[1].data = "submodule bsub {belongs-to a {prefix a;} container c {typedef g {type int;}}}";
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
-    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"g\" of typedef statement - scoped type collide with a top-level type.", NULL);
-
-    /* collision of module's top-levels */
-    str = "module a {namespace urn:a; prefix a; typedef test {type string;} typedef test {type int8;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"test\" of typedef statement - name collision with another top-level type.", NULL);
-
-    /* collision of submodule's top-levels */
-    submod = "submodule asub {belongs-to a {prefix a;} typedef g {type int;} typedef g {type int;}}";
-    str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub;}";
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod);
-    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"g\" of typedef statement - name collision with another top-level type.", NULL);
-
-    /* collision of module's top-level with submodule's top-levels */
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type string;}}");
-    str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"x\" of typedef statement - name collision with another top-level type.", NULL);
-
-    /* collision of submodule's top-level with another submodule's top-levels */
-    str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}";
-    list[0].data = "submodule asub {belongs-to a {prefix a;} typedef g {type int;}}";
-    list[1].data = "submodule bsub {belongs-to a {prefix a;} typedef g {type int;}}";
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
-    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"g\" of typedef statement - name collision with another top-level type.", NULL);
-
-    /* error in type-stmt */
-    str = "module a {namespace urn:a; prefix a; container c {typedef x {type t{}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Unexpected end-of-input.", "Line number 1.");
-    UTEST_LOG_CLEAN;
-
-    /* no collision if the same names are in different scope */
-    str = "module a {yang-version 1.1; namespace urn:a; prefix a;"
-            "container c {typedef g {type int;}} container d {typedef g {type int;}}}";
-    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
-}
-
-void
-test_collision_grouping(void **state)
-{
-    const char *str;
-    char *submod;
-    struct module_clb_list list[3] = {0};
-
-    list[0].name = "asub";
-    list[1].name = "bsub";
-
-    /* collision in node's scope */
-    str = "module a {namespace urn:a; prefix a; container c {grouping y; grouping y;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"y\" of grouping statement - name collision with sibling grouping.", NULL);
-
-    /* collision with parent node */
-    str = "module a {namespace urn:a; prefix a; container c {container d {grouping y;} grouping y;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"y\" of grouping statement - name collision with another scoped grouping.", NULL);
-
-    /* collision with module's top-level */
-    str = "module a {namespace urn:a; prefix a; grouping x; container c {grouping x;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"x\" of grouping statement - scoped grouping collide with a top-level grouping.", NULL);
-
-    /* collision of submodule's node with module's top-level */
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} container c {grouping x;}}");
-    str = "module a {namespace urn:a; prefix a; include b; grouping x;}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"x\" of grouping statement - scoped grouping collide with a top-level grouping.", NULL);
-
-    /* collision of module's node with submodule's top-level */
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} grouping x;}");
-    str = "module a {namespace urn:a; prefix a; include b; container c {grouping x;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"x\" of grouping statement - scoped grouping collide with a top-level grouping.", NULL);
-
-    /* collision of submodule's node with another submodule's top-level */
-    str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}";
-    list[0].data = "submodule asub {belongs-to a {prefix a;} grouping g;}";
-    list[1].data = "submodule bsub {belongs-to a {prefix a;} container c {grouping g;}}";
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
-    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"g\" of grouping statement - scoped grouping collide with a top-level grouping.", NULL);
-
-    /* collision of module's top-levels */
-    str = "module a {namespace urn:a; prefix a; grouping test; grouping test;}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"test\" of grouping statement - name collision with another top-level grouping.", NULL);
-
-    /* collision of submodule's top-levels */
-    submod = "submodule asub {belongs-to a {prefix a;} grouping g; grouping g;}";
-    str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub;}";
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod);
-    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"g\" of grouping statement - name collision with another top-level grouping.", NULL);
-
-    /* collision of module's top-level with submodule's top-levels */
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} grouping x;}");
-    str = "module a {namespace urn:a; prefix a; include b; grouping x;}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"x\" of grouping statement - name collision with another top-level grouping.", NULL);
-
-    /* collision of submodule's top-level with another submodule's top-levels */
-    str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}";
-    list[0].data = "submodule asub {belongs-to a {prefix a;} grouping g;}";
-    list[1].data = "submodule bsub {belongs-to a {prefix a;} grouping g;}";
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
-    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"g\" of grouping statement - name collision with another top-level grouping.", NULL);
-
-    /* collision in nested groupings, top-level */
-    str = "module a {namespace urn:a; prefix a; grouping g {grouping g;}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"g\" of grouping statement - scoped grouping collide with a top-level grouping.", NULL);
-
-    /* collision in nested groupings, in node */
-    str = "module a {namespace urn:a; prefix a; container c {grouping g {grouping g;}}}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"g\" of grouping statement - name collision with another scoped grouping.", NULL);
-
-    /* no collision if the same names are in different scope */
-    str = "module a {yang-version 1.1; namespace urn:a; prefix a;"
-            "container c {grouping g;} container d {grouping g;}}";
-    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
-}
-
-void
-test_collision_identity(void **state)
-{
-    const char *str;
-    char *submod;
-    struct module_clb_list list[3] = {0};
-
-    list[0].name = "asub";
-    list[1].name = "bsub";
-
-    /* collision of module's top-levels */
-    str = "module a {yang-version 1.1; namespace urn:a; prefix a; identity g; identity g;}";
-    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"g\" of identity statement - name collision with another top-level identity.", NULL);
-
-    /* collision of submodule's top-levels */
-    submod = "submodule asub {belongs-to a {prefix a;} identity g; identity g;}";
-    str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub;}";
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod);
-    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"g\" of identity statement - name collision with another top-level identity.", NULL);
-
-    /* collision of module's top-level with submodule's top-levels */
-    submod = "submodule asub {belongs-to a {prefix a;} identity g;}";
-    str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; identity g;}";
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod);
-    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"g\" of identity statement - name collision with another top-level identity.", NULL);
-
-    /* collision of submodule's top-level with another submodule's top-levels */
-    str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}";
-    list[0].data = "submodule asub {belongs-to a {prefix a;} identity g;}";
-    list[1].data = "submodule bsub {belongs-to a {prefix a;} identity g;}";
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
-    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"g\" of identity statement - name collision with another top-level identity.", NULL);
-}
-
-void
-test_collision_feature(void **state)
-{
-    const char *str;
-    char *submod;
-    struct module_clb_list list[3] = {0};
-
-    list[0].name = "asub";
-    list[1].name = "bsub";
-
-    /* collision of module's top-levels */
-    str = "module a {yang-version 1.1; namespace urn:a; prefix a; feature g; feature g;}";
-    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"g\" of feature statement - name collision with another top-level feature.", NULL);
-
-    /* collision of submodule's top-levels */
-    submod = "submodule asub {belongs-to a {prefix a;} feature g; feature g;}";
-    str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub;}";
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod);
-    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"g\" of feature statement - name collision with another top-level feature.", NULL);
-
-    /* collision of module's top-level with submodule's top-levels */
-    submod = "submodule asub {belongs-to a {prefix a;} feature g;}";
-    str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; feature g;}";
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod);
-    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"g\" of feature statement - name collision with another top-level feature.", NULL);
-
-    /* collision of submodule's top-level with another submodule's top-levels */
-    str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}";
-    list[0].data = "submodule asub {belongs-to a {prefix a;} feature g;}";
-    list[1].data = "submodule bsub {belongs-to a {prefix a;} feature g;}";
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
-    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
-    CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL,
-            "Duplicate identifier \"g\" of feature statement - name collision with another top-level feature.", NULL);
-}
-
-void
-test_accessible_tree(void **state)
-{
-    const char *str;
-
-    /* config -> config */
-    str = "module a {\n"
-            "    namespace urn:a;\n"
-            "    prefix a;\n"
-            "    container cont {\n"
-            "        leaf l {\n"
-            "            type empty;\n"
-            "        }\n"
-            "    }\n"
-            "    container cont2 {\n"
-            "        leaf l2 {\n"
-            "            must ../../cont/l;\n"
-            "            type leafref {\n"
-            "                path /cont/l;\n"
-            "            }\n"
-            "        }\n"
-            "    }\n"
-            "}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
-    CHECK_LOG_CTX(NULL, NULL);
-
-    /* config -> state leafref */
-    str = "module b {\n"
-            "    namespace urn:b;\n"
-            "    prefix b;\n"
-            "    container cont {\n"
-            "        config false;\n"
-            "        leaf l {\n"
-            "            type empty;\n"
-            "        }\n"
-            "    }\n"
-            "    container cont2 {\n"
-            "        leaf l2 {\n"
-            "            type leafref {\n"
-            "                path /cont/l;\n"
-            "            }\n"
-            "        }\n"
-            "    }\n"
-            "}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Invalid leafref path \"/cont/l\" - target is supposed to represent configuration data"
-            " (as the leafref does), but it does not.", "Schema location \"/b:cont2/l2\".");
-
-    /* config -> state must */
-    str = "module b {\n"
-            "    namespace urn:b;\n"
-            "    prefix b;\n"
-            "    container cont {\n"
-            "        config false;\n"
-            "        leaf l {\n"
-            "            type empty;\n"
-            "        }\n"
-            "    }\n"
-            "    container cont2 {\n"
-            "        leaf l2 {\n"
-            "            must ../../cont/l;\n"
-            "            type empty;\n"
-            "        }\n"
-            "    }\n"
-            "}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
-    CHECK_LOG_CTX("Schema node \"cont\" for parent \"<config-root>\" not found; in expr \"../../cont\" "
-            "with context node \"/b:cont2/l2\".", NULL);
-
-    /* state -> config */
-    str = "module c {\n"
-            "    namespace urn:c;\n"
-            "    prefix c;\n"
-            "    container cont {\n"
-            "        leaf l {\n"
-            "            type empty;\n"
-            "        }\n"
-            "    }\n"
-            "    container cont2 {\n"
-            "        config false;\n"
-            "        leaf l2 {\n"
-            "            must ../../cont/l;\n"
-            "            type leafref {\n"
-            "                path /cont/l;\n"
-            "            }\n"
-            "        }\n"
-            "    }\n"
-            "}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
-    CHECK_LOG_CTX(NULL, NULL);
-
-    /* notif -> state */
-    str = "module d {\n"
-            "    namespace urn:d;\n"
-            "    prefix d;\n"
-            "    container cont {\n"
-            "        config false;\n"
-            "        leaf l {\n"
-            "            type empty;\n"
-            "        }\n"
-            "    }\n"
-            "    notification notif {\n"
-            "        leaf l2 {\n"
-            "            must ../../cont/l;\n"
-            "            type leafref {\n"
-            "                path /cont/l;\n"
-            "            }\n"
-            "        }\n"
-            "    }\n"
-            "}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
-    CHECK_LOG_CTX(NULL, NULL);
-
-    /* notif -> notif */
-    str = "module e {\n"
-            "    namespace urn:e;\n"
-            "    prefix e;\n"
-            "    notification notif {\n"
-            "        leaf l {\n"
-            "            type empty;\n"
-            "        }\n"
-            "        leaf l2 {\n"
-            "            must ../../notif/l;\n"
-            "            type leafref {\n"
-            "                path /notif/l;\n"
-            "            }\n"
-            "        }\n"
-            "    }\n"
-            "}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
-    CHECK_LOG_CTX(NULL, NULL);
-
-    /* rpc input -> state */
-    str = "module f {\n"
-            "    namespace urn:f;\n"
-            "    prefix f;\n"
-            "    container cont {\n"
-            "        config false;\n"
-            "        leaf l {\n"
-            "            type empty;\n"
-            "        }\n"
-            "    }\n"
-            "    rpc rp {\n"
-            "        input {\n"
-            "            leaf l2 {\n"
-            "                must ../../cont/l;\n"
-            "                type leafref {\n"
-            "                    path /cont/l;\n"
-            "                }\n"
-            "            }\n"
-            "        }\n"
-            "    }\n"
-            "}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
-    CHECK_LOG_CTX(NULL, NULL);
-
-    /* rpc input -> rpc input */
-    str = "module g {\n"
-            "    namespace urn:g;\n"
-            "    prefix g;\n"
-            "    rpc rp {\n"
-            "        input {\n"
-            "            leaf l {\n"
-            "                type empty;\n"
-            "            }\n"
-            "            leaf l2 {\n"
-            "                must ../l;\n"
-            "                type leafref {\n"
-            "                    path /rp/l;\n"
-            "                }\n"
-            "            }\n"
-            "        }\n"
-            "    }\n"
-            "}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
-    CHECK_LOG_CTX(NULL, NULL);
-
-    /* rpc input -> rpc output leafref */
-    str = "module h {\n"
-            "    namespace urn:h;\n"
-            "    prefix h;\n"
-            "    rpc rp {\n"
-            "        input {\n"
-            "            leaf l2 {\n"
-            "                type leafref {\n"
-            "                    path /rp/l;\n"
-            "                }\n"
-            "            }\n"
-            "        }\n"
-            "        output {\n"
-            "            leaf l {\n"
-            "                type empty;\n"
-            "            }\n"
-            "        }\n"
-            "    }\n"
-            "}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Not found node \"l\" in path.", "Schema location \"/h:rp/input/l2\".");
-
-    /* rpc input -> rpc output must */
-    str = "module h {\n"
-            "    namespace urn:h;\n"
-            "    prefix h;\n"
-            "    rpc rp {\n"
-            "        input {\n"
-            "            leaf l2 {\n"
-            "                must ../l;\n"
-            "                type empty;\n"
-            "            }\n"
-            "        }\n"
-            "        output {\n"
-            "            leaf l {\n"
-            "                type empty;\n"
-            "            }\n"
-            "        }\n"
-            "    }\n"
-            "}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
-    CHECK_LOG_CTX("Schema node \"l\" for parent \"/h:rp\" not found; in expr \"../l\" with context node \"/h:rp/input/l2\".", NULL);
-
-    /* rpc input -> notif leafref */
-    str = "module i {\n"
-            "    namespace urn:i;\n"
-            "    prefix i;\n"
-            "    rpc rp {\n"
-            "        input {\n"
-            "            leaf l2 {\n"
-            "                type leafref {\n"
-            "                    path ../../notif/l;\n"
-            "                }\n"
-            "            }\n"
-            "        }\n"
-            "    }\n"
-            "    notification notif {\n"
-            "        leaf l {\n"
-            "            type empty;\n"
-            "        }\n"
-            "    }\n"
-            "}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Not found node \"notif\" in path.", "Schema location \"/i:rp/input/l2\".");
-
-    /* rpc input -> notif must */
-    str = "module i {\n"
-            "    namespace urn:i;\n"
-            "    prefix i;\n"
-            "    rpc rp {\n"
-            "        input {\n"
-            "            leaf l2 {\n"
-            "                must /notif/l;\n"
-            "                type empty;\n"
-            "            }\n"
-            "        }\n"
-            "    }\n"
-            "    notification notif {\n"
-            "        leaf l {\n"
-            "            type empty;\n"
-            "        }\n"
-            "    }\n"
-            "}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
-    CHECK_LOG_CTX("Schema node \"notif\" for parent \"<root>\" not found; in expr \"/notif\" "
-            "with context node \"/i:rp/input/l2\".", NULL);
-
-    /* action output -> state */
-    str = "module j {\n"
-            "    yang-version 1.1;\n"
-            "    namespace urn:j;\n"
-            "    prefix j;\n"
-            "    container cont {\n"
-            "        list ll {\n"
-            "            key k;\n"
-            "            leaf k {\n"
-            "                type string;\n"
-            "            }\n"
-            "            action act {\n"
-            "                output {\n"
-            "                    leaf l2 {\n"
-            "                        must /cont/l;\n"
-            "                        type leafref {\n"
-            "                            path ../../../l;\n"
-            "                        }\n"
-            "                    }\n"
-            "                }\n"
-            "            }\n"
-            "        }\n"
-            "        leaf l {\n"
-            "            config false;\n"
-            "            type empty;\n"
-            "        }\n"
-            "    }\n"
-            "}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
-    CHECK_LOG_CTX(NULL, NULL);
-
-    /* action output -> action input leafref */
-    str = "module k {\n"
-            "    yang-version 1.1;\n"
-            "    namespace urn:k;\n"
-            "    prefix k;\n"
-            "    container cont {\n"
-            "        list ll {\n"
-            "            key k;\n"
-            "            leaf k {\n"
-            "                type string;\n"
-            "            }\n"
-            "            action act {\n"
-            "                input {\n"
-            "                    leaf l {\n"
-            "                        type empty;\n"
-            "                    }\n"
-            "                }\n"
-            "                output {\n"
-            "                    leaf l2 {\n"
-            "                        type leafref {\n"
-            "                            path ../l;\n"
-            "                        }\n"
-            "                    }\n"
-            "                }\n"
-            "            }\n"
-            "        }\n"
-            "    }\n"
-            "}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Not found node \"l\" in path.", "Schema location \"/k:cont/ll/act/output/l2\".");
-
-    /* action output -> action input must */
-    str = "module k {\n"
-            "    yang-version 1.1;\n"
-            "    namespace urn:k;\n"
-            "    prefix k;\n"
-            "    container cont {\n"
-            "        list ll {\n"
-            "            key k;\n"
-            "            leaf k {\n"
-            "                type string;\n"
-            "            }\n"
-            "            action act {\n"
-            "                input {\n"
-            "                    leaf l {\n"
-            "                        type empty;\n"
-            "                    }\n"
-            "                }\n"
-            "                output {\n"
-            "                    leaf l2 {\n"
-            "                        must /cont/ll/act/l;\n"
-            "                        type empty;\n"
-            "                    }\n"
-            "                }\n"
-            "            }\n"
-            "        }\n"
-            "    }\n"
-            "}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
-    CHECK_LOG_CTX("Schema node \"l\" for parent \"/k:cont/ll/act\" not found; in expr \"/cont/ll/act/l\" "
-            "with context node \"/k:cont/ll/act/output/l2\".", NULL);
-}
-
-void
-test_includes(void **state)
-{
-    struct lys_module *mod;
-
-    {
-        /* YANG 1.0 - the missing include sub_a_two in main_a will be injected from sub_a_one */
-        struct module_clb_list list[] = {
-            {"main_a", "module main_a { namespace urn:test:main_a; prefix ma; include sub_a_one;}"},
-            {"sub_a_one", "submodule sub_a_one { belongs-to main_a { prefix ma; } include sub_a_two;}"},
-            {"sub_a_two", "submodule sub_a_two { belongs-to main_a { prefix ma; } }"},
-            {NULL, NULL}
-        };
-
-        ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
-        mod = ly_ctx_load_module(UTEST_LYCTX, "main_a", NULL, NULL);
-        assert_non_null(mod);
-        assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->includes));
-        assert_true(mod->parsed->includes[1].injected);
-    }
-
-    {
-        /* YANG 1.1 - the missing include sub_b_two in main_b is error */
-        struct module_clb_list list[] = {
-            {"main_b", "module main_b { yang-version 1.1; namespace urn:test:main_b; prefix mb; include sub_b_one;}"},
-            {"sub_b_one", "submodule sub_b_one { yang-version 1.1; belongs-to main_b { prefix mb; } include sub_b_two;}"},
-            {"sub_b_two", "submodule sub_b_two { yang-version 1.1; belongs-to main_b { prefix mb; } }"},
-            {NULL, NULL}
-        };
-
-        ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
-        mod = ly_ctx_load_module(UTEST_LYCTX, "main_b", NULL, NULL);
-        assert_null(mod);
-        CHECK_LOG_CTX("Loading \"main_b\" module failed.", NULL,
-                "Data model \"main_b\" not found in local searchdirs.", NULL,
-                "Parsing module \"main_b\" failed.", NULL,
-                "Including \"sub_b_one\" submodule into \"main_b\" failed.", NULL,
-                "Data model \"sub_b_one\" not found in local searchdirs.", NULL,
-                "Parsing submodule \"sub_b_one\" failed.", NULL,
-                "YANG 1.1 requires all submodules to be included from main module. But submodule \"sub_b_one\" includes "
-                "submodule \"sub_b_two\" which is not included by main module \"main_b\".", NULL,
-                "YANG version 1.1 expects all includes in main module, includes in submodules (sub_b_one) are not necessary.", NULL);
-    }
-
-    {
-        /* YANG 1.1 - all includes are in main_c, includes in submodules are not necessary, so expect warning */
-        struct module_clb_list list[] = {
-            {"main_c", "module main_c { yang-version 1.1; namespace urn:test:main_c; prefix mc; include sub_c_one; include sub_c_two;}"},
-            {"sub_c_one", "submodule sub_c_one { yang-version 1.1; belongs-to main_c { prefix mc; } include sub_c_two;}"},
-            {"sub_c_two", "submodule sub_c_two { yang-version 1.1; belongs-to main_c { prefix mc; } include sub_c_one;}"},
-            {NULL, NULL}
-        };
-
-        ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
-        mod = ly_ctx_load_module(UTEST_LYCTX, "main_c", NULL, NULL);
-        assert_non_null(mod);
-        assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->includes));
-        assert_false(mod->parsed->includes[1].injected);
-        /* result is ok, but log includes the warning */
-        CHECK_LOG_CTX("YANG version 1.1 expects all includes in main module, includes in submodules (sub_c_two) are not necessary.", NULL);
-    }
-}
-
-void
-test_key_order(void **state)
-{
-    struct lys_module *mod;
-    const struct lysc_node *node;
-
-    struct module_clb_list list1[] = {
-        {"a", "module a {"
-            "yang-version 1.1;"
-            "namespace urn:test:a;"
-            "prefix a;"
-            "list l {"
-            "  key \"k1 k2\";"
-            "  leaf k2 {type string;}"
-            "  leaf k1 {type string;}"
-            "}"
-            "}"},
-        {NULL, NULL}
-    };
-
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list1);
-    mod = ly_ctx_load_module(UTEST_LYCTX, "a", NULL, NULL);
-    assert_non_null(mod);
-
-    node = lysc_node_child(mod->compiled->data);
-    assert_string_equal("k1", node->name);
-    node = node->next;
-    assert_string_equal("k2", node->name);
-
-    struct module_clb_list list2[] = {
-        {"b", "module b {"
-            "yang-version 1.1;"
-            "namespace urn:test:b;"
-            "prefix b;"
-            "list l {"
-            "  key \"k1 k2 k3 k4\";"
-            "  leaf k4 {type string;}"
-            "  container c {"
-            "    leaf l1 {type string;}"
-            "  }"
-            "  leaf k2 {type string;}"
-            "  leaf l2 {type string;}"
-            "  leaf k1 {type string;}"
-            "  leaf k3 {type string;}"
-            "}"
-            "}"},
-        {NULL, NULL}
-    };
-
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list2);
-    mod = ly_ctx_load_module(UTEST_LYCTX, "b", NULL, NULL);
-    assert_non_null(mod);
-
-    node = lysc_node_child(mod->compiled->data);
-    assert_string_equal("k1", node->name);
-    node = node->next;
-    assert_string_equal("k2", node->name);
-    node = node->next;
-    assert_string_equal("k3", node->name);
-    node = node->next;
-    assert_string_equal("k4", node->name);
-}
-
-void
-test_disabled_enum(void **state)
-{
-    const char *str;
-
-    /* no enabled enum */
-    str = "module a {"
-            "yang-version 1.1;"
-            "namespace urn:test:a;"
-            "prefix a;"
-            "feature f;"
-            "leaf l {type enumeration {"
-            "  enum e1 {if-feature f;}"
-            "  enum e2 {if-feature f;}"
-            "}}"
-            "}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Enumeration type of node \"l\" without any (or all disabled) valid values.", "Schema location \"/a:l\".");
-
-    /* disabled default value */
-    str = "module a {"
-            "yang-version 1.1;"
-            "namespace urn:test:a;"
-            "prefix a;"
-            "feature f;"
-            "leaf l {"
-            "  type enumeration {"
-            "    enum e1 {if-feature f;}"
-            "    enum e2;"
-            "  }"
-            "  default e1;"
-            "}"
-            "}";
-    assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
-    CHECK_LOG_CTX("Invalid default - value does not fit the type (Invalid enumeration value \"e1\".).",
-            "Schema location \"/a:l\".");
-}
diff --git a/tests/utests/schema/test_schema_extensions.c b/tests/utests/schema/test_schema_extensions.c
deleted file mode 100644
index b1d8504..0000000
--- a/tests/utests/schema/test_schema_extensions.c
+++ /dev/null
@@ -1,461 +0,0 @@
-/*
- * @file test_schema_extensions.c
- * @author: Radek Krejci <rkrejci@cesnet.cz>
- * @brief unit tests for YANG (YIN) extension statements and their instances in schemas
- *
- * Copyright (c) 2018-2021 CESNET, z.s.p.o.
- *
- * This source code is licensed under BSD 3-Clause License (the "License").
- * You may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     https://opensource.org/licenses/BSD-3-Clause
- */
-#include "test_schema.h"
-
-#include <string.h>
-
-#include "context.h"
-#include "log.h"
-#include "plugins_exts.h"
-#include "tree_schema.h"
-
-void
-test_extension_argument(void **state)
-{
-    struct lys_module *mod;
-    const char *mod_def_yang = "module a {\n"
-            "  namespace \"urn:a\";\n"
-            "  prefix a;\n\n"
-            "  extension e {\n"
-            "    argument name;\n"
-            "  }\n\n"
-            "  a:e \"aaa\";\n"
-            "}\n";
-    const char *mod_def_yin =
-            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
-            "<module name=\"a\"\n"
-            "        xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
-            "        xmlns:a=\"urn:a\">\n"
-            "  <namespace uri=\"urn:a\"/>\n"
-            "  <prefix value=\"a\"/>\n"
-            "  <extension name=\"e\">\n"
-            "    <argument name=\"name\"/>\n"
-            "  </extension>\n"
-            "  <a:e name=\"aaa\"/>\n"
-            "</module>\n";
-    const char *mod_test_yin, *mod_test_yang;
-    char *printed;
-
-    mod_test_yang = "module b {\n"
-            "  namespace \"urn:b\";\n"
-            "  prefix b;\n\n"
-            "  import a {\n"
-            "    prefix a;\n"
-            "  }\n\n"
-            "  a:e \"xxx\";\n"
-            "}\n";
-    mod_test_yin =
-            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
-            "<module name=\"b\"\n"
-            "        xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
-            "        xmlns:b=\"urn:b\"\n"
-            "        xmlns:a=\"urn:a\">\n"
-            "  <namespace uri=\"urn:b\"/>\n"
-            "  <prefix value=\"b\"/>\n"
-            "  <import module=\"a\">\n"
-            "    <prefix value=\"a\"/>\n"
-            "  </import>\n"
-            "  <a:e name=\"xxx\"/>\n"
-            "</module>\n";
-
-    /* from YANG */
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_def_yang);
-    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_test_yang, LYS_IN_YANG, &mod));
-    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
-    assert_string_equal(printed, mod_test_yang);
-    free(printed);
-
-    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
-    assert_string_equal(printed, mod_test_yin);
-    free(printed);
-
-    assert_non_null(mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
-    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
-    assert_string_equal(printed, mod_def_yang);
-    free(printed);
-
-    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
-    assert_string_equal(printed, mod_def_yin);
-    free(printed);
-
-    /* context reset */
-    ly_ctx_destroy(UTEST_LYCTX);
-    ly_ctx_new(NULL, 0, &UTEST_LYCTX);
-
-    /* from YIN */
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_def_yin);
-    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, &mod));
-    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
-    assert_string_equal(printed, mod_test_yang);
-    free(printed);
-
-    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
-    assert_string_equal(printed, mod_test_yin);
-    free(printed);
-
-    assert_non_null(mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
-    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
-    assert_string_equal(printed, mod_def_yang);
-    free(printed);
-
-    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
-    assert_string_equal(printed, mod_def_yin);
-    free(printed);
-}
-
-void
-test_extension_argument_element(void **state)
-{
-    struct lys_module *mod;
-    const char *mod_def_yang = "module a {\n"
-            "  namespace \"urn:a\";\n"
-            "  prefix a;\n\n"
-            "  extension e {\n"
-            "    argument name {\n"
-            "      yin-element true;\n"
-            "    }\n"
-            "  }\n\n"
-            "  a:e \"aaa\";\n"
-            "}\n";
-    const char *mod_def_yin =
-            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
-            "<module name=\"a\"\n"
-            "        xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
-            "        xmlns:a=\"urn:a\">\n"
-            "  <namespace uri=\"urn:a\"/>\n"
-            "  <prefix value=\"a\"/>\n"
-            "  <extension name=\"e\">\n"
-            "    <argument name=\"name\">\n"
-            "      <yin-element value=\"true\"/>\n"
-            "    </argument>\n"
-            "  </extension>\n"
-            "  <a:e>\n"
-            "    <a:name>aaa</a:name>\n"
-            "  </a:e>\n"
-            "</module>\n";
-    const char *mod_test_yin, *mod_test_yang;
-    char *printed;
-
-    mod_test_yang = "module b {\n"
-            "  namespace \"urn:b\";\n"
-            "  prefix b;\n\n"
-            "  import a {\n"
-            "    prefix a;\n"
-            "  }\n\n"
-            "  a:e \"xxx\";\n"
-            "}\n";
-    mod_test_yin =
-            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
-            "<module name=\"b\"\n"
-            "        xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
-            "        xmlns:b=\"urn:b\"\n"
-            "        xmlns:a=\"urn:a\">\n"
-            "  <namespace uri=\"urn:b\"/>\n"
-            "  <prefix value=\"b\"/>\n"
-            "  <import module=\"a\">\n"
-            "    <prefix value=\"a\"/>\n"
-            "  </import>\n"
-            "  <a:e>\n"
-            "    <a:name>xxx</a:name>\n"
-            "  </a:e>\n"
-            "</module>\n";
-
-    /* from YANG */
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_def_yang);
-    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_test_yang, LYS_IN_YANG, &mod));
-    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
-    assert_string_equal(printed, mod_test_yang);
-    free(printed);
-
-    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
-    assert_string_equal(printed, mod_test_yin);
-    free(printed);
-
-    assert_non_null(mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
-    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
-    assert_string_equal(printed, mod_def_yang);
-    free(printed);
-
-    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
-    assert_string_equal(printed, mod_def_yin);
-    free(printed);
-
-    /* context reset */
-    ly_ctx_destroy(UTEST_LYCTX);
-    ly_ctx_new(NULL, 0, &UTEST_LYCTX);
-
-    /* from YIN */
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_def_yin);
-    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, &mod));
-    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
-    assert_string_equal(printed, mod_test_yang);
-    free(printed);
-
-    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
-    assert_string_equal(printed, mod_test_yin);
-    free(printed);
-
-    assert_non_null(mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
-    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
-    assert_string_equal(printed, mod_def_yang);
-    free(printed);
-
-    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
-    assert_string_equal(printed, mod_def_yin);
-    free(printed);
-
-    /* invalid */
-    mod_test_yang = "module x { namespace \"urn:x\"; prefix x; import a { prefix a; } a:e; }";
-    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yang, LYS_IN_YANG, NULL));
-    CHECK_LOG_CTX("Extension instance \"a:e\" misses argument element \"name\".", "/x:{extension='a:e'}");
-
-    mod_test_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
-            "<module name=\"x\"\n"
-            "        xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
-            "        xmlns:x=\"urn:x\"\n"
-            "        xmlns:a=\"urn:a\">\n"
-            "  <namespace uri=\"urn:x\"/>\n"
-            "  <prefix value=\"x\"/>\n"
-            "  <import module=\"a\">\n"
-            "    <prefix value=\"a\"/>\n"
-            "  </import>\n\n"
-            "  <a:e/>\n"
-            "</module>\n";
-    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, NULL));
-    CHECK_LOG_CTX("Extension instance \"a:e\" misses argument element \"name\".", "/x:{extension='a:e'}");
-
-    mod_test_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
-            "<module name=\"x\"\n"
-            "        xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
-            "        xmlns:x=\"urn:x\"\n"
-            "        xmlns:a=\"urn:a\">\n"
-            "  <namespace uri=\"urn:x\"/>\n"
-            "  <prefix value=\"x\"/>\n"
-            "  <import module=\"a\">\n"
-            "    <prefix value=\"a\"/>\n"
-            "  </import>\n\n"
-            "  <a:e name=\"xxx\"/>\n"
-            "</module>\n";
-    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, NULL));
-    CHECK_LOG_CTX("Extension instance \"a:e\" misses argument element \"name\".", "/x:{extension='a:e'}");
-
-    mod_test_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
-            "<module name=\"x\"\n"
-            "        xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
-            "        xmlns:x=\"urn:x\"\n"
-            "        xmlns:a=\"urn:a\">\n"
-            "  <namespace uri=\"urn:x\"/>\n"
-            "  <prefix value=\"x\"/>\n"
-            "  <import module=\"a\">\n"
-            "    <prefix value=\"a\"/>\n"
-            "  </import>\n\n"
-            "  <a:e>\n"
-            "    <x:name>xxx</x:name>\n"
-            "  </a:e>\n"
-            "</module>\n";
-    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, NULL));
-    CHECK_LOG_CTX("Extension instance \"a:e\" element and its argument element \"name\" are expected in the same namespace, but they differ.",
-            "/x:{extension='a:e'}");
-
-    mod_test_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
-            "<module name=\"x\"\n"
-            "        xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
-            "        xmlns:x=\"urn:x\"\n"
-            "        xmlns:a=\"urn:a\">\n"
-            "  <namespace uri=\"urn:x\"/>\n"
-            "  <prefix value=\"x\"/>\n"
-            "  <import module=\"a\">\n"
-            "    <prefix value=\"a\"/>\n"
-            "  </import>\n\n"
-            "  <a:e>\n"
-            "    <a:value>xxx</a:value>\n"
-            "  </a:e>\n"
-            "</module>\n";
-    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, NULL));
-    CHECK_LOG_CTX("Extension instance \"a:e\" expects argument element \"name\" as its first XML child, but \"value\" element found.",
-            "/x:{extension='a:e'}");
-
-}
-
-void
-test_extension_compile(void **state)
-{
-    struct lys_module *mod;
-    struct lysc_ctx cctx = {0};
-    struct lysp_ext_instance ext_p = {0};
-    struct lysp_stmt child = {0};
-    struct lysc_ext_instance ext_c = {0};
-    struct lysc_ext_substmt *substmt;
-    LY_ERR rc = LY_SUCCESS;
-
-    /* current module, whatever */
-    mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang");
-    assert_true(mod);
-
-    /* compile context */
-    cctx.ctx = UTEST_LYCTX;
-    cctx.cur_mod = mod;
-    cctx.pmod = mod->parsed;
-    cctx.path_len = 1;
-    cctx.path[0] = '/';
-
-    /* parsed ext instance */
-    lydict_insert(UTEST_LYCTX, "pref:my-ext", 0, &ext_p.name);
-    ext_p.format = LY_VALUE_JSON;
-    ext_p.parent_stmt = LY_STMT_MODULE;
-
-    /* compiled ext instance */
-    ext_c.parent_stmt = ext_p.parent_stmt;
-    // ext_c.parent =
-    LY_ARRAY_NEW_GOTO(UTEST_LYCTX, ext_c.substmts, substmt, rc, cleanup);
-
-    /*
-     * error-message
-     */
-    ext_p.child = &child;
-    lydict_insert(UTEST_LYCTX, "error-message", 0, &child.stmt);
-    lydict_insert(UTEST_LYCTX, "my error", 0, &child.arg);
-    child.format = LY_VALUE_JSON;
-    child.kw = LY_STMT_ERROR_MESSAGE;
-
-    substmt->stmt = LY_STMT_ERROR_MESSAGE;
-    substmt->cardinality = LY_STMT_CARD_OPT;
-    substmt->storage = &ext_c.data;
-
-    /* compile */
-    assert_int_equal(LY_SUCCESS, lys_compile_extension_instance(&cctx, &ext_p, &ext_c));
-
-    /* check */
-    assert_string_equal(ext_c.data, "my error");
-
-cleanup:
-    lydict_remove(UTEST_LYCTX, ext_p.name);
-    lydict_remove(UTEST_LYCTX, child.stmt);
-    lydict_remove(UTEST_LYCTX, child.arg);
-    LY_ARRAY_FREE(ext_c.substmts);
-
-    lydict_remove(UTEST_LYCTX, ext_c.data);
-    if (rc) {
-        fail();
-    }
-}
-
-void
-test_ext_recursive(void **state)
-{
-    const char *mod_base_yang, *mod_imp_yang, *mod_base_yin, *mod_imp_yin;
-
-    mod_imp_yang = "module b {\n"
-            "  namespace \"urn:b\";\n"
-            "  prefix b;\n\n"
-            "  extension use-in {\n"
-            "    argument name {\n"
-            "      b:arg-type {\n"
-            "        type string;\n"
-            "      }\n"
-            "    }\n"
-            "    b:use-in \"extension\";\n"
-            "    b:occurence \"*\";\n"
-            "  }\n"
-            "\n"
-            "  extension substatement {\n"
-            "    argument name {\n"
-            "      b:arg-type {\n"
-            "        type string;\n"
-            "      }\n"
-            "    }\n"
-            "    b:use-in \"extension\";\n"
-            "    b:occurence \"*\";\n"
-            "    b:substatement \"b:occurence\";\n"
-            "  }\n"
-            "\n"
-            "  extension arg-type {\n"
-            "    b:use-in \"argument\";\n"
-            "    b:substatement \"type\" {\n"
-            "      b:occurence \"1\";\n"
-            "    }\n"
-            "    b:substatement \"default\";\n"
-            "  }\n"
-            "\n"
-            "  extension occurence {\n"
-            "    argument value {\n"
-            "      b:arg-type {\n"
-            "        type enumeration {\n"
-            "          enum \"?\";\n"
-            "          enum \"*\";\n"
-            "          enum \"+\";\n"
-            "          enum \"1\";\n"
-            "        }\n"
-            "      }\n"
-            "    }\n"
-            "    b:use-in \"extension\";\n"
-            "  }\n"
-            "}\n";
-
-    mod_imp_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
-            "<module name=\"b\"\n"
-            "        xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
-            "        xmlns:b=\"urn:b\"\n"
-            "        xmlns:a=\"urn:a\">\n"
-            "  <namespace uri=\"urn:b\"/>\n"
-            "  <prefix value=\"b\"/>\n"
-            "  <import module=\"a\">\n"
-            "    <prefix value=\"a\"/>\n"
-            "  </import>\n\n"
-            "  <a:e name=\"xxx\"/>\n"
-            "</module>\n";
-
-    mod_base_yang = "module a {\n"
-            "  namespace \"urn:a\";\n"
-            "  prefix a;\n\n"
-            "  import b {\n"
-            "    prefix b;\n"
-            "  }\n"
-            "\n"
-            "  extension abstract {\n"
-            "    b:use-in \"identity\";\n"
-            "  }\n"
-            "\n"
-            "  identity mount-id;\n"
-            "\n"
-            "  identity yang-lib-id {\n"
-            "    base mount-id;\n"
-            "    a:abstract;\n"
-            "  }\n"
-            "}\n";
-
-    mod_base_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
-            "<module name=\"a\"\n"
-            "        xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
-            "        xmlns:a=\"urn:a\">\n"
-            "  <namespace uri=\"urn:a\"/>\n"
-            "  <prefix value=\"a\"/>\n\n"
-            "  <extension name=\"e\">\n"
-            "    <argument name=\"name\"/>\n"
-            "  </extension>\n\n"
-            "  <a:e name=\"aaa\"/>\n"
-            "</module>\n";
-
-    /* from YANG */
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_imp_yang);
-    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_base_yang, LYS_IN_YANG, NULL));
-
-    /* context reset */
-    ly_ctx_destroy(UTEST_LYCTX);
-    ly_ctx_new(NULL, 0, &UTEST_LYCTX);
-
-    /* from YIN */
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_imp_yin);
-    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_base_yin, LYS_IN_YIN, NULL));
-}
diff --git a/tests/utests/schema/test_schema_stmts.c b/tests/utests/schema/test_schema_stmts.c
deleted file mode 100644
index 81a9832..0000000
--- a/tests/utests/schema/test_schema_stmts.c
+++ /dev/null
@@ -1,251 +0,0 @@
-/*
- * @file test_schema_stmts.c
- * @author: Radek Krejci <rkrejci@cesnet.cz>
- * @brief unit tests for YANG (YIN) statements in (sub)modules
- *
- * Copyright (c) 2018-2020 CESNET, z.s.p.o.
- *
- * This source code is licensed under BSD 3-Clause License (the "License").
- * You may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     https://opensource.org/licenses/BSD-3-Clause
- */
-#include "test_schema.h"
-
-#include <string.h>
-
-#include "context.h"
-#include "log.h"
-#include "tree_schema.h"
-
-void
-test_identity(void **state)
-{
-    struct lys_module *mod, *mod_imp;
-
-    /*
-     * parsing YANG
-     */
-    TEST_STMT_DUP(1, 0, "identity id", "description", "a", "b", "1");
-    TEST_STMT_DUP(1, 0, "identity id", "reference", "a", "b", "1");
-    TEST_STMT_DUP(1, 0, "identity id", "status", "current", "obsolete", "1");
-
-    /* full content */
-    TEST_SCHEMA_OK(1, 0, "identityone",
-            "identity test {base \"a\";base b; description text;reference \'another text\';status current; if-feature x;if-feature y; identityone:ext;}"
-            "identity a; identity b; extension ext; feature x; feature y;", mod);
-    assert_non_null(mod->parsed->identities);
-    assert_int_equal(3, LY_ARRAY_COUNT(mod->parsed->identities));
-
-    /* invalid substatement */
-    TEST_STMT_SUBSTM_ERR(0, "identity", "organization", "XXX");
-
-    /*
-     * parsing YIN
-     */
-    /* max subelems */
-    TEST_SCHEMA_OK(1, 1, "identityone-yin", "<identity name=\"ident-name\">"
-            "<if-feature name=\"iff\"/>"
-            "<base name=\"base-name\"/>"
-            "<status value=\"deprecated\"/>"
-            "<description><text>desc</text></description>"
-            "<reference><text>ref</text></reference>"
-            /* TODO yin-extension-prefix-compilation-bug "<myext:ext xmlns:myext=\"urn:libyang:test:identityone-yin\"/>" */
-            "</identity><extension name=\"ext\"/><identity name=\"base-name\"/><feature name=\"iff\"/>", mod);
-    assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->identities));
-    assert_string_equal(mod->parsed->identities[0].name, "ident-name");
-    assert_string_equal(mod->parsed->identities[0].bases[0], "base-name");
-    assert_string_equal(mod->parsed->identities[0].iffeatures[0].str, "iff");
-    assert_string_equal(mod->parsed->identities[0].dsc, "desc");
-    assert_string_equal(mod->parsed->identities[0].ref, "ref");
-    assert_true(mod->parsed->identities[0].flags & LYS_STATUS_DEPRC);
-    /*assert_string_equal(mod->parsed->identities[0].exts[0].name, "ext");
-    assert_non_null(mod->parsed->identities[0].exts[0].compiled);
-    assert_int_equal(mod->parsed->identities[0].exts[0].yin, 1);
-    assert_int_equal(mod->parsed->identities[0].exts[0].insubstmt_index, 0);
-    assert_int_equal(mod->parsed->identities[0].exts[0].insubstmt, LYEXT_SUBSTMT_SELF);*/
-
-    /* min subelems */
-    TEST_SCHEMA_OK(1, 1, "identitytwo-yin", "<identity name=\"ident-name\" />", mod);
-    assert_int_equal(1, LY_ARRAY_COUNT(mod->parsed->identities));
-    assert_string_equal(mod->parsed->identities[0].name, "ident-name");
-
-    /* invalid substatement */
-    TEST_SCHEMA_PARSE_ERR(0, 1, "inv", "<identity name=\"ident-name\"><if-feature name=\"iff\"/></identity>",
-            "Invalid sub-elemnt \"if-feature\" of \"identity\" element - "
-            "this sub-element is allowed only in modules with version 1.1 or newer.", "Line number 1.");
-
-    /*
-     * compiling
-     */
-    TEST_SCHEMA_OK(0, 0, "a", "identity a1;", mod_imp);
-    TEST_SCHEMA_OK(1, 0, "b", "import a {prefix a;}"
-            "identity b1; identity b2; identity b3 {base b1; base b:b2; base a:a1;}"
-            "identity b4 {base b:b1; base b3;}", mod);
-    assert_non_null(mod_imp->compiled);
-    assert_non_null(mod_imp->identities);
-    assert_non_null(mod->identities);
-    assert_non_null(mod_imp->identities[0].derived);
-    assert_int_equal(1, LY_ARRAY_COUNT(mod_imp->identities[0].derived));
-    assert_ptr_equal(mod_imp->identities[0].derived[0], &mod->identities[2]);
-    assert_non_null(mod->identities[0].derived);
-    assert_int_equal(2, LY_ARRAY_COUNT(mod->identities[0].derived));
-    assert_ptr_equal(mod->identities[0].derived[0], &mod->identities[2]);
-    assert_ptr_equal(mod->identities[0].derived[1], &mod->identities[3]);
-    assert_non_null(mod->identities[1].derived);
-    assert_int_equal(1, LY_ARRAY_COUNT(mod->identities[1].derived));
-    assert_ptr_equal(mod->identities[1].derived[0], &mod->identities[2]);
-    assert_non_null(mod->identities[2].derived);
-    assert_int_equal(1, LY_ARRAY_COUNT(mod->identities[2].derived));
-    assert_ptr_equal(mod->identities[2].derived[0], &mod->identities[3]);
-
-    TEST_SCHEMA_OK(1, 0, "c", "identity c2 {base c1;} identity c1;", mod);
-    assert_int_equal(1, LY_ARRAY_COUNT(mod->identities[1].derived));
-    assert_ptr_equal(mod->identities[1].derived[0], &mod->identities[0]);
-
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule inv_sub {belongs-to inv {prefix inv;} identity i1;}");
-    TEST_SCHEMA_ERR(0, 0, "inv", "identity i1 {base i2;}", "Unable to find base (i2) of identity \"i1\".", "/inv:{identity='i1'}");
-    TEST_SCHEMA_ERR(0, 0, "inv", "identity i1 {base i1;}", "Identity \"i1\" is derived from itself.", "/inv:{identity='i1'}");
-    TEST_SCHEMA_ERR(0, 0, "inv", "identity i1 {base i2;}identity i2 {base i3;}identity i3 {base i1;}",
-            "Identity \"i1\" is indirectly derived from itself.", "/inv:{identity='i3'}");
-
-    /* base in non-implemented module */
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb,
-            "module base {namespace \"urn\"; prefix b; identity i1; identity i2 {base i1;}}");
-    TEST_SCHEMA_OK(0, 0, "ident", "import base {prefix b;} identity ii {base b:i1;}", mod);
-
-    /* default value from non-implemented module */
-    TEST_SCHEMA_ERR(0, 0, "ident2", "import base {prefix b;} leaf l {type identityref {base b:i1;} default b:i2;}",
-            "Invalid default - value does not fit the type (Invalid identityref \"b:i2\" value"
-            " - identity found in non-implemented module \"base\".).", "Schema location \"/ident2:l\".");
-
-    /* default value in typedef from non-implemented module */
-    TEST_SCHEMA_ERR(0, 0, "ident2", "import base {prefix b;} typedef t1 {type identityref {base b:i1;} default b:i2;}"
-            "leaf l {type t1;}", "Invalid default - value does not fit the type (Invalid"
-            " identityref \"b:i2\" value - identity found in non-implemented module \"base\".).", "Schema location \"/ident2:l\".");
-
-    /*
-     * printing
-     */
-
-    /*
-     * cleanup
-     */
-}
-
-void
-test_feature(void **state)
-{
-    struct lys_module *mod;
-    const struct lysp_feature *f;
-
-    /*
-     * parsing YANG
-     */
-
-    TEST_STMT_DUP(1, 0, "feature f", "description", "a", "b", "1");
-    TEST_STMT_DUP(1, 0, "feature f", "reference", "a", "b", "1");
-    TEST_STMT_DUP(1, 0, "feature f", "status", "current", "obsolete", "1");
-
-    /* full content */
-    TEST_SCHEMA_OK(1, 0, "featureone",
-            "feature test {description text;reference \'another text\';status current; if-feature x; if-feature y; featureone:ext;}"
-            "extension ext; feature x; feature y;", mod);
-    assert_non_null(mod->parsed->features);
-    assert_int_equal(3, LY_ARRAY_COUNT(mod->parsed->features));
-
-    /* invalid substatement */
-    TEST_STMT_SUBSTM_ERR(0, "feature", "organization", "XXX");
-
-    /*
-     * parsing YIN
-     */
-    /* max subelems */
-    TEST_SCHEMA_OK(0, 1, "featureone-yin", "<feature name=\"feature-name\">"
-            "<if-feature name=\"iff\"/>"
-            "<status value=\"deprecated\"/>"
-            "<description><text>desc</text></description>"
-            "<reference><text>ref</text></reference>"
-            /* TODO yin-extension-prefix-compilation-bug "<myext:ext xmlns:myext=\"urn:libyang:test:featureone-yin\"/>" */
-            "</feature><extension name=\"ext\"/><feature name=\"iff\"/>", mod);
-    assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->features));
-    assert_string_equal(mod->parsed->features[0].name, "feature-name");
-    assert_string_equal(mod->parsed->features[0].dsc, "desc");
-    assert_true(mod->parsed->features[0].flags & LYS_STATUS_DEPRC);
-    assert_string_equal(mod->parsed->features[0].iffeatures[0].str, "iff");
-    assert_string_equal(mod->parsed->features[0].ref, "ref");
-    /*assert_string_equal(mod->parsed->features[0].exts[0].name, "ext");
-    assert_int_equal(mod->parsed->features[0].exts[0].insubstmt_index, 0);
-    assert_int_equal(mod->parsed->features[0].exts[0].insubstmt, LYEXT_SUBSTMT_SELF);*/
-
-    /* min subelems */
-    TEST_SCHEMA_OK(0, 1, "featuretwo-yin", "<feature name=\"feature-name\"/>", mod)
-    assert_int_equal(1, LY_ARRAY_COUNT(mod->parsed->features));
-    assert_string_equal(mod->parsed->features[0].name, "feature-name");
-
-    /* invalid substatement */
-    TEST_SCHEMA_PARSE_ERR(0, 1, "inv", "<feature name=\"feature-name\"><organization><text>org</text></organization></feature>",
-            "Unexpected sub-element \"organization\" of \"feature\" element.", "Line number 1.");
-
-    /*
-     * compiling
-     */
-
-    TEST_SCHEMA_OK(1, 0, "a", "feature f1 {description test1;reference test2;status current;} feature f2; feature f3;\n"
-            "feature orfeature {if-feature \"f1 or f2\";}\n"
-            "feature andfeature {if-feature \"f1 and f2\";}\n"
-            "feature f6 {if-feature \"not f1\";}\n"
-            "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n"
-            "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n"
-            "feature f9 {if-feature \"not not f1\";}", mod);
-    assert_non_null(mod->parsed->features);
-    assert_int_equal(9, LY_ARRAY_COUNT(mod->parsed->features));
-
-    /* all features are disabled by default */
-    LY_ARRAY_FOR(mod->parsed->features, struct lysp_feature, f) {
-        assert_false(f->flags & LYS_FENABLED);
-    }
-
-    /* some invalid expressions */
-    TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f{if-feature f1;}",
-            "Invalid value \"f1\" of if-feature - unable to find feature \"f1\".", NULL);
-    TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2{if-feature 'f and';}",
-            "Invalid value \"f and\" of if-feature - unexpected end of expression.", NULL);
-    TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f{if-feature 'or';}",
-            "Invalid value \"or\" of if-feature - unexpected end of expression.", NULL);
-    TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2{if-feature '(f1';}",
-            "Invalid value \"(f1\" of if-feature - non-matching opening and closing parentheses.", NULL);
-    TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2{if-feature 'f1)';}",
-            "Invalid value \"f1)\" of if-feature - non-matching opening and closing parentheses.", NULL);
-    TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2{if-feature ---;}",
-            "Invalid value \"---\" of if-feature - unable to find feature \"---\".", NULL);
-    TEST_SCHEMA_PARSE_ERR(0, 0, "inv", "feature f1; feature f2{if-feature 'not f1';}",
-            "Invalid value \"not f1\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", NULL);
-
-    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule inv_sub {belongs-to inv {prefix inv;} feature f1;}");
-    TEST_SCHEMA_PARSE_ERR(0, 0, "inv", "feature f1 {if-feature f2;} feature f2 {if-feature f1;}",
-            "Feature \"f1\" is indirectly referenced from itself.", NULL);
-    TEST_SCHEMA_PARSE_ERR(0, 0, "inv", "feature f1 {if-feature f1;}",
-            "Feature \"f1\" is referenced from itself.", NULL);
-    TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f {if-feature ();}",
-            "Invalid value \"()\" of if-feature - number of features in expression does not match the required number of operands for the operations.", NULL);
-    TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f {if-feature 'f1(';}",
-            "Invalid value \"f1(\" of if-feature - non-matching opening and closing parentheses.", NULL);
-    TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f {if-feature 'and f1';}",
-            "Invalid value \"and f1\" of if-feature - missing feature/expression before \"and\" operation.", NULL);
-    TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f {if-feature 'f1 not ';}",
-            "Invalid value \"f1 not \" of if-feature - unexpected end of expression.", NULL);
-    TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f {if-feature 'f1 not not ';}",
-            "Invalid value \"f1 not not \" of if-feature - unexpected end of expression.", NULL);
-    TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2; feature f {if-feature 'or f1 f2';}",
-            "Invalid value \"or f1 f2\" of if-feature - missing feature/expression before \"or\" operation.", NULL);
-
-    /*
-     * printing
-     */
-
-    /*
-     * cleanup
-     */
-}
diff --git a/tests/utests/schema/test_yang.c b/tests/utests/schema/test_yang.c
index 34c6d36..bb30a92 100644
--- a/tests/utests/schema/test_yang.c
+++ b/tests/utests/schema/test_yang.c
@@ -24,7 +24,7 @@
 #include "schema_compile.h"
 #include "tree_edit.h"
 #include "tree_schema.h"
-#include "tree_schema_internal.h"
+#include "tree_schema_free.h"
 
 /* originally static functions from parser_yang.c and parser_yin.c */
 LY_ERR buf_add_char(struct ly_ctx *ctx, struct ly_in *in, size_t len, char **buf, size_t *buf_len, size_t *buf_used);
@@ -57,6 +57,7 @@
 LY_ERR parse_type_enum_value_pos(struct lys_yang_parser_ctx *ctx, enum ly_stmt val_kw, int64_t *value, uint16_t *flags, struct lysp_ext_instance **exts);
 
 struct lys_yang_parser_ctx *YCTX;
+struct lysf_ctx fctx;
 
 struct ly_in in = {0};
 
@@ -91,19 +92,24 @@
     YCTX->in = &in;
     LOG_LOCINIT(NULL, NULL, NULL, &in);
 
+    fctx.ctx = PARSER_CTX(YCTX);
+    fctx.mod = pmod->mod;
+
     return 0;
 }
 
 static int
 teardown(void **state)
 {
-    lys_module_free(PARSER_CUR_PMOD(YCTX)->mod, 0);
+    lys_module_free(&fctx, PARSER_CUR_PMOD(YCTX)->mod, 0);
     LOG_LOCBACK(0, 0, 0, 1);
 
     ly_set_free(YCTX->parsed_mods, NULL);
     free(YCTX);
     YCTX = NULL;
 
+    lysf_ctx_erase(&fctx);
+
     UTEST_TEARDOWN;
 
     return 0;
@@ -717,7 +723,7 @@
     struct ly_ctx *ly_ctx = PARSER_CUR_PMOD(ctx)->mod->ctx;
     struct lysp_module *pmod;
 
-    lys_module_free(PARSER_CUR_PMOD(ctx)->mod, 0);
+    lys_module_free(&fctx, PARSER_CUR_PMOD(ctx)->mod, 0);
     pmod = calloc(1, sizeof *pmod);
     ctx->parsed_mods->objs[0] = pmod;
     pmod->mod = calloc(1, sizeof *pmod->mod);
@@ -725,6 +731,7 @@
     pmod->mod->ctx = ly_ctx;
 
     ctx->in->line = 1;
+    fctx.mod = pmod->mod;
 
     return pmod;
 }
@@ -735,7 +742,7 @@
     struct ly_ctx *ly_ctx = PARSER_CUR_PMOD(ctx)->mod->ctx;
     struct lysp_submodule *submod;
 
-    lys_module_free(PARSER_CUR_PMOD(ctx)->mod, 0);
+    lys_module_free(&fctx, PARSER_CUR_PMOD(ctx)->mod, 0);
     submod = calloc(1, sizeof *submod);
     ctx->parsed_mods->objs[0] = submod;
     submod->mod = calloc(1, sizeof *submod->mod);
@@ -743,6 +750,8 @@
     submod->mod->parsed = (struct lysp_module *)submod;
     submod->mod->ctx = ly_ctx;
 
+    fctx.mod = submod->mod;
+
     return submod;
 }
 
@@ -937,7 +946,7 @@
     assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m));
     CHECK_LOG_CTX("Trailing garbage \"module q {names...\" after module, expected end-of-input.", "Line number 1.");
     yang_parser_ctx_free(ctx_p);
-    lys_module_free(m, 0);
+    lys_module_free(&fctx, m, 0);
 
     in.current = "prefix " SCHEMA_BEGINNING "}";
     m = calloc(1, sizeof *m);
@@ -945,7 +954,7 @@
     assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m));
     CHECK_LOG_CTX("Invalid keyword \"prefix\", expected \"module\" or \"submodule\".", "Line number 1.");
     yang_parser_ctx_free(ctx_p);
-    lys_module_free(m, 0);
+    lys_module_free(&fctx, m, 0);
 
     in.current = "module " SCHEMA_BEGINNING "leaf enum {type enumeration {enum seven { position 7;}}}}";
     m = calloc(1, sizeof *m);
@@ -953,7 +962,7 @@
     assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m));
     CHECK_LOG_CTX("Invalid keyword \"position\" as a child of \"enum\".", "Line number 1.");
     yang_parser_ctx_free(ctx_p);
-    lys_module_free(m, 0);
+    lys_module_free(&fctx, m, 0);
 
     /* extensions */
     TEST_GENERIC("prefix:test;}", mod->exts,
@@ -1094,7 +1103,7 @@
     in.current = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
     assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node**)&c)); \
     CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)c); c = NULL;
+    lysp_node_free(&fctx, (struct lysp_node*)c); c = NULL;
 
     TEST_DUP("config", "true", "false");
     TEST_DUP("description", "text1", "text2");
@@ -1118,24 +1127,24 @@
     assert_non_null(c->typedefs);
     ly_set_erase(&YCTX->tpdfs_nodes, NULL);
     ly_set_erase(&YCTX->grps_nodes, NULL);
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)c); c = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)c); c = NULL;
 
     /* invalid */
     in.current = " cont {augment /root;} ...";
     assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
     CHECK_LOG_CTX("Invalid keyword \"augment\" as a child of \"container\".", "Line number 1.");
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)c); c = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)c); c = NULL;
     in.current = " cont {nonsence true;} ...";
     assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
     CHECK_LOG_CTX("Invalid character sequence \"nonsence\", expected a keyword.", "Line number 1.");
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)c); c = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)c); c = NULL;
 
     PARSER_CUR_PMOD(YCTX)->version = 1; /* simulate YANG 1.0 */
     in.current = " cont {action x;} ...";
     assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
     CHECK_LOG_CTX("Invalid keyword \"action\" as a child of \"container\" - "
             "the statement is allowed only in YANG 1.1 modules.", "Line number 1.");
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)c); c = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)c); c = NULL;
 }
 
 static void
@@ -1148,7 +1157,7 @@
     in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
     assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node**)&l)); \
     CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)l); l = NULL;
+    lysp_node_free(&fctx, (struct lysp_node*)l); l = NULL;
 
     TEST_DUP("config", "true", "false");
     TEST_DUP("default", "x", "y");
@@ -1170,25 +1179,25 @@
     assert_string_equal("yyy", l->units);
     assert_string_equal("string", l->type.name);
     assert_non_null(l->musts);
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)l); l = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)l); l = NULL;
 
     /* full content - now with mandatory */
     in.current = "l {mandatory true; type string;} ...";
     assert_int_equal(LY_SUCCESS, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
     CHECK_LYSP_NODE(l, NULL, 0, LYS_MAND_TRUE, 0, "l", 0, LYS_LEAF, 0, NULL, 0);
     assert_string_equal("string", l->type.name);
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)l); l = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)l); l = NULL;
 
     /* invalid */
     in.current = " l {description \"missing type\";} ...";
     assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
     CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"leaf\".", "Line number 1.");
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)l); l = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)l); l = NULL;
 
     in.current = "l { type iid { path qpud wrong {";
     assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
     CHECK_LOG_CTX("Invalid character sequence \"wrong\", expected a keyword.", "Line number 1.");
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)l); l = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)l); l = NULL;
 }
 
 static void
@@ -1203,7 +1212,7 @@
     in.current = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
     assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node**)&ll)); \
     CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)ll); ll = NULL;
+    lysp_node_free(&fctx, (struct lysp_node*)ll); ll = NULL;
 
     TEST_DUP("config", "true", "false");
     TEST_DUP("description", "text1", "text2");
@@ -1233,7 +1242,7 @@
     assert_string_equal("string", ll->type.name);
     assert_non_null(ll->musts);
     assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)ll); ll = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)ll); ll = NULL;
 
     /* full content - now with min-elements */
     in.current = "ll {min-elements 10; type string;} ...";
@@ -1243,19 +1252,19 @@
     assert_int_equal(0, ll->max);
     assert_int_equal(10, ll->min);
     assert_int_equal(LYS_SET_MIN, ll->flags);
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)ll); ll = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)ll); ll = NULL;
 
     /* invalid */
     in.current = " ll {description \"missing type\";} ...";
     assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
     CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"leaf-list\".", "Line number 1.");
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)ll); ll = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)ll); ll = NULL;
 
     PARSER_CUR_PMOD(YCTX)->version = 1; /* simulate YANG 1.0 - default statement is not allowed */
     in.current = " ll {default xx; type string;} ...";
     assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
     CHECK_LOG_CTX("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules.", "Line number 1.");
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)ll); ll = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)ll); ll = NULL;
 }
 
 static void
@@ -1271,7 +1280,7 @@
     in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
     assert_int_equal(LY_EVALID, parse_list(YCTX, NULL, (struct lysp_node**)&l)); \
     CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)l); l = NULL;
+    lysp_node_free(&fctx, (struct lysp_node*)l); l = NULL;
 
     TEST_DUP("config", "true", "false");
     TEST_DUP("description", "text1", "text2");
@@ -1301,14 +1310,14 @@
     assert_non_null(l->musts);
     ly_set_erase(&YCTX->tpdfs_nodes, NULL);
     ly_set_erase(&YCTX->grps_nodes, NULL);
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)l); l = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)l); l = NULL;
 
     /* invalid content */
     PARSER_CUR_PMOD(YCTX)->version = 1; /* simulate YANG 1.0 */
     in.current = "l {action x;} ...";
     assert_int_equal(LY_EVALID, parse_list(YCTX, NULL, (struct lysp_node **)&l));
     CHECK_LOG_CTX("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules.", "Line number 1.");
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)l); l = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)l); l = NULL;
 }
 
 static void
@@ -1323,7 +1332,7 @@
     in.current = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
     assert_int_equal(LY_EVALID, parse_choice(YCTX, NULL, (struct lysp_node**)&ch)); \
     CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)ch); ch = NULL;
+    lysp_node_free(&fctx, (struct lysp_node*)ch); ch = NULL;
 
     TEST_DUP("config", "true", "false");
     TEST_DUP("default", "a", "b");
@@ -1339,14 +1348,14 @@
             "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
     assert_int_equal(LY_SUCCESS, parse_choice(YCTX, NULL, (struct lysp_node **)&ch));
     CHECK_LYSP_NODE(ch, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, 1, "ch", 0, LYS_CHOICE, 0, "test", 1);
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)ch); ch = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)ch); ch = NULL;
 
     /* full content - the default missing from the previous node */
     in.current = "ch {default c;case c;} ...";
     assert_int_equal(LY_SUCCESS, parse_choice(YCTX, NULL, (struct lysp_node **)&ch));
     CHECK_LYSP_NODE(ch, NULL, 0, 0, 0, "ch", 0, LYS_CHOICE, 0, NULL, 0);
     assert_string_equal("c", ch->dflt.str);
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)ch); ch = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)ch); ch = NULL;
 }
 
 static void
@@ -1361,7 +1370,7 @@
     in.current = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
     assert_int_equal(LY_EVALID, parse_case(YCTX, NULL, (struct lysp_node**)&cs)); \
     CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)cs); cs = NULL;
+    lysp_node_free(&fctx, (struct lysp_node*)cs); cs = NULL;
 
     TEST_DUP("description", "text1", "text2");
     TEST_DUP("reference", "1", "2");
@@ -1374,13 +1383,13 @@
             "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
     assert_int_equal(LY_SUCCESS, parse_case(YCTX, NULL, (struct lysp_node **)&cs));
     CHECK_LYSP_NODE(cs, "test", 1, LYS_STATUS_CURR, 1, "cs", 0, LYS_CASE, 0, "test", 1);
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)cs); cs = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)cs); cs = NULL;
 
     /* invalid content */
     in.current = "cs {config true} ...";
     assert_int_equal(LY_EVALID, parse_case(YCTX, NULL, (struct lysp_node **)&cs));
     CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"case\".", "Line number 1.");
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)cs); cs = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)cs); cs = NULL;
 }
 
 static void
@@ -1399,7 +1408,7 @@
     in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
     assert_int_equal(LY_EVALID, parse_any(YCTX, kw, NULL, (struct lysp_node**)&any)); \
     CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)any); any = NULL;
+    lysp_node_free(&fctx, (struct lysp_node*)any); any = NULL;
 
     TEST_DUP("config", "true", "false");
     TEST_DUP("description", "text1", "text2");
@@ -1416,7 +1425,7 @@
     uint16_t node_type = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
     CHECK_LYSP_NODE(any, "test", 1, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, 1, "any", 0, node_type, 0, "test", 1);
     assert_non_null(any->musts);
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)any); any = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)any); any = NULL;
 }
 
 static void
@@ -1444,7 +1453,7 @@
     in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
     assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp)); \
     CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, &grp->node); grp = NULL;
+    lysp_node_free(&fctx, &grp->node); grp = NULL;
 
     TEST_DUP("description", "text1", "text2");
     TEST_DUP("reference", "1", "2");
@@ -1465,20 +1474,20 @@
     assert_int_equal(LYS_STATUS_CURR, grp->flags);
     ly_set_erase(&YCTX->tpdfs_nodes, NULL);
     ly_set_erase(&YCTX->grps_nodes, NULL);
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, &grp->node);
+    lysp_node_free(&fctx, &grp->node);
     grp = NULL;
 
     /* invalid content */
     in.current = "grp {config true} ...";
     assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp));
     CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"grouping\".", "Line number 1.");
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, &grp->node);
+    lysp_node_free(&fctx, &grp->node);
     grp = NULL;
 
     in.current = "grp {must 'expr'} ...";
     assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp));
     CHECK_LOG_CTX("Invalid keyword \"must\" as a child of \"grouping\".", "Line number 1.");
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, &grp->node);
+    lysp_node_free(&fctx, &grp->node);
     grp = NULL;
 }
 
@@ -1496,7 +1505,7 @@
     in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
     assert_int_equal(LY_EVALID, parse_action(YCTX, NULL, &rpcs)); \
     CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)rpcs); rpcs = NULL;
+    lysp_node_free(&fctx, (struct lysp_node*)rpcs); rpcs = NULL;
 
     TEST_DUP("description", "text1", "text2");
     TEST_DUP("input", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
@@ -1541,15 +1550,15 @@
 
     ly_set_erase(&YCTX->tpdfs_nodes, NULL);
     ly_set_erase(&YCTX->grps_nodes, NULL);
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)rpcs); rpcs = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)rpcs); rpcs = NULL;
 
     /* invalid content */
     in.current = "func {config true} ...";
     assert_int_equal(LY_EVALID, parse_action(YCTX, NULL, &rpcs));
     CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"rpc\".", "Line number 1.");
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)rpcs); rpcs = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)rpcs); rpcs = NULL;
 
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)c);
+    lysp_node_free(&fctx, (struct lysp_node *)c);
 }
 
 static void
@@ -1566,7 +1575,7 @@
     in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
     assert_int_equal(LY_EVALID, parse_notif(YCTX, NULL, &notifs)); \
     CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)notifs); notifs = NULL;
+    lysp_node_free(&fctx, (struct lysp_node*)notifs); notifs = NULL;
 
     TEST_DUP("description", "text1", "text2");
     TEST_DUP("reference", "1", "2");
@@ -1594,15 +1603,15 @@
 
     ly_set_erase(&YCTX->tpdfs_nodes, NULL);
     ly_set_erase(&YCTX->grps_nodes, NULL);
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)notifs); notifs = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)notifs); notifs = NULL;
 
     /* invalid content */
     in.current = "ntf {config true} ...";
     assert_int_equal(LY_EVALID, parse_notif(YCTX, NULL, &notifs));
     CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"notification\".", "Line number 1.");
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)notifs); notifs = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)notifs); notifs = NULL;
 
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)c);
+    lysp_node_free(&fctx, (struct lysp_node *)c);
 }
 
 static void
@@ -1617,7 +1626,7 @@
     in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
     assert_int_equal(LY_EVALID, parse_uses(YCTX, NULL, (struct lysp_node**)&u)); \
     CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)u); u = NULL;
+    lysp_node_free(&fctx, (struct lysp_node*)u); u = NULL;
 
     TEST_DUP("description", "text1", "text2");
     TEST_DUP("reference", "1", "2");
@@ -1631,7 +1640,7 @@
     CHECK_LYSP_NODE(u, "test", 1, LYS_STATUS_CURR, 1, "grpref", 0, LYS_USES, 0, "test", 1);
     assert_non_null(u->augments);
     assert_non_null(u->refines);
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)u); u = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)u); u = NULL;
 }
 
 static void
@@ -1646,7 +1655,7 @@
     in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
     assert_int_equal(LY_EVALID, parse_augment(YCTX, NULL, &a)); \
     CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)a); a = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)a); a = NULL;
 
     TEST_DUP("description", "text1", "text2");
     TEST_DUP("reference", "1", "2");
@@ -1668,7 +1677,7 @@
     assert_non_null(a->when);
     assert_null(a->parent);
     assert_int_equal(LYS_STATUS_CURR, a->flags);
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)a); a = NULL;
+    lysp_node_free(&fctx, (struct lysp_node *)a); a = NULL;
 }
 
 static void
diff --git a/tests/utests/schema/test_yin.c b/tests/utests/schema/test_yin.c
index 6f75f6b..6b8a4b4 100644
--- a/tests/utests/schema/test_yin.c
+++ b/tests/utests/schema/test_yin.c
@@ -110,6 +110,7 @@
     CHECK_LYSP_EXT_INSTANCE((NODE), NULL, 1, INSUBSTMT, 0, "myext:c-define", LY_VALUE_XML)
 
 struct lys_yin_parser_ctx *YCTX;
+struct lysf_ctx fctx;
 
 static int
 setup_ctx(void **state)
@@ -140,13 +141,16 @@
 
     setup_ctx(state);
 
+    fctx.ctx = UTEST_LYCTX;
+    fctx.mod = PARSER_CUR_PMOD(YCTX)->mod;
+
     return 0;
 }
 
 static int
 teardown_ctx(void **UNUSED(state))
 {
-    lys_module_free(PARSER_CUR_PMOD(YCTX)->mod, 0);
+    lys_module_free(&fctx, PARSER_CUR_PMOD(YCTX)->mod, 0);
     yin_parser_ctx_free(YCTX);
     YCTX = NULL;
 
@@ -158,6 +162,8 @@
 {
     teardown_ctx(state);
 
+    lysf_ctx_erase(&fctx);
+
     UTEST_TEARDOWN;
 
     return 0;
@@ -1286,7 +1292,7 @@
     CHECK_LYSP_TYPE_ENUM(type.enums, "desc...", 1, flags, 1, "enum-name", "ref...", 55);
     assert_string_equal(type.enums->iffeatures[0].str, "feature");
     TEST_1_CHECK_LYSP_EXT_INSTANCE(type.enums->exts, LY_STMT_ENUM);
-    lysp_type_free(UTEST_LYCTX, &type);
+    lysp_type_free(&fctx, &type);
     memset(&type, 0, sizeof type);
 
     data = ELEMENT_WRAPPER_START
@@ -1294,7 +1300,7 @@
             ELEMENT_WRAPPER_END;
     assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
     assert_string_equal(type.enums->name, "enum-name");
-    lysp_type_free(UTEST_LYCTX, &type);
+    lysp_type_free(&fctx, &type);
     memset(&type, 0, sizeof type);
 }
 
@@ -1320,7 +1326,7 @@
     CHECK_LYSP_TYPE_ENUM(type.bits, "desc...", 1, flags, 1, "bit-name", "ref...", 55);
     assert_string_equal(type.bits->iffeatures[0].str, "feature");
     TEST_1_CHECK_LYSP_EXT_INSTANCE(type.bits->exts, LY_STMT_BIT);
-    lysp_type_free(UTEST_LYCTX, &type);
+    lysp_type_free(&fctx, &type);
     memset(&type, 0, sizeof type);
 
     data = ELEMENT_WRAPPER_START
@@ -1328,7 +1334,7 @@
             ELEMENT_WRAPPER_END;
     assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
     CHECK_LYSP_TYPE_ENUM(type.bits, NULL, 0, 0, 0, "bit-name", NULL, 0);
-    lysp_type_free(UTEST_LYCTX, &type);
+    lysp_type_free(&fctx, &type);
     memset(&type, 0, sizeof type);
 }
 
@@ -1518,7 +1524,7 @@
             "err-app-tag", "err-msg", 1, "ref");
     assert_true(type.flags & LYS_SET_LENGTH);
     TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.length->exts[0]), LY_STMT_LENGTH);
-    lysp_type_free(UTEST_LYCTX, &type);
+    lysp_type_free(&fctx, &type);
     memset(&type, 0, sizeof(type));
 
     /* min subelems */
@@ -1529,14 +1535,14 @@
     assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
     CHECK_LYSP_RESTR(type.length, "length-str", NULL,
             NULL, NULL, 0, NULL);
-    lysp_type_free(UTEST_LYCTX, &type);
+    lysp_type_free(&fctx, &type);
     assert_true(type.flags & LYS_SET_LENGTH);
     memset(&type, 0, sizeof(type));
 
     data = ELEMENT_WRAPPER_START "<length></length>" ELEMENT_WRAPPER_END;
     assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
     CHECK_LOG_CTX("Missing mandatory attribute value of length element.", "Line number 1.");
-    lysp_type_free(UTEST_LYCTX, &type);
+    lysp_type_free(&fctx, &type);
     memset(&type, 0, sizeof(type));
 }
 
@@ -1587,14 +1593,14 @@
     CHECK_LYSP_RESTR(type.patterns, "\x015super_pattern", "\"pattern-desc\"",
             "err-app-tag-value", "err-msg-value", 1, "pattern-ref");
     TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.patterns->exts[0]), LY_STMT_PATTERN);
-    lysp_type_free(UTEST_LYCTX, &type);
+    lysp_type_free(&fctx, &type);
     memset(&type, 0, sizeof(type));
 
     /* min subelems */
     data = ELEMENT_WRAPPER_START "<pattern value=\"pattern\"> </pattern>" ELEMENT_WRAPPER_END;
     assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
     CHECK_LYSP_RESTR(type.patterns, "\x006pattern", NULL, NULL, NULL, 0, NULL);
-    lysp_type_free(UTEST_LYCTX, &type);
+    lysp_type_free(&fctx, &type);
     memset(&type, 0, sizeof(type));
 }
 
@@ -1690,7 +1696,7 @@
             "err-app-tag", "err-msg", 1, "ref");
     assert_true(type.flags & LYS_SET_RANGE);
     TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.range->exts[0]), LY_STMT_RANGE);
-    lysp_type_free(UTEST_LYCTX, &type);
+    lysp_type_free(&fctx, &type);
     memset(&type, 0, sizeof(type));
 
     /* min subelems */
@@ -1698,7 +1704,7 @@
     assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
     CHECK_LYSP_RESTR(type.range, "range-str", NULL,
             NULL, NULL, 0, NULL);
-    lysp_type_free(UTEST_LYCTX, &type);
+    lysp_type_free(&fctx, &type);
     memset(&type, 0, sizeof(type));
 }
 
@@ -1713,7 +1719,7 @@
     assert_int_equal(type.require_instance, 1);
     assert_true(type.flags & LYS_SET_REQINST);
     TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.exts[0]), LY_STMT_REQUIRE_INSTANCE);
-    lysp_type_free(UTEST_LYCTX, &type);
+    lysp_type_free(&fctx, &type);
     memset(&type, 0, sizeof(type));
 
     data = ELEMENT_WRAPPER_START "<require-instance value=\"false\"/>" ELEMENT_WRAPPER_END;
@@ -1841,13 +1847,13 @@
     assert_true(type.flags & LYS_SET_RANGE);
     assert_true(type.flags & LYS_SET_REQINST);
     assert_true(type.flags & LYS_SET_TYPE);
-    lysp_type_free(UTEST_LYCTX, &type);
+    lysp_type_free(&fctx, &type);
     memset(&type, 0, sizeof(type));
 
     /* min subelems */
     data = ELEMENT_WRAPPER_START "<type name=\"type-name\"/>" ELEMENT_WRAPPER_END;
     assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
-    lysp_type_free(UTEST_LYCTX, &type);
+    lysp_type_free(&fctx, &type);
     memset(&type, 0, sizeof(type));
 }
 
@@ -1950,7 +1956,7 @@
     CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
     assert_string_equal(parsed->iffeatures[0].str, "feature");
     TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_ANYXML);
-    lysp_node_free(UTEST_LYCTX, siblings);
+    lysp_node_free(&fctx, siblings);
     siblings = NULL;
 
     /* anydata max subelems */
@@ -1975,7 +1981,7 @@
     CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
     assert_string_equal(parsed->iffeatures[0].str, "feature");
     TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_ANYDATA);
-    lysp_node_free(UTEST_LYCTX, siblings);
+    lysp_node_free(&fctx, siblings);
     siblings = NULL;
 
     /* min subelems */
@@ -1986,7 +1992,7 @@
     assert_ptr_equal(parsed->parent, node_meta.parent);
     CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
             "any-name", 0, LYS_ANYDATA, 1, NULL, 0);
-    lysp_node_free(UTEST_LYCTX, siblings);
+    lysp_node_free(&fctx, siblings);
 }
 
 static void
@@ -2027,7 +2033,7 @@
     assert_string_equal(parsed->type.name, "type");
     assert_string_equal(parsed->units, "uni");
     assert_string_equal(parsed->dflt.str, "def-val");
-    lysp_node_free(UTEST_LYCTX, siblings);
+    lysp_node_free(&fctx, siblings);
     siblings = NULL;
 
     /* min elements */
@@ -2036,7 +2042,7 @@
     parsed = (struct lysp_node_leaf *)siblings;
     assert_string_equal(parsed->name, "leaf");
     assert_string_equal(parsed->type.name, "type");
-    lysp_node_free(UTEST_LYCTX, siblings);
+    lysp_node_free(&fctx, siblings);
     siblings = NULL;
 }
 
@@ -2081,7 +2087,7 @@
     assert_string_equal(parsed->units, "uni");
     CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
     TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_LEAF_LIST);
-    lysp_node_free(UTEST_LYCTX, siblings);
+    lysp_node_free(&fctx, siblings);
     siblings = NULL;
 
     data = ELEMENT_WRAPPER_START
@@ -2112,7 +2118,7 @@
     assert_string_equal(parsed->type.name, "type");
     assert_string_equal(parsed->units, "uni");
     TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_LEAF_LIST);
-    lysp_node_free(UTEST_LYCTX, siblings);
+    lysp_node_free(&fctx, siblings);
     siblings = NULL;
 
     data = ELEMENT_WRAPPER_START
@@ -2143,7 +2149,7 @@
     assert_int_equal(parsed->max, 15);
     assert_string_equal(parsed->type.name, "type");
     assert_string_equal(parsed->units, "uni");
-    lysp_node_free(UTEST_LYCTX, siblings);
+    lysp_node_free(&fctx, siblings);
     siblings = NULL;
 
     data = ELEMENT_WRAPPER_START
@@ -2155,7 +2161,7 @@
     parsed = (struct lysp_node_leaflist *)siblings;
     assert_string_equal(parsed->name, "llist");
     assert_string_equal(parsed->type.name, "type");
-    lysp_node_free(UTEST_LYCTX, siblings);
+    lysp_node_free(&fctx, siblings);
     siblings = NULL;
 
     /* invalid combinations */
@@ -2168,7 +2174,7 @@
             ELEMENT_WRAPPER_END;
     assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_EVALID);
     CHECK_LOG_CTX("Invalid combination of min-elements and max-elements: min value 15 is bigger than the max value 5.", "Line number 4.");
-    lysp_node_free(UTEST_LYCTX, siblings);
+    lysp_node_free(&fctx, siblings);
     siblings = NULL;
 
     data = ELEMENT_WRAPPER_START
@@ -2180,7 +2186,7 @@
             ELEMENT_WRAPPER_END;
     assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_EVALID);
     CHECK_LOG_CTX("Invalid combination of sub-elemnts \"min-elements\" and \"default\" in \"leaf-list\" element.", "Line number 5.");
-    lysp_node_free(UTEST_LYCTX, siblings);
+    lysp_node_free(&fctx, siblings);
     siblings = NULL;
 
     data = ELEMENT_WRAPPER_START
@@ -2189,7 +2195,7 @@
             ELEMENT_WRAPPER_END;
     assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_EVALID);
     CHECK_LOG_CTX("Missing mandatory sub-element \"type\" of \"leaf-list\" element.", "Line number 1.");
-    lysp_node_free(UTEST_LYCTX, siblings);
+    lysp_node_free(&fctx, siblings);
     siblings = NULL;
 }
 
@@ -2255,14 +2261,14 @@
     assert_string_equal(parsed->refines->nodeid, "target");
     assert_string_equal(parsed->augments->nodeid, "target");
     TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_USES);
-    lysp_node_free(UTEST_LYCTX, siblings);
+    lysp_node_free(&fctx, siblings);
     siblings = NULL;
 
     /* min subelems */
     data = ELEMENT_WRAPPER_START "<uses name=\"uses-name\"/>" ELEMENT_WRAPPER_END;
     assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
     assert_string_equal(siblings[0].name, "uses-name");
-    lysp_node_free(UTEST_LYCTX, siblings);
+    lysp_node_free(&fctx, siblings);
     siblings = NULL;
 }
 
@@ -2338,7 +2344,7 @@
     assert_string_equal(parsed->typedefs->name, "tpdf");
     assert_string_equal(parsed->uniques->str, "utag");
     TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_LIST);
-    lysp_node_free(UTEST_LYCTX, siblings);
+    lysp_node_free(&fctx, siblings);
     ly_set_erase(&YCTX->tpdfs_nodes, NULL);
     siblings = NULL;
 
@@ -2348,7 +2354,7 @@
     parsed = (struct lysp_node_list *)&siblings[0];
     CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
             "list-name", 0, LYS_LIST, 0, NULL, 0);
-    lysp_node_free(UTEST_LYCTX, siblings);
+    lysp_node_free(&fctx, siblings);
     siblings = NULL;
 }
 
@@ -2410,14 +2416,14 @@
     assert_string_equal(notifs->ref, "ref");
     assert_string_equal(notifs->typedefs->name, "tpdf");
     TEST_1_CHECK_LYSP_EXT_INSTANCE(&(notifs->exts[0]), LY_STMT_NOTIFICATION);
-    lysp_node_free(UTEST_LYCTX, (struct lysp_node *)notifs);
+    lysp_node_free(&fctx, (struct lysp_node *)notifs);
     notifs = NULL;
 
     /* min subelems */
     data = ELEMENT_WRAPPER_START "<notification name=\"notif-name\" />" ELEMENT_WRAPPER_END;
     assert_int_equal(test_element_helper(state, data, &notif_meta, NULL, NULL), LY_SUCCESS);
     assert_string_equal(notifs->name, "notif-name");
-    lysp_node_free(UTEST_LYCTX, (struct lysp_node *)notifs);
+    lysp_node_free(&fctx, (struct lysp_node *)notifs);
     notifs = NULL;
 }
 
@@ -2472,14 +2478,14 @@
     assert_string_equal(grps->child->next->next->next->next->next->next->next->name, "choice");
     assert_int_equal(grps->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
     TEST_1_CHECK_LYSP_EXT_INSTANCE(&(grps->exts[0]), LY_STMT_GROUPING);
-    lysp_node_free(UTEST_LYCTX, &grps->node);
+    lysp_node_free(&fctx, &grps->node);
     grps = NULL;
 
     /* min subelems */
     data = ELEMENT_WRAPPER_START "<grouping name=\"grp-name\" />" ELEMENT_WRAPPER_END;
     assert_int_equal(test_element_helper(state, data, &grp_meta, NULL, NULL), LY_SUCCESS);
     assert_string_equal(grps->name, "grp-name");
-    lysp_node_free(UTEST_LYCTX, &grps->node);
+    lysp_node_free(&fctx, &grps->node);
     grps = NULL;
 }
 
@@ -2551,7 +2557,7 @@
     assert_string_equal(parsed->notifs->name, "notf");
     assert_string_equal(parsed->actions->name, "act");
     TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_CONTAINER);
-    lysp_node_free(UTEST_LYCTX, siblings);
+    lysp_node_free(&fctx, siblings);
     ly_set_erase(&YCTX->tpdfs_nodes, NULL);
     siblings = NULL;
 
@@ -2561,7 +2567,7 @@
     parsed = (struct lysp_node_container *)siblings;
     CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
             "cont-name", 0, LYS_CONTAINER, 0, NULL, 0);
-    lysp_node_free(UTEST_LYCTX, siblings);
+    lysp_node_free(&fctx, siblings);
     siblings = NULL;
 }
 
@@ -2619,7 +2625,7 @@
     assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
     assert_null(parsed->child->next->next->next->next->next->next->next->next);
     TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_CASE);
-    lysp_node_free(UTEST_LYCTX, siblings);
+    lysp_node_free(&fctx, siblings);
     siblings = NULL;
 
     /* min subelems */
@@ -2628,7 +2634,7 @@
     parsed = (struct lysp_node_case *)siblings;
     CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
             "case-name", 0, LYS_CASE, 0, NULL, 0);
-    lysp_node_free(UTEST_LYCTX, siblings);
+    lysp_node_free(&fctx, siblings);
     siblings = NULL;
 }
 
@@ -2689,7 +2695,7 @@
     assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
     assert_null(parsed->child->next->next->next->next->next->next->next->next);
     TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_CHOICE);
-    lysp_node_free(UTEST_LYCTX, siblings);
+    lysp_node_free(&fctx, siblings);
     siblings = NULL;
 
     /* min subelems */
@@ -2699,7 +2705,7 @@
     assert_string_equal(parsed->name, "choice-name");
     CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
             "choice-name", 0, LYS_CHOICE, 0, NULL, 0);
-    lysp_node_free(UTEST_LYCTX, siblings);
+    lysp_node_free(&fctx, siblings);
     siblings = NULL;
 }
 
@@ -2751,7 +2757,7 @@
     assert_int_equal(inout.child->next->next->next->next->next->next->next->nodetype, LYS_USES);
     assert_null(inout.child->next->next->next->next->next->next->next->next);
     TEST_1_CHECK_LYSP_EXT_INSTANCE(&(inout.exts[0]), LY_STMT_INPUT);
-    lysp_node_free(UTEST_LYCTX, (struct lysp_node *)&inout);
+    lysp_node_free(&fctx, (struct lysp_node *)&inout);
     memset(&inout, 0, sizeof inout);
 
     /* max subelements */
@@ -2795,24 +2801,24 @@
     assert_int_equal(inout.child->next->next->next->next->next->next->next->nodetype, LYS_USES);
     assert_null(inout.child->next->next->next->next->next->next->next->next);
     TEST_1_CHECK_LYSP_EXT_INSTANCE(&(inout.exts[0]), LY_STMT_OUTPUT);
-    lysp_node_free(UTEST_LYCTX, (struct lysp_node *)&inout);
+    lysp_node_free(&fctx, (struct lysp_node *)&inout);
     memset(&inout, 0, sizeof inout);
 
     /* min subelems */
     data = ELEMENT_WRAPPER_START "<input><leaf name=\"l\"><type name=\"empty\"/></leaf></input>" ELEMENT_WRAPPER_END;
     assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
-    lysp_node_free(UTEST_LYCTX, (struct lysp_node *)&inout);
+    lysp_node_free(&fctx, (struct lysp_node *)&inout);
     memset(&inout, 0, sizeof inout);
 
     data = ELEMENT_WRAPPER_START "<output><leaf name=\"l\"><type name=\"empty\"/></leaf></output>" ELEMENT_WRAPPER_END;
     assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
-    lysp_node_free(UTEST_LYCTX, (struct lysp_node *)&inout);
+    lysp_node_free(&fctx, (struct lysp_node *)&inout);
     memset(&inout, 0, sizeof inout);
 
     /* invalid combinations */
     data = ELEMENT_WRAPPER_START "<input name=\"test\"/>" ELEMENT_WRAPPER_END;
     assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_EVALID);
-    lysp_node_free(UTEST_LYCTX, (struct lysp_node *)&inout);
+    lysp_node_free(&fctx, (struct lysp_node *)&inout);
     CHECK_LOG_CTX("Unexpected attribute \"name\" of \"input\" element.", "Line number 1.");
     memset(&inout, 0, sizeof inout);
 }
@@ -2859,7 +2865,7 @@
     assert_string_equal(actions->output.musts->arg.str, "cond");
     assert_string_equal(actions->input.child->name, "uses-name");
     TEST_1_CHECK_LYSP_EXT_INSTANCE(&(actions->exts[0]), LY_STMT_ACTION);
-    lysp_node_free(UTEST_LYCTX, (struct lysp_node *)actions);
+    lysp_node_free(&fctx, (struct lysp_node *)actions);
     actions = NULL;
 
     PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
@@ -2892,14 +2898,14 @@
     assert_string_equal(actions->input.child->name, "uses-name");
     assert_string_equal(actions->output.musts->arg.str, "cond");
     TEST_1_CHECK_LYSP_EXT_INSTANCE(&(actions->exts[0]), LY_STMT_RPC);
-    lysp_node_free(UTEST_LYCTX, (struct lysp_node *)actions);
+    lysp_node_free(&fctx, (struct lysp_node *)actions);
     actions = NULL;
 
     /* min subelems */
     data = ELEMENT_WRAPPER_START "<action name=\"act\" />" ELEMENT_WRAPPER_END;
     assert_int_equal(test_element_helper(state, data, &act_meta, NULL, NULL), LY_SUCCESS);
     assert_string_equal(actions->name, "act");
-    lysp_node_free(UTEST_LYCTX, (struct lysp_node *)actions);
+    lysp_node_free(&fctx, (struct lysp_node *)actions);
     actions = NULL;
 }
 
@@ -2963,13 +2969,13 @@
     assert_string_equal(augments->actions->name, "action");
     assert_string_equal(augments->notifs->name, "notif");
     TEST_1_CHECK_LYSP_EXT_INSTANCE(&(augments->exts[0]), LY_STMT_AUGMENT);
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)augments);
+    lysp_node_free(&fctx, (struct lysp_node *)augments);
     augments = NULL;
 
     data = ELEMENT_WRAPPER_START "<augment target-node=\"target\" />" ELEMENT_WRAPPER_END;
     assert_int_equal(test_element_helper(state, data, &aug_meta, NULL, NULL), LY_SUCCESS);
     assert_string_equal(augments->nodeid, "target");
-    lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)augments);
+    lysp_node_free(&fctx, (struct lysp_node *)augments);
     augments = NULL;
 }
 
@@ -3031,13 +3037,15 @@
     struct ly_ctx *ly_ctx = PARSER_CUR_PMOD(ctx)->mod->ctx;
     struct lysp_module *pmod;
 
-    lys_module_free(PARSER_CUR_PMOD(ctx)->mod, 0);
+    lys_module_free(&fctx, PARSER_CUR_PMOD(ctx)->mod, 0);
     pmod = calloc(1, sizeof *pmod);
     ctx->parsed_mods->objs[0] = pmod;
     pmod->mod = calloc(1, sizeof *pmod->mod);
     pmod->mod->parsed = pmod;
     pmod->mod->ctx = ly_ctx;
 
+    fctx.mod = pmod->mod;
+
     return pmod;
 }
 
@@ -3161,7 +3169,7 @@
     struct ly_ctx *ly_ctx = PARSER_CUR_PMOD(ctx)->mod->ctx;
     struct lysp_submodule *submod;
 
-    lys_module_free(PARSER_CUR_PMOD(ctx)->mod, 0);
+    lys_module_free(&fctx, PARSER_CUR_PMOD(ctx)->mod, 0);
     submod = calloc(1, sizeof *submod);
     ctx->parsed_mods->objs[0] = submod;
     submod->mod = calloc(1, sizeof *submod->mod);
@@ -3169,6 +3177,8 @@
     submod->mod->parsed = (struct lysp_module *)submod;
     submod->mod->ctx = ly_ctx;
 
+    fctx.mod = submod->mod;
+
     return submod;
 }
 
@@ -3320,7 +3330,7 @@
     assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS);
     assert_null(mod->parsed->exts->child->next->child);
     assert_string_equal(mod->parsed->exts->child->next->arg, "test");
-    lys_module_free(mod, 0);
+    lys_module_free(&fctx, mod, 0);
     yin_parser_ctx_free(yin_ctx);
     ly_in_free(in, 0);
     mod = NULL;
@@ -3358,7 +3368,7 @@
             "</module>\n";
     assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
     assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS);
-    lys_module_free(mod, 0);
+    lys_module_free(&fctx, mod, 0);
     yin_parser_ctx_free(yin_ctx);
     ly_in_free(in, 0);
     mod = NULL;
@@ -3373,7 +3383,7 @@
             "</module>\n";
     assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
     assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS);
-    lys_module_free(mod, 0);
+    lys_module_free(&fctx, mod, 0);
     yin_parser_ctx_free(yin_ctx);
     ly_in_free(in, 0);
     mod = NULL;
@@ -3386,7 +3396,7 @@
     assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
     assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_EINVAL);
     CHECK_LOG_CTX("Input data contains submodule which cannot be parsed directly without its main module.", NULL);
-    lys_module_free(mod, 0);
+    lys_module_free(&fctx, mod, 0);
     yin_parser_ctx_free(yin_ctx);
     ly_in_free(in, 0);
 
@@ -3401,7 +3411,7 @@
     assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
     assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_EVALID);
     CHECK_LOG_CTX("Trailing garbage \"<module>\" after module, expected end-of-input.", "Line number 6.");
-    lys_module_free(mod, 0);
+    lys_module_free(&fctx, mod, 0);
     yin_parser_ctx_free(yin_ctx);
     ly_in_free(in, 0);
     mod = NULL;
@@ -3444,7 +3454,7 @@
             "</submodule>";
     assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
     assert_int_equal(yin_parse_submodule(&yin_ctx, UTEST_LYCTX, (struct lys_parser_ctx *)YCTX, in, &submod), LY_SUCCESS);
-    lysp_module_free((struct lysp_module *)submod);
+    lysp_module_free(&fctx, (struct lysp_module *)submod);
     yin_parser_ctx_free(yin_ctx);
     ly_in_free(in, 0);
     yin_ctx = NULL;
@@ -3459,7 +3469,7 @@
             "</submodule>";
     assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
     assert_int_equal(yin_parse_submodule(&yin_ctx, UTEST_LYCTX, (struct lys_parser_ctx *)YCTX, in, &submod), LY_SUCCESS);
-    lysp_module_free((struct lysp_module *)submod);
+    lysp_module_free(&fctx, (struct lysp_module *)submod);
     yin_parser_ctx_free(yin_ctx);
     ly_in_free(in, 0);
     yin_ctx = NULL;
@@ -3471,7 +3481,7 @@
     assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
     assert_int_equal(yin_parse_submodule(&yin_ctx, UTEST_LYCTX, (struct lys_parser_ctx *)YCTX, in, &submod), LY_EINVAL);
     CHECK_LOG_CTX("Input data contains module in situation when a submodule is expected.", NULL);
-    lysp_module_free((struct lysp_module *)submod);
+    lysp_module_free(&fctx, (struct lysp_module *)submod);
     yin_parser_ctx_free(yin_ctx);
     ly_in_free(in, 0);
     yin_ctx = NULL;
@@ -3493,7 +3503,7 @@
     assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
     assert_int_equal(yin_parse_submodule(&yin_ctx, UTEST_LYCTX, (struct lys_parser_ctx *)YCTX, in, &submod), LY_EVALID);
     CHECK_LOG_CTX("Trailing garbage \"<submodule name...\" after submodule, expected end-of-input.", "Line number 8.");
-    lysp_module_free((struct lysp_module *)submod);
+    lysp_module_free(&fctx, (struct lysp_module *)submod);
     yin_parser_ctx_free(yin_ctx);
     ly_in_free(in, 0);
     yin_ctx = NULL;