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/babeltrace.h>
28 #include <babeltrace/list.h>
29 #include "ctf-scanner.h"
30 #include "ctf-parser.h"
33 #define _cds_list_first_entry(ptr, type, member) \
34 cds_list_entry((ptr)->next, type, member)
36 #define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
39 int _ctf_visitor_semantic_check(FILE *fd
, int depth
, struct ctf_node
*node
);
42 int ctf_visitor_unary_expression(FILE *fd
, int depth
, struct ctf_node
*node
)
44 struct ctf_node
*iter
;
45 int is_ctf_exp
= 0, is_ctf_exp_left
= 0;
47 switch (node
->parent
->type
) {
48 case NODE_CTF_EXPRESSION
:
50 cds_list_for_each_entry(iter
, &node
->parent
->u
.ctf_expression
.left
,
55 * We are a left child of a ctf expression.
56 * We are only allowed to be a string.
58 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
59 fprintf(fd
, "[error]: semantic error (left child of a ctf expression is only allowed to be a string)\n");
66 /* Right child of a ctf expression can be any type of unary exp. */
68 case NODE_TYPE_DECLARATOR
:
70 * We are the length of a type declarator.
72 switch (node
->u
.unary_expression
.type
) {
73 case UNARY_UNSIGNED_CONSTANT
:
77 fprintf(fd
, "[error]: semantic error (children of type declarator and enum can only be unsigned numeric constants or references to fields (a.b.c))\n");
84 * We are the size of a struct align attribute.
86 switch (node
->u
.unary_expression
.type
) {
87 case UNARY_UNSIGNED_CONSTANT
:
90 fprintf(fd
, "[error]: semantic error (structure alignment attribute can only be unsigned numeric constants)\n");
96 /* The enumerator's parent has validated its validity already. */
99 case NODE_UNARY_EXPRESSION
:
101 * We disallow nested unary expressions and "sbrac" unary
104 fprintf(fd
, "[error]: semantic error (nested unary expressions not allowed ( () and [] ))\n");
112 case NODE_TYPEALIAS_TARGET
:
113 case NODE_TYPEALIAS_ALIAS
:
115 case NODE_TYPE_SPECIFIER
:
117 case NODE_FLOATING_POINT
:
121 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
127 switch (node
->u
.unary_expression
.link
) {
128 case UNARY_LINK_UNKNOWN
:
129 /* We don't allow empty link except on the first node of the list */
130 if (is_ctf_exp
&& _cds_list_first_entry(is_ctf_exp_left
?
131 &node
->parent
->u
.ctf_expression
.left
:
132 &node
->parent
->u
.ctf_expression
.right
,
135 fprintf(fd
, "[error]: semantic error (empty link not allowed except on first node of unary expression (need to separate nodes with \".\" or \"->\")\n");
140 case UNARY_ARROWLINK
:
141 /* We only allow -> and . links between children of ctf_expression. */
142 if (node
->parent
->type
!= NODE_CTF_EXPRESSION
) {
143 fprintf(fd
, "[error]: semantic error (links \".\" and \"->\" are only allowed as children of ctf expression)\n");
147 * Only strings can be separated linked by . or ->.
148 * This includes "", '' and non-quoted identifiers.
150 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
151 fprintf(fd
, "[error]: semantic error (links \".\" and \"->\" are only allowed to separate strings and identifiers)\n");
154 /* We don't allow link on the first node of the list */
155 if (is_ctf_exp
&& _cds_list_first_entry(is_ctf_exp_left
?
156 &node
->parent
->u
.ctf_expression
.left
:
157 &node
->parent
->u
.ctf_expression
.right
,
160 fprintf(fd
, "[error]: semantic error (links \".\" and \"->\" are not allowed before first node of the unary expression list)\n");
164 case UNARY_DOTDOTDOT
:
165 /* We only allow ... link between children of enumerator. */
166 if (node
->parent
->type
!= NODE_ENUMERATOR
) {
167 fprintf(fd
, "[error]: semantic error (link \"...\" is only allowed within enumerator)\n");
170 /* We don't allow link on the first node of the list */
171 if (_cds_list_first_entry(&node
->parent
->u
.enumerator
.values
,
174 fprintf(fd
, "[error]: semantic error (link \"...\" is not allowed on the first node of the unary expression list)\n");
179 fprintf(fd
, "[error] %s: unknown expression link type %d\n", __func__
,
180 (int) node
->u
.unary_expression
.link
);
186 fprintf(fd
, "[error] %s: incoherent parent type %s for node type %s\n", __func__
,
187 node_type(node
->parent
), node_type(node
));
188 return -EINVAL
; /* Incoherent structure */
191 fprintf(fd
, "[error] %s: semantic error (parent type %s for node type %s)\n", __func__
,
192 node_type(node
->parent
), node_type(node
));
193 return -EPERM
; /* Structure not allowed */
197 int ctf_visitor_type_specifier_list(FILE *fd
, int depth
, struct ctf_node
*node
)
199 switch (node
->parent
->type
) {
200 case NODE_CTF_EXPRESSION
:
201 case NODE_TYPE_DECLARATOR
:
203 case NODE_TYPEALIAS_TARGET
:
204 case NODE_TYPEALIAS_ALIAS
:
206 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
213 case NODE_UNARY_EXPRESSION
:
215 case NODE_TYPE_SPECIFIER
:
216 case NODE_TYPE_SPECIFIER_LIST
:
218 case NODE_FLOATING_POINT
:
221 case NODE_ENUMERATOR
:
229 fprintf(fd
, "[error] %s: incoherent parent type %s for node type %s\n", __func__
,
230 node_type(node
->parent
), node_type(node
));
231 return -EINVAL
; /* Incoherent structure */
235 int ctf_visitor_type_specifier(FILE *fd
, int depth
, struct ctf_node
*node
)
237 switch (node
->parent
->type
) {
238 case NODE_TYPE_SPECIFIER_LIST
:
241 case NODE_CTF_EXPRESSION
:
242 case NODE_TYPE_DECLARATOR
:
244 case NODE_TYPEALIAS_TARGET
:
245 case NODE_TYPEALIAS_ALIAS
:
247 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
252 case NODE_UNARY_EXPRESSION
:
254 case NODE_TYPE_SPECIFIER
:
256 case NODE_FLOATING_POINT
:
259 case NODE_ENUMERATOR
:
267 fprintf(fd
, "[error] %s: incoherent parent type %s for node type %s\n", __func__
,
268 node_type(node
->parent
), node_type(node
));
269 return -EINVAL
; /* Incoherent structure */
273 int ctf_visitor_type_declarator(FILE *fd
, int depth
, struct ctf_node
*node
)
276 struct ctf_node
*iter
;
280 switch (node
->parent
->type
) {
281 case NODE_TYPE_DECLARATOR
:
283 * A nested type declarator is not allowed to contain pointers.
285 if (!cds_list_empty(&node
->u
.type_declarator
.pointers
))
288 case NODE_TYPEALIAS_TARGET
:
290 case NODE_TYPEALIAS_ALIAS
:
292 * Only accept alias name containing:
294 * - identifier * (any number of pointers)
295 * NOT accepting alias names containing [] (would otherwise
296 * cause semantic clash for later declarations of
297 * arrays/sequences of elements, where elements could be
298 * arrays/sequences themselves (if allowed in typealias).
299 * NOT accepting alias with identifier. The declarator should
300 * be either empty or contain pointer(s).
302 if (node
->u
.type_declarator
.type
== TYPEDEC_NESTED
)
304 switch (node
->u
.type_declarator
.type
) {
305 case TYPESPEC_FLOATING_POINT
:
306 case TYPESPEC_INTEGER
:
307 case TYPESPEC_STRING
:
308 case TYPESPEC_STRUCT
:
309 case TYPESPEC_VARIANT
:
311 if (cds_list_empty(&node
->u
.type_declarator
.pointers
))
317 if (node
->u
.type_declarator
.type
== TYPEDEC_ID
&&
318 node
->u
.type_declarator
.u
.id
!= NULL
)
322 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
329 case NODE_CTF_EXPRESSION
:
330 case NODE_UNARY_EXPRESSION
:
332 case NODE_TYPE_SPECIFIER
:
334 case NODE_FLOATING_POINT
:
337 case NODE_ENUMERATOR
:
345 cds_list_for_each_entry(iter
, &node
->u
.type_declarator
.pointers
,
347 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
352 switch (node
->u
.type_declarator
.type
) {
357 if (node
->u
.type_declarator
.u
.nested
.type_declarator
) {
358 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1,
359 node
->u
.type_declarator
.u
.nested
.type_declarator
);
363 cds_list_for_each_entry(iter
, &node
->u
.type_declarator
.u
.nested
.length
,
365 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
366 fprintf(fd
, "[error] %s: expecting unary expression as length\n", __func__
);
369 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
373 if (node
->u
.type_declarator
.bitfield_len
) {
374 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1,
375 node
->u
.type_declarator
.bitfield_len
);
381 case TYPEDEC_UNKNOWN
:
383 fprintf(fd
, "[error] %s: unknown type declarator %d\n", __func__
,
384 (int) node
->u
.type_declarator
.type
);
391 fprintf(fd
, "[error] %s: incoherent parent type %s for node type %s\n", __func__
,
392 node_type(node
->parent
), node_type(node
));
393 return -EINVAL
; /* Incoherent structure */
396 fprintf(fd
, "[error] %s: semantic error (parent type %s for node type %s)\n", __func__
,
397 node_type(node
->parent
), node_type(node
));
398 return -EPERM
; /* Structure not allowed */
402 int _ctf_visitor_semantic_check(FILE *fd
, int depth
, struct ctf_node
*node
)
405 struct ctf_node
*iter
;
407 switch (node
->type
) {
409 cds_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
, siblings
) {
410 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
414 cds_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
415 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
419 cds_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
420 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
424 cds_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
425 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
432 switch (node
->parent
->type
) {
439 cds_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
440 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
446 switch (node
->parent
->type
) {
453 cds_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
454 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
460 switch (node
->parent
->type
) {
467 cds_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
468 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
474 case NODE_CTF_EXPRESSION
:
475 switch (node
->parent
->type
) {
480 case NODE_FLOATING_POINT
:
485 case NODE_CTF_EXPRESSION
:
486 case NODE_UNARY_EXPRESSION
:
488 case NODE_TYPEALIAS_TARGET
:
489 case NODE_TYPEALIAS_ALIAS
:
490 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
492 case NODE_TYPE_SPECIFIER
:
493 case NODE_TYPE_SPECIFIER_LIST
:
495 case NODE_TYPE_DECLARATOR
:
496 case NODE_ENUMERATOR
:
505 cds_list_for_each_entry(iter
, &node
->u
.ctf_expression
.left
, siblings
) {
506 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
510 cds_list_for_each_entry(iter
, &node
->u
.ctf_expression
.right
, siblings
) {
511 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
517 case NODE_UNARY_EXPRESSION
:
518 return ctf_visitor_unary_expression(fd
, depth
, node
);
521 switch (node
->parent
->type
) {
530 case NODE_CTF_EXPRESSION
:
531 case NODE_UNARY_EXPRESSION
:
533 case NODE_TYPEALIAS_TARGET
:
534 case NODE_TYPEALIAS_ALIAS
:
536 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
537 case NODE_TYPE_SPECIFIER
:
538 case NODE_TYPE_SPECIFIER_LIST
:
540 case NODE_TYPE_DECLARATOR
:
541 case NODE_FLOATING_POINT
:
544 case NODE_ENUMERATOR
:
551 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1,
552 node
->u
._typedef
.type_specifier_list
);
555 cds_list_for_each_entry(iter
, &node
->u
._typedef
.type_declarators
, siblings
) {
556 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
562 case NODE_TYPEALIAS_TARGET
:
566 switch (node
->parent
->type
) {
574 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1,
575 node
->u
.typealias_target
.type_specifier_list
);
579 cds_list_for_each_entry(iter
, &node
->u
.typealias_target
.type_declarators
, siblings
) {
580 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
585 if (nr_declarators
> 1) {
586 fprintf(fd
, "[error] %s: Too many declarators in typealias alias (%d, max is 1)\n", __func__
, nr_declarators
);
593 case NODE_TYPEALIAS_ALIAS
:
597 switch (node
->parent
->type
) {
605 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1,
606 node
->u
.typealias_alias
.type_specifier_list
);
610 cds_list_for_each_entry(iter
, &node
->u
.typealias_alias
.type_declarators
, siblings
) {
611 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
616 if (nr_declarators
> 1) {
617 fprintf(fd
, "[error] %s: Too many declarators in typealias alias (%d, max is 1)\n", __func__
, nr_declarators
);
625 switch (node
->parent
->type
) {
634 case NODE_CTF_EXPRESSION
:
635 case NODE_UNARY_EXPRESSION
:
637 case NODE_TYPEALIAS_TARGET
:
638 case NODE_TYPEALIAS_ALIAS
:
640 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
641 case NODE_TYPE_SPECIFIER
:
642 case NODE_TYPE_SPECIFIER_LIST
:
644 case NODE_TYPE_DECLARATOR
:
645 case NODE_FLOATING_POINT
:
648 case NODE_ENUMERATOR
:
654 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, node
->u
.typealias
.target
);
657 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, node
->u
.typealias
.alias
);
662 case NODE_TYPE_SPECIFIER_LIST
:
663 ret
= ctf_visitor_type_specifier_list(fd
, depth
, node
);
667 case NODE_TYPE_SPECIFIER
:
668 ret
= ctf_visitor_type_specifier(fd
, depth
, node
);
673 switch (node
->parent
->type
) {
674 case NODE_TYPE_DECLARATOR
:
680 case NODE_TYPE_DECLARATOR
:
681 ret
= ctf_visitor_type_declarator(fd
, depth
, node
);
686 case NODE_FLOATING_POINT
:
687 switch (node
->parent
->type
) {
688 case NODE_TYPE_SPECIFIER
:
693 case NODE_UNARY_EXPRESSION
:
696 cds_list_for_each_entry(iter
, &node
->u
.floating_point
.expressions
, siblings
) {
697 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
703 switch (node
->parent
->type
) {
704 case NODE_TYPE_SPECIFIER
:
711 cds_list_for_each_entry(iter
, &node
->u
.integer
.expressions
, siblings
) {
712 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
718 switch (node
->parent
->type
) {
719 case NODE_TYPE_SPECIFIER
:
724 case NODE_UNARY_EXPRESSION
:
728 cds_list_for_each_entry(iter
, &node
->u
.string
.expressions
, siblings
) {
729 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
734 case NODE_ENUMERATOR
:
735 switch (node
->parent
->type
) {
742 * Enumerators are only allows to contain:
743 * numeric unary expression
744 * or num. unary exp. ... num. unary exp
749 cds_list_for_each_entry(iter
, &node
->u
.enumerator
.values
,
752 case 0: if (iter
->type
!= NODE_UNARY_EXPRESSION
753 || (iter
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
754 && iter
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
)
755 || iter
->u
.unary_expression
.link
!= UNARY_LINK_UNKNOWN
) {
756 fprintf(fd
, "[error]: semantic error (first unary expression of enumerator is unexpected)\n");
760 case 1: if (iter
->type
!= NODE_UNARY_EXPRESSION
761 || (iter
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
762 && iter
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
)
763 || iter
->u
.unary_expression
.link
!= UNARY_DOTDOTDOT
) {
764 fprintf(fd
, "[error]: semantic error (second unary expression of enumerator is unexpected)\n");
774 cds_list_for_each_entry(iter
, &node
->u
.enumerator
.values
, siblings
) {
775 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
781 switch (node
->parent
->type
) {
782 case NODE_TYPE_SPECIFIER
:
787 case NODE_UNARY_EXPRESSION
:
792 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, node
->u
._enum
.container_type
);
796 cds_list_for_each_entry(iter
, &node
->u
._enum
.enumerator_list
, siblings
) {
797 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
803 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
804 switch (node
->parent
->type
) {
811 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1,
812 node
->u
.struct_or_variant_declaration
.type_specifier_list
);
815 cds_list_for_each_entry(iter
, &node
->u
.struct_or_variant_declaration
.type_declarators
, siblings
) {
816 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
822 switch (node
->parent
->type
) {
823 case NODE_TYPE_SPECIFIER
:
828 case NODE_UNARY_EXPRESSION
:
831 cds_list_for_each_entry(iter
, &node
->u
.variant
.declaration_list
, siblings
) {
832 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
839 switch (node
->parent
->type
) {
840 case NODE_TYPE_SPECIFIER
:
845 case NODE_UNARY_EXPRESSION
:
848 cds_list_for_each_entry(iter
, &node
->u
._struct
.declaration_list
, siblings
) {
849 ret
= _ctf_visitor_semantic_check(fd
, depth
+ 1, iter
);
857 fprintf(fd
, "[error] %s: unknown node type %d\n", __func__
,
864 fprintf(fd
, "[error] %s: incoherent parent type %s for node type %s\n", __func__
,
865 node_type(node
->parent
), node_type(node
));
866 return -EINVAL
; /* Incoherent structure */
869 fprintf(fd
, "[error] %s: semantic error (parent type %s for node type %s)\n", __func__
,
870 node_type(node
->parent
), node_type(node
));
871 return -EPERM
; /* Structure not allowed */
874 int ctf_visitor_semantic_check(FILE *fd
, int depth
, struct ctf_node
*node
)
879 * First make sure we create the parent links for all children. Let's
880 * take the safe route and recreate them at each validation, just in
881 * case the structure has changed.
883 printf_verbose("CTF visitor: parent links creation... ");
884 ret
= ctf_visitor_parent_links(fd
, depth
, node
);
887 printf_verbose("done.\n");
888 printf_verbose("CTF visitor: semantic check... ");
889 ret
= _ctf_visitor_semantic_check(fd
, depth
, node
);
892 printf_verbose("done.\n");