Notification iterator: transform precondition checks to BT_ASSERT_PRE()
[babeltrace.git] / lib / ctf-ir / field-types.c
index 7371e5bfcf5bb483e30bb13590524f771ff69dd1..a04384d26a9a58d8c6aeccdf89d68277fef96df0 100644 (file)
@@ -40,6 +40,7 @@
 #include <babeltrace/ref.h>
 #include <babeltrace/compiler-internal.h>
 #include <babeltrace/endian-internal.h>
+#include <babeltrace/assert-internal.h>
 #include <float.h>
 #include <inttypes.h>
 #include <stdlib.h>
@@ -397,7 +398,7 @@ gint compare_enumeration_mappings_unsigned(struct enumeration_mapping **a,
 static
 void bt_field_type_init(struct bt_field_type *type, bt_bool init_bo)
 {
-       assert(type && (type->id > BT_FIELD_TYPE_ID_UNKNOWN) &&
+       BT_ASSERT(type && (type->id > BT_FIELD_TYPE_ID_UNKNOWN) &&
                (type->id < BT_FIELD_TYPE_ID_NR));
 
        bt_object_init(type, bt_field_type_destroy);
@@ -411,7 +412,7 @@ void bt_field_type_init(struct bt_field_type *type, bt_bool init_bo)
                BT_LOGD("Setting initial field type's byte order: bo=%s",
                        bt_byte_order_string(bo));
                ret = bt_field_type_set_byte_order(type, bo);
-               assert(ret == 0);
+               BT_ASSERT(ret == 0);
        }
 
        type->alignment = 1;
@@ -425,19 +426,7 @@ int add_structure_field(GPtrArray *fields,
 {
        int ret = 0;
        GQuark name_quark = g_quark_from_string(field_name);
-       GQuark underscore_name_quark;
        struct structure_field *field;
-       GString *underscore_name = g_string_new(NULL);
-
-       if (!underscore_name) {
-               BT_LOGE_STR("Failed to allocate a GString.");
-               ret = -1;
-               goto end;
-       }
-
-       g_string_assign(underscore_name, "_");
-       g_string_append(underscore_name, field_name);
-       underscore_name_quark = g_quark_from_string(underscore_name->str);
 
        /* Make sure structure does not contain a field of the same name */
        if (g_hash_table_lookup_extended(field_name_to_index,
@@ -448,14 +437,6 @@ int add_structure_field(GPtrArray *fields,
                goto end;
        }
 
-       if (g_hash_table_lookup_extended(field_name_to_index,
-                       GUINT_TO_POINTER(underscore_name_quark), NULL, NULL)) {
-               BT_LOGW("Structure or variant field type already contains a field type with this name: "
-                       "field-name=\"%s\"", underscore_name->str);
-               ret = -1;
-               goto end;
-       }
-
        field = g_new0(struct structure_field, 1);
        if (!field) {
                BT_LOGE_STR("Failed to allocate one structure/variant field type field.");
@@ -469,14 +450,10 @@ int add_structure_field(GPtrArray *fields,
        g_hash_table_insert(field_name_to_index,
                GUINT_TO_POINTER(name_quark),
                GUINT_TO_POINTER(fields->len));
-       g_hash_table_insert(field_name_to_index,
-               GUINT_TO_POINTER(underscore_name_quark),
-               GUINT_TO_POINTER(fields->len));
        g_ptr_array_add(fields, field);
        BT_LOGV("Added structure/variant field type field: field-ft-addr=%p, "
                "field-name=\"%s\"", field_type, field_name);
 end:
-       g_string_free(underscore_name, TRUE);
        return ret;
 }
 
@@ -488,7 +465,7 @@ void bt_field_type_destroy(struct bt_object *obj)
 
        type = container_of(obj, struct bt_field_type, base);
        type_id = type->id;
-       assert(type_id > BT_FIELD_TYPE_ID_UNKNOWN &&
+       BT_ASSERT(type_id > BT_FIELD_TYPE_ID_UNKNOWN &&
                type_id < BT_FIELD_TYPE_ID_NR);
        type_destroy_funcs[type_id](type);
 }
@@ -603,7 +580,7 @@ int bt_field_type_enumeration_validate(struct bt_field_type *type)
        struct bt_field_type *container_type =
                bt_field_type_enumeration_get_container_type(type);
 
-       assert(container_type);
+       BT_ASSERT(container_type);
        ret = bt_field_type_validate(container_type);
        if (ret) {
                BT_LOGW("Invalid enumeration field type: container type is invalid: "
@@ -643,7 +620,7 @@ int bt_field_type_sequence_validate(struct bt_field_type *type)
        }
 
        element_type = bt_field_type_sequence_get_element_type(type);
-       assert(element_type);
+       BT_ASSERT(element_type);
        ret = bt_field_type_validate(element_type);
        if (ret) {
                BT_LOGW("Invalid sequence field type: invalid element field type: "
@@ -664,7 +641,7 @@ int bt_field_type_array_validate(struct bt_field_type *type)
        struct bt_field_type *element_type = NULL;
 
        element_type = bt_field_type_array_get_element_type(type);
-       assert(element_type);
+       BT_ASSERT(element_type);
        ret = bt_field_type_validate(element_type);
        if (ret) {
                BT_LOGW("Invalid array field type: invalid element field type: "
@@ -684,14 +661,14 @@ int bt_field_type_structure_validate(struct bt_field_type *type)
        int64_t field_count = bt_field_type_structure_get_field_count(type);
        int64_t i;
 
-       assert(field_count >= 0);
+       BT_ASSERT(field_count >= 0);
 
        for (i = 0; i < field_count; ++i) {
                const char *field_name;
 
                ret = bt_field_type_structure_get_field_by_index(type,
                        &field_name, &child_type, i);
-               assert(ret == 0);
+               BT_ASSERT(ret == 0);
                ret = bt_field_type_validate(child_type);
                if (ret) {
                        BT_LOGW("Invalid structure field type: "
@@ -785,7 +762,7 @@ int bt_field_type_variant_validate(struct bt_field_type *type)
 
                ret = bt_field_type_variant_get_field_by_index(type,
                        &field_name, &child_type, i);
-               assert(ret == 0);
+               BT_ASSERT(ret == 0);
                ret = bt_field_type_validate(child_type);
                if (ret) {
                        BT_LOGW("Invalid variant field type: "
@@ -819,7 +796,7 @@ int bt_field_type_validate(struct bt_field_type *type)
        int ret = 0;
        enum bt_field_type_id id = bt_field_type_get_type_id(type);
 
-       assert(type);
+       BT_ASSERT(type);
 
        if (type->valid) {
                /* Already marked as valid */
@@ -1297,7 +1274,7 @@ bt_field_type_enumeration_find_mappings_by_name(
        iter = bt_field_type_enumeration_find_mappings_type(
                        type, ITERATOR_BY_NAME);
        if (!iter) {
-               BT_LOGE("Cannot create enumeration field type mapping iterator: "
+               BT_LOGW("Cannot create enumeration field type mapping iterator: "
                        "ft-addr=%p, mapping-name=\"%s\"", type, name);
                goto error;
        }
@@ -1386,7 +1363,7 @@ bt_field_type_enumeration_find_mappings_by_signed_value(
        iter = bt_field_type_enumeration_find_mappings_type(
                        type, ITERATOR_BY_SIGNED_VALUE);
        if (!iter) {
-               BT_LOGE("Cannot create enumeration field type mapping iterator: "
+               BT_LOGW("Cannot create enumeration field type mapping iterator: "
                        "ft-addr=%p, value=%" PRId64, type, value);
                goto error;
        }
@@ -1415,7 +1392,7 @@ bt_field_type_enumeration_find_mappings_by_unsigned_value(
        iter = bt_field_type_enumeration_find_mappings_type(
                        type, ITERATOR_BY_UNSIGNED_VALUE);
        if (!iter) {
-               BT_LOGE("Cannot create enumeration field type mapping iterator: "
+               BT_LOGW("Cannot create enumeration field type mapping iterator: "
                        "ft-addr=%p, value=%" PRIu64, type, value);
                goto error;
        }
@@ -1509,7 +1486,7 @@ int bt_field_type_enumeration_get_mapping_signed(
 
        if (mapping_name) {
                *mapping_name = g_quark_to_string(mapping->string);
-               assert(*mapping_name);
+               BT_ASSERT(*mapping_name);
        }
 
        if (range_begin) {
@@ -1547,7 +1524,7 @@ int bt_field_type_enumeration_get_mapping_unsigned(
 
        if (mapping_name) {
                *mapping_name = g_quark_to_string(mapping->string);
-               assert(*mapping_name);
+               BT_ASSERT(*mapping_name);
        }
 
        if (range_begin) {
@@ -2030,6 +2007,35 @@ error:
        return NULL;
 }
 
+BT_HIDDEN
+int bt_field_type_structure_replace_field(struct bt_field_type *type,
+               const char *field_name, struct bt_field_type *field_type)
+{
+       int ret = 0;
+       struct bt_field_type_structure *structure;
+       GQuark name_quark;
+       uint64_t i;
+
+       BT_ASSERT(type);
+       BT_ASSERT(field_name);
+       BT_ASSERT(field_type);
+       BT_ASSERT(type->id == BT_FIELD_TYPE_ID_STRUCT);
+       structure = container_of(type, struct bt_field_type_structure, parent);
+       name_quark = g_quark_from_string(field_name);
+
+       for (i = 0; i < structure->fields->len; i++) {
+               struct structure_field *field = g_ptr_array_index(
+                       structure->fields, i);
+
+               if (field->name == name_quark) {
+                       bt_put(field->type);
+                       field->type = bt_get(field_type);
+               }
+       }
+
+       return ret;
+}
+
 int bt_field_type_structure_add_field(struct bt_field_type *type,
                struct bt_field_type *field_type,
                const char *field_name)
@@ -2160,7 +2166,7 @@ int bt_field_type_structure_get_field_by_index(
        }
        if (field_name) {
                *field_name = g_quark_to_string(field->name);
-               assert(*field_name);
+               BT_ASSERT(*field_name);
        }
 end:
        return ret;
@@ -2220,7 +2226,7 @@ struct bt_field_type *bt_field_type_variant_create(
                "tag-ft-addr=%p, tag-field-name=\"%s\"",
                enum_tag, tag_name);
 
-       if (tag_name && bt_identifier_is_valid(tag_name)) {
+       if (tag_name && !bt_identifier_is_valid(tag_name)) {
                BT_LOGW("Invalid parameter: tag field name is not a valid CTF identifier: "
                        "tag-ft-addr=%p, tag-field-name=\"%s\"",
                        enum_tag, tag_name);
@@ -2343,7 +2349,7 @@ int bt_field_type_variant_set_tag_name(
                goto end;
        }
 
-       if (bt_identifier_is_valid(name)) {
+       if (!bt_identifier_is_valid(name)) {
                BT_LOGW("Invalid parameter: tag field name is not a valid CTF identifier: "
                        "variant-ft-addr=%p, tag-field-name=\"%s\"",
                        type, name);
@@ -2520,7 +2526,7 @@ struct bt_field_type *bt_field_type_variant_get_field_type_from_tag(
        iter = bt_field_enumeration_get_mappings(tag);
        ret = bt_field_type_enumeration_mapping_iterator_next(iter);
        if (!iter || ret) {
-               BT_LOGE("Cannot get enumeration field type mapping iterator from enumeration field: "
+               BT_LOGW("Cannot get enumeration field type mapping iterator from enumeration field: "
                        "enum-field-addr=%p", tag);
                goto end;
        }
@@ -2606,7 +2612,7 @@ int bt_field_type_variant_get_field_by_index(struct bt_field_type *type,
        }
        if (field_name) {
                *field_name = g_quark_to_string(field->name);
-               assert(*field_name);
+               BT_ASSERT(*field_name);
        }
 end:
        return ret;
@@ -2756,7 +2762,7 @@ struct bt_field_type *bt_field_type_sequence_create(
                goto error;
        }
 
-       if (bt_identifier_is_valid(length_field_name)) {
+       if (!bt_identifier_is_valid(length_field_name)) {
                BT_LOGW("Invalid parameter: length field name is not a valid CTF identifier: "
                        "length-field-name=\"%s\"", length_field_name);
                goto error;
@@ -2977,7 +2983,7 @@ int bt_field_type_get_alignment(struct bt_field_type *type)
                struct bt_field_type *element =
                        bt_field_type_sequence_get_element_type(type);
 
-               assert(element);
+               BT_ASSERT(element);
                ret = bt_field_type_get_alignment(element);
                bt_put(element);
                break;
@@ -2987,7 +2993,7 @@ int bt_field_type_get_alignment(struct bt_field_type *type)
                struct bt_field_type *element =
                        bt_field_type_array_get_element_type(type);
 
-               assert(element);
+               BT_ASSERT(element);
                ret = bt_field_type_get_alignment(element);
                bt_put(element);
                break;
@@ -2998,16 +3004,16 @@ int bt_field_type_get_alignment(struct bt_field_type *type)
 
                element_count = bt_field_type_structure_get_field_count(
                        type);
-               assert(element_count >= 0);
+               BT_ASSERT(element_count >= 0);
 
                for (i = 0; i < element_count; i++) {
-                       struct bt_field_type *field;
+                       struct bt_field_type *field = NULL;
                        int field_alignment;
 
                        ret = bt_field_type_structure_get_field_by_index(
                                type, NULL, &field, i);
-                       assert(ret == 0);
-                       assert(field);
+                       BT_ASSERT(ret == 0);
+                       BT_ASSERT(field);
                        field_alignment = bt_field_type_get_alignment(
                                field);
                        bt_put(field);
@@ -3021,6 +3027,15 @@ int bt_field_type_get_alignment(struct bt_field_type *type)
                ret = (int) type->alignment;
                break;
        }
+       case BT_FIELD_TYPE_ID_ENUM:
+       {
+               struct bt_field_type *container =
+                       bt_field_type_enumeration_get_container_type(type);
+
+               ret = bt_field_type_get_alignment(container);
+               bt_put(container);
+               break;
+       }
        case BT_FIELD_TYPE_ID_UNKNOWN:
                BT_LOGW("Invalid parameter: unknown field type ID: "
                        "addr=%p, ft-id=%d", type, type_id);
@@ -3143,7 +3158,7 @@ enum bt_byte_order bt_field_type_get_byte_order(
                goto end;
        }
 
-       assert(ret == BT_BYTE_ORDER_NATIVE ||
+       BT_ASSERT(ret == BT_BYTE_ORDER_NATIVE ||
                ret == BT_BYTE_ORDER_LITTLE_ENDIAN ||
                ret == BT_BYTE_ORDER_BIG_ENDIAN ||
                ret == BT_BYTE_ORDER_NETWORK);
@@ -3256,7 +3271,7 @@ void bt_ctf_field_type_put(struct bt_field_type *type)
 }
 
 BT_HIDDEN
-void bt_field_type_freeze(struct bt_field_type *type)
+void _bt_field_type_freeze(struct bt_field_type *type)
 {
        if (!type || type->frozen) {
                return;
@@ -3340,8 +3355,8 @@ int bt_field_type_serialize(struct bt_field_type *type,
 {
        int ret;
 
-       assert(type);
-       assert(context);
+       BT_ASSERT(type);
+       BT_ASSERT(context);
 
        /* Make sure field type is valid before serializing it */
        ret = bt_field_type_validate(type);
@@ -3749,6 +3764,7 @@ void bt_field_type_enumeration_freeze(struct bt_field_type *type)
                type, struct bt_field_type_enumeration, parent);
 
        BT_LOGD("Freezing enumeration field type object: addr=%p", type);
+       type->alignment = bt_field_type_get_alignment(type);
        set_enumeration_range_overlap(type);
        generic_field_type_freeze(type);
        BT_LOGD("Freezing enumeration field type object's container field type: int-ft-addr=%p",
@@ -3877,7 +3893,7 @@ void append_field_name(struct metadata_context *context,
 {
        g_string_append_c(context->string, ' ');
 
-       if (bt_identifier_is_valid(name) || *name == '_') {
+       if (!bt_identifier_is_valid(name) || *name == '_') {
                g_string_append_c(context->string, '_');
        }
 
@@ -3905,7 +3921,7 @@ int bt_field_type_integer_serialize(struct bt_field_type *type,
                const char *clock_name = bt_clock_class_get_name(
                        integer->mapped_clock);
 
-               assert(clock_name);
+               BT_ASSERT(clock_name);
                g_string_append_printf(context->string,
                        "; map = clock.%s.value", clock_name);
        }
@@ -3928,9 +3944,9 @@ int bt_field_type_enumeration_serialize(struct bt_field_type *type,
        BT_LOGD("Serializing enumeration field type's metadata: "
                "ft-addr=%p, metadata-context-addr=%p", type, context);
        container_type = bt_field_type_enumeration_get_container_type(type);
-       assert(container_type);
+       BT_ASSERT(container_type);
        container_signed = bt_ctf_field_type_integer_get_signed(container_type);
-       assert(container_signed >= 0);
+       BT_ASSERT(container_signed >= 0);
        g_string_append(context->string, "enum : ");
        BT_LOGD_STR("Serializing enumeration field type's container field type's metadata.");
        ret = bt_field_type_serialize(enumeration->container, context);
@@ -3944,18 +3960,25 @@ int bt_field_type_enumeration_serialize(struct bt_field_type *type,
        for (entry = 0; entry < enumeration->entries->len; entry++) {
                struct enumeration_mapping *mapping =
                        enumeration->entries->pdata[entry];
+               const char *label = g_quark_to_string(mapping->string);
+
+               g_string_append(context->string, "\"");
+
+               if (!bt_identifier_is_valid(label) || label[0] == '_') {
+                       g_string_append(context->string, "_");
+               }
+
+               g_string_append_printf(context->string, "%s\" = ", label);
 
                if (container_signed) {
                        if (mapping->range_start._signed ==
                                mapping->range_end._signed) {
                                g_string_append_printf(context->string,
-                                       "\"%s\" = %" PRId64,
-                                       g_quark_to_string(mapping->string),
+                                       "%" PRId64,
                                        mapping->range_start._signed);
                        } else {
                                g_string_append_printf(context->string,
-                                       "\"%s\" = %" PRId64 " ... %" PRId64,
-                                       g_quark_to_string(mapping->string),
+                                       "%" PRId64 " ... %" PRId64,
                                        mapping->range_start._signed,
                                        mapping->range_end._signed);
                        }
@@ -3963,13 +3986,11 @@ int bt_field_type_enumeration_serialize(struct bt_field_type *type,
                        if (mapping->range_start._unsigned ==
                                mapping->range_end._unsigned) {
                                g_string_append_printf(context->string,
-                                       "\"%s\" = %" PRIu64,
-                                       g_quark_to_string(mapping->string),
+                                       "%" PRIu64,
                                        mapping->range_start._unsigned);
                        } else {
                                g_string_append_printf(context->string,
-                                       "\"%s\" = %" PRIu64 " ... %" PRIu64,
-                                       g_quark_to_string(mapping->string),
+                                       "%" PRIu64 " ... %" PRIu64,
                                        mapping->range_start._unsigned,
                                        mapping->range_end._unsigned);
                        }
@@ -4752,17 +4773,26 @@ int bt_field_type_integer_compare(struct bt_field_type *type_a,
                goto end;
        }
 
-       /* Mapped clock */
-       if (int_type_a->mapped_clock != int_type_b->mapped_clock) {
-               BT_LOGV("Integer field types differ: different mapped clock classes: "
-                       "ft-a-mapped-clock-class-addr=%p, "
-                       "ft-b-mapped-clock-class-addr=%p, "
-                       "ft-a-mapped-clock-class-name=\"%s\", "
-                       "ft-b-mapped-clock-class-name=\"%s\"",
-                       int_type_a->mapped_clock, int_type_b->mapped_clock,
-                       int_type_a->mapped_clock ? bt_clock_class_get_name(int_type_a->mapped_clock) : "",
-                       int_type_b->mapped_clock ? bt_clock_class_get_name(int_type_b->mapped_clock) : "");
-               goto end;
+       /* Mapped clock class */
+       if (int_type_a->mapped_clock) {
+               if (!int_type_b->mapped_clock) {
+                       BT_LOGV_STR("Integer field types differ: field type A "
+                               "has a mapped clock class, but field type B "
+                               "does not.");
+                       goto end;
+               }
+
+               if (bt_clock_class_compare(int_type_a->mapped_clock,
+                               int_type_b->mapped_clock) != 0) {
+                       BT_LOGV_STR("Integer field types differ: different "
+                               "mapped clock classes.");
+               }
+       } else {
+               if (int_type_b->mapped_clock) {
+                       BT_LOGV_STR("Integer field types differ: field type A "
+                               "has no description, but field type B has one.");
+                       goto end;
+               }
        }
 
        /* Equal */
This page took 0.031631 seconds and 4 git commands to generate.