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.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #include <babeltrace/babeltrace-internal.h>
36 #include <babeltrace/list-internal.h>
41 #define BT_LOG_TAG "PLUGIN-CTF-METADATA-VISITOR-SEMANTIC-VALIDATOR"
44 #define _bt_list_first_entry(ptr, type, member) \
45 bt_list_entry((ptr)->next, type, member)
48 int _ctf_visitor_semantic_check(int depth
, struct ctf_node
*node
);
51 int ctf_visitor_unary_expression(int depth
, struct ctf_node
*node
)
53 struct ctf_node
*iter
;
54 int is_ctf_exp
= 0, is_ctf_exp_left
= 0;
56 switch (node
->parent
->type
) {
57 case NODE_CTF_EXPRESSION
:
59 bt_list_for_each_entry(iter
, &node
->parent
->u
.ctf_expression
.left
,
64 * We are a left child of a ctf expression.
65 * We are only allowed to be a string.
67 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
68 BT_LOGE("semantic error (left child of a ctf expression is only allowed to be a string)");
74 /* Right child of a ctf expression can be any type of unary exp. */
76 case NODE_TYPE_DECLARATOR
:
78 * We are the length of a type declarator.
80 switch (node
->u
.unary_expression
.type
) {
81 case UNARY_UNSIGNED_CONSTANT
:
85 BT_LOGE("semantic error (children of type declarator and enum can only be unsigned numeric constants or references to fields (a.b.c))");
92 * We are the size of a struct align attribute.
94 switch (node
->u
.unary_expression
.type
) {
95 case UNARY_UNSIGNED_CONSTANT
:
98 BT_LOGE("semantic error (structure alignment attribute can only be unsigned numeric constants)");
103 case NODE_ENUMERATOR
:
104 /* The enumerator's parent has validated its validity already. */
107 case NODE_UNARY_EXPRESSION
:
109 * We disallow nested unary expressions and "sbrac" unary
112 BT_LOGE("semantic error (nested unary expressions not allowed ( () and [] ))");
123 case NODE_TYPEALIAS_TARGET
:
124 case NODE_TYPEALIAS_ALIAS
:
126 case NODE_TYPE_SPECIFIER
:
128 case NODE_FLOATING_POINT
:
132 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
138 switch (node
->u
.unary_expression
.link
) {
139 case UNARY_LINK_UNKNOWN
:
140 /* We don't allow empty link except on the first node of the list */
141 if (is_ctf_exp
&& _bt_list_first_entry(is_ctf_exp_left
?
142 &node
->parent
->u
.ctf_expression
.left
:
143 &node
->parent
->u
.ctf_expression
.right
,
146 BT_LOGE("semantic error (empty link not allowed except on first node of unary expression (need to separate nodes with \".\" or \"->\")");
151 case UNARY_ARROWLINK
:
152 /* We only allow -> and . links between children of ctf_expression. */
153 if (node
->parent
->type
!= NODE_CTF_EXPRESSION
) {
154 BT_LOGE("semantic error (links \".\" and \"->\" are only allowed as children of ctf expression)");
158 * Only strings can be separated linked by . or ->.
159 * This includes "", '' and non-quoted identifiers.
161 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
162 BT_LOGE("semantic error (links \".\" and \"->\" are only allowed to separate strings and identifiers)");
165 /* We don't allow link on the first node of the list */
166 if (is_ctf_exp
&& _bt_list_first_entry(is_ctf_exp_left
?
167 &node
->parent
->u
.ctf_expression
.left
:
168 &node
->parent
->u
.ctf_expression
.right
,
171 BT_LOGE("semantic error (links \".\" and \"->\" are not allowed before first node of the unary expression list)");
175 case UNARY_DOTDOTDOT
:
176 /* We only allow ... link between children of enumerator. */
177 if (node
->parent
->type
!= NODE_ENUMERATOR
) {
178 BT_LOGE("semantic error (link \"...\" is only allowed within enumerator)");
181 /* We don't allow link on the first node of the list */
182 if (_bt_list_first_entry(&node
->parent
->u
.enumerator
.values
,
185 BT_LOGE("semantic error (link \"...\" is not allowed on the first node of the unary expression list)");
190 BT_LOGE("%s: unknown expression link type %d", __func__
,
191 (int) node
->u
.unary_expression
.link
);
197 BT_LOGE("%s: incoherent parent type %s for node type %s", __func__
,
198 node_type(node
->parent
), node_type(node
));
199 return -EINVAL
; /* Incoherent structure */
202 BT_LOGE("%s: semantic error (parent type %s for node type %s)", __func__
,
203 node_type(node
->parent
), node_type(node
));
204 return -EPERM
; /* Structure not allowed */
208 int ctf_visitor_type_specifier_list(int depth
, struct ctf_node
*node
)
210 switch (node
->parent
->type
) {
211 case NODE_CTF_EXPRESSION
:
212 case NODE_TYPE_DECLARATOR
:
214 case NODE_TYPEALIAS_TARGET
:
215 case NODE_TYPEALIAS_ALIAS
:
217 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
227 case NODE_UNARY_EXPRESSION
:
229 case NODE_TYPE_SPECIFIER
:
230 case NODE_TYPE_SPECIFIER_LIST
:
232 case NODE_FLOATING_POINT
:
235 case NODE_ENUMERATOR
:
243 BT_LOGE("%s: incoherent parent type %s for node type %s", __func__
,
244 node_type(node
->parent
), node_type(node
));
245 return -EINVAL
; /* Incoherent structure */
249 int ctf_visitor_type_specifier(int depth
, struct ctf_node
*node
)
251 switch (node
->parent
->type
) {
252 case NODE_TYPE_SPECIFIER_LIST
:
255 case NODE_CTF_EXPRESSION
:
256 case NODE_TYPE_DECLARATOR
:
258 case NODE_TYPEALIAS_TARGET
:
259 case NODE_TYPEALIAS_ALIAS
:
261 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
269 case NODE_UNARY_EXPRESSION
:
271 case NODE_TYPE_SPECIFIER
:
273 case NODE_FLOATING_POINT
:
276 case NODE_ENUMERATOR
:
284 BT_LOGE("%s: incoherent parent type %s for node type %s", __func__
,
285 node_type(node
->parent
), node_type(node
));
286 return -EINVAL
; /* Incoherent structure */
290 int ctf_visitor_type_declarator(int depth
, struct ctf_node
*node
)
293 struct ctf_node
*iter
;
297 switch (node
->parent
->type
) {
298 case NODE_TYPE_DECLARATOR
:
300 * A nested type declarator is not allowed to contain pointers.
302 if (!bt_list_empty(&node
->u
.type_declarator
.pointers
))
305 case NODE_TYPEALIAS_TARGET
:
307 case NODE_TYPEALIAS_ALIAS
:
309 * Only accept alias name containing:
311 * - identifier * (any number of pointers)
312 * NOT accepting alias names containing [] (would otherwise
313 * cause semantic clash for later declarations of
314 * arrays/sequences of elements, where elements could be
315 * arrays/sequences themselves (if allowed in typealias).
316 * NOT accepting alias with identifier. The declarator should
317 * be either empty or contain pointer(s).
319 if (node
->u
.type_declarator
.type
== TYPEDEC_NESTED
)
321 bt_list_for_each_entry(iter
, &node
->parent
->u
.typealias_alias
.type_specifier_list
->u
.type_specifier_list
.head
,
323 switch (iter
->u
.type_specifier
.type
) {
324 case TYPESPEC_FLOATING_POINT
:
325 case TYPESPEC_INTEGER
:
326 case TYPESPEC_STRING
:
327 case TYPESPEC_STRUCT
:
328 case TYPESPEC_VARIANT
:
330 if (bt_list_empty(&node
->u
.type_declarator
.pointers
))
337 if (node
->u
.type_declarator
.type
== TYPEDEC_ID
&&
338 node
->u
.type_declarator
.u
.id
!= NULL
)
342 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
352 case NODE_CTF_EXPRESSION
:
353 case NODE_UNARY_EXPRESSION
:
355 case NODE_TYPE_SPECIFIER
:
357 case NODE_FLOATING_POINT
:
360 case NODE_ENUMERATOR
:
368 bt_list_for_each_entry(iter
, &node
->u
.type_declarator
.pointers
,
370 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
375 switch (node
->u
.type_declarator
.type
) {
380 if (node
->u
.type_declarator
.u
.nested
.type_declarator
) {
381 ret
= _ctf_visitor_semantic_check(depth
+ 1,
382 node
->u
.type_declarator
.u
.nested
.type_declarator
);
386 if (!node
->u
.type_declarator
.u
.nested
.abstract_array
) {
387 bt_list_for_each_entry(iter
, &node
->u
.type_declarator
.u
.nested
.length
,
389 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
390 BT_LOGE("%s: expecting unary expression as length", __func__
);
393 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
398 if (node
->parent
->type
== NODE_TYPEALIAS_TARGET
) {
399 BT_LOGE("%s: abstract array declarator not permitted as target of typealias", __func__
);
403 if (node
->u
.type_declarator
.bitfield_len
) {
404 ret
= _ctf_visitor_semantic_check(depth
+ 1,
405 node
->u
.type_declarator
.bitfield_len
);
411 case TYPEDEC_UNKNOWN
:
413 BT_LOGE("%s: unknown type declarator %d", __func__
,
414 (int) node
->u
.type_declarator
.type
);
421 BT_LOGE("%s: incoherent parent type %s for node type %s", __func__
,
422 node_type(node
->parent
), node_type(node
));
423 return -EINVAL
; /* Incoherent structure */
426 BT_LOGE("%s: semantic error (parent type %s for node type %s)", __func__
,
427 node_type(node
->parent
), node_type(node
));
428 return -EPERM
; /* Structure not allowed */
432 int _ctf_visitor_semantic_check(int depth
, struct ctf_node
*node
)
435 struct ctf_node
*iter
;
440 switch (node
->type
) {
442 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
, siblings
) {
443 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
447 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
448 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
452 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
453 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
457 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
458 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
465 switch (node
->parent
->type
) {
472 bt_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
473 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
479 switch (node
->parent
->type
) {
486 bt_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
487 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
493 switch (node
->parent
->type
) {
500 bt_list_for_each_entry(iter
, &node
->u
.env
.declaration_list
, siblings
) {
501 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
507 switch (node
->parent
->type
) {
514 bt_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
515 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
521 switch (node
->parent
->type
) {
528 bt_list_for_each_entry(iter
, &node
->u
.clock
.declaration_list
, siblings
) {
529 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
535 switch (node
->parent
->type
) {
542 bt_list_for_each_entry(iter
, &node
->u
.callsite
.declaration_list
, siblings
) {
543 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
549 case NODE_CTF_EXPRESSION
:
550 switch (node
->parent
->type
) {
558 case NODE_FLOATING_POINT
:
563 case NODE_CTF_EXPRESSION
:
564 case NODE_UNARY_EXPRESSION
:
566 case NODE_TYPEALIAS_TARGET
:
567 case NODE_TYPEALIAS_ALIAS
:
568 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
570 case NODE_TYPE_SPECIFIER
:
571 case NODE_TYPE_SPECIFIER_LIST
:
573 case NODE_TYPE_DECLARATOR
:
574 case NODE_ENUMERATOR
:
583 bt_list_for_each_entry(iter
, &node
->u
.ctf_expression
.left
, siblings
) {
584 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
588 bt_list_for_each_entry(iter
, &node
->u
.ctf_expression
.right
, siblings
) {
589 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
595 case NODE_UNARY_EXPRESSION
:
596 return ctf_visitor_unary_expression(depth
, node
);
599 switch (node
->parent
->type
) {
608 case NODE_CTF_EXPRESSION
:
609 case NODE_UNARY_EXPRESSION
:
611 case NODE_TYPEALIAS_TARGET
:
612 case NODE_TYPEALIAS_ALIAS
:
614 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
615 case NODE_TYPE_SPECIFIER
:
616 case NODE_TYPE_SPECIFIER_LIST
:
618 case NODE_TYPE_DECLARATOR
:
619 case NODE_FLOATING_POINT
:
622 case NODE_ENUMERATOR
:
632 ret
= _ctf_visitor_semantic_check(depth
+ 1,
633 node
->u
._typedef
.type_specifier_list
);
636 bt_list_for_each_entry(iter
, &node
->u
._typedef
.type_declarators
, siblings
) {
637 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
643 case NODE_TYPEALIAS_TARGET
:
647 switch (node
->parent
->type
) {
655 ret
= _ctf_visitor_semantic_check(depth
+ 1,
656 node
->u
.typealias_target
.type_specifier_list
);
660 bt_list_for_each_entry(iter
, &node
->u
.typealias_target
.type_declarators
, siblings
) {
661 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
666 if (nr_declarators
> 1) {
667 BT_LOGE("%s: Too many declarators in typealias alias (%d, max is 1)", __func__
, nr_declarators
);
674 case NODE_TYPEALIAS_ALIAS
:
678 switch (node
->parent
->type
) {
686 ret
= _ctf_visitor_semantic_check(depth
+ 1,
687 node
->u
.typealias_alias
.type_specifier_list
);
691 bt_list_for_each_entry(iter
, &node
->u
.typealias_alias
.type_declarators
, siblings
) {
692 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
697 if (nr_declarators
> 1) {
698 BT_LOGE("%s: Too many declarators in typealias alias (%d, max is 1)", __func__
, nr_declarators
);
706 switch (node
->parent
->type
) {
715 case NODE_CTF_EXPRESSION
:
716 case NODE_UNARY_EXPRESSION
:
718 case NODE_TYPEALIAS_TARGET
:
719 case NODE_TYPEALIAS_ALIAS
:
721 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
722 case NODE_TYPE_SPECIFIER
:
723 case NODE_TYPE_SPECIFIER_LIST
:
725 case NODE_TYPE_DECLARATOR
:
726 case NODE_FLOATING_POINT
:
729 case NODE_ENUMERATOR
:
738 ret
= _ctf_visitor_semantic_check(depth
+ 1, node
->u
.typealias
.target
);
741 ret
= _ctf_visitor_semantic_check(depth
+ 1, node
->u
.typealias
.alias
);
746 case NODE_TYPE_SPECIFIER_LIST
:
747 ret
= ctf_visitor_type_specifier_list(depth
, node
);
751 case NODE_TYPE_SPECIFIER
:
752 ret
= ctf_visitor_type_specifier(depth
, node
);
757 switch (node
->parent
->type
) {
758 case NODE_TYPE_DECLARATOR
:
764 case NODE_TYPE_DECLARATOR
:
765 ret
= ctf_visitor_type_declarator(depth
, node
);
770 case NODE_FLOATING_POINT
:
771 switch (node
->parent
->type
) {
772 case NODE_TYPE_SPECIFIER
:
777 case NODE_UNARY_EXPRESSION
:
780 bt_list_for_each_entry(iter
, &node
->u
.floating_point
.expressions
, siblings
) {
781 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
787 switch (node
->parent
->type
) {
788 case NODE_TYPE_SPECIFIER
:
795 bt_list_for_each_entry(iter
, &node
->u
.integer
.expressions
, siblings
) {
796 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
802 switch (node
->parent
->type
) {
803 case NODE_TYPE_SPECIFIER
:
808 case NODE_UNARY_EXPRESSION
:
812 bt_list_for_each_entry(iter
, &node
->u
.string
.expressions
, siblings
) {
813 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
818 case NODE_ENUMERATOR
:
819 switch (node
->parent
->type
) {
826 * Enumerators are only allows to contain:
827 * numeric unary expression
828 * or num. unary exp. ... num. unary exp
833 bt_list_for_each_entry(iter
, &node
->u
.enumerator
.values
,
836 case 0: if (iter
->type
!= NODE_UNARY_EXPRESSION
837 || (iter
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
838 && iter
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
)
839 || iter
->u
.unary_expression
.link
!= UNARY_LINK_UNKNOWN
) {
840 BT_LOGE("semantic error (first unary expression of enumerator is unexpected)");
844 case 1: if (iter
->type
!= NODE_UNARY_EXPRESSION
845 || (iter
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
846 && iter
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
)
847 || iter
->u
.unary_expression
.link
!= UNARY_DOTDOTDOT
) {
848 BT_LOGE("semantic error (second unary expression of enumerator is unexpected)");
858 bt_list_for_each_entry(iter
, &node
->u
.enumerator
.values
, siblings
) {
859 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
865 switch (node
->parent
->type
) {
866 case NODE_TYPE_SPECIFIER
:
871 case NODE_UNARY_EXPRESSION
:
876 ret
= _ctf_visitor_semantic_check(depth
+ 1, node
->u
._enum
.container_type
);
880 bt_list_for_each_entry(iter
, &node
->u
._enum
.enumerator_list
, siblings
) {
881 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
887 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
888 switch (node
->parent
->type
) {
895 ret
= _ctf_visitor_semantic_check(depth
+ 1,
896 node
->u
.struct_or_variant_declaration
.type_specifier_list
);
899 bt_list_for_each_entry(iter
, &node
->u
.struct_or_variant_declaration
.type_declarators
, siblings
) {
900 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
906 switch (node
->parent
->type
) {
907 case NODE_TYPE_SPECIFIER
:
912 case NODE_UNARY_EXPRESSION
:
915 bt_list_for_each_entry(iter
, &node
->u
.variant
.declaration_list
, siblings
) {
916 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
923 switch (node
->parent
->type
) {
924 case NODE_TYPE_SPECIFIER
:
929 case NODE_UNARY_EXPRESSION
:
932 bt_list_for_each_entry(iter
, &node
->u
._struct
.declaration_list
, siblings
) {
933 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
941 BT_LOGE("%s: unknown node type %d", __func__
,
948 BT_LOGE("%s: incoherent parent type %s for node type %s", __func__
,
949 node_type(node
->parent
), node_type(node
));
950 return -EINVAL
; /* Incoherent structure */
953 BT_LOGE("%s: semantic error (parent type %s for node type %s)", __func__
,
954 node_type(node
->parent
), node_type(node
));
955 return -EPERM
; /* Structure not allowed */
958 int ctf_visitor_semantic_check(int depth
, struct ctf_node
*node
)
963 * First make sure we create the parent links for all children. Let's
964 * take the safe route and recreate them at each validation, just in
965 * case the structure has changed.
967 BT_LOGV("CTF visitor: parent links creation... ");
968 ret
= ctf_visitor_parent_links(depth
, node
);
973 BT_LOGV("CTF visitor: semantic check... ");
974 ret
= _ctf_visitor_semantic_check(depth
, node
);