#define _cds_list_first_entry(ptr, type, member) \
cds_list_entry((ptr)->next, type, member)
-#define printf_dbg(fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
+#define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
static
int _ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node);
int ctf_visitor_unary_expression(FILE *fd, int depth, struct ctf_node *node)
{
struct ctf_node *iter;
- int is_ctf_exp_left = 0;
+ int is_ctf_exp = 0, is_ctf_exp_left = 0;
switch (node->parent->type) {
case NODE_CTF_EXPRESSION:
+ is_ctf_exp = 1;
cds_list_for_each_entry(iter, &node->parent->u.ctf_expression.left,
siblings) {
if (iter == node) {
* We are a left child of a ctf expression.
* We are only allowed to be a string.
*/
- if (node->u.unary_expression.type != UNARY_STRING)
+ if (node->u.unary_expression.type != UNARY_STRING) {
+ fprintf(fd, "[error]: semantic error (left child of a ctf expression is only allowed to be a string)\n");
+
goto errperm;
+ }
break;
}
}
/* 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. We can only be
- * a numeric constant.
+ * We are the length of a type declarator or the size of an enum
+ * container. We can only be a numeric constant.
*/
switch (node->u.unary_expression.type) {
case UNARY_SIGNED_CONSTANT:
case UNARY_UNSIGNED_CONSTANT:
break;
default:
+ fprintf(fd, "[error]: semantic error (children of type declarator and enum can only be numeric constants)\n");
goto errperm;
}
break; /* OK */
* We disallow nested unary expressions and "sbrac" unary
* expressions.
*/
+ fprintf(fd, "[error]: semantic error (nested unary expressions not allowed ( () and [] ))\n");
goto errperm;
case NODE_ROOT:
case NODE_POINTER:
case NODE_FLOATING_POINT:
case NODE_INTEGER:
- case NODE_ENUM:
case NODE_STRING:
case NODE_STRUCT_OR_VARIANT_DECLARATION:
case NODE_VARIANT:
switch (node->u.unary_expression.link) {
case UNARY_LINK_UNKNOWN:
/* We don't allow empty link except on the first node of the list */
- if (_cds_list_first_entry(is_ctf_exp_left ?
+ if (is_ctf_exp && _cds_list_first_entry(is_ctf_exp_left ?
&node->parent->u.ctf_expression.left :
&node->parent->u.ctf_expression.right,
struct ctf_node,
- siblings) != node)
+ siblings) != node) {
+ fprintf(fd, "[error]: semantic error (empty link not allowed except on first node of unary expression (need to separate nodes with \".\" or \"->\")\n");
goto errperm;
+ }
break; /* OK */
case UNARY_DOTLINK:
case UNARY_ARROWLINK:
/* We only allow -> and . links between children of ctf_expression. */
- if (node->parent->type != NODE_CTF_EXPRESSION)
- goto errperm;
- /* We don't allow link on the first node of the list */
- if (_cds_list_first_entry(is_ctf_exp_left ?
- &node->parent->u.ctf_expression.left :
- &node->parent->u.ctf_expression.right,
- struct ctf_node,
- siblings) == node)
+ if (node->parent->type != NODE_CTF_EXPRESSION) {
+ fprintf(fd, "[error]: semantic error (links \".\" and \"->\" are only allowed as children of ctf expression)\n");
goto errperm;
+ }
/*
* Only strings can be separated linked by . or ->.
* This includes "", '' and non-quoted identifiers.
*/
- if (node->u.unary_expression.type != UNARY_STRING)
+ if (node->u.unary_expression.type != UNARY_STRING) {
+ fprintf(fd, "[error]: semantic error (links \".\" and \"->\" are only allowed to separate strings and identifiers)\n");
goto errperm;
+ }
+ /* We don't allow link on the first node of the list */
+ if (is_ctf_exp && _cds_list_first_entry(is_ctf_exp_left ?
+ &node->parent->u.ctf_expression.left :
+ &node->parent->u.ctf_expression.right,
+ struct ctf_node,
+ siblings) == node) {
+ fprintf(fd, "[error]: semantic error (links \".\" and \"->\" are not allowed before first node of the unary expression list)\n");
+ goto errperm;
+ }
break;
case UNARY_DOTDOTDOT:
/* We only allow ... link between children of enumerator. */
- if (node->parent->type != NODE_ENUMERATOR)
+ if (node->parent->type != NODE_ENUMERATOR) {
+ fprintf(fd, "[error]: semantic error (link \"...\" is only allowed within enumerator)\n");
goto errperm;
+ }
/* We don't allow link on the first node of the list */
if (_cds_list_first_entry(&node->parent->u.enumerator.values,
struct ctf_node,
- siblings) == node)
- return -EPERM;
+ siblings) == node) {
+ fprintf(fd, "[error]: semantic error (link \"...\" is not allowed on the first node of the unary expression list)\n");
+ goto errperm;
+ }
break;
default:
fprintf(fd, "[error] %s: unknown expression link type %d\n", __func__,
return 0;
errinval:
- fprintf(fd, "[error] %s: incoherent parent type %d for node type %d\n", __func__,
- (int) node->parent->type, (int) node->type);
+ 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 */
errperm:
+ fprintf(fd, "[error] %s: semantic error (parent type %s for node type %s)\n", __func__,
+ node_type(node->parent), node_type(node));
return -EPERM; /* Structure not allowed */
}
case NODE_TYPEALIAS_TARGET:
case NODE_TYPEALIAS_ALIAS:
case NODE_ENUM:
+ case NODE_STRUCT_OR_VARIANT_DECLARATION:
break; /* OK */
case NODE_ROOT:
case NODE_INTEGER:
case NODE_STRING:
case NODE_ENUMERATOR:
- case NODE_STRUCT_OR_VARIANT_DECLARATION:
case NODE_VARIANT:
case NODE_STRUCT:
default:
}
return 0;
errinval:
- fprintf(fd, "[error] %s: incoherent parent type %d for node type %d\n", __func__,
- (int) node->parent->type, (int) node->type);
+ 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 */
}
*/
if (!cds_list_empty(&node->u.type_declarator.pointers))
goto errperm;
- /* Fall-through */
- case NODE_TYPEDEF:
+ break; /* OK */
case NODE_TYPEALIAS_TARGET:
+ break; /* OK */
case NODE_TYPEALIAS_ALIAS:
+ /*
+ * Only accept alias name containing:
+ * - identifier
+ * - identifier * (any number of pointers)
+ * NOT accepting alias names containing [] (would otherwise
+ * cause semantic clash for later declarations of
+ * arrays/sequences of elements, where elements could be
+ * arrays/sequences themselves (if allowed in typealias).
+ * NOT accepting alias with identifier. The declarator should
+ * be either empty or contain pointer(s).
+ */
+ if (node->u.type_declarator.type == TYPEDEC_NESTED)
+ goto errperm;
+ if (cds_list_empty(&node->u.type_declarator.pointers))
+ goto errperm;
+ if (node->u.type_declarator.type == TYPEDEC_ID &&
+ node->u.type_declarator.u.id != NULL)
+ goto errperm;
+ break; /* OK */
+ case NODE_TYPEDEF:
case NODE_STRUCT_OR_VARIANT_DECLARATION:
break; /* OK */
return 0;
errinval:
- fprintf(fd, "[error] %s: incoherent parent type %d for node type %d\n", __func__,
- (int) node->parent->type, (int) node->type);
+ 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 */
errperm:
+ fprintf(fd, "[error] %s: semantic error (parent type %s for node type %s)\n", __func__,
+ node_type(node->parent), node_type(node));
return -EPERM; /* Structure not allowed */
}
depth--;
break;
case NODE_TYPEALIAS_TARGET:
+ {
+ int nr_declarators;
+
switch (node->parent->type) {
case NODE_TYPEALIAS:
break; /* OK */
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);
if (ret)
return ret;
+ nr_declarators++;
+ }
+ if (nr_declarators > 1) {
+ fprintf(fd, "[error] %s: Too many declarators in typealias alias (%d, max is 1)\n", __func__, nr_declarators);
+
+ return -EINVAL;
}
depth--;
break;
+ }
case NODE_TYPEALIAS_ALIAS:
+ {
+ int nr_declarators;
+
switch (node->parent->type) {
case NODE_TYPEALIAS:
break; /* OK */
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);
if (ret)
return ret;
+ nr_declarators++;
+ }
+ if (nr_declarators > 1) {
+ fprintf(fd, "[error] %s: Too many declarators in typealias alias (%d, max is 1)\n", __func__, nr_declarators);
+
+ return -EINVAL;
}
depth--;
break;
+ }
case NODE_TYPEALIAS:
switch (node->parent->type) {
case NODE_ROOT:
{
int count = 0;
- cds_list_for_each_entry(iter, &node->parent->u.enumerator.values,
+ cds_list_for_each_entry(iter, &node->u.enumerator.values,
siblings) {
switch (count++) {
case 0: if (iter->type != NODE_UNARY_EXPRESSION
|| (iter->u.unary_expression.type != UNARY_SIGNED_CONSTANT
&& iter->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT)
- || iter->u.unary_expression.link != UNARY_LINK_UNKNOWN)
+ || iter->u.unary_expression.link != UNARY_LINK_UNKNOWN) {
+ fprintf(fd, "[error]: semantic error (first unary expression of enumerator is unexpected)\n");
goto errperm;
+ }
break;
case 1: if (iter->type != NODE_UNARY_EXPRESSION
|| (iter->u.unary_expression.type != UNARY_SIGNED_CONSTANT
&& iter->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT)
- || iter->u.unary_expression.link != UNARY_DOTDOTDOT)
+ || iter->u.unary_expression.link != UNARY_DOTDOTDOT) {
+ fprintf(fd, "[error]: semantic error (second unary expression of enumerator is unexpected)\n");
goto errperm;
+ }
break;
default:
goto errperm;
return ret;
errinval:
- fprintf(fd, "[error] %s: incoherent parent type %d for node type %d\n", __func__,
- (int) node->parent->type, (int) node->type);
+ 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 */
errperm:
+ fprintf(fd, "[error] %s: semantic error (parent type %s for node type %s)\n", __func__,
+ node_type(node->parent), node_type(node));
return -EPERM; /* Structure not allowed */
}
* take the safe route and recreate them at each validation, just in
* case the structure has changed.
*/
+ fprintf(fd, "CTF visitor: parent links creation... ");
ret = ctf_visitor_parent_links(fd, depth, node);
if (ret)
return ret;
- return _ctf_visitor_semantic_check(fd, depth, node);
+ fprintf(fd, "done.\n");
+ fprintf(fd, "CTF visitor: semantic check... ");
+ ret = _ctf_visitor_semantic_check(fd, depth, node);
+ if (ret)
+ return ret;
+ fprintf(fd, "done.\n");
+ return ret;
}