Rename C++ header files to .hpp
[lttng-tools.git] / src / common / filter / filter-visitor-xml.cpp
1 /*
2 * filter-visitor-xml.c
3 *
4 * LTTng filter XML pretty printer visitor
5 *
6 * Copyright 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * SPDX-License-Identifier: LGPL-2.1-only
9 *
10 */
11
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <inttypes.h>
17 #include "filter-ast.hpp"
18 #include "filter-parser.hpp"
19
20 #include <common/compat/errno.hpp>
21 #include <common/macros.hpp>
22
23 #define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
24
25 static
26 int recursive_visit_print(struct filter_node *node, FILE *stream, int indent);
27
28 static
29 void print_tabs(FILE *fd, int depth)
30 {
31 int i;
32
33 for (i = 0; i < depth; i++)
34 fprintf(fd, "\t");
35 }
36
37 static
38 int recursive_visit_print_expression(struct filter_node *node,
39 FILE *stream, int indent)
40 {
41 struct filter_node *iter_node;
42
43 if (!node) {
44 fprintf(stderr, "[error] %s: NULL child\n", __func__);
45 return -EINVAL;
46 }
47 switch (node->u.expression.type) {
48 case AST_EXP_UNKNOWN:
49 default:
50 fprintf(stderr, "[error] %s: unknown expression\n", __func__);
51 return -EINVAL;
52 case AST_EXP_STRING:
53 print_tabs(stream, indent);
54 fprintf(stream, "<string value=\"%s\"/>\n",
55 node->u.expression.u.string);
56 break;
57 case AST_EXP_CONSTANT:
58 print_tabs(stream, indent);
59 fprintf(stream, "<constant value=\"%" PRIu64 "\"/>\n",
60 node->u.expression.u.constant);
61 break;
62 case AST_EXP_FLOAT_CONSTANT:
63 print_tabs(stream, indent);
64 fprintf(stream, "<float_constant value=\"%lg\"/>\n",
65 node->u.expression.u.float_constant);
66 break;
67 case AST_EXP_IDENTIFIER: /* fall-through */
68 case AST_EXP_GLOBAL_IDENTIFIER:
69 print_tabs(stream, indent);
70 fprintf(stream, "<%s value=\"%s\"/>\n",
71 node->u.expression.type == AST_EXP_IDENTIFIER ?
72 "identifier" : "global_identifier",
73 node->u.expression.u.identifier);
74 iter_node = node->u.expression.next;
75 while (iter_node) {
76 print_tabs(stream, indent);
77 fprintf(stream, "<bracket>\n");
78 if (recursive_visit_print_expression(iter_node,
79 stream, indent + 1)) {
80 return -EINVAL;
81 }
82 print_tabs(stream, indent);
83 fprintf(stream, "</bracket>\n");
84 iter_node = iter_node->u.expression.next;
85
86 }
87 break;
88 case AST_EXP_NESTED:
89 return recursive_visit_print(node->u.expression.u.child,
90 stream, indent + 1);
91 }
92 return 0;
93 }
94
95
96 static
97 int recursive_visit_print(struct filter_node *node, FILE *stream, int indent)
98 {
99 int ret;
100
101 if (!node) {
102 fprintf(stderr, "[error] %s: NULL child\n", __func__);
103 return -EINVAL;
104 }
105 switch (node->type) {
106 case NODE_UNKNOWN:
107 default:
108 fprintf(stderr, "[error] %s: unknown node type\n", __func__);
109 return -EINVAL;
110 case NODE_ROOT:
111 print_tabs(stream, indent);
112 fprintf(stream, "<root>\n");
113 ret = recursive_visit_print(node->u.root.child, stream,
114 indent + 1);
115 print_tabs(stream, indent);
116 fprintf(stream, "</root>\n");
117 return ret;
118 case NODE_EXPRESSION:
119 print_tabs(stream, indent);
120 fprintf(stream, "<expression>\n");
121 ret = recursive_visit_print_expression(node, stream,
122 indent + 1);
123 print_tabs(stream, indent);
124 fprintf(stream, "</expression>\n");
125 return ret;
126 case NODE_OP:
127 print_tabs(stream, indent);
128 fprintf(stream, "<op type=");
129 switch (node->u.op.type) {
130 case AST_OP_UNKNOWN:
131 default:
132 fprintf(stderr, "[error] %s: unknown op\n", __func__);
133 return -EINVAL;
134 case AST_OP_MUL:
135 fprintf(stream, "\"*\"");
136 break;
137 case AST_OP_DIV:
138 fprintf(stream, "\"/\"");
139 break;
140 case AST_OP_MOD:
141 fprintf(stream, "\"%%\"");
142 break;
143 case AST_OP_PLUS:
144 fprintf(stream, "\"+\"");
145 break;
146 case AST_OP_MINUS:
147 fprintf(stream, "\"-\"");
148 break;
149 case AST_OP_BIT_RSHIFT:
150 fprintf(stream, "\">>\"");
151 break;
152 case AST_OP_BIT_LSHIFT:
153 fprintf(stream, "\"<<\"");
154 break;
155 case AST_OP_AND:
156 fprintf(stream, "\"&&\"");
157 break;
158 case AST_OP_OR:
159 fprintf(stream, "\"||\"");
160 break;
161 case AST_OP_BIT_AND:
162 fprintf(stream, "\"&\"");
163 break;
164 case AST_OP_BIT_OR:
165 fprintf(stream, "\"|\"");
166 break;
167 case AST_OP_BIT_XOR:
168 fprintf(stream, "\"^\"");
169 break;
170
171 case AST_OP_EQ:
172 fprintf(stream, "\"==\"");
173 break;
174 case AST_OP_NE:
175 fprintf(stream, "\"!=\"");
176 break;
177 case AST_OP_GT:
178 fprintf(stream, "\">\"");
179 break;
180 case AST_OP_LT:
181 fprintf(stream, "\"<\"");
182 break;
183 case AST_OP_GE:
184 fprintf(stream, "\">=\"");
185 break;
186 case AST_OP_LE:
187 fprintf(stream, "\"<=\"");
188 break;
189 }
190 fprintf(stream, ">\n");
191 ret = recursive_visit_print(node->u.op.lchild,
192 stream, indent + 1);
193 if (ret)
194 return ret;
195 ret = recursive_visit_print(node->u.op.rchild,
196 stream, indent + 1);
197 if (ret)
198 return ret;
199 print_tabs(stream, indent);
200 fprintf(stream, "</op>\n");
201 return ret;
202 case NODE_UNARY_OP:
203 print_tabs(stream, indent);
204 fprintf(stream, "<unary_op type=");
205 switch (node->u.unary_op.type) {
206 case AST_UNARY_UNKNOWN:
207 default:
208 fprintf(stderr, "[error] %s: unknown unary_op\n", __func__);
209 return -EINVAL;
210 case AST_UNARY_PLUS:
211 fprintf(stream, "\"+\"");
212 break;
213 case AST_UNARY_MINUS:
214 fprintf(stream, "\"-\"");
215 break;
216 case AST_UNARY_NOT:
217 fprintf(stream, "\"!\"");
218 break;
219 case AST_UNARY_BIT_NOT:
220 fprintf(stream, "\"~\"");
221 break;
222 }
223 fprintf(stream, ">\n");
224 ret = recursive_visit_print(node->u.unary_op.child,
225 stream, indent + 1);
226 print_tabs(stream, indent);
227 fprintf(stream, "</unary_op>\n");
228 return ret;
229 }
230 return 0;
231 }
232
233 int filter_visitor_print_xml(struct filter_parser_ctx *ctx, FILE *stream,
234 int indent)
235 {
236 return recursive_visit_print(&ctx->ast->root, stream, indent);
237 }
This page took 0.034586 seconds and 4 git commands to generate.