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