selinux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 8/8 v2] libsepol/cil: When writing AST use line marks for src_info nodes
@ 2021-08-17 20:20 James Carter
  0 siblings, 0 replies; only message in thread
From: James Carter @ 2021-08-17 20:20 UTC (permalink / raw)
  To: selinux; +Cc: nicolas.iooss, James Carter

In order to retain as much information as possible, when writing
out the CIL AST, use line mark notation to write out src_info
nodes. This includes using line marks to denote the original CIL
files the AST comes from.

The line numbers will not always be exactly correct because any
blank lines and comments in the original files will not be
represented in the AST.

Line marks are not written for the parse tree because the line
numbers will be widely inaccurate since each token will be on
a different line.

Signed-off-by: James Carter <jwcart2@gmail.com>
---
 libsepol/cil/src/cil_write_ast.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/libsepol/cil/src/cil_write_ast.c b/libsepol/cil/src/cil_write_ast.c
index 186070c1..d7f00bcc 100644
--- a/libsepol/cil/src/cil_write_ast.c
+++ b/libsepol/cil/src/cil_write_ast.c
@@ -546,6 +546,18 @@ static const char *macro_param_flavor_to_string(enum cil_flavor flavor)
 	return str;
 }
 
+void cil_write_src_info_node(FILE *out, struct cil_tree_node *node)
+{
+	struct cil_src_info *info = node->data;
+	if (info->kind == CIL_KEY_SRC_CIL || info->kind == CIL_KEY_SRC_HLL_LMS) {
+		fprintf(out, ";;* lms %u %s\n", info->hll_line, info->path);
+	} else if (info->kind == CIL_KEY_SRC_HLL_LMX) {
+		fprintf(out, ";;* lmx %u %s\n", info->hll_line, info->path);
+	} else {
+		fprintf(out, ";;* <?SRC_INFO_KIND> %u %s\n", info->hll_line, info->path);
+	}
+}
+
 void cil_write_ast_node(FILE *out, struct cil_tree_node *node)
 {
 	if (!node->data) {
@@ -1508,8 +1520,10 @@ static int __write_cil_ast_node_helper(struct cil_tree_node *node, uint32_t *fin
 {
 	struct cil_write_ast_args *args = extra_args;
 
-	if (node->flavor == CIL_SRC_INFO)
+	if (node->flavor == CIL_SRC_INFO) {
+		cil_write_src_info_node(args->out, node);
 		return SEPOL_OK;
+	}
 
 	fprintf(args->out, "%*s", args->depth*4, "");
 
@@ -1539,7 +1553,10 @@ static int __write_cil_ast_last_child_helper(struct cil_tree_node *node, void *e
 	struct cil_write_ast_args *args = extra_args;
 	struct cil_tree_node *parent = node->parent;
 
-	if (parent->flavor == CIL_SRC_INFO || parent->flavor == CIL_ROOT) {
+	if (parent->flavor == CIL_ROOT) {
+		return SEPOL_OK;
+	} else if (parent->flavor == CIL_SRC_INFO) {
+		fprintf(args->out, ";;* lme\n");
 		return SEPOL_OK;
 	}
 
-- 
2.31.1


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2021-08-17 20:22 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-17 20:20 [PATCH 8/8 v2] libsepol/cil: When writing AST use line marks for src_info nodes James Carter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).