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
27 #define BT_LOG_TAG "PLUGIN-CTF-METADATA-SEMANTIC-VALIDATOR-VISITOR"
38 #include <babeltrace/list-internal.h>
43 #define _bt_list_first_entry(ptr, type, member) \
44 bt_list_entry((ptr)->next, type, member)
47 int _ctf_visitor_semantic_check(int depth
, struct ctf_node
*node
);
50 int ctf_visitor_unary_expression(int depth
, struct ctf_node
*node
)
52 struct ctf_node
*iter
;
53 int is_ctf_exp
= 0, is_ctf_exp_left
= 0;
55 switch (node
->parent
->type
) {
56 case NODE_CTF_EXPRESSION
:
58 bt_list_for_each_entry(iter
, &node
->parent
->u
.ctf_expression
.left
,
63 * We are a left child of a ctf expression.
64 * We are only allowed to be a string.
66 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
67 _BT_LOGE_LINENO(node
->lineno
,
68 "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_LINENO(node
->lineno
,
86 "Children of type declarator and `enum` can only be unsigned numeric constants or references to fields (e.g., `a.b.c`).");
93 * We are the size of a struct align attribute.
95 switch (node
->u
.unary_expression
.type
) {
96 case UNARY_UNSIGNED_CONSTANT
:
99 _BT_LOGE_LINENO(node
->lineno
,
100 "Structure alignment attribute can only be an unsigned numeric constant.");
105 case NODE_ENUMERATOR
:
106 /* The enumerator's parent has validated its validity already. */
109 case NODE_UNARY_EXPRESSION
:
111 * We disallow nested unary expressions and "sbrac" unary
114 _BT_LOGE_LINENO(node
->lineno
,
115 "Nested unary expressions not allowed (`()` and `[]`).");
126 case NODE_TYPEALIAS_TARGET
:
127 case NODE_TYPEALIAS_ALIAS
:
129 case NODE_TYPE_SPECIFIER
:
131 case NODE_FLOATING_POINT
:
135 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
141 switch (node
->u
.unary_expression
.link
) {
142 case UNARY_LINK_UNKNOWN
:
143 /* We don't allow empty link except on the first node of the list */
144 if (is_ctf_exp
&& _bt_list_first_entry(is_ctf_exp_left
?
145 &node
->parent
->u
.ctf_expression
.left
:
146 &node
->parent
->u
.ctf_expression
.right
,
149 _BT_LOGE_LINENO(node
->lineno
,
150 "Empty link is not allowed except on first node of unary expression (need to separate nodes with `.` or `->`).");
155 case UNARY_ARROWLINK
:
156 /* We only allow -> and . links between children of ctf_expression. */
157 if (node
->parent
->type
!= NODE_CTF_EXPRESSION
) {
158 _BT_LOGE_LINENO(node
->lineno
,
159 "Links `.` and `->` are only allowed as children of CTF expression.");
163 * Only strings can be separated linked by . or ->.
164 * This includes "", '' and non-quoted identifiers.
166 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
167 _BT_LOGE_LINENO(node
->lineno
,
168 "Links `.` and `->` are only allowed to separate strings and identifiers.");
171 /* We don't allow link on the first node of the list */
172 if (is_ctf_exp
&& _bt_list_first_entry(is_ctf_exp_left
?
173 &node
->parent
->u
.ctf_expression
.left
:
174 &node
->parent
->u
.ctf_expression
.right
,
177 _BT_LOGE_LINENO(node
->lineno
,
178 "Links `.` and `->` are not allowed before first node of the unary expression list.");
182 case UNARY_DOTDOTDOT
:
183 /* We only allow ... link between children of enumerator. */
184 if (node
->parent
->type
!= NODE_ENUMERATOR
) {
185 _BT_LOGE_LINENO(node
->lineno
,
186 "Link `...` is only allowed within enumerator.");
189 /* We don't allow link on the first node of the list */
190 if (_bt_list_first_entry(&node
->parent
->u
.enumerator
.values
,
193 _BT_LOGE_LINENO(node
->lineno
,
194 "Link `...` is not allowed on the first node of the unary expression list.");
199 _BT_LOGE_LINENO(node
->lineno
,
200 "Unknown expression link type: type=%d",
201 node
->u
.unary_expression
.link
);
207 _BT_LOGE_LINENO(node
->lineno
,
208 "Incoherent parent node's type: node-type=%s, parent-node-type=%s",
209 node_type(node
), node_type(node
->parent
));
210 return -EINVAL
; /* Incoherent structure */
213 _BT_LOGE_LINENO(node
->lineno
,
214 "Semantic error: node-type=%s, parent-node-type=%s",
215 node_type(node
), node_type(node
->parent
));
216 return -EPERM
; /* Structure not allowed */
220 int ctf_visitor_type_specifier_list(int depth
, struct ctf_node
*node
)
222 switch (node
->parent
->type
) {
223 case NODE_CTF_EXPRESSION
:
224 case NODE_TYPE_DECLARATOR
:
226 case NODE_TYPEALIAS_TARGET
:
227 case NODE_TYPEALIAS_ALIAS
:
229 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
239 case NODE_UNARY_EXPRESSION
:
241 case NODE_TYPE_SPECIFIER
:
242 case NODE_TYPE_SPECIFIER_LIST
:
244 case NODE_FLOATING_POINT
:
247 case NODE_ENUMERATOR
:
255 _BT_LOGE_LINENO(node
->lineno
,
256 "Incoherent parent node's type: node-type=%s, parent-node-type=%s",
257 node_type(node
), node_type(node
->parent
));
258 return -EINVAL
; /* Incoherent structure */
262 int ctf_visitor_type_specifier(int depth
, struct ctf_node
*node
)
264 switch (node
->parent
->type
) {
265 case NODE_TYPE_SPECIFIER_LIST
:
268 case NODE_CTF_EXPRESSION
:
269 case NODE_TYPE_DECLARATOR
:
271 case NODE_TYPEALIAS_TARGET
:
272 case NODE_TYPEALIAS_ALIAS
:
274 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
282 case NODE_UNARY_EXPRESSION
:
284 case NODE_TYPE_SPECIFIER
:
286 case NODE_FLOATING_POINT
:
289 case NODE_ENUMERATOR
:
297 _BT_LOGE_LINENO(node
->lineno
,
298 "Incoherent parent node's type: node-type=%s, parent-node-type=%s",
299 node_type(node
), node_type(node
->parent
));
300 return -EINVAL
; /* Incoherent structure */
304 int ctf_visitor_type_declarator(int depth
, struct ctf_node
*node
)
307 struct ctf_node
*iter
;
311 switch (node
->parent
->type
) {
312 case NODE_TYPE_DECLARATOR
:
314 * A nested type declarator is not allowed to contain pointers.
316 if (!bt_list_empty(&node
->u
.type_declarator
.pointers
))
319 case NODE_TYPEALIAS_TARGET
:
321 case NODE_TYPEALIAS_ALIAS
:
323 * Only accept alias name containing:
325 * - identifier * (any number of pointers)
326 * NOT accepting alias names containing [] (would otherwise
327 * cause semantic clash for later declarations of
328 * arrays/sequences of elements, where elements could be
329 * arrays/sequences themselves (if allowed in typealias).
330 * NOT accepting alias with identifier. The declarator should
331 * be either empty or contain pointer(s).
333 if (node
->u
.type_declarator
.type
== TYPEDEC_NESTED
)
335 bt_list_for_each_entry(iter
, &node
->parent
->u
.typealias_alias
.type_specifier_list
->u
.type_specifier_list
.head
,
337 switch (iter
->u
.type_specifier
.type
) {
338 case TYPESPEC_FLOATING_POINT
:
339 case TYPESPEC_INTEGER
:
340 case TYPESPEC_STRING
:
341 case TYPESPEC_STRUCT
:
342 case TYPESPEC_VARIANT
:
344 if (bt_list_empty(&node
->u
.type_declarator
.pointers
))
351 if (node
->u
.type_declarator
.type
== TYPEDEC_ID
&&
352 node
->u
.type_declarator
.u
.id
!= NULL
)
356 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
366 case NODE_CTF_EXPRESSION
:
367 case NODE_UNARY_EXPRESSION
:
369 case NODE_TYPE_SPECIFIER
:
371 case NODE_FLOATING_POINT
:
374 case NODE_ENUMERATOR
:
382 bt_list_for_each_entry(iter
, &node
->u
.type_declarator
.pointers
,
384 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
389 switch (node
->u
.type_declarator
.type
) {
394 if (node
->u
.type_declarator
.u
.nested
.type_declarator
) {
395 ret
= _ctf_visitor_semantic_check(depth
+ 1,
396 node
->u
.type_declarator
.u
.nested
.type_declarator
);
400 if (!node
->u
.type_declarator
.u
.nested
.abstract_array
) {
401 bt_list_for_each_entry(iter
, &node
->u
.type_declarator
.u
.nested
.length
,
403 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
404 _BT_LOGE_LINENO(node
->lineno
,
405 "Expecting unary expression as length: node-type=%s",
409 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
414 if (node
->parent
->type
== NODE_TYPEALIAS_TARGET
) {
415 _BT_LOGE_LINENO(node
->lineno
,
416 "Abstract array declarator not permitted as target of type alias.");
420 if (node
->u
.type_declarator
.bitfield_len
) {
421 ret
= _ctf_visitor_semantic_check(depth
+ 1,
422 node
->u
.type_declarator
.bitfield_len
);
428 case TYPEDEC_UNKNOWN
:
430 _BT_LOGE_LINENO(node
->lineno
,
431 "Unknown type declarator: type=%d",
432 node
->u
.type_declarator
.type
);
439 _BT_LOGE_LINENO(node
->lineno
,
440 "Incoherent parent node's type: node-type=%s, parent-node-type=%s",
441 node_type(node
), node_type(node
->parent
));
442 return -EINVAL
; /* Incoherent structure */
445 _BT_LOGE_LINENO(node
->lineno
,
446 "Semantic error: node-type=%s, parent-node-type=%s",
447 node_type(node
), node_type(node
->parent
));
448 return -EPERM
; /* Structure not allowed */
452 int _ctf_visitor_semantic_check(int depth
, struct ctf_node
*node
)
455 struct ctf_node
*iter
;
460 switch (node
->type
) {
462 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
, siblings
) {
463 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
467 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
468 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
472 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
473 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
477 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
478 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
485 switch (node
->parent
->type
) {
492 bt_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
493 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
499 switch (node
->parent
->type
) {
506 bt_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
507 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
513 switch (node
->parent
->type
) {
520 bt_list_for_each_entry(iter
, &node
->u
.env
.declaration_list
, siblings
) {
521 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
527 switch (node
->parent
->type
) {
534 bt_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
535 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
541 switch (node
->parent
->type
) {
548 bt_list_for_each_entry(iter
, &node
->u
.clock
.declaration_list
, siblings
) {
549 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
555 switch (node
->parent
->type
) {
562 bt_list_for_each_entry(iter
, &node
->u
.callsite
.declaration_list
, siblings
) {
563 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
569 case NODE_CTF_EXPRESSION
:
570 switch (node
->parent
->type
) {
578 case NODE_FLOATING_POINT
:
583 case NODE_CTF_EXPRESSION
:
584 case NODE_UNARY_EXPRESSION
:
586 case NODE_TYPEALIAS_TARGET
:
587 case NODE_TYPEALIAS_ALIAS
:
588 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
590 case NODE_TYPE_SPECIFIER
:
591 case NODE_TYPE_SPECIFIER_LIST
:
593 case NODE_TYPE_DECLARATOR
:
594 case NODE_ENUMERATOR
:
603 bt_list_for_each_entry(iter
, &node
->u
.ctf_expression
.left
, siblings
) {
604 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
608 bt_list_for_each_entry(iter
, &node
->u
.ctf_expression
.right
, siblings
) {
609 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
615 case NODE_UNARY_EXPRESSION
:
616 return ctf_visitor_unary_expression(depth
, node
);
619 switch (node
->parent
->type
) {
628 case NODE_CTF_EXPRESSION
:
629 case NODE_UNARY_EXPRESSION
:
631 case NODE_TYPEALIAS_TARGET
:
632 case NODE_TYPEALIAS_ALIAS
:
634 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
635 case NODE_TYPE_SPECIFIER
:
636 case NODE_TYPE_SPECIFIER_LIST
:
638 case NODE_TYPE_DECLARATOR
:
639 case NODE_FLOATING_POINT
:
642 case NODE_ENUMERATOR
:
652 ret
= _ctf_visitor_semantic_check(depth
+ 1,
653 node
->u
._typedef
.type_specifier_list
);
656 bt_list_for_each_entry(iter
, &node
->u
._typedef
.type_declarators
, siblings
) {
657 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
663 case NODE_TYPEALIAS_TARGET
:
667 switch (node
->parent
->type
) {
675 ret
= _ctf_visitor_semantic_check(depth
+ 1,
676 node
->u
.typealias_target
.type_specifier_list
);
680 bt_list_for_each_entry(iter
, &node
->u
.typealias_target
.type_declarators
, siblings
) {
681 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
686 if (nr_declarators
> 1) {
687 _BT_LOGE_LINENO(node
->lineno
,
688 "Too many declarators in type alias's name (maximum is 1): count=%d",
695 case NODE_TYPEALIAS_ALIAS
:
699 switch (node
->parent
->type
) {
707 ret
= _ctf_visitor_semantic_check(depth
+ 1,
708 node
->u
.typealias_alias
.type_specifier_list
);
712 bt_list_for_each_entry(iter
, &node
->u
.typealias_alias
.type_declarators
, siblings
) {
713 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
718 if (nr_declarators
> 1) {
719 _BT_LOGE_LINENO(node
->lineno
,
720 "Too many declarators in type alias's name (maximum is 1): count=%d",
728 switch (node
->parent
->type
) {
737 case NODE_CTF_EXPRESSION
:
738 case NODE_UNARY_EXPRESSION
:
740 case NODE_TYPEALIAS_TARGET
:
741 case NODE_TYPEALIAS_ALIAS
:
743 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
744 case NODE_TYPE_SPECIFIER
:
745 case NODE_TYPE_SPECIFIER_LIST
:
747 case NODE_TYPE_DECLARATOR
:
748 case NODE_FLOATING_POINT
:
751 case NODE_ENUMERATOR
:
760 ret
= _ctf_visitor_semantic_check(depth
+ 1, node
->u
.typealias
.target
);
763 ret
= _ctf_visitor_semantic_check(depth
+ 1, node
->u
.typealias
.alias
);
768 case NODE_TYPE_SPECIFIER_LIST
:
769 ret
= ctf_visitor_type_specifier_list(depth
, node
);
773 case NODE_TYPE_SPECIFIER
:
774 ret
= ctf_visitor_type_specifier(depth
, node
);
779 switch (node
->parent
->type
) {
780 case NODE_TYPE_DECLARATOR
:
786 case NODE_TYPE_DECLARATOR
:
787 ret
= ctf_visitor_type_declarator(depth
, node
);
792 case NODE_FLOATING_POINT
:
793 switch (node
->parent
->type
) {
794 case NODE_TYPE_SPECIFIER
:
799 case NODE_UNARY_EXPRESSION
:
802 bt_list_for_each_entry(iter
, &node
->u
.floating_point
.expressions
, siblings
) {
803 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
809 switch (node
->parent
->type
) {
810 case NODE_TYPE_SPECIFIER
:
817 bt_list_for_each_entry(iter
, &node
->u
.integer
.expressions
, siblings
) {
818 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
824 switch (node
->parent
->type
) {
825 case NODE_TYPE_SPECIFIER
:
830 case NODE_UNARY_EXPRESSION
:
834 bt_list_for_each_entry(iter
, &node
->u
.string
.expressions
, siblings
) {
835 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
840 case NODE_ENUMERATOR
:
841 switch (node
->parent
->type
) {
848 * Enumerators are only allows to contain:
849 * numeric unary expression
850 * or num. unary exp. ... num. unary exp
855 bt_list_for_each_entry(iter
, &node
->u
.enumerator
.values
,
858 case 0: if (iter
->type
!= NODE_UNARY_EXPRESSION
859 || (iter
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
860 && iter
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
)
861 || iter
->u
.unary_expression
.link
!= UNARY_LINK_UNKNOWN
) {
862 _BT_LOGE_LINENO(iter
->lineno
,
863 "First unary expression of enumerator is unexpected.");
867 case 1: if (iter
->type
!= NODE_UNARY_EXPRESSION
868 || (iter
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
869 && iter
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
)
870 || iter
->u
.unary_expression
.link
!= UNARY_DOTDOTDOT
) {
871 _BT_LOGE_LINENO(iter
->lineno
,
872 "Second unary expression of enumerator is unexpected.");
882 bt_list_for_each_entry(iter
, &node
->u
.enumerator
.values
, siblings
) {
883 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
889 switch (node
->parent
->type
) {
890 case NODE_TYPE_SPECIFIER
:
895 case NODE_UNARY_EXPRESSION
:
900 ret
= _ctf_visitor_semantic_check(depth
+ 1, node
->u
._enum
.container_type
);
904 bt_list_for_each_entry(iter
, &node
->u
._enum
.enumerator_list
, siblings
) {
905 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
911 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
912 switch (node
->parent
->type
) {
919 ret
= _ctf_visitor_semantic_check(depth
+ 1,
920 node
->u
.struct_or_variant_declaration
.type_specifier_list
);
923 bt_list_for_each_entry(iter
, &node
->u
.struct_or_variant_declaration
.type_declarators
, siblings
) {
924 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
930 switch (node
->parent
->type
) {
931 case NODE_TYPE_SPECIFIER
:
936 case NODE_UNARY_EXPRESSION
:
939 bt_list_for_each_entry(iter
, &node
->u
.variant
.declaration_list
, siblings
) {
940 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
947 switch (node
->parent
->type
) {
948 case NODE_TYPE_SPECIFIER
:
953 case NODE_UNARY_EXPRESSION
:
956 bt_list_for_each_entry(iter
, &node
->u
._struct
.declaration_list
, siblings
) {
957 ret
= _ctf_visitor_semantic_check(depth
+ 1, iter
);
965 _BT_LOGE_LINENO(node
->lineno
,
966 "Unknown node type: type=%d", node
->type
);
972 _BT_LOGE_LINENO(node
->lineno
,
973 "Incoherent parent node's type: node-type=%s, parent-node-type=%s",
974 node_type(node
), node_type(node
->parent
));
975 return -EINVAL
; /* Incoherent structure */
978 _BT_LOGE_LINENO(node
->lineno
,
979 "Semantic error: node-type=%s, parent-node-type=%s",
980 node_type(node
), node_type(node
->parent
));
981 return -EPERM
; /* Structure not allowed */
984 int ctf_visitor_semantic_check(int depth
, struct ctf_node
*node
)
989 * First make sure we create the parent links for all children. Let's
990 * take the safe route and recreate them at each validation, just in
991 * case the structure has changed.
993 ret
= ctf_visitor_parent_links(depth
, node
);
995 _BT_LOGE_LINENO(node
->lineno
,
996 "Cannot create parent links in metadata's AST: "
1001 ret
= _ctf_visitor_semantic_check(depth
, node
);
1003 _BT_LOGE_LINENO(node
->lineno
,
1004 "Cannot check metadata's AST semantics: "