2 * ctf-visitor-semantic-validator.c
4 * Common Trace Format Metadata Semantic Validator.
6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
27 #include <babeltrace/list.h>
28 #include "ctf-scanner.h"
29 #include "ctf-parser.h"
32 #define _cds_list_first_entry(ptr, type, member) \
33 cds_list_entry((ptr)->next, type, member)
35 #define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
38 int _ctf_visitor_semantic_check(FILE *fd
, int depth
, struct ctf_node
*node
);
41 int ctf_visitor_unary_expression(FILE *fd
, int depth
, struct ctf_node
*node
)
43 struct ctf_node
*iter
;
44 int is_ctf_exp
= 0, is_ctf_exp_left
= 0;
46 switch (node
->parent
->type
) {
47 case NODE_CTF_EXPRESSION
:
49 cds_list_for_each_entry(iter
, &node
->parent
->u
.ctf_expression
.left
,
54 * We are a left child of a ctf expression.
55 * We are only allowed to be a string.
57 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
58 fprintf(fd
, "[error]: semantic error (left child of a ctf expression is only allowed to be a string)\n");
65 /* Right child of a ctf expression can be any type of unary exp. */
67 case NODE_TYPE_DECLARATOR
:
70 * We are the length of a type declarator or the size of an enum
71 * container. We can only be a numeric constant.
73 switch (node
->u
.unary_expression
.type
) {
74 case UNARY_SIGNED_CONSTANT
:
75 case UNARY_UNSIGNED_CONSTANT
:
78 fprintf(fd
, "[error]: semantic error (children of type declarator and enum can only be numeric constants)\n");
83 /* The enumerator's parent has validated its validity already. */
86 case NODE_UNARY_EXPRESSION
:
88 * We disallow nested unary expressions and "sbrac" unary
91 fprintf(fd
, "[error]: semantic error (nested unary expressions not allowed ( () and [] ))\n");
99 case NODE_TYPEALIAS_TARGET
:
100 case NODE_TYPEALIAS_ALIAS
:
102 case NODE_TYPE_SPECIFIER
:
104 case NODE_FLOATING_POINT
:
107 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
114 switch (node
->u
.unary_expression
.link
) {
115 case UNARY_LINK_UNKNOWN
:
116 /* We don't allow empty link except on the first node of the list */
117 if (is_ctf_exp
&& _cds_list_first_entry(is_ctf_exp_left
?
118 &node
->parent
->u
.ctf_expression
.left
:
119 &node
->parent
->u
.ctf_expression
.right
,
122 fprintf(fd
, "[error]: semantic error (empty link not allowed except on first node of unary expression (need to separate nodes with \".\" or \"->\")\n");
127 case UNARY_ARROWLINK
:
128 /* We only allow -> and . links between children of ctf_expression. */
129 if (node
->parent
->type
!= NODE_CTF_EXPRESSION
) {
130 fprintf(fd
, "[error]: semantic error (links \".\" and \"->\" are only allowed as children of ctf expression)\n");
134 * Only strings can be separated linked by . or ->.
135 * This includes "", '' and non-quoted identifiers.
137 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
138 fprintf(fd
, "[error]: semantic error (links \".\" and \"->\" are only allowed to separate strings and identifiers)\n");
141 /* We don't allow link on the first node of the list */
142 if (is_ctf_exp
&& _cds_list_first_entry(is_ctf_exp_left
?
143 &node
->parent
->u
.ctf_expression
.left
:
144 &node
->parent
->u
.ctf_expression
.right
,
147 fprintf(fd
, "[error]: semantic error (links \".\" and \"->\" are not allowed before first node of the unary expression list)\n");
151 case UNARY_DOTDOTDOT
:
152 /* We only allow ... link between children of enumerator. */
153 if (node
->parent
->type
!= NODE_ENUMERATOR
) {
154 fprintf(fd
, "[error]: semantic error (link \"...\" is only allowed within enumerator)\n");
157 /* We don't allow link on the first node of the list */
158 if (_cds_list_first_entry(&node
->parent
->u
.enumerator
.values
,
161 fprintf(fd
, "[error]: semantic error (link \"...\" is not allowed on the first node of the unary expression list)\n");
166 fprintf(fd
, "[error] %s: unknown expression link type %d\n", __func__
,
167 (int) node
->u
.unary_expression
.link
);
173 fprintf(fd
, "[error] %s: incoherent parent type %s for node type %s\n", __func__
,
174 node_type(node
->parent
), node_type(node
));
175 return -EINVAL
; /* Incoherent structure */
178 fprintf(fd
, "[error] %s: semantic error (parent type %s for node type %s)\n", __func__
,
179 node_type(node
->parent
), node_type(node
));
180 return -EPERM
; /* Structure not allowed */
184 int ctf_visitor_type_specifier(FILE *fd
, int depth
, struct ctf_node
*node
)
186 switch (node
->parent
->type
) {
187 case NODE_CTF_EXPRESSION
:
188 case NODE_TYPE_DECLARATOR
:
190 case NODE_TYPEALIAS_TARGET
:
191 case NODE_TYPEALIAS_ALIAS
:
193 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
200 case NODE_UNARY_EXPRESSION
:
202 case NODE_TYPE_SPECIFIER
:
204 case NODE_FLOATING_POINT
:
207 case NODE_ENUMERATOR
:
215 fprintf(fd
, "[error] %s: incoherent parent type %s for node type %s\n", __func__
,
216 node_type(node
->parent
), node_type(node
));
217 return -EINVAL
; /* Incoherent structure */
221 int ctf_visitor_type_declarator(FILE *fd
, int depth
, struct ctf_node
*node
)
224 struct ctf_node
*iter
;
228 switch (node
->parent
->type
) {
229 case NODE_TYPE_DECLARATOR
:
231 * A nested type declarator is not allowed to contain pointers.
233 if (!cds_list_empty(&node
->u
.type_declarator
.pointers
))
237 case NODE_TYPEALIAS_TARGET
:
238 case NODE_TYPEALIAS_ALIAS
:
239 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
246 case NODE_CTF_EXPRESSION
:
247 case NODE_UNARY_EXPRESSION
:
249 case NODE_TYPE_SPECIFIER
:
251 case NODE_FLOATING_POINT
:
254 case NODE_ENUMERATOR
:
262 if (!cds_list_empty(&node
->u
.type_declarator
.pointers
)) {
263 cds_list_for_each_entry(iter
, &node
->u
.type_declarator
.pointers
,
265 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
271 switch (node
->u
.type_declarator
.type
) {
275 if (node
->u
.type_declarator
.u
.nested
.type_declarator
) {
276 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1,
277 node
->u
.type_declarator
.u
.nested
.type_declarator
);
281 if (node
->u
.type_declarator
.u
.nested
.length
) {
282 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1,
283 node
->u
.type_declarator
.u
.nested
.length
);
287 if (node
->u
.type_declarator
.bitfield_len
) {
288 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1,
289 node
->u
.type_declarator
.bitfield_len
);
294 case TYPEDEC_UNKNOWN
:
296 fprintf(fd
, "[error] %s: unknown type declarator %d\n", __func__
,
297 (int) node
->u
.type_declarator
.type
);
304 fprintf(fd
, "[error] %s: incoherent parent type %s for node type %s\n", __func__
,
305 node_type(node
->parent
), node_type(node
));
306 return -EINVAL
; /* Incoherent structure */
309 fprintf(fd
, "[error] %s: semantic error (parent type %s for node type %s)\n", __func__
,
310 node_type(node
->parent
), node_type(node
));
311 return -EPERM
; /* Structure not allowed */
315 int _ctf_visitor_semantic_check(FILE *fd
, int depth
, struct ctf_node
*node
)
318 struct ctf_node
*iter
;
320 switch (node
->type
) {
322 cds_list_for_each_entry(iter
, &node
->u
.root
._typedef
,
324 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
328 cds_list_for_each_entry(iter
, &node
->u
.root
.typealias
,
330 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
334 cds_list_for_each_entry(iter
, &node
->u
.root
.declaration_specifier
, siblings
) {
335 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
339 cds_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
340 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
344 cds_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
345 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
349 cds_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
350 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
357 switch (node
->parent
->type
) {
364 cds_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
365 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
371 switch (node
->parent
->type
) {
378 cds_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
379 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
385 switch (node
->parent
->type
) {
392 cds_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
393 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
399 case NODE_CTF_EXPRESSION
:
400 switch (node
->parent
->type
) {
405 case NODE_FLOATING_POINT
:
410 case NODE_CTF_EXPRESSION
:
411 case NODE_UNARY_EXPRESSION
:
413 case NODE_TYPEALIAS_TARGET
:
414 case NODE_TYPEALIAS_ALIAS
:
415 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
417 case NODE_TYPE_SPECIFIER
:
419 case NODE_TYPE_DECLARATOR
:
420 case NODE_ENUMERATOR
:
429 cds_list_for_each_entry(iter
, &node
->u
.ctf_expression
.left
, siblings
) {
430 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
434 cds_list_for_each_entry(iter
, &node
->u
.ctf_expression
.right
, siblings
) {
435 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
441 case NODE_UNARY_EXPRESSION
:
442 return ctf_visitor_unary_expression(fd
, depth
, node
);
445 switch (node
->parent
->type
) {
454 case NODE_CTF_EXPRESSION
:
455 case NODE_UNARY_EXPRESSION
:
457 case NODE_TYPEALIAS_TARGET
:
458 case NODE_TYPEALIAS_ALIAS
:
460 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
461 case NODE_TYPE_SPECIFIER
:
463 case NODE_TYPE_DECLARATOR
:
464 case NODE_FLOATING_POINT
:
467 case NODE_ENUMERATOR
:
474 cds_list_for_each_entry(iter
, &node
->u
._typedef
.declaration_specifier
, siblings
) {
475 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
479 cds_list_for_each_entry(iter
, &node
->u
._typedef
.type_declarators
, siblings
) {
480 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
486 case NODE_TYPEALIAS_TARGET
:
487 switch (node
->parent
->type
) {
495 cds_list_for_each_entry(iter
, &node
->u
.typealias_target
.declaration_specifier
, siblings
) {
496 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
500 cds_list_for_each_entry(iter
, &node
->u
.typealias_target
.type_declarators
, siblings
) {
501 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
507 case NODE_TYPEALIAS_ALIAS
:
508 switch (node
->parent
->type
) {
516 cds_list_for_each_entry(iter
, &node
->u
.typealias_alias
.declaration_specifier
, siblings
) {
517 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
521 cds_list_for_each_entry(iter
, &node
->u
.typealias_alias
.type_declarators
, siblings
) {
522 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
529 switch (node
->parent
->type
) {
538 case NODE_CTF_EXPRESSION
:
539 case NODE_UNARY_EXPRESSION
:
541 case NODE_TYPEALIAS_TARGET
:
542 case NODE_TYPEALIAS_ALIAS
:
544 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
545 case NODE_TYPE_SPECIFIER
:
547 case NODE_TYPE_DECLARATOR
:
548 case NODE_FLOATING_POINT
:
551 case NODE_ENUMERATOR
:
557 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, node
->u
.typealias
.target
);
560 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, node
->u
.typealias
.alias
);
565 case NODE_TYPE_SPECIFIER
:
566 ret
= ctf_visitor_type_specifier(fd
, depth
, node
);
571 switch (node
->parent
->type
) {
572 case NODE_TYPE_DECLARATOR
:
578 case NODE_TYPE_DECLARATOR
:
579 ret
= ctf_visitor_type_declarator(fd
, depth
, node
);
584 case NODE_FLOATING_POINT
:
585 switch (node
->parent
->type
) {
586 case NODE_CTF_EXPRESSION
:
588 case NODE_TYPEALIAS_TARGET
:
589 case NODE_TYPEALIAS_ALIAS
:
590 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
598 case NODE_TYPE_SPECIFIER
:
600 case NODE_TYPE_DECLARATOR
:
601 case NODE_FLOATING_POINT
:
604 case NODE_ENUMERATOR
:
611 case NODE_UNARY_EXPRESSION
:
614 cds_list_for_each_entry(iter
, &node
->u
.floating_point
.expressions
, siblings
) {
615 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
621 switch (node
->parent
->type
) {
622 case NODE_CTF_EXPRESSION
:
623 case NODE_UNARY_EXPRESSION
:
625 case NODE_TYPEALIAS_TARGET
:
626 case NODE_TYPEALIAS_ALIAS
:
627 case NODE_TYPE_DECLARATOR
:
629 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
637 case NODE_TYPE_SPECIFIER
:
639 case NODE_FLOATING_POINT
:
642 case NODE_ENUMERATOR
:
650 cds_list_for_each_entry(iter
, &node
->u
.integer
.expressions
, siblings
) {
651 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
657 switch (node
->parent
->type
) {
658 case NODE_CTF_EXPRESSION
:
660 case NODE_TYPEALIAS_TARGET
:
661 case NODE_TYPEALIAS_ALIAS
:
662 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
670 case NODE_TYPE_SPECIFIER
:
672 case NODE_TYPE_DECLARATOR
:
673 case NODE_FLOATING_POINT
:
676 case NODE_ENUMERATOR
:
683 case NODE_UNARY_EXPRESSION
:
687 cds_list_for_each_entry(iter
, &node
->u
.string
.expressions
, siblings
) {
688 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
693 case NODE_ENUMERATOR
:
694 switch (node
->parent
->type
) {
701 * Enumerators are only allows to contain:
702 * numeric unary expression
703 * or num. unary exp. ... num. unary exp
708 cds_list_for_each_entry(iter
, &node
->u
.enumerator
.values
,
711 case 0: if (iter
->type
!= NODE_UNARY_EXPRESSION
712 || (iter
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
713 && iter
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
)
714 || iter
->u
.unary_expression
.link
!= UNARY_LINK_UNKNOWN
) {
715 fprintf(fd
, "[error]: semantic error (first unary expression of enumerator is unexpected)\n");
719 case 1: if (iter
->type
!= NODE_UNARY_EXPRESSION
720 || (iter
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
721 && iter
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
)
722 || iter
->u
.unary_expression
.link
!= UNARY_DOTDOTDOT
) {
723 fprintf(fd
, "[error]: semantic error (second unary expression of enumerator is unexpected)\n");
733 cds_list_for_each_entry(iter
, &node
->u
.enumerator
.values
, siblings
) {
734 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
740 switch (node
->parent
->type
) {
745 case NODE_CTF_EXPRESSION
:
747 case NODE_TYPEALIAS_TARGET
:
748 case NODE_TYPEALIAS_ALIAS
:
749 case NODE_TYPE_DECLARATOR
:
750 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
754 case NODE_TYPE_SPECIFIER
:
756 case NODE_FLOATING_POINT
:
759 case NODE_ENUMERATOR
:
766 case NODE_UNARY_EXPRESSION
:
771 if (node
->u
._enum
.container_type
) {
772 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, node
->u
._enum
.container_type
);
777 cds_list_for_each_entry(iter
, &node
->u
._enum
.enumerator_list
, siblings
) {
778 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
784 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
785 switch (node
->parent
->type
) {
792 cds_list_for_each_entry(iter
, &node
->u
.struct_or_variant_declaration
.declaration_specifier
, siblings
) {
793 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
797 cds_list_for_each_entry(iter
, &node
->u
.struct_or_variant_declaration
.type_declarators
, siblings
) {
798 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
804 switch (node
->parent
->type
) {
809 case NODE_CTF_EXPRESSION
:
811 case NODE_TYPEALIAS_TARGET
:
812 case NODE_TYPEALIAS_ALIAS
:
813 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
817 case NODE_TYPE_SPECIFIER
:
819 case NODE_TYPE_DECLARATOR
:
820 case NODE_FLOATING_POINT
:
823 case NODE_ENUMERATOR
:
830 case NODE_UNARY_EXPRESSION
:
833 cds_list_for_each_entry(iter
, &node
->u
.variant
.declaration_list
, siblings
) {
834 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
841 switch (node
->parent
->type
) {
846 case NODE_CTF_EXPRESSION
:
848 case NODE_TYPEALIAS_TARGET
:
849 case NODE_TYPEALIAS_ALIAS
:
850 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
854 case NODE_TYPE_SPECIFIER
:
856 case NODE_TYPE_DECLARATOR
:
857 case NODE_FLOATING_POINT
:
860 case NODE_ENUMERATOR
:
867 case NODE_UNARY_EXPRESSION
:
870 cds_list_for_each_entry(iter
, &node
->u
._struct
.declaration_list
, siblings
) {
871 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
879 fprintf(fd
, "[error] %s: unknown node type %d\n", __func__
,
886 fprintf(fd
, "[error] %s: incoherent parent type %s for node type %s\n", __func__
,
887 node_type(node
->parent
), node_type(node
));
888 return -EINVAL
; /* Incoherent structure */
891 fprintf(fd
, "[error] %s: semantic error (parent type %s for node type %s)\n", __func__
,
892 node_type(node
->parent
), node_type(node
));
893 return -EPERM
; /* Structure not allowed */
896 int ctf_visitor_semantic_check(FILE *fd
, int depth
, struct ctf_node
*node
)
901 * First make sure we create the parent links for all children. Let's
902 * take the safe route and recreate them at each validation, just in
903 * case the structure has changed.
905 fprintf(fd
, "CTF visitor: parent links creation... ");
906 ret
= ctf_visitor_parent_links(fd
, depth
, node
);
909 fprintf(fd
, "done.\n");
910 fprintf(fd
, "CTF visitor: semantic check... ");
911 ret
= _ctf_visitor_semantic_check(fd
, depth
, node
);
914 fprintf(fd
, "done.\n");