Babeltrace-wide warning fixes
[babeltrace.git] / formats / ctf / metadata / ctf-visitor-xml.c
CommitLineData
7de8808c
MD
1/*
2 * ctf-visitor-xml.c
3 *
4 * Common Trace Format Metadata Visitor (XML dump).
5 *
6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19#include <stdio.h>
20#include <unistd.h>
21#include <string.h>
22#include <stdlib.h>
23#include <assert.h>
7de8808c
MD
24#include <glib.h>
25#include <errno.h>
fe41395a 26#include <babeltrace/list.h>
7de8808c
MD
27#include "ctf-scanner.h"
28#include "ctf-parser.h"
29#include "ctf-ast.h"
30
31#define printf_dbg(fmt, args...) fprintf(stderr, "%s: " fmt, __func__, ## args)
32
33static void print_tabs(FILE *fd, int depth)
34{
35 int i;
36
37 for (i = 0; i < depth; i++)
38 fprintf(fd, "\t");
39}
40
41int ctf_visitor_print_unary_expression(FILE *fd, int depth, struct ctf_node *node)
42{
43 int ret = 0;
44
48a01768
MD
45 switch (node->u.unary_expression.link) {
46 case UNARY_LINK_UNKNOWN:
47 break;
48 case UNARY_DOTLINK:
49 print_tabs(fd, depth);
50 fprintf(fd, "<dotlink/>\n");
51 break;
52 case UNARY_ARROWLINK:
53 print_tabs(fd, depth);
54 fprintf(fd, "<arrowlink/>\n");
55 break;
56 case UNARY_DOTDOTDOT:
57 print_tabs(fd, depth);
58 fprintf(fd, "<dotdotdot/>\n");
59 break;
60 default:
61 fprintf(stderr, "[error] %s: unknown expression link type %d\n", __func__,
62 (int) node->u.unary_expression.link);
63 return -EINVAL;
64 }
65
7de8808c
MD
66 switch (node->u.unary_expression.type) {
67 case UNARY_STRING:
68 print_tabs(fd, depth);
69 fprintf(fd, "<unary_expression value=");
70 fprintf(fd, "\"%s\"", node->u.unary_expression.u.string);
48a01768 71 fprintf(fd, " />\n");
7de8808c
MD
72 break;
73 case UNARY_SIGNED_CONSTANT:
74 print_tabs(fd, depth);
75 fprintf(fd, "<unary_expression value=");
76 fprintf(fd, "%lld", node->u.unary_expression.u.signed_constant);
48a01768 77 fprintf(fd, " />\n");
7de8808c
MD
78 break;
79 case UNARY_UNSIGNED_CONSTANT:
80 print_tabs(fd, depth);
81 fprintf(fd, "<unary_expression value=");
82 fprintf(fd, "%llu", node->u.unary_expression.u.signed_constant);
48a01768 83 fprintf(fd, " />\n");
7de8808c
MD
84 break;
85 case UNARY_SBRAC:
86 print_tabs(fd, depth);
87 fprintf(fd, "<unary_expression_sbrac>");
88 ret = ctf_visitor_print_unary_expression(fd, depth + 1,
89 node->u.unary_expression.u.sbrac_exp);
90 if (ret)
91 return ret;
92 print_tabs(fd, depth);
93 fprintf(fd, "</unary_expression_sbrac>");
94 break;
48a01768
MD
95 case UNARY_NESTED:
96 print_tabs(fd, depth);
97 fprintf(fd, "<unary_expression_nested>");
98 ret = ctf_visitor_print_unary_expression(fd, depth + 1,
99 node->u.unary_expression.u.nested_exp);
100 if (ret)
101 return ret;
102 print_tabs(fd, depth);
103 fprintf(fd, "</unary_expression_nested>");
104 break;
7de8808c
MD
105
106 case UNARY_UNKNOWN:
107 default:
108 fprintf(stderr, "[error] %s: unknown expression type %d\n", __func__,
109 (int) node->u.unary_expression.type);
110 return -EINVAL;
111 }
7de8808c
MD
112 return 0;
113}
114
115int ctf_visitor_print_type_specifier(FILE *fd, int depth, struct ctf_node *node)
116{
117 print_tabs(fd, depth);
48a01768 118 fprintf(fd, "<type_specifier \"");
7de8808c
MD
119
120 switch (node->u.type_specifier.type) {
121 case TYPESPEC_VOID:
122 fprintf(fd, "void");
123 break;
124 case TYPESPEC_CHAR:
125 fprintf(fd, "char");
126 break;
127 case TYPESPEC_SHORT:
128 fprintf(fd, "short");
129 break;
130 case TYPESPEC_INT:
131 fprintf(fd, "int");
132 break;
133 case TYPESPEC_LONG:
134 fprintf(fd, "long");
135 break;
136 case TYPESPEC_FLOAT:
137 fprintf(fd, "float");
138 break;
139 case TYPESPEC_DOUBLE:
140 fprintf(fd, "double");
141 break;
142 case TYPESPEC_SIGNED:
143 fprintf(fd, "signed");
144 break;
145 case TYPESPEC_UNSIGNED:
146 fprintf(fd, "unsigned");
147 break;
148 case TYPESPEC_BOOL:
149 fprintf(fd, "bool");
150 break;
151 case TYPESPEC_COMPLEX:
152 fprintf(fd, "complex");
153 break;
154 case TYPESPEC_CONST:
155 fprintf(fd, "const");
156 break;
157 case TYPESPEC_ID_TYPE:
158 fprintf(fd, "%s", node->u.type_specifier.id_type);
159 break;
160
161 case TYPESPEC_UNKNOWN:
162 default:
163 fprintf(stderr, "[error] %s: unknown type specifier %d\n", __func__,
164 (int) node->u.type_specifier.type);
165 return -EINVAL;
166 }
167 fprintf(fd, "\"/>\n");
168 return 0;
169}
170
171int ctf_visitor_print_type_declarator(FILE *fd, int depth, struct ctf_node *node)
172{
173 int ret = 0;
174 struct ctf_node *iter;
175
176 print_tabs(fd, depth);
177 fprintf(fd, "<type_declarator>\n");
178 depth++;
179
48a01768
MD
180 if (!cds_list_empty(&node->u.type_declarator.pointers)) {
181 print_tabs(fd, depth);
182 fprintf(fd, "<pointers>\n");
183 cds_list_for_each_entry(iter, &node->u.type_declarator.pointers,
184 siblings) {
185 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
186 if (ret)
187 return ret;
188 }
189 print_tabs(fd, depth);
190 fprintf(fd, "</pointers>\n");
7de8808c 191 }
7de8808c
MD
192
193 switch (node->u.type_declarator.type) {
194 case TYPEDEC_ID:
0009a725
MD
195 if (node->u.type_declarator.u.id) {
196 print_tabs(fd, depth);
197 fprintf(fd, "<id \"");
198 fprintf(fd, "%s", node->u.type_declarator.u.id);
199 fprintf(fd, "\" />\n");
200 }
7de8808c
MD
201 break;
202 case TYPEDEC_NESTED:
203 if (node->u.type_declarator.u.nested.type_declarator) {
204 print_tabs(fd, depth);
205 fprintf(fd, "<type_declarator>\n");
206 ret = ctf_visitor_print_xml(fd, depth + 1,
207 node->u.type_declarator.u.nested.type_declarator);
208 if (ret)
209 return ret;
210 print_tabs(fd, depth);
211 fprintf(fd, "</type_declarator>\n");
212 }
213 if (node->u.type_declarator.u.nested.length) {
214 print_tabs(fd, depth);
215 fprintf(fd, "<length>\n");
216 ret = ctf_visitor_print_xml(fd, depth + 1,
217 node->u.type_declarator.u.nested.length);
218 if (ret)
219 return ret;
220 print_tabs(fd, depth);
221 fprintf(fd, "</length>\n");
222 }
223 if (node->u.type_declarator.u.nested.abstract_array) {
224 print_tabs(fd, depth);
225 fprintf(fd, "<length>\n");
226 print_tabs(fd, depth);
227 fprintf(fd, "</length>\n");
228 }
229 if (node->u.type_declarator.bitfield_len) {
230 print_tabs(fd, depth);
231 fprintf(fd, "<bitfield_len>\n");
232 ret = ctf_visitor_print_xml(fd, depth + 1,
233 node->u.type_declarator.bitfield_len);
234 if (ret)
235 return ret;
236 print_tabs(fd, depth);
237 fprintf(fd, "</bitfield_len>\n");
238 }
239 break;
240 case TYPEDEC_UNKNOWN:
241 default:
242 fprintf(stderr, "[error] %s: unknown type declarator %d\n", __func__,
243 (int) node->u.type_declarator.type);
244 return -EINVAL;
245 }
246
247 depth--;
248 print_tabs(fd, depth);
249 fprintf(fd, "</type_declarator>\n");
250 return 0;
251}
252
253int ctf_visitor_print_xml(FILE *fd, int depth, struct ctf_node *node)
254{
255 int ret = 0;
256 struct ctf_node *iter;
257
258 switch (node->type) {
259 case NODE_ROOT:
260 print_tabs(fd, depth);
261 fprintf(fd, "<root>\n");
262 cds_list_for_each_entry(iter, &node->u.root._typedef,
263 siblings) {
264 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
265 if (ret)
266 return ret;
267 }
268 cds_list_for_each_entry(iter, &node->u.root.typealias,
269 siblings) {
270 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
271 if (ret)
272 return ret;
273 }
274 cds_list_for_each_entry(iter, &node->u.root.declaration_specifier, siblings) {
275 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
276 if (ret)
277 return ret;
278 }
279 cds_list_for_each_entry(iter, &node->u.root.trace, siblings) {
280 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
281 if (ret)
282 return ret;
283 }
284 cds_list_for_each_entry(iter, &node->u.root.stream, siblings) {
285 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
286 if (ret)
287 return ret;
288 }
289 cds_list_for_each_entry(iter, &node->u.root.event, siblings) {
290 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
291 if (ret)
292 return ret;
293 }
294 print_tabs(fd, depth);
295 fprintf(fd, "</root>\n");
296 break;
297
298 case NODE_EVENT:
299 print_tabs(fd, depth);
300 fprintf(fd, "<event>\n");
301 cds_list_for_each_entry(iter, &node->u.event.declaration_list, siblings) {
302 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
303 if (ret)
304 return ret;
305 }
306 print_tabs(fd, depth);
307 fprintf(fd, "</event>\n");
308 break;
309 case NODE_STREAM:
310 print_tabs(fd, depth);
311 fprintf(fd, "<stream>\n");
312 cds_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) {
313 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
314 if (ret)
315 return ret;
316 }
317 print_tabs(fd, depth);
318 fprintf(fd, "</stream>\n");
319 break;
320 case NODE_TRACE:
321 print_tabs(fd, depth);
322 fprintf(fd, "<trace>\n");
323 cds_list_for_each_entry(iter, &node->u.trace.declaration_list, siblings) {
324 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
325 if (ret)
326 return ret;
327 }
328 print_tabs(fd, depth);
329 fprintf(fd, "</trace>\n");
330 break;
331
332 case NODE_CTF_EXPRESSION:
333 print_tabs(fd, depth);
334 fprintf(fd, "<ctf_expression>\n");
335 depth++;
336 print_tabs(fd, depth);
337 fprintf(fd, "<left>\n");
48a01768
MD
338 cds_list_for_each_entry(iter, &node->u.ctf_expression.left, siblings) {
339 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
340 if (ret)
341 return ret;
342 }
343
7de8808c
MD
344 print_tabs(fd, depth);
345 fprintf(fd, "</left>\n");
346
347 print_tabs(fd, depth);
348 fprintf(fd, "<right>\n");
48a01768
MD
349 cds_list_for_each_entry(iter, &node->u.ctf_expression.right, siblings) {
350 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
351 if (ret)
352 return ret;
353 }
7de8808c
MD
354 print_tabs(fd, depth);
355 fprintf(fd, "</right>\n");
356 depth--;
357 print_tabs(fd, depth);
358 fprintf(fd, "</ctf_expression>\n");
359 break;
360 case NODE_UNARY_EXPRESSION:
361 return ctf_visitor_print_unary_expression(fd, depth, node);
362
363 case NODE_TYPEDEF:
364 print_tabs(fd, depth);
365 fprintf(fd, "<typedef>\n");
366 depth++;
367 print_tabs(fd, depth);
368 fprintf(fd, "<declaration_specifier>\n");
369 cds_list_for_each_entry(iter, &node->u._typedef.declaration_specifier, siblings) {
370 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
371 if (ret)
372 return ret;
373 }
374 print_tabs(fd, depth);
375 fprintf(fd, "</declaration_specifier>\n");
376
377 print_tabs(fd, depth);
378 fprintf(fd, "<type_declarators>\n");
379 cds_list_for_each_entry(iter, &node->u._typedef.type_declarators, siblings) {
380 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
381 if (ret)
382 return ret;
383 }
384 print_tabs(fd, depth);
385 fprintf(fd, "</type_declarators>\n");
386 depth--;
387 print_tabs(fd, depth);
388 fprintf(fd, "</typedef>\n");
389 break;
390 case NODE_TYPEALIAS_TARGET:
391 print_tabs(fd, depth);
392 fprintf(fd, "<target>\n");
393 depth++;
394
395 print_tabs(fd, depth);
396 fprintf(fd, "<declaration_specifier>\n");
397 cds_list_for_each_entry(iter, &node->u.typealias_target.declaration_specifier, siblings) {
398 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
399 if (ret)
400 return ret;
401 }
402 print_tabs(fd, depth);
403 fprintf(fd, "</declaration_specifier>\n");
404
405 print_tabs(fd, depth);
406 fprintf(fd, "<type_declarators>\n");
407 cds_list_for_each_entry(iter, &node->u.typealias_target.type_declarators, siblings) {
408 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
409 if (ret)
410 return ret;
411 }
412 print_tabs(fd, depth);
413 fprintf(fd, "</type_declarators>\n");
414
415 depth--;
416 print_tabs(fd, depth);
417 fprintf(fd, "</target>\n");
418 break;
419 case NODE_TYPEALIAS_ALIAS:
420 print_tabs(fd, depth);
421 fprintf(fd, "<alias>\n");
422 depth++;
423
424 print_tabs(fd, depth);
425 fprintf(fd, "<declaration_specifier>\n");
426 cds_list_for_each_entry(iter, &node->u.typealias_alias.declaration_specifier, siblings) {
427 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
428 if (ret)
429 return ret;
430 }
431 print_tabs(fd, depth);
432 fprintf(fd, "</declaration_specifier>\n");
433
434 print_tabs(fd, depth);
435 fprintf(fd, "<type_declarators>\n");
436 cds_list_for_each_entry(iter, &node->u.typealias_alias.type_declarators, siblings) {
437 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
438 if (ret)
439 return ret;
440 }
441 print_tabs(fd, depth);
442 fprintf(fd, "</type_declarators>\n");
443
444 depth--;
445 print_tabs(fd, depth);
446 fprintf(fd, "</alias>\n");
447 break;
448 case NODE_TYPEALIAS:
449 print_tabs(fd, depth);
450 fprintf(fd, "<typealias>\n");
451 ret = ctf_visitor_print_xml(fd, depth + 1, node->u.typealias.target);
452 if (ret)
453 return ret;
454 ret = ctf_visitor_print_xml(fd, depth + 1, node->u.typealias.alias);
455 if (ret)
456 return ret;
457 print_tabs(fd, depth);
458 fprintf(fd, "</typealias>\n");
459 break;
460
461 case NODE_TYPE_SPECIFIER:
462 ret = ctf_visitor_print_type_specifier(fd, depth, node);
463 if (ret)
464 return ret;
465 break;
466 case NODE_POINTER:
467 print_tabs(fd, depth);
468 if (node->u.pointer.const_qualifier)
469 fprintf(fd, "<const_pointer />\n");
470 else
471 fprintf(fd, "<pointer />\n");
472 break;
473 case NODE_TYPE_DECLARATOR:
474 ret = ctf_visitor_print_type_declarator(fd, depth, node);
475 if (ret)
476 return ret;
477 break;
478
479 case NODE_FLOATING_POINT:
480 print_tabs(fd, depth);
481 fprintf(fd, "<floating_point>\n");
482 cds_list_for_each_entry(iter, &node->u.floating_point.expressions, siblings) {
483 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
484 if (ret)
485 return ret;
486 }
487 print_tabs(fd, depth);
488 fprintf(fd, "</floating_point>\n");
489 break;
490 case NODE_INTEGER:
491 print_tabs(fd, depth);
492 fprintf(fd, "<integer>\n");
493 cds_list_for_each_entry(iter, &node->u.integer.expressions, siblings) {
494 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
495 if (ret)
496 return ret;
497 }
498 print_tabs(fd, depth);
499 fprintf(fd, "</integer>\n");
500 break;
501 case NODE_STRING:
502 print_tabs(fd, depth);
503 fprintf(fd, "<string>\n");
504 cds_list_for_each_entry(iter, &node->u.string.expressions, siblings) {
505 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
506 if (ret)
507 return ret;
508 }
509 print_tabs(fd, depth);
510 fprintf(fd, "</string>\n");
511 break;
512 case NODE_ENUMERATOR:
513 print_tabs(fd, depth);
514 fprintf(fd, "<enumerator");
515 if (node->u.enumerator.id)
516 fprintf(fd, " id=\"%s\"", node->u.enumerator.id);
517 fprintf(fd, ">\n");
48a01768
MD
518 cds_list_for_each_entry(iter, &node->u.enumerator.values, siblings) {
519 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
7de8808c
MD
520 if (ret)
521 return ret;
522 }
523 print_tabs(fd, depth);
48a01768 524 fprintf(fd, "</enumerator>\n");
7de8808c
MD
525 break;
526 case NODE_ENUM:
527 print_tabs(fd, depth);
528 if (node->u._struct.name)
529 fprintf(fd, "<enum name=\"%s\">\n",
530 node->u._enum.enum_id);
531 else
532 fprintf(fd, "<enum >\n");
533 depth++;
534
535 if (node->u._enum.container_type) {
536 print_tabs(fd, depth);
48a01768 537 fprintf(fd, "<container_type>\n");
7de8808c
MD
538 ret = ctf_visitor_print_xml(fd, depth + 1, node->u._enum.container_type);
539 if (ret)
540 return ret;
541 print_tabs(fd, depth);
48a01768 542 fprintf(fd, "</container_type>\n");
7de8808c
MD
543 }
544
545 print_tabs(fd, depth);
48a01768 546 fprintf(fd, "<enumerator_list>\n");
7de8808c
MD
547 cds_list_for_each_entry(iter, &node->u._enum.enumerator_list, siblings) {
548 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
549 if (ret)
550 return ret;
551 }
552 print_tabs(fd, depth);
48a01768 553 fprintf(fd, "</enumerator_list>\n");
7de8808c
MD
554
555 depth--;
556 print_tabs(fd, depth);
557 fprintf(fd, "</enum>\n");
558 break;
559 case NODE_STRUCT_OR_VARIANT_DECLARATION:
560 print_tabs(fd, depth);
561 fprintf(fd, "<declaration_specifier>\n");
562 cds_list_for_each_entry(iter, &node->u.struct_or_variant_declaration.declaration_specifier, siblings) {
563 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
564 if (ret)
565 return ret;
566 }
567 print_tabs(fd, depth);
568 fprintf(fd, "</declaration_specifier>\n");
569
570 print_tabs(fd, depth);
571 fprintf(fd, "<type_declarators>\n");
572 cds_list_for_each_entry(iter, &node->u.struct_or_variant_declaration.type_declarators, siblings) {
573 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
574 if (ret)
575 return ret;
576 }
577 print_tabs(fd, depth);
578 fprintf(fd, "</type_declarators>\n");
579 break;
580 case NODE_VARIANT:
581 print_tabs(fd, depth);
582 fprintf(fd, "<variant");
583 if (node->u.variant.name)
584 fprintf(fd, " name=\"%s\"", node->u.variant.name);
585 if (node->u.variant.choice)
586 fprintf(fd, " choice=\"%s\"", node->u.variant.choice);
587 fprintf(fd, ">\n");
588 cds_list_for_each_entry(iter, &node->u.variant.declaration_list, siblings) {
589 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
590 if (ret)
591 return ret;
592 }
593 print_tabs(fd, depth);
594 fprintf(fd, "</variant>\n");
595 break;
596 case NODE_STRUCT:
597 print_tabs(fd, depth);
598 if (node->u._struct.name)
599 fprintf(fd, "<struct name=\"%s\">\n",
600 node->u._struct.name);
601 else
602 fprintf(fd, "<struct>\n");
603 cds_list_for_each_entry(iter, &node->u._struct.declaration_list, siblings) {
604 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
605 if (ret)
606 return ret;
607 }
608 print_tabs(fd, depth);
609 fprintf(fd, "</struct>\n");
610 break;
611
612 case NODE_UNKNOWN:
613 default:
614 fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
615 (int) node->type);
616 return -EINVAL;
617 }
618 return ret;
619}
This page took 0.04542 seconds and 4 git commands to generate.