be5ed8f440e1b51c5672962f0684bc46ebf875d1
[babeltrace.git] / formats / ctf / metadata / ctf-visitor-semantic-validator.c
1 /*
2 * ctf-visitor-semantic-validator.c
3 *
4 * Common Trace Format Metadata Semantic Validator.
5 *
6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <assert.h>
24 #include <glib.h>
25 #include <inttypes.h>
26 #include <errno.h>
27 #include <babeltrace/babeltrace.h>
28 #include <babeltrace/list.h>
29 #include "ctf-scanner.h"
30 #include "ctf-parser.h"
31 #include "ctf-ast.h"
32
33 #define _cds_list_first_entry(ptr, type, member) \
34 cds_list_entry((ptr)->next, type, member)
35
36 #define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
37
38 static
39 int _ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node);
40
41 static
42 int ctf_visitor_unary_expression(FILE *fd, int depth, struct ctf_node *node)
43 {
44 struct ctf_node *iter;
45 int is_ctf_exp = 0, is_ctf_exp_left = 0;
46
47 switch (node->parent->type) {
48 case NODE_CTF_EXPRESSION:
49 is_ctf_exp = 1;
50 cds_list_for_each_entry(iter, &node->parent->u.ctf_expression.left,
51 siblings) {
52 if (iter == node) {
53 is_ctf_exp_left = 1;
54 /*
55 * We are a left child of a ctf expression.
56 * We are only allowed to be a string.
57 */
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");
60
61 goto errperm;
62 }
63 break;
64 }
65 }
66 /* Right child of a ctf expression can be any type of unary exp. */
67 break; /* OK */
68 case NODE_TYPE_DECLARATOR:
69 /*
70 * We are the length of a type declarator.
71 */
72 switch (node->u.unary_expression.type) {
73 case UNARY_UNSIGNED_CONSTANT:
74 break;
75 default:
76 fprintf(fd, "[error]: semantic error (children of type declarator and enum can only be unsigned numeric constants)\n");
77 goto errperm;
78 }
79 break; /* OK */
80
81 case NODE_STRUCT:
82 /*
83 * We are the size of a struct align attribute.
84 */
85 switch (node->u.unary_expression.type) {
86 case UNARY_UNSIGNED_CONSTANT:
87 break;
88 default:
89 fprintf(fd, "[error]: semantic error (structure alignment attribute can only be unsigned numeric constants)\n");
90 goto errperm;
91 }
92 break;
93
94 case NODE_ENUMERATOR:
95 /* The enumerator's parent has validated its validity already. */
96 break; /* OK */
97
98 case NODE_UNARY_EXPRESSION:
99 /*
100 * We disallow nested unary expressions and "sbrac" unary
101 * expressions.
102 */
103 fprintf(fd, "[error]: semantic error (nested unary expressions not allowed ( () and [] ))\n");
104 goto errperm;
105
106 case NODE_ROOT:
107 case NODE_EVENT:
108 case NODE_STREAM:
109 case NODE_TRACE:
110 case NODE_TYPEDEF:
111 case NODE_TYPEALIAS_TARGET:
112 case NODE_TYPEALIAS_ALIAS:
113 case NODE_TYPEALIAS:
114 case NODE_TYPE_SPECIFIER:
115 case NODE_POINTER:
116 case NODE_FLOATING_POINT:
117 case NODE_INTEGER:
118 case NODE_STRING:
119 case NODE_ENUM:
120 case NODE_STRUCT_OR_VARIANT_DECLARATION:
121 case NODE_VARIANT:
122 default:
123 goto errinval;
124 }
125
126 switch (node->u.unary_expression.link) {
127 case UNARY_LINK_UNKNOWN:
128 /* We don't allow empty link except on the first node of the list */
129 if (is_ctf_exp && _cds_list_first_entry(is_ctf_exp_left ?
130 &node->parent->u.ctf_expression.left :
131 &node->parent->u.ctf_expression.right,
132 struct ctf_node,
133 siblings) != node) {
134 fprintf(fd, "[error]: semantic error (empty link not allowed except on first node of unary expression (need to separate nodes with \".\" or \"->\")\n");
135 goto errperm;
136 }
137 break; /* OK */
138 case UNARY_DOTLINK:
139 case UNARY_ARROWLINK:
140 /* We only allow -> and . links between children of ctf_expression. */
141 if (node->parent->type != NODE_CTF_EXPRESSION) {
142 fprintf(fd, "[error]: semantic error (links \".\" and \"->\" are only allowed as children of ctf expression)\n");
143 goto errperm;
144 }
145 /*
146 * Only strings can be separated linked by . or ->.
147 * This includes "", '' and non-quoted identifiers.
148 */
149 if (node->u.unary_expression.type != UNARY_STRING) {
150 fprintf(fd, "[error]: semantic error (links \".\" and \"->\" are only allowed to separate strings and identifiers)\n");
151 goto errperm;
152 }
153 /* We don't allow link on the first node of the list */
154 if (is_ctf_exp && _cds_list_first_entry(is_ctf_exp_left ?
155 &node->parent->u.ctf_expression.left :
156 &node->parent->u.ctf_expression.right,
157 struct ctf_node,
158 siblings) == node) {
159 fprintf(fd, "[error]: semantic error (links \".\" and \"->\" are not allowed before first node of the unary expression list)\n");
160 goto errperm;
161 }
162 break;
163 case UNARY_DOTDOTDOT:
164 /* We only allow ... link between children of enumerator. */
165 if (node->parent->type != NODE_ENUMERATOR) {
166 fprintf(fd, "[error]: semantic error (link \"...\" is only allowed within enumerator)\n");
167 goto errperm;
168 }
169 /* We don't allow link on the first node of the list */
170 if (_cds_list_first_entry(&node->parent->u.enumerator.values,
171 struct ctf_node,
172 siblings) == node) {
173 fprintf(fd, "[error]: semantic error (link \"...\" is not allowed on the first node of the unary expression list)\n");
174 goto errperm;
175 }
176 break;
177 default:
178 fprintf(fd, "[error] %s: unknown expression link type %d\n", __func__,
179 (int) node->u.unary_expression.link);
180 return -EINVAL;
181 }
182 return 0;
183
184 errinval:
185 fprintf(fd, "[error] %s: incoherent parent type %s for node type %s\n", __func__,
186 node_type(node->parent), node_type(node));
187 return -EINVAL; /* Incoherent structure */
188
189 errperm:
190 fprintf(fd, "[error] %s: semantic error (parent type %s for node type %s)\n", __func__,
191 node_type(node->parent), node_type(node));
192 return -EPERM; /* Structure not allowed */
193 }
194
195 static
196 int ctf_visitor_type_specifier_list(FILE *fd, int depth, struct ctf_node *node)
197 {
198 switch (node->parent->type) {
199 case NODE_CTF_EXPRESSION:
200 case NODE_TYPE_DECLARATOR:
201 case NODE_TYPEDEF:
202 case NODE_TYPEALIAS_TARGET:
203 case NODE_TYPEALIAS_ALIAS:
204 case NODE_ENUM:
205 case NODE_STRUCT_OR_VARIANT_DECLARATION:
206 case NODE_ROOT:
207 break; /* OK */
208
209 case NODE_EVENT:
210 case NODE_STREAM:
211 case NODE_TRACE:
212 case NODE_UNARY_EXPRESSION:
213 case NODE_TYPEALIAS:
214 case NODE_TYPE_SPECIFIER:
215 case NODE_TYPE_SPECIFIER_LIST:
216 case NODE_POINTER:
217 case NODE_FLOATING_POINT:
218 case NODE_INTEGER:
219 case NODE_STRING:
220 case NODE_ENUMERATOR:
221 case NODE_VARIANT:
222 case NODE_STRUCT:
223 default:
224 goto errinval;
225 }
226 return 0;
227 errinval:
228 fprintf(fd, "[error] %s: incoherent parent type %s for node type %s\n", __func__,
229 node_type(node->parent), node_type(node));
230 return -EINVAL; /* Incoherent structure */
231 }
232
233 static
234 int ctf_visitor_type_specifier(FILE *fd, int depth, struct ctf_node *node)
235 {
236 switch (node->parent->type) {
237 case NODE_TYPE_SPECIFIER_LIST:
238 break; /* OK */
239
240 case NODE_CTF_EXPRESSION:
241 case NODE_TYPE_DECLARATOR:
242 case NODE_TYPEDEF:
243 case NODE_TYPEALIAS_TARGET:
244 case NODE_TYPEALIAS_ALIAS:
245 case NODE_ENUM:
246 case NODE_STRUCT_OR_VARIANT_DECLARATION:
247 case NODE_ROOT:
248 case NODE_EVENT:
249 case NODE_STREAM:
250 case NODE_TRACE:
251 case NODE_UNARY_EXPRESSION:
252 case NODE_TYPEALIAS:
253 case NODE_TYPE_SPECIFIER:
254 case NODE_POINTER:
255 case NODE_FLOATING_POINT:
256 case NODE_INTEGER:
257 case NODE_STRING:
258 case NODE_ENUMERATOR:
259 case NODE_VARIANT:
260 case NODE_STRUCT:
261 default:
262 goto errinval;
263 }
264 return 0;
265 errinval:
266 fprintf(fd, "[error] %s: incoherent parent type %s for node type %s\n", __func__,
267 node_type(node->parent), node_type(node));
268 return -EINVAL; /* Incoherent structure */
269 }
270
271 static
272 int ctf_visitor_type_declarator(FILE *fd, int depth, struct ctf_node *node)
273 {
274 int ret = 0;
275 struct ctf_node *iter;
276
277 depth++;
278
279 switch (node->parent->type) {
280 case NODE_TYPE_DECLARATOR:
281 /*
282 * A nested type declarator is not allowed to contain pointers.
283 */
284 if (!cds_list_empty(&node->u.type_declarator.pointers))
285 goto errperm;
286 break; /* OK */
287 case NODE_TYPEALIAS_TARGET:
288 break; /* OK */
289 case NODE_TYPEALIAS_ALIAS:
290 /*
291 * Only accept alias name containing:
292 * - identifier
293 * - identifier * (any number of pointers)
294 * NOT accepting alias names containing [] (would otherwise
295 * cause semantic clash for later declarations of
296 * arrays/sequences of elements, where elements could be
297 * arrays/sequences themselves (if allowed in typealias).
298 * NOT accepting alias with identifier. The declarator should
299 * be either empty or contain pointer(s).
300 */
301 if (node->u.type_declarator.type == TYPEDEC_NESTED)
302 goto errperm;
303 switch (node->u.type_declarator.type) {
304 case TYPESPEC_FLOATING_POINT:
305 case TYPESPEC_INTEGER:
306 case TYPESPEC_STRING:
307 case TYPESPEC_STRUCT:
308 case TYPESPEC_VARIANT:
309 case TYPESPEC_ENUM:
310 if (cds_list_empty(&node->u.type_declarator.pointers))
311 goto errperm;
312 break;
313 default:
314 break;
315 }
316 if (node->u.type_declarator.type == TYPEDEC_ID &&
317 node->u.type_declarator.u.id != NULL)
318 goto errperm;
319 break; /* OK */
320 case NODE_TYPEDEF:
321 case NODE_STRUCT_OR_VARIANT_DECLARATION:
322 break; /* OK */
323
324 case NODE_ROOT:
325 case NODE_EVENT:
326 case NODE_STREAM:
327 case NODE_TRACE:
328 case NODE_CTF_EXPRESSION:
329 case NODE_UNARY_EXPRESSION:
330 case NODE_TYPEALIAS:
331 case NODE_TYPE_SPECIFIER:
332 case NODE_POINTER:
333 case NODE_FLOATING_POINT:
334 case NODE_INTEGER:
335 case NODE_STRING:
336 case NODE_ENUMERATOR:
337 case NODE_ENUM:
338 case NODE_VARIANT:
339 case NODE_STRUCT:
340 default:
341 goto errinval;
342 }
343
344 if (!cds_list_empty(&node->u.type_declarator.pointers)) {
345 cds_list_for_each_entry(iter, &node->u.type_declarator.pointers,
346 siblings) {
347 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
348 if (ret)
349 return ret;
350 }
351 }
352
353 switch (node->u.type_declarator.type) {
354 case TYPEDEC_ID:
355 break;
356 case TYPEDEC_NESTED:
357 {
358 if (node->u.type_declarator.u.nested.type_declarator) {
359 ret = _ctf_visitor_semantic_check(fd, depth + 1,
360 node->u.type_declarator.u.nested.type_declarator);
361 if (ret)
362 return ret;
363 }
364 ret = _ctf_visitor_semantic_check(fd, depth + 1,
365 node->u.type_declarator.u.nested.length);
366 if (ret)
367 return ret;
368 if (node->u.type_declarator.bitfield_len) {
369 ret = _ctf_visitor_semantic_check(fd, depth + 1,
370 node->u.type_declarator.bitfield_len);
371 if (ret)
372 return ret;
373 }
374 break;
375 }
376 case TYPEDEC_UNKNOWN:
377 default:
378 fprintf(fd, "[error] %s: unknown type declarator %d\n", __func__,
379 (int) node->u.type_declarator.type);
380 return -EINVAL;
381 }
382 depth--;
383 return 0;
384
385 errinval:
386 fprintf(fd, "[error] %s: incoherent parent type %s for node type %s\n", __func__,
387 node_type(node->parent), node_type(node));
388 return -EINVAL; /* Incoherent structure */
389
390 errperm:
391 fprintf(fd, "[error] %s: semantic error (parent type %s for node type %s)\n", __func__,
392 node_type(node->parent), node_type(node));
393 return -EPERM; /* Structure not allowed */
394 }
395
396 static
397 int _ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node)
398 {
399 int ret = 0;
400 struct ctf_node *iter;
401
402 switch (node->type) {
403 case NODE_ROOT:
404 cds_list_for_each_entry(iter, &node->u.root.declaration_list, siblings) {
405 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
406 if (ret)
407 return ret;
408 }
409 cds_list_for_each_entry(iter, &node->u.root.trace, siblings) {
410 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
411 if (ret)
412 return ret;
413 }
414 cds_list_for_each_entry(iter, &node->u.root.stream, siblings) {
415 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
416 if (ret)
417 return ret;
418 }
419 cds_list_for_each_entry(iter, &node->u.root.event, siblings) {
420 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
421 if (ret)
422 return ret;
423 }
424 break;
425
426 case NODE_EVENT:
427 switch (node->parent->type) {
428 case NODE_ROOT:
429 break; /* OK */
430 default:
431 goto errinval;
432 }
433
434 cds_list_for_each_entry(iter, &node->u.event.declaration_list, siblings) {
435 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
436 if (ret)
437 return ret;
438 }
439 break;
440 case NODE_STREAM:
441 switch (node->parent->type) {
442 case NODE_ROOT:
443 break; /* OK */
444 default:
445 goto errinval;
446 }
447
448 cds_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) {
449 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
450 if (ret)
451 return ret;
452 }
453 break;
454 case NODE_TRACE:
455 switch (node->parent->type) {
456 case NODE_ROOT:
457 break; /* OK */
458 default:
459 goto errinval;
460 }
461
462 cds_list_for_each_entry(iter, &node->u.trace.declaration_list, siblings) {
463 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
464 if (ret)
465 return ret;
466 }
467 break;
468
469 case NODE_CTF_EXPRESSION:
470 switch (node->parent->type) {
471 case NODE_ROOT:
472 case NODE_EVENT:
473 case NODE_STREAM:
474 case NODE_TRACE:
475 case NODE_FLOATING_POINT:
476 case NODE_INTEGER:
477 case NODE_STRING:
478 break; /* OK */
479
480 case NODE_CTF_EXPRESSION:
481 case NODE_UNARY_EXPRESSION:
482 case NODE_TYPEDEF:
483 case NODE_TYPEALIAS_TARGET:
484 case NODE_TYPEALIAS_ALIAS:
485 case NODE_STRUCT_OR_VARIANT_DECLARATION:
486 case NODE_TYPEALIAS:
487 case NODE_TYPE_SPECIFIER:
488 case NODE_TYPE_SPECIFIER_LIST:
489 case NODE_POINTER:
490 case NODE_TYPE_DECLARATOR:
491 case NODE_ENUMERATOR:
492 case NODE_ENUM:
493 case NODE_VARIANT:
494 case NODE_STRUCT:
495 default:
496 goto errinval;
497 }
498
499 depth++;
500 cds_list_for_each_entry(iter, &node->u.ctf_expression.left, siblings) {
501 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
502 if (ret)
503 return ret;
504 }
505 cds_list_for_each_entry(iter, &node->u.ctf_expression.right, siblings) {
506 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
507 if (ret)
508 return ret;
509 }
510 depth--;
511 break;
512 case NODE_UNARY_EXPRESSION:
513 return ctf_visitor_unary_expression(fd, depth, node);
514
515 case NODE_TYPEDEF:
516 switch (node->parent->type) {
517 case NODE_ROOT:
518 case NODE_EVENT:
519 case NODE_STREAM:
520 case NODE_TRACE:
521 case NODE_VARIANT:
522 case NODE_STRUCT:
523 break; /* OK */
524
525 case NODE_CTF_EXPRESSION:
526 case NODE_UNARY_EXPRESSION:
527 case NODE_TYPEDEF:
528 case NODE_TYPEALIAS_TARGET:
529 case NODE_TYPEALIAS_ALIAS:
530 case NODE_TYPEALIAS:
531 case NODE_STRUCT_OR_VARIANT_DECLARATION:
532 case NODE_TYPE_SPECIFIER:
533 case NODE_TYPE_SPECIFIER_LIST:
534 case NODE_POINTER:
535 case NODE_TYPE_DECLARATOR:
536 case NODE_FLOATING_POINT:
537 case NODE_INTEGER:
538 case NODE_STRING:
539 case NODE_ENUMERATOR:
540 case NODE_ENUM:
541 default:
542 goto errinval;
543 }
544
545 depth++;
546 ret = _ctf_visitor_semantic_check(fd, depth + 1,
547 node->u._typedef.type_specifier_list);
548 if (ret)
549 return ret;
550 cds_list_for_each_entry(iter, &node->u._typedef.type_declarators, siblings) {
551 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
552 if (ret)
553 return ret;
554 }
555 depth--;
556 break;
557 case NODE_TYPEALIAS_TARGET:
558 {
559 int nr_declarators;
560
561 switch (node->parent->type) {
562 case NODE_TYPEALIAS:
563 break; /* OK */
564 default:
565 goto errinval;
566 }
567
568 depth++;
569 ret = _ctf_visitor_semantic_check(fd, depth + 1,
570 node->u.typealias_target.type_specifier_list);
571 if (ret)
572 return ret;
573 nr_declarators = 0;
574 cds_list_for_each_entry(iter, &node->u.typealias_target.type_declarators, siblings) {
575 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
576 if (ret)
577 return ret;
578 nr_declarators++;
579 }
580 if (nr_declarators > 1) {
581 fprintf(fd, "[error] %s: Too many declarators in typealias alias (%d, max is 1)\n", __func__, nr_declarators);
582
583 return -EINVAL;
584 }
585 depth--;
586 break;
587 }
588 case NODE_TYPEALIAS_ALIAS:
589 {
590 int nr_declarators;
591
592 switch (node->parent->type) {
593 case NODE_TYPEALIAS:
594 break; /* OK */
595 default:
596 goto errinval;
597 }
598
599 depth++;
600 ret = _ctf_visitor_semantic_check(fd, depth + 1,
601 node->u.typealias_alias.type_specifier_list);
602 if (ret)
603 return ret;
604 nr_declarators = 0;
605 cds_list_for_each_entry(iter, &node->u.typealias_alias.type_declarators, siblings) {
606 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
607 if (ret)
608 return ret;
609 nr_declarators++;
610 }
611 if (nr_declarators > 1) {
612 fprintf(fd, "[error] %s: Too many declarators in typealias alias (%d, max is 1)\n", __func__, nr_declarators);
613
614 return -EINVAL;
615 }
616 depth--;
617 break;
618 }
619 case NODE_TYPEALIAS:
620 switch (node->parent->type) {
621 case NODE_ROOT:
622 case NODE_EVENT:
623 case NODE_STREAM:
624 case NODE_TRACE:
625 case NODE_VARIANT:
626 case NODE_STRUCT:
627 break; /* OK */
628
629 case NODE_CTF_EXPRESSION:
630 case NODE_UNARY_EXPRESSION:
631 case NODE_TYPEDEF:
632 case NODE_TYPEALIAS_TARGET:
633 case NODE_TYPEALIAS_ALIAS:
634 case NODE_TYPEALIAS:
635 case NODE_STRUCT_OR_VARIANT_DECLARATION:
636 case NODE_TYPE_SPECIFIER:
637 case NODE_TYPE_SPECIFIER_LIST:
638 case NODE_POINTER:
639 case NODE_TYPE_DECLARATOR:
640 case NODE_FLOATING_POINT:
641 case NODE_INTEGER:
642 case NODE_STRING:
643 case NODE_ENUMERATOR:
644 case NODE_ENUM:
645 default:
646 goto errinval;
647 }
648
649 ret = _ctf_visitor_semantic_check(fd, depth + 1, node->u.typealias.target);
650 if (ret)
651 return ret;
652 ret = _ctf_visitor_semantic_check(fd, depth + 1, node->u.typealias.alias);
653 if (ret)
654 return ret;
655 break;
656
657 case NODE_TYPE_SPECIFIER_LIST:
658 ret = ctf_visitor_type_specifier_list(fd, depth, node);
659 if (ret)
660 return ret;
661 break;
662 case NODE_TYPE_SPECIFIER:
663 ret = ctf_visitor_type_specifier(fd, depth, node);
664 if (ret)
665 return ret;
666 break;
667 case NODE_POINTER:
668 switch (node->parent->type) {
669 case NODE_TYPE_DECLARATOR:
670 break; /* OK */
671 default:
672 goto errinval;
673 }
674 break;
675 case NODE_TYPE_DECLARATOR:
676 ret = ctf_visitor_type_declarator(fd, depth, node);
677 if (ret)
678 return ret;
679 break;
680
681 case NODE_FLOATING_POINT:
682 switch (node->parent->type) {
683 case NODE_TYPE_SPECIFIER:
684 break; /* OK */
685 default:
686 goto errinval;
687
688 case NODE_UNARY_EXPRESSION:
689 goto errperm;
690 }
691 cds_list_for_each_entry(iter, &node->u.floating_point.expressions, siblings) {
692 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
693 if (ret)
694 return ret;
695 }
696 break;
697 case NODE_INTEGER:
698 switch (node->parent->type) {
699 case NODE_TYPE_SPECIFIER:
700 break; /* OK */
701 default:
702 goto errinval;
703
704 }
705
706 cds_list_for_each_entry(iter, &node->u.integer.expressions, siblings) {
707 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
708 if (ret)
709 return ret;
710 }
711 break;
712 case NODE_STRING:
713 switch (node->parent->type) {
714 case NODE_TYPE_SPECIFIER:
715 break; /* OK */
716 default:
717 goto errinval;
718
719 case NODE_UNARY_EXPRESSION:
720 goto errperm;
721 }
722
723 cds_list_for_each_entry(iter, &node->u.string.expressions, siblings) {
724 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
725 if (ret)
726 return ret;
727 }
728 break;
729 case NODE_ENUMERATOR:
730 switch (node->parent->type) {
731 case NODE_ENUM:
732 break;
733 default:
734 goto errinval;
735 }
736 /*
737 * Enumerators are only allows to contain:
738 * numeric unary expression
739 * or num. unary exp. ... num. unary exp
740 */
741 {
742 int count = 0;
743
744 cds_list_for_each_entry(iter, &node->u.enumerator.values,
745 siblings) {
746 switch (count++) {
747 case 0: if (iter->type != NODE_UNARY_EXPRESSION
748 || (iter->u.unary_expression.type != UNARY_SIGNED_CONSTANT
749 && iter->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT)
750 || iter->u.unary_expression.link != UNARY_LINK_UNKNOWN) {
751 fprintf(fd, "[error]: semantic error (first unary expression of enumerator is unexpected)\n");
752 goto errperm;
753 }
754 break;
755 case 1: if (iter->type != NODE_UNARY_EXPRESSION
756 || (iter->u.unary_expression.type != UNARY_SIGNED_CONSTANT
757 && iter->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT)
758 || iter->u.unary_expression.link != UNARY_DOTDOTDOT) {
759 fprintf(fd, "[error]: semantic error (second unary expression of enumerator is unexpected)\n");
760 goto errperm;
761 }
762 break;
763 default:
764 goto errperm;
765 }
766 }
767 }
768
769 cds_list_for_each_entry(iter, &node->u.enumerator.values, siblings) {
770 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
771 if (ret)
772 return ret;
773 }
774 break;
775 case NODE_ENUM:
776 switch (node->parent->type) {
777 case NODE_TYPE_SPECIFIER:
778 break; /* OK */
779 default:
780 goto errinval;
781
782 case NODE_UNARY_EXPRESSION:
783 goto errperm;
784 }
785
786 depth++;
787 ret = _ctf_visitor_semantic_check(fd, depth + 1, node->u._enum.container_type);
788 if (ret)
789 return ret;
790
791 cds_list_for_each_entry(iter, &node->u._enum.enumerator_list, siblings) {
792 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
793 if (ret)
794 return ret;
795 }
796 depth--;
797 break;
798 case NODE_STRUCT_OR_VARIANT_DECLARATION:
799 switch (node->parent->type) {
800 case NODE_STRUCT:
801 case NODE_VARIANT:
802 break;
803 default:
804 goto errinval;
805 }
806 ret = _ctf_visitor_semantic_check(fd, depth + 1,
807 node->u.struct_or_variant_declaration.type_specifier_list);
808 if (ret)
809 return ret;
810 cds_list_for_each_entry(iter, &node->u.struct_or_variant_declaration.type_declarators, siblings) {
811 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
812 if (ret)
813 return ret;
814 }
815 break;
816 case NODE_VARIANT:
817 switch (node->parent->type) {
818 case NODE_TYPE_SPECIFIER:
819 break; /* OK */
820 default:
821 goto errinval;
822
823 case NODE_UNARY_EXPRESSION:
824 goto errperm;
825 }
826 cds_list_for_each_entry(iter, &node->u.variant.declaration_list, siblings) {
827 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
828 if (ret)
829 return ret;
830 }
831 break;
832
833 case NODE_STRUCT:
834 switch (node->parent->type) {
835 case NODE_TYPE_SPECIFIER:
836 break; /* OK */
837 default:
838 goto errinval;
839
840 case NODE_UNARY_EXPRESSION:
841 goto errperm;
842 }
843 cds_list_for_each_entry(iter, &node->u._struct.declaration_list, siblings) {
844 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
845 if (ret)
846 return ret;
847 }
848 break;
849
850 case NODE_UNKNOWN:
851 default:
852 fprintf(fd, "[error] %s: unknown node type %d\n", __func__,
853 (int) node->type);
854 return -EINVAL;
855 }
856 return ret;
857
858 errinval:
859 fprintf(fd, "[error] %s: incoherent parent type %s for node type %s\n", __func__,
860 node_type(node->parent), node_type(node));
861 return -EINVAL; /* Incoherent structure */
862
863 errperm:
864 fprintf(fd, "[error] %s: semantic error (parent type %s for node type %s)\n", __func__,
865 node_type(node->parent), node_type(node));
866 return -EPERM; /* Structure not allowed */
867 }
868
869 int ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node)
870 {
871 int ret = 0;
872
873 /*
874 * First make sure we create the parent links for all children. Let's
875 * take the safe route and recreate them at each validation, just in
876 * case the structure has changed.
877 */
878 printf_verbose("CTF visitor: parent links creation... ");
879 ret = ctf_visitor_parent_links(fd, depth, node);
880 if (ret)
881 return ret;
882 printf_verbose("done.\n");
883 printf_verbose("CTF visitor: semantic check... ");
884 ret = _ctf_visitor_semantic_check(fd, depth, node);
885 if (ret)
886 return ret;
887 printf_verbose("done.\n");
888 return ret;
889 }
This page took 0.04613 seconds and 3 git commands to generate.