| 1 | /* |
| 2 | * ctf-visitor-parent-links.c |
| 3 | * |
| 4 | * Common Trace Format Metadata Parent Link Creator. |
| 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 | * 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 | #define BT_COMP_LOG_SELF_COMP (log_cfg->self_comp) |
| 28 | #define BT_LOG_OUTPUT_LEVEL (log_cfg->log_level) |
| 29 | #define BT_LOG_TAG "PLUGIN/CTF/META/PARENT-LINKS-VISITOR" |
| 30 | #include "logging/comp-logging.h" |
| 31 | |
| 32 | #include <stdio.h> |
| 33 | #include <unistd.h> |
| 34 | #include <string.h> |
| 35 | #include <stdlib.h> |
| 36 | #include "common/assert.h" |
| 37 | #include <glib.h> |
| 38 | #include <inttypes.h> |
| 39 | #include <errno.h> |
| 40 | #include "common/macros.h" |
| 41 | #include "common/list.h" |
| 42 | #include "scanner.h" |
| 43 | #include "ast.h" |
| 44 | #include "logging.h" |
| 45 | |
| 46 | static |
| 47 | int ctf_visitor_unary_expression(int depth, struct ctf_node *node, |
| 48 | struct meta_log_config *log_cfg) |
| 49 | { |
| 50 | int ret = 0; |
| 51 | |
| 52 | switch (node->u.unary_expression.link) { |
| 53 | case UNARY_LINK_UNKNOWN: |
| 54 | case UNARY_DOTLINK: |
| 55 | case UNARY_ARROWLINK: |
| 56 | case UNARY_DOTDOTDOT: |
| 57 | break; |
| 58 | default: |
| 59 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 60 | "Unknown expression link type: type=%d\n", |
| 61 | node->u.unary_expression.link); |
| 62 | return -EINVAL; |
| 63 | } |
| 64 | |
| 65 | switch (node->u.unary_expression.type) { |
| 66 | case UNARY_STRING: |
| 67 | case UNARY_SIGNED_CONSTANT: |
| 68 | case UNARY_UNSIGNED_CONSTANT: |
| 69 | break; |
| 70 | case UNARY_SBRAC: |
| 71 | node->u.unary_expression.u.sbrac_exp->parent = node; |
| 72 | ret = ctf_visitor_unary_expression(depth + 1, |
| 73 | node->u.unary_expression.u.sbrac_exp, |
| 74 | log_cfg); |
| 75 | if (ret) |
| 76 | return ret; |
| 77 | break; |
| 78 | |
| 79 | case UNARY_UNKNOWN: |
| 80 | default: |
| 81 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 82 | "Unknown expression link type: type=%d\n", |
| 83 | node->u.unary_expression.link); |
| 84 | return -EINVAL; |
| 85 | } |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static |
| 90 | int ctf_visitor_type_specifier(int depth, struct ctf_node *node, |
| 91 | struct meta_log_config *log_cfg) |
| 92 | { |
| 93 | int ret; |
| 94 | |
| 95 | switch (node->u.field_class_specifier.type) { |
| 96 | case TYPESPEC_VOID: |
| 97 | case TYPESPEC_CHAR: |
| 98 | case TYPESPEC_SHORT: |
| 99 | case TYPESPEC_INT: |
| 100 | case TYPESPEC_LONG: |
| 101 | case TYPESPEC_FLOAT: |
| 102 | case TYPESPEC_DOUBLE: |
| 103 | case TYPESPEC_SIGNED: |
| 104 | case TYPESPEC_UNSIGNED: |
| 105 | case TYPESPEC_BOOL: |
| 106 | case TYPESPEC_COMPLEX: |
| 107 | case TYPESPEC_IMAGINARY: |
| 108 | case TYPESPEC_CONST: |
| 109 | case TYPESPEC_ID_TYPE: |
| 110 | break; |
| 111 | case TYPESPEC_FLOATING_POINT: |
| 112 | case TYPESPEC_INTEGER: |
| 113 | case TYPESPEC_STRING: |
| 114 | case TYPESPEC_STRUCT: |
| 115 | case TYPESPEC_VARIANT: |
| 116 | case TYPESPEC_ENUM: |
| 117 | node->u.field_class_specifier.node->parent = node; |
| 118 | ret = ctf_visitor_parent_links(depth + 1, |
| 119 | node->u.field_class_specifier.node, |
| 120 | log_cfg); |
| 121 | if (ret) |
| 122 | return ret; |
| 123 | break; |
| 124 | |
| 125 | case TYPESPEC_UNKNOWN: |
| 126 | default: |
| 127 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 128 | "Unknown type specifier: type=%d\n", |
| 129 | node->u.field_class_specifier.type); |
| 130 | return -EINVAL; |
| 131 | } |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static |
| 136 | int ctf_visitor_field_class_declarator(int depth, struct ctf_node *node, |
| 137 | struct meta_log_config *log_cfg) |
| 138 | { |
| 139 | int ret = 0; |
| 140 | struct ctf_node *iter; |
| 141 | |
| 142 | depth++; |
| 143 | |
| 144 | bt_list_for_each_entry(iter, &node->u.field_class_declarator.pointers, |
| 145 | siblings) { |
| 146 | iter->parent = node; |
| 147 | ret = ctf_visitor_parent_links(depth + 1, iter, log_cfg); |
| 148 | if (ret) |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | switch (node->u.field_class_declarator.type) { |
| 153 | case TYPEDEC_ID: |
| 154 | break; |
| 155 | case TYPEDEC_NESTED: |
| 156 | if (node->u.field_class_declarator.u.nested.field_class_declarator) { |
| 157 | node->u.field_class_declarator.u.nested.field_class_declarator->parent = node; |
| 158 | ret = ctf_visitor_parent_links(depth + 1, |
| 159 | node->u.field_class_declarator.u.nested.field_class_declarator, |
| 160 | log_cfg); |
| 161 | if (ret) |
| 162 | return ret; |
| 163 | } |
| 164 | if (!node->u.field_class_declarator.u.nested.abstract_array) { |
| 165 | bt_list_for_each_entry(iter, &node->u.field_class_declarator.u.nested.length, |
| 166 | siblings) { |
| 167 | iter->parent = node; |
| 168 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 169 | log_cfg); |
| 170 | if (ret) |
| 171 | return ret; |
| 172 | } |
| 173 | } |
| 174 | if (node->u.field_class_declarator.bitfield_len) { |
| 175 | node->u.field_class_declarator.bitfield_len = node; |
| 176 | ret = ctf_visitor_parent_links(depth + 1, |
| 177 | node->u.field_class_declarator.bitfield_len, |
| 178 | log_cfg); |
| 179 | if (ret) |
| 180 | return ret; |
| 181 | } |
| 182 | break; |
| 183 | case TYPEDEC_UNKNOWN: |
| 184 | default: |
| 185 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 186 | "Unknown type declarator: type=%d\n", |
| 187 | node->u.field_class_declarator.type); |
| 188 | return -EINVAL; |
| 189 | } |
| 190 | depth--; |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | int ctf_visitor_parent_links(int depth, struct ctf_node *node, |
| 195 | struct meta_log_config *log_cfg) |
| 196 | { |
| 197 | int ret = 0; |
| 198 | struct ctf_node *iter; |
| 199 | |
| 200 | if (node->visited) |
| 201 | return 0; |
| 202 | |
| 203 | switch (node->type) { |
| 204 | case NODE_ROOT: |
| 205 | bt_list_for_each_entry(iter, &node->u.root.declaration_list, siblings) { |
| 206 | iter->parent = node; |
| 207 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 208 | log_cfg); |
| 209 | if (ret) |
| 210 | return ret; |
| 211 | } |
| 212 | bt_list_for_each_entry(iter, &node->u.root.trace, siblings) { |
| 213 | iter->parent = node; |
| 214 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 215 | log_cfg); |
| 216 | if (ret) |
| 217 | return ret; |
| 218 | } |
| 219 | bt_list_for_each_entry(iter, &node->u.root.stream, siblings) { |
| 220 | iter->parent = node; |
| 221 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 222 | log_cfg); |
| 223 | if (ret) |
| 224 | return ret; |
| 225 | } |
| 226 | bt_list_for_each_entry(iter, &node->u.root.event, siblings) { |
| 227 | iter->parent = node; |
| 228 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 229 | log_cfg); |
| 230 | if (ret) |
| 231 | return ret; |
| 232 | } |
| 233 | bt_list_for_each_entry(iter, &node->u.root.clock, siblings) { |
| 234 | iter->parent = node; |
| 235 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 236 | log_cfg); |
| 237 | if (ret) |
| 238 | return ret; |
| 239 | } |
| 240 | bt_list_for_each_entry(iter, &node->u.root.callsite, siblings) { |
| 241 | iter->parent = node; |
| 242 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 243 | log_cfg); |
| 244 | if (ret) |
| 245 | return ret; |
| 246 | } |
| 247 | break; |
| 248 | |
| 249 | case NODE_EVENT: |
| 250 | bt_list_for_each_entry(iter, &node->u.event.declaration_list, siblings) { |
| 251 | iter->parent = node; |
| 252 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 253 | log_cfg); |
| 254 | if (ret) |
| 255 | return ret; |
| 256 | } |
| 257 | break; |
| 258 | case NODE_STREAM: |
| 259 | bt_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) { |
| 260 | iter->parent = node; |
| 261 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 262 | log_cfg); |
| 263 | if (ret) |
| 264 | return ret; |
| 265 | } |
| 266 | break; |
| 267 | case NODE_ENV: |
| 268 | bt_list_for_each_entry(iter, &node->u.env.declaration_list, siblings) { |
| 269 | iter->parent = node; |
| 270 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 271 | log_cfg); |
| 272 | if (ret) |
| 273 | return ret; |
| 274 | } |
| 275 | break; |
| 276 | case NODE_TRACE: |
| 277 | bt_list_for_each_entry(iter, &node->u.trace.declaration_list, siblings) { |
| 278 | iter->parent = node; |
| 279 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 280 | log_cfg); |
| 281 | if (ret) |
| 282 | return ret; |
| 283 | } |
| 284 | break; |
| 285 | case NODE_CLOCK: |
| 286 | bt_list_for_each_entry(iter, &node->u.clock.declaration_list, siblings) { |
| 287 | iter->parent = node; |
| 288 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 289 | log_cfg); |
| 290 | if (ret) |
| 291 | return ret; |
| 292 | } |
| 293 | break; |
| 294 | case NODE_CALLSITE: |
| 295 | bt_list_for_each_entry(iter, &node->u.callsite.declaration_list, siblings) { |
| 296 | iter->parent = node; |
| 297 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 298 | log_cfg); |
| 299 | if (ret) |
| 300 | return ret; |
| 301 | } |
| 302 | break; |
| 303 | |
| 304 | case NODE_CTF_EXPRESSION: |
| 305 | depth++; |
| 306 | bt_list_for_each_entry(iter, &node->u.ctf_expression.left, siblings) { |
| 307 | iter->parent = node; |
| 308 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 309 | log_cfg); |
| 310 | if (ret) |
| 311 | return ret; |
| 312 | } |
| 313 | bt_list_for_each_entry(iter, &node->u.ctf_expression.right, siblings) { |
| 314 | iter->parent = node; |
| 315 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 316 | log_cfg); |
| 317 | if (ret) |
| 318 | return ret; |
| 319 | } |
| 320 | depth--; |
| 321 | break; |
| 322 | case NODE_UNARY_EXPRESSION: |
| 323 | return ctf_visitor_unary_expression(depth, node, log_cfg); |
| 324 | |
| 325 | case NODE_TYPEDEF: |
| 326 | depth++; |
| 327 | node->u.field_class_def.field_class_specifier_list->parent = node; |
| 328 | ret = ctf_visitor_parent_links(depth + 1, |
| 329 | node->u.field_class_def.field_class_specifier_list, |
| 330 | log_cfg); |
| 331 | if (ret) |
| 332 | return ret; |
| 333 | bt_list_for_each_entry(iter, &node->u.field_class_def.field_class_declarators, siblings) { |
| 334 | iter->parent = node; |
| 335 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 336 | log_cfg); |
| 337 | if (ret) |
| 338 | return ret; |
| 339 | } |
| 340 | depth--; |
| 341 | break; |
| 342 | case NODE_TYPEALIAS_TARGET: |
| 343 | depth++; |
| 344 | node->u.field_class_alias_target.field_class_specifier_list->parent = node; |
| 345 | ret = ctf_visitor_parent_links(depth + 1, |
| 346 | node->u.field_class_alias_target.field_class_specifier_list, |
| 347 | log_cfg); |
| 348 | if (ret) |
| 349 | return ret; |
| 350 | bt_list_for_each_entry(iter, &node->u.field_class_alias_target.field_class_declarators, siblings) { |
| 351 | iter->parent = node; |
| 352 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 353 | log_cfg); |
| 354 | if (ret) |
| 355 | return ret; |
| 356 | } |
| 357 | depth--; |
| 358 | break; |
| 359 | case NODE_TYPEALIAS_ALIAS: |
| 360 | depth++; |
| 361 | node->u.field_class_alias_name.field_class_specifier_list->parent = node; |
| 362 | ret = ctf_visitor_parent_links(depth + 1, |
| 363 | node->u.field_class_alias_name.field_class_specifier_list, |
| 364 | log_cfg); |
| 365 | if (ret) |
| 366 | return ret; |
| 367 | bt_list_for_each_entry(iter, &node->u.field_class_alias_name.field_class_declarators, siblings) { |
| 368 | iter->parent = node; |
| 369 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 370 | log_cfg); |
| 371 | if (ret) |
| 372 | return ret; |
| 373 | } |
| 374 | depth--; |
| 375 | break; |
| 376 | case NODE_TYPEALIAS: |
| 377 | node->u.field_class_alias.target->parent = node; |
| 378 | ret = ctf_visitor_parent_links(depth + 1, |
| 379 | node->u.field_class_alias.target, log_cfg); |
| 380 | if (ret) |
| 381 | return ret; |
| 382 | node->u.field_class_alias.alias->parent = node; |
| 383 | ret = ctf_visitor_parent_links(depth + 1, |
| 384 | node->u.field_class_alias.alias, log_cfg); |
| 385 | if (ret) |
| 386 | return ret; |
| 387 | break; |
| 388 | |
| 389 | case NODE_TYPE_SPECIFIER_LIST: |
| 390 | bt_list_for_each_entry(iter, &node->u.field_class_specifier_list.head, siblings) { |
| 391 | iter->parent = node; |
| 392 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 393 | log_cfg); |
| 394 | if (ret) |
| 395 | return ret; |
| 396 | } |
| 397 | break; |
| 398 | |
| 399 | case NODE_TYPE_SPECIFIER: |
| 400 | ret = ctf_visitor_type_specifier(depth, node, log_cfg); |
| 401 | if (ret) |
| 402 | return ret; |
| 403 | break; |
| 404 | case NODE_POINTER: |
| 405 | break; |
| 406 | case NODE_TYPE_DECLARATOR: |
| 407 | ret = ctf_visitor_field_class_declarator(depth, node, |
| 408 | log_cfg); |
| 409 | if (ret) |
| 410 | return ret; |
| 411 | break; |
| 412 | |
| 413 | case NODE_FLOATING_POINT: |
| 414 | bt_list_for_each_entry(iter, &node->u.floating_point.expressions, siblings) { |
| 415 | iter->parent = node; |
| 416 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 417 | log_cfg); |
| 418 | if (ret) |
| 419 | return ret; |
| 420 | } |
| 421 | break; |
| 422 | case NODE_INTEGER: |
| 423 | bt_list_for_each_entry(iter, &node->u.integer.expressions, siblings) { |
| 424 | iter->parent = node; |
| 425 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 426 | log_cfg); |
| 427 | if (ret) |
| 428 | return ret; |
| 429 | } |
| 430 | break; |
| 431 | case NODE_STRING: |
| 432 | bt_list_for_each_entry(iter, &node->u.string.expressions, siblings) { |
| 433 | iter->parent = node; |
| 434 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 435 | log_cfg); |
| 436 | if (ret) |
| 437 | return ret; |
| 438 | } |
| 439 | break; |
| 440 | case NODE_ENUMERATOR: |
| 441 | bt_list_for_each_entry(iter, &node->u.enumerator.values, siblings) { |
| 442 | iter->parent = node; |
| 443 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 444 | log_cfg); |
| 445 | if (ret) |
| 446 | return ret; |
| 447 | } |
| 448 | break; |
| 449 | case NODE_ENUM: |
| 450 | depth++; |
| 451 | if (node->u._enum.container_field_class) { |
| 452 | ret = ctf_visitor_parent_links(depth + 1, |
| 453 | node->u._enum.container_field_class, log_cfg); |
| 454 | if (ret) |
| 455 | return ret; |
| 456 | } |
| 457 | |
| 458 | bt_list_for_each_entry(iter, &node->u._enum.enumerator_list, siblings) { |
| 459 | iter->parent = node; |
| 460 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 461 | log_cfg); |
| 462 | if (ret) |
| 463 | return ret; |
| 464 | } |
| 465 | depth--; |
| 466 | break; |
| 467 | case NODE_STRUCT_OR_VARIANT_DECLARATION: |
| 468 | node->u.struct_or_variant_declaration.field_class_specifier_list->parent = node; |
| 469 | ret = ctf_visitor_parent_links(depth + 1, |
| 470 | node->u.struct_or_variant_declaration.field_class_specifier_list, |
| 471 | log_cfg); |
| 472 | if (ret) |
| 473 | return ret; |
| 474 | bt_list_for_each_entry(iter, &node->u.struct_or_variant_declaration.field_class_declarators, siblings) { |
| 475 | iter->parent = node; |
| 476 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 477 | log_cfg); |
| 478 | if (ret) |
| 479 | return ret; |
| 480 | } |
| 481 | break; |
| 482 | case NODE_VARIANT: |
| 483 | bt_list_for_each_entry(iter, &node->u.variant.declaration_list, siblings) { |
| 484 | iter->parent = node; |
| 485 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 486 | log_cfg); |
| 487 | if (ret) |
| 488 | return ret; |
| 489 | } |
| 490 | break; |
| 491 | case NODE_STRUCT: |
| 492 | bt_list_for_each_entry(iter, &node->u._struct.declaration_list, siblings) { |
| 493 | iter->parent = node; |
| 494 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 495 | log_cfg); |
| 496 | if (ret) |
| 497 | return ret; |
| 498 | } |
| 499 | bt_list_for_each_entry(iter, &node->u._struct.min_align, |
| 500 | siblings) { |
| 501 | iter->parent = node; |
| 502 | ret = ctf_visitor_parent_links(depth + 1, iter, |
| 503 | log_cfg); |
| 504 | if (ret) |
| 505 | return ret; |
| 506 | } |
| 507 | break; |
| 508 | |
| 509 | case NODE_UNKNOWN: |
| 510 | default: |
| 511 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 512 | "Unknown node type: type=%d\n", node->type); |
| 513 | return -EINVAL; |
| 514 | } |
| 515 | return ret; |
| 516 | } |