4 * LTTng filter XML pretty printer visitor
6 * Copyright 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * SPDX-License-Identifier: LGPL-2.1-only
18 #include "filter-ast.h"
19 #include "filter-parser.h"
21 #include <common/compat/errno.h>
22 #include <common/macros.h>
24 #define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
27 int recursive_visit_print(struct filter_node
*node
, FILE *stream
, int indent
);
30 void print_tabs(FILE *fd
, int depth
)
34 for (i
= 0; i
< depth
; i
++)
39 int recursive_visit_print_expression(struct filter_node
*node
,
40 FILE *stream
, int indent
)
42 struct filter_node
*iter_node
;
45 fprintf(stderr
, "[error] %s: NULL child\n", __func__
);
48 switch (node
->u
.expression
.type
) {
51 fprintf(stderr
, "[error] %s: unknown expression\n", __func__
);
54 print_tabs(stream
, indent
);
55 fprintf(stream
, "<string value=\"%s\"/>\n",
56 node
->u
.expression
.u
.string
);
58 case AST_EXP_CONSTANT
:
59 print_tabs(stream
, indent
);
60 fprintf(stream
, "<constant value=\"%" PRIu64
"\"/>\n",
61 node
->u
.expression
.u
.constant
);
63 case AST_EXP_FLOAT_CONSTANT
:
64 print_tabs(stream
, indent
);
65 fprintf(stream
, "<float_constant value=\"%lg\"/>\n",
66 node
->u
.expression
.u
.float_constant
);
68 case AST_EXP_IDENTIFIER
: /* fall-through */
69 case AST_EXP_GLOBAL_IDENTIFIER
:
70 print_tabs(stream
, indent
);
71 fprintf(stream
, "<%s value=\"%s\"/>\n",
72 node
->u
.expression
.type
== AST_EXP_IDENTIFIER
?
73 "identifier" : "global_identifier",
74 node
->u
.expression
.u
.identifier
);
75 iter_node
= node
->u
.expression
.next
;
77 print_tabs(stream
, indent
);
78 fprintf(stream
, "<bracket>\n");
79 if (recursive_visit_print_expression(iter_node
,
80 stream
, indent
+ 1)) {
83 print_tabs(stream
, indent
);
84 fprintf(stream
, "</bracket>\n");
85 iter_node
= iter_node
->u
.expression
.next
;
90 return recursive_visit_print(node
->u
.expression
.u
.child
,
98 int recursive_visit_print(struct filter_node
*node
, FILE *stream
, int indent
)
103 fprintf(stderr
, "[error] %s: NULL child\n", __func__
);
106 switch (node
->type
) {
109 fprintf(stderr
, "[error] %s: unknown node type\n", __func__
);
112 print_tabs(stream
, indent
);
113 fprintf(stream
, "<root>\n");
114 ret
= recursive_visit_print(node
->u
.root
.child
, stream
,
116 print_tabs(stream
, indent
);
117 fprintf(stream
, "</root>\n");
119 case NODE_EXPRESSION
:
120 print_tabs(stream
, indent
);
121 fprintf(stream
, "<expression>\n");
122 ret
= recursive_visit_print_expression(node
, stream
,
124 print_tabs(stream
, indent
);
125 fprintf(stream
, "</expression>\n");
128 print_tabs(stream
, indent
);
129 fprintf(stream
, "<op type=");
130 switch (node
->u
.op
.type
) {
133 fprintf(stderr
, "[error] %s: unknown op\n", __func__
);
136 fprintf(stream
, "\"*\"");
139 fprintf(stream
, "\"/\"");
142 fprintf(stream
, "\"%%\"");
145 fprintf(stream
, "\"+\"");
148 fprintf(stream
, "\"-\"");
150 case AST_OP_BIT_RSHIFT
:
151 fprintf(stream
, "\">>\"");
153 case AST_OP_BIT_LSHIFT
:
154 fprintf(stream
, "\"<<\"");
157 fprintf(stream
, "\"&&\"");
160 fprintf(stream
, "\"||\"");
163 fprintf(stream
, "\"&\"");
166 fprintf(stream
, "\"|\"");
169 fprintf(stream
, "\"^\"");
173 fprintf(stream
, "\"==\"");
176 fprintf(stream
, "\"!=\"");
179 fprintf(stream
, "\">\"");
182 fprintf(stream
, "\"<\"");
185 fprintf(stream
, "\">=\"");
188 fprintf(stream
, "\"<=\"");
191 fprintf(stream
, ">\n");
192 ret
= recursive_visit_print(node
->u
.op
.lchild
,
196 ret
= recursive_visit_print(node
->u
.op
.rchild
,
200 print_tabs(stream
, indent
);
201 fprintf(stream
, "</op>\n");
204 print_tabs(stream
, indent
);
205 fprintf(stream
, "<unary_op type=");
206 switch (node
->u
.unary_op
.type
) {
207 case AST_UNARY_UNKNOWN
:
209 fprintf(stderr
, "[error] %s: unknown unary_op\n", __func__
);
212 fprintf(stream
, "\"+\"");
214 case AST_UNARY_MINUS
:
215 fprintf(stream
, "\"-\"");
218 fprintf(stream
, "\"!\"");
220 case AST_UNARY_BIT_NOT
:
221 fprintf(stream
, "\"~\"");
224 fprintf(stream
, ">\n");
225 ret
= recursive_visit_print(node
->u
.unary_op
.child
,
227 print_tabs(stream
, indent
);
228 fprintf(stream
, "</unary_op>\n");
235 int filter_visitor_print_xml(struct filter_parser_ctx
*ctx
, FILE *stream
,
238 return recursive_visit_print(&ctx
->ast
->root
, stream
, indent
);