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
))
236 case NODE_TYPEALIAS_TARGET
:
238 case NODE_TYPEALIAS_ALIAS
:
240 * Only accept alias name containing:
242 * - identifier * (any number of pointers)
243 * NOT accepting alias names containing [] (would otherwise
244 * cause semantic clash for later declarations of
245 * arrays/sequences of elements, where elements could be
246 * arrays/sequences themselves (if allowed in typealias).
247 * NOT accepting alias with identifier. The declarator should
248 * be either empty or contain pointer(s).
250 if (node
->u
.type_declarator
.type
== TYPEDEC_NESTED
)
252 if (cds_list_empty(&node
->u
.type_declarator
.pointers
))
254 if (node
->u
.type_declarator
.type
== TYPEDEC_ID
&&
255 node
->u
.type_declarator
.u
.id
!= NULL
)
259 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
266 case NODE_CTF_EXPRESSION
:
267 case NODE_UNARY_EXPRESSION
:
269 case NODE_TYPE_SPECIFIER
:
271 case NODE_FLOATING_POINT
:
274 case NODE_ENUMERATOR
:
282 if (!cds_list_empty(&node
->u
.type_declarator
.pointers
)) {
283 cds_list_for_each_entry(iter
, &node
->u
.type_declarator
.pointers
,
285 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
291 switch (node
->u
.type_declarator
.type
) {
295 if (node
->u
.type_declarator
.u
.nested
.type_declarator
) {
296 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1,
297 node
->u
.type_declarator
.u
.nested
.type_declarator
);
301 if (node
->u
.type_declarator
.u
.nested
.length
) {
302 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1,
303 node
->u
.type_declarator
.u
.nested
.length
);
307 if (node
->u
.type_declarator
.bitfield_len
) {
308 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1,
309 node
->u
.type_declarator
.bitfield_len
);
314 case TYPEDEC_UNKNOWN
:
316 fprintf(fd
, "[error] %s: unknown type declarator %d\n", __func__
,
317 (int) node
->u
.type_declarator
.type
);
324 fprintf(fd
, "[error] %s: incoherent parent type %s for node type %s\n", __func__
,
325 node_type(node
->parent
), node_type(node
));
326 return -EINVAL
; /* Incoherent structure */
329 fprintf(fd
, "[error] %s: semantic error (parent type %s for node type %s)\n", __func__
,
330 node_type(node
->parent
), node_type(node
));
331 return -EPERM
; /* Structure not allowed */
335 int _ctf_visitor_semantic_check(FILE *fd
, int depth
, struct ctf_node
*node
)
338 struct ctf_node
*iter
;
340 switch (node
->type
) {
342 cds_list_for_each_entry(iter
, &node
->u
.root
._typedef
,
344 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
348 cds_list_for_each_entry(iter
, &node
->u
.root
.typealias
,
350 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
354 cds_list_for_each_entry(iter
, &node
->u
.root
.declaration_specifier
, siblings
) {
355 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
359 cds_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
360 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
364 cds_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
365 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
369 cds_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
370 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
377 switch (node
->parent
->type
) {
384 cds_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
385 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
391 switch (node
->parent
->type
) {
398 cds_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
399 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
405 switch (node
->parent
->type
) {
412 cds_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
413 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
419 case NODE_CTF_EXPRESSION
:
420 switch (node
->parent
->type
) {
425 case NODE_FLOATING_POINT
:
430 case NODE_CTF_EXPRESSION
:
431 case NODE_UNARY_EXPRESSION
:
433 case NODE_TYPEALIAS_TARGET
:
434 case NODE_TYPEALIAS_ALIAS
:
435 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
437 case NODE_TYPE_SPECIFIER
:
439 case NODE_TYPE_DECLARATOR
:
440 case NODE_ENUMERATOR
:
449 cds_list_for_each_entry(iter
, &node
->u
.ctf_expression
.left
, siblings
) {
450 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
454 cds_list_for_each_entry(iter
, &node
->u
.ctf_expression
.right
, siblings
) {
455 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
461 case NODE_UNARY_EXPRESSION
:
462 return ctf_visitor_unary_expression(fd
, depth
, node
);
465 switch (node
->parent
->type
) {
474 case NODE_CTF_EXPRESSION
:
475 case NODE_UNARY_EXPRESSION
:
477 case NODE_TYPEALIAS_TARGET
:
478 case NODE_TYPEALIAS_ALIAS
:
480 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
481 case NODE_TYPE_SPECIFIER
:
483 case NODE_TYPE_DECLARATOR
:
484 case NODE_FLOATING_POINT
:
487 case NODE_ENUMERATOR
:
494 cds_list_for_each_entry(iter
, &node
->u
._typedef
.declaration_specifier
, siblings
) {
495 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
499 cds_list_for_each_entry(iter
, &node
->u
._typedef
.type_declarators
, siblings
) {
500 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
506 case NODE_TYPEALIAS_TARGET
:
510 switch (node
->parent
->type
) {
518 cds_list_for_each_entry(iter
, &node
->u
.typealias_target
.declaration_specifier
, siblings
) {
519 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
524 cds_list_for_each_entry(iter
, &node
->u
.typealias_target
.type_declarators
, siblings
) {
525 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
530 if (nr_declarators
> 1) {
531 fprintf(fd
, "[error] %s: Too many declarators in typealias alias (%d, max is 1)\n", __func__
, nr_declarators
);
538 case NODE_TYPEALIAS_ALIAS
:
542 switch (node
->parent
->type
) {
550 cds_list_for_each_entry(iter
, &node
->u
.typealias_alias
.declaration_specifier
, siblings
) {
551 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
556 cds_list_for_each_entry(iter
, &node
->u
.typealias_alias
.type_declarators
, siblings
) {
557 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
562 if (nr_declarators
> 1) {
563 fprintf(fd
, "[error] %s: Too many declarators in typealias alias (%d, max is 1)\n", __func__
, nr_declarators
);
571 switch (node
->parent
->type
) {
580 case NODE_CTF_EXPRESSION
:
581 case NODE_UNARY_EXPRESSION
:
583 case NODE_TYPEALIAS_TARGET
:
584 case NODE_TYPEALIAS_ALIAS
:
586 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
587 case NODE_TYPE_SPECIFIER
:
589 case NODE_TYPE_DECLARATOR
:
590 case NODE_FLOATING_POINT
:
593 case NODE_ENUMERATOR
:
599 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, node
->u
.typealias
.target
);
602 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, node
->u
.typealias
.alias
);
607 case NODE_TYPE_SPECIFIER
:
608 ret
= ctf_visitor_type_specifier(fd
, depth
, node
);
613 switch (node
->parent
->type
) {
614 case NODE_TYPE_DECLARATOR
:
620 case NODE_TYPE_DECLARATOR
:
621 ret
= ctf_visitor_type_declarator(fd
, depth
, node
);
626 case NODE_FLOATING_POINT
:
627 switch (node
->parent
->type
) {
628 case NODE_CTF_EXPRESSION
:
630 case NODE_TYPEALIAS_TARGET
:
631 case NODE_TYPEALIAS_ALIAS
:
632 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
640 case NODE_TYPE_SPECIFIER
:
642 case NODE_TYPE_DECLARATOR
:
643 case NODE_FLOATING_POINT
:
646 case NODE_ENUMERATOR
:
653 case NODE_UNARY_EXPRESSION
:
656 cds_list_for_each_entry(iter
, &node
->u
.floating_point
.expressions
, siblings
) {
657 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
663 switch (node
->parent
->type
) {
664 case NODE_CTF_EXPRESSION
:
665 case NODE_UNARY_EXPRESSION
:
667 case NODE_TYPEALIAS_TARGET
:
668 case NODE_TYPEALIAS_ALIAS
:
669 case NODE_TYPE_DECLARATOR
:
671 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
679 case NODE_TYPE_SPECIFIER
:
681 case NODE_FLOATING_POINT
:
684 case NODE_ENUMERATOR
:
692 cds_list_for_each_entry(iter
, &node
->u
.integer
.expressions
, siblings
) {
693 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
699 switch (node
->parent
->type
) {
700 case NODE_CTF_EXPRESSION
:
702 case NODE_TYPEALIAS_TARGET
:
703 case NODE_TYPEALIAS_ALIAS
:
704 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
712 case NODE_TYPE_SPECIFIER
:
714 case NODE_TYPE_DECLARATOR
:
715 case NODE_FLOATING_POINT
:
718 case NODE_ENUMERATOR
:
725 case NODE_UNARY_EXPRESSION
:
729 cds_list_for_each_entry(iter
, &node
->u
.string
.expressions
, siblings
) {
730 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
735 case NODE_ENUMERATOR
:
736 switch (node
->parent
->type
) {
743 * Enumerators are only allows to contain:
744 * numeric unary expression
745 * or num. unary exp. ... num. unary exp
750 cds_list_for_each_entry(iter
, &node
->u
.enumerator
.values
,
753 case 0: if (iter
->type
!= NODE_UNARY_EXPRESSION
754 || (iter
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
755 && iter
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
)
756 || iter
->u
.unary_expression
.link
!= UNARY_LINK_UNKNOWN
) {
757 fprintf(fd
, "[error]: semantic error (first unary expression of enumerator is unexpected)\n");
761 case 1: if (iter
->type
!= NODE_UNARY_EXPRESSION
762 || (iter
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
763 && iter
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
)
764 || iter
->u
.unary_expression
.link
!= UNARY_DOTDOTDOT
) {
765 fprintf(fd
, "[error]: semantic error (second unary expression of enumerator is unexpected)\n");
775 cds_list_for_each_entry(iter
, &node
->u
.enumerator
.values
, siblings
) {
776 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
782 switch (node
->parent
->type
) {
787 case NODE_CTF_EXPRESSION
:
789 case NODE_TYPEALIAS_TARGET
:
790 case NODE_TYPEALIAS_ALIAS
:
791 case NODE_TYPE_DECLARATOR
:
792 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
796 case NODE_TYPE_SPECIFIER
:
798 case NODE_FLOATING_POINT
:
801 case NODE_ENUMERATOR
:
808 case NODE_UNARY_EXPRESSION
:
813 if (node
->u
._enum
.container_type
) {
814 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, node
->u
._enum
.container_type
);
819 cds_list_for_each_entry(iter
, &node
->u
._enum
.enumerator_list
, siblings
) {
820 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
826 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
827 switch (node
->parent
->type
) {
834 cds_list_for_each_entry(iter
, &node
->u
.struct_or_variant_declaration
.declaration_specifier
, siblings
) {
835 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
839 cds_list_for_each_entry(iter
, &node
->u
.struct_or_variant_declaration
.type_declarators
, siblings
) {
840 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
846 switch (node
->parent
->type
) {
851 case NODE_CTF_EXPRESSION
:
853 case NODE_TYPEALIAS_TARGET
:
854 case NODE_TYPEALIAS_ALIAS
:
855 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
859 case NODE_TYPE_SPECIFIER
:
861 case NODE_TYPE_DECLARATOR
:
862 case NODE_FLOATING_POINT
:
865 case NODE_ENUMERATOR
:
872 case NODE_UNARY_EXPRESSION
:
875 cds_list_for_each_entry(iter
, &node
->u
.variant
.declaration_list
, siblings
) {
876 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
883 switch (node
->parent
->type
) {
888 case NODE_CTF_EXPRESSION
:
890 case NODE_TYPEALIAS_TARGET
:
891 case NODE_TYPEALIAS_ALIAS
:
892 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
896 case NODE_TYPE_SPECIFIER
:
898 case NODE_TYPE_DECLARATOR
:
899 case NODE_FLOATING_POINT
:
902 case NODE_ENUMERATOR
:
909 case NODE_UNARY_EXPRESSION
:
912 cds_list_for_each_entry(iter
, &node
->u
._struct
.declaration_list
, siblings
) {
913 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
921 fprintf(fd
, "[error] %s: unknown node type %d\n", __func__
,
928 fprintf(fd
, "[error] %s: incoherent parent type %s for node type %s\n", __func__
,
929 node_type(node
->parent
), node_type(node
));
930 return -EINVAL
; /* Incoherent structure */
933 fprintf(fd
, "[error] %s: semantic error (parent type %s for node type %s)\n", __func__
,
934 node_type(node
->parent
), node_type(node
));
935 return -EPERM
; /* Structure not allowed */
938 int ctf_visitor_semantic_check(FILE *fd
, int depth
, struct ctf_node
*node
)
943 * First make sure we create the parent links for all children. Let's
944 * take the safe route and recreate them at each validation, just in
945 * case the structure has changed.
947 fprintf(fd
, "CTF visitor: parent links creation... ");
948 ret
= ctf_visitor_parent_links(fd
, depth
, node
);
951 fprintf(fd
, "done.\n");
952 fprintf(fd
, "CTF visitor: semantic check... ");
953 ret
= _ctf_visitor_semantic_check(fd
, depth
, node
);
956 fprintf(fd
, "done.\n");