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