X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=blobdiff_plain;f=formats%2Fctf%2Fmetadata%2Fctf-visitor-semantic-validator.c;h=b89f7bd7660d0d363faf4dab4dd77d009bb57320;hp=6f46bfbdf5df6d342cef56ad3bb6892fd813619d;hb=7c7835b045c7457dc13ac83a7af1b1c1df5843cb;hpb=7d4192cb80178ad534e460d56f67e55679c7d8ee diff --git a/formats/ctf/metadata/ctf-visitor-semantic-validator.c b/formats/ctf/metadata/ctf-visitor-semantic-validator.c index 6f46bfbd..b89f7bd7 100644 --- a/formats/ctf/metadata/ctf-visitor-semantic-validator.c +++ b/formats/ctf/metadata/ctf-visitor-semantic-validator.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include "ctf-scanner.h" #include "ctf-parser.h" @@ -65,20 +66,32 @@ int ctf_visitor_unary_expression(FILE *fd, int depth, struct ctf_node *node) /* Right child of a ctf expression can be any type of unary exp. */ break; /* OK */ case NODE_TYPE_DECLARATOR: - case NODE_ENUM: /* - * We are the length of a type declarator or the size of an enum - * container. We can only be a numeric constant. + * We are the length of a type declarator. */ switch (node->u.unary_expression.type) { - case UNARY_SIGNED_CONSTANT: case UNARY_UNSIGNED_CONSTANT: + case UNARY_STRING: break; default: - fprintf(fd, "[error]: semantic error (children of type declarator and enum can only be numeric constants)\n"); + 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"); goto errperm; } break; /* OK */ + + case NODE_STRUCT: + /* + * We are the size of a struct align attribute. + */ + switch (node->u.unary_expression.type) { + case UNARY_UNSIGNED_CONSTANT: + break; + default: + fprintf(fd, "[error]: semantic error (structure alignment attribute can only be unsigned numeric constants)\n"); + goto errperm; + } + break; + case NODE_ENUMERATOR: /* The enumerator's parent has validated its validity already. */ break; /* OK */ @@ -104,9 +117,9 @@ int ctf_visitor_unary_expression(FILE *fd, int depth, struct ctf_node *node) case NODE_FLOATING_POINT: case NODE_INTEGER: case NODE_STRING: + case NODE_ENUM: case NODE_STRUCT_OR_VARIANT_DECLARATION: case NODE_VARIANT: - case NODE_STRUCT: default: goto errinval; } @@ -181,7 +194,7 @@ errperm: } static -int ctf_visitor_type_specifier(FILE *fd, int depth, struct ctf_node *node) +int ctf_visitor_type_specifier_list(FILE *fd, int depth, struct ctf_node *node) { switch (node->parent->type) { case NODE_CTF_EXPRESSION: @@ -191,8 +204,47 @@ int ctf_visitor_type_specifier(FILE *fd, int depth, struct ctf_node *node) case NODE_TYPEALIAS_ALIAS: case NODE_ENUM: case NODE_STRUCT_OR_VARIANT_DECLARATION: + case NODE_ROOT: break; /* OK */ + case NODE_EVENT: + case NODE_STREAM: + case NODE_TRACE: + case NODE_UNARY_EXPRESSION: + case NODE_TYPEALIAS: + case NODE_TYPE_SPECIFIER: + case NODE_TYPE_SPECIFIER_LIST: + case NODE_POINTER: + case NODE_FLOATING_POINT: + case NODE_INTEGER: + case NODE_STRING: + case NODE_ENUMERATOR: + case NODE_VARIANT: + case NODE_STRUCT: + default: + goto errinval; + } + return 0; +errinval: + fprintf(fd, "[error] %s: incoherent parent type %s for node type %s\n", __func__, + node_type(node->parent), node_type(node)); + return -EINVAL; /* Incoherent structure */ +} + +static +int ctf_visitor_type_specifier(FILE *fd, int depth, struct ctf_node *node) +{ + switch (node->parent->type) { + case NODE_TYPE_SPECIFIER_LIST: + break; /* OK */ + + case NODE_CTF_EXPRESSION: + case NODE_TYPE_DECLARATOR: + case NODE_TYPEDEF: + case NODE_TYPEALIAS_TARGET: + case NODE_TYPEALIAS_ALIAS: + case NODE_ENUM: + case NODE_STRUCT_OR_VARIANT_DECLARATION: case NODE_ROOT: case NODE_EVENT: case NODE_STREAM: @@ -249,8 +301,22 @@ int ctf_visitor_type_declarator(FILE *fd, int depth, struct ctf_node *node) */ if (node->u.type_declarator.type == TYPEDEC_NESTED) goto errperm; - if (cds_list_empty(&node->u.type_declarator.pointers)) - goto errperm; + cds_list_for_each_entry(iter, &node->parent->u.typealias_alias.type_specifier_list->u.type_specifier_list.head, + siblings) { + switch (iter->u.type_specifier.type) { + case TYPESPEC_FLOATING_POINT: + case TYPESPEC_INTEGER: + case TYPESPEC_STRING: + case TYPESPEC_STRUCT: + case TYPESPEC_VARIANT: + case TYPESPEC_ENUM: + if (cds_list_empty(&node->u.type_declarator.pointers)) + goto errperm; + break; + default: + break; + } + } if (node->u.type_declarator.type == TYPEDEC_ID && node->u.type_declarator.u.id != NULL) goto errperm; @@ -279,13 +345,11 @@ int ctf_visitor_type_declarator(FILE *fd, int depth, struct ctf_node *node) goto errinval; } - if (!cds_list_empty(&node->u.type_declarator.pointers)) { - cds_list_for_each_entry(iter, &node->u.type_declarator.pointers, - siblings) { - ret = _ctf_visitor_semantic_check(fd, depth + 1, iter); - if (ret) - return ret; - } + cds_list_for_each_entry(iter, &node->u.type_declarator.pointers, + siblings) { + ret = _ctf_visitor_semantic_check(fd, depth + 1, iter); + if (ret) + return ret; } switch (node->u.type_declarator.type) { @@ -293,23 +357,27 @@ int ctf_visitor_type_declarator(FILE *fd, int depth, struct ctf_node *node) break; case TYPEDEC_NESTED: { - int nr_nest_len; - if (node->u.type_declarator.u.nested.type_declarator) { ret = _ctf_visitor_semantic_check(fd, depth + 1, node->u.type_declarator.u.nested.type_declarator); if (ret) return ret; } - cds_list_for_each_entry(iter, &node->u.type_declarator.u.nested.length, - siblings) { - ret = _ctf_visitor_semantic_check(fd, depth + 1, - iter); - if (ret) - return ret; - nr_nest_len++; - if (iter->type == NODE_UNARY_EXPRESSION && nr_nest_len > 1) { - goto errperm; + if (!node->u.type_declarator.u.nested.abstract_array) { + cds_list_for_each_entry(iter, &node->u.type_declarator.u.nested.length, + siblings) { + if (iter->type != NODE_UNARY_EXPRESSION) { + fprintf(fd, "[error] %s: expecting unary expression as length\n", __func__); + return -EINVAL; + } + ret = _ctf_visitor_semantic_check(fd, depth + 1, iter); + if (ret) + return ret; + } + } else { + if (node->parent->type == NODE_TYPEALIAS_TARGET) { + fprintf(fd, "[error] %s: abstract array declarator not permitted as target of typealias\n", __func__); + return -EINVAL; } } if (node->u.type_declarator.bitfield_len) { @@ -348,19 +416,7 @@ int _ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node) switch (node->type) { case NODE_ROOT: - cds_list_for_each_entry(iter, &node->u.root._typedef, - siblings) { - ret = _ctf_visitor_semantic_check(fd, depth + 1, iter); - if (ret) - return ret; - } - cds_list_for_each_entry(iter, &node->u.root.typealias, - siblings) { - ret = _ctf_visitor_semantic_check(fd, depth + 1, iter); - if (ret) - return ret; - } - cds_list_for_each_entry(iter, &node->u.root.declaration_specifier, siblings) { + cds_list_for_each_entry(iter, &node->u.root.declaration_list, siblings) { ret = _ctf_visitor_semantic_check(fd, depth + 1, iter); if (ret) return ret; @@ -444,6 +500,7 @@ int _ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node) case NODE_STRUCT_OR_VARIANT_DECLARATION: case NODE_TYPEALIAS: case NODE_TYPE_SPECIFIER: + case NODE_TYPE_SPECIFIER_LIST: case NODE_POINTER: case NODE_TYPE_DECLARATOR: case NODE_ENUMERATOR: @@ -488,6 +545,7 @@ int _ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node) case NODE_TYPEALIAS: case NODE_STRUCT_OR_VARIANT_DECLARATION: case NODE_TYPE_SPECIFIER: + case NODE_TYPE_SPECIFIER_LIST: case NODE_POINTER: case NODE_TYPE_DECLARATOR: case NODE_FLOATING_POINT: @@ -500,11 +558,10 @@ int _ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node) } depth++; - cds_list_for_each_entry(iter, &node->u._typedef.declaration_specifier, siblings) { - ret = _ctf_visitor_semantic_check(fd, depth + 1, iter); - if (ret) - return ret; - } + ret = _ctf_visitor_semantic_check(fd, depth + 1, + node->u._typedef.type_specifier_list); + if (ret) + return ret; cds_list_for_each_entry(iter, &node->u._typedef.type_declarators, siblings) { ret = _ctf_visitor_semantic_check(fd, depth + 1, iter); if (ret) @@ -524,11 +581,10 @@ int _ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node) } depth++; - cds_list_for_each_entry(iter, &node->u.typealias_target.declaration_specifier, siblings) { - ret = _ctf_visitor_semantic_check(fd, depth + 1, iter); - if (ret) - return ret; - } + ret = _ctf_visitor_semantic_check(fd, depth + 1, + node->u.typealias_target.type_specifier_list); + if (ret) + return ret; nr_declarators = 0; cds_list_for_each_entry(iter, &node->u.typealias_target.type_declarators, siblings) { ret = _ctf_visitor_semantic_check(fd, depth + 1, iter); @@ -556,11 +612,10 @@ int _ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node) } depth++; - cds_list_for_each_entry(iter, &node->u.typealias_alias.declaration_specifier, siblings) { - ret = _ctf_visitor_semantic_check(fd, depth + 1, iter); - if (ret) - return ret; - } + ret = _ctf_visitor_semantic_check(fd, depth + 1, + node->u.typealias_alias.type_specifier_list); + if (ret) + return ret; nr_declarators = 0; cds_list_for_each_entry(iter, &node->u.typealias_alias.type_declarators, siblings) { ret = _ctf_visitor_semantic_check(fd, depth + 1, iter); @@ -594,6 +649,7 @@ int _ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node) case NODE_TYPEALIAS: case NODE_STRUCT_OR_VARIANT_DECLARATION: case NODE_TYPE_SPECIFIER: + case NODE_TYPE_SPECIFIER_LIST: case NODE_POINTER: case NODE_TYPE_DECLARATOR: case NODE_FLOATING_POINT: @@ -613,6 +669,11 @@ int _ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node) return ret; break; + case NODE_TYPE_SPECIFIER_LIST: + ret = ctf_visitor_type_specifier_list(fd, depth, node); + if (ret) + return ret; + break; case NODE_TYPE_SPECIFIER: ret = ctf_visitor_type_specifier(fd, depth, node); if (ret) @@ -634,28 +695,8 @@ int _ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node) case NODE_FLOATING_POINT: switch (node->parent->type) { - case NODE_CTF_EXPRESSION: - case NODE_TYPEDEF: - case NODE_TYPEALIAS_TARGET: - case NODE_TYPEALIAS_ALIAS: - case NODE_STRUCT_OR_VARIANT_DECLARATION: - break; /* OK */ - - case NODE_ROOT: - case NODE_EVENT: - case NODE_STREAM: - case NODE_TRACE: - case NODE_TYPEALIAS: case NODE_TYPE_SPECIFIER: - case NODE_POINTER: - case NODE_TYPE_DECLARATOR: - case NODE_FLOATING_POINT: - case NODE_INTEGER: - case NODE_STRING: - case NODE_ENUMERATOR: - case NODE_ENUM: - case NODE_VARIANT: - case NODE_STRUCT: + break; /* OK */ default: goto errinval; @@ -670,29 +711,8 @@ int _ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node) break; case NODE_INTEGER: switch (node->parent->type) { - case NODE_CTF_EXPRESSION: - case NODE_UNARY_EXPRESSION: - case NODE_TYPEDEF: - case NODE_TYPEALIAS_TARGET: - case NODE_TYPEALIAS_ALIAS: - case NODE_TYPE_DECLARATOR: - case NODE_ENUM: - case NODE_STRUCT_OR_VARIANT_DECLARATION: - break; /* OK */ - - case NODE_ROOT: - case NODE_EVENT: - case NODE_STREAM: - case NODE_TRACE: - case NODE_TYPEALIAS: case NODE_TYPE_SPECIFIER: - case NODE_POINTER: - case NODE_FLOATING_POINT: - case NODE_INTEGER: - case NODE_STRING: - case NODE_ENUMERATOR: - case NODE_VARIANT: - case NODE_STRUCT: + break; /* OK */ default: goto errinval; @@ -706,28 +726,8 @@ int _ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node) break; case NODE_STRING: switch (node->parent->type) { - case NODE_CTF_EXPRESSION: - case NODE_TYPEDEF: - case NODE_TYPEALIAS_TARGET: - case NODE_TYPEALIAS_ALIAS: - case NODE_STRUCT_OR_VARIANT_DECLARATION: - break; /* OK */ - - case NODE_ROOT: - case NODE_EVENT: - case NODE_STREAM: - case NODE_TRACE: - case NODE_TYPEALIAS: case NODE_TYPE_SPECIFIER: - case NODE_POINTER: - case NODE_TYPE_DECLARATOR: - case NODE_FLOATING_POINT: - case NODE_INTEGER: - case NODE_STRING: - case NODE_ENUMERATOR: - case NODE_ENUM: - case NODE_VARIANT: - case NODE_STRUCT: + break; /* OK */ default: goto errinval; @@ -789,28 +789,8 @@ int _ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node) break; case NODE_ENUM: switch (node->parent->type) { - case NODE_ROOT: - case NODE_EVENT: - case NODE_STREAM: - case NODE_TRACE: - case NODE_CTF_EXPRESSION: - case NODE_TYPEDEF: - case NODE_TYPEALIAS_TARGET: - case NODE_TYPEALIAS_ALIAS: - case NODE_TYPE_DECLARATOR: - case NODE_STRUCT_OR_VARIANT_DECLARATION: - break; /* OK */ - - case NODE_TYPEALIAS: case NODE_TYPE_SPECIFIER: - case NODE_POINTER: - case NODE_FLOATING_POINT: - case NODE_INTEGER: - case NODE_STRING: - case NODE_ENUMERATOR: - case NODE_ENUM: - case NODE_VARIANT: - case NODE_STRUCT: + break; /* OK */ default: goto errinval; @@ -819,12 +799,9 @@ int _ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node) } depth++; - cds_list_for_each_entry(iter, &node->u._enum.container_type, - siblings) { - ret = _ctf_visitor_semantic_check(fd, depth + 1, iter); - if (ret) - return ret; - } + ret = _ctf_visitor_semantic_check(fd, depth + 1, node->u._enum.container_type); + if (ret) + return ret; cds_list_for_each_entry(iter, &node->u._enum.enumerator_list, siblings) { ret = _ctf_visitor_semantic_check(fd, depth + 1, iter); @@ -841,11 +818,10 @@ int _ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node) default: goto errinval; } - cds_list_for_each_entry(iter, &node->u.struct_or_variant_declaration.declaration_specifier, siblings) { - ret = _ctf_visitor_semantic_check(fd, depth + 1, iter); - if (ret) - return ret; - } + ret = _ctf_visitor_semantic_check(fd, depth + 1, + node->u.struct_or_variant_declaration.type_specifier_list); + if (ret) + return ret; cds_list_for_each_entry(iter, &node->u.struct_or_variant_declaration.type_declarators, siblings) { ret = _ctf_visitor_semantic_check(fd, depth + 1, iter); if (ret) @@ -854,28 +830,8 @@ int _ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node) break; case NODE_VARIANT: switch (node->parent->type) { - case NODE_ROOT: - case NODE_EVENT: - case NODE_STREAM: - case NODE_TRACE: - case NODE_CTF_EXPRESSION: - case NODE_TYPEDEF: - case NODE_TYPEALIAS_TARGET: - case NODE_TYPEALIAS_ALIAS: - case NODE_STRUCT_OR_VARIANT_DECLARATION: - break; /* OK */ - - case NODE_TYPEALIAS: case NODE_TYPE_SPECIFIER: - case NODE_POINTER: - case NODE_TYPE_DECLARATOR: - case NODE_FLOATING_POINT: - case NODE_INTEGER: - case NODE_STRING: - case NODE_ENUMERATOR: - case NODE_ENUM: - case NODE_VARIANT: - case NODE_STRUCT: + break; /* OK */ default: goto errinval; @@ -891,28 +847,8 @@ int _ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node) case NODE_STRUCT: switch (node->parent->type) { - case NODE_ROOT: - case NODE_EVENT: - case NODE_STREAM: - case NODE_TRACE: - case NODE_CTF_EXPRESSION: - case NODE_TYPEDEF: - case NODE_TYPEALIAS_TARGET: - case NODE_TYPEALIAS_ALIAS: - case NODE_STRUCT_OR_VARIANT_DECLARATION: - break; /* OK */ - - case NODE_TYPEALIAS: case NODE_TYPE_SPECIFIER: - case NODE_POINTER: - case NODE_TYPE_DECLARATOR: - case NODE_FLOATING_POINT: - case NODE_INTEGER: - case NODE_STRING: - case NODE_ENUMERATOR: - case NODE_ENUM: - case NODE_VARIANT: - case NODE_STRUCT: + break; /* OK */ default: goto errinval; @@ -954,15 +890,15 @@ int ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node) * take the safe route and recreate them at each validation, just in * case the structure has changed. */ - fprintf(fd, "CTF visitor: parent links creation... "); + printf_verbose("CTF visitor: parent links creation... "); ret = ctf_visitor_parent_links(fd, depth, node); if (ret) return ret; - fprintf(fd, "done.\n"); - fprintf(fd, "CTF visitor: semantic check... "); + printf_verbose("done.\n"); + printf_verbose("CTF visitor: semantic check... "); ret = _ctf_visitor_semantic_check(fd, depth, node); if (ret) return ret; - fprintf(fd, "done.\n"); + printf_verbose("done.\n"); return ret; }