Sort includes in C++ files
[babeltrace.git] / src / plugins / ctf / common / metadata / visitor-generate-ir.cpp
index 50d8d441fe7ed312a16b0ab99762950bc8a332b6..e9483a994a7058d8821d80bf49170167d374b252 100644 (file)
@@ -7,33 +7,37 @@
  * Common Trace Format metadata visitor (generates CTF IR objects).
  */
 
+#include <string>
+
+#include <ctype.h>
+#include <errno.h>
+#include <glib.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <babeltrace2/babeltrace.h>
+
 #define BT_COMP_LOG_SELF_COMP       (ctx->log_cfg.self_comp)
 #define BT_COMP_LOG_SELF_COMP_CLASS (ctx->log_cfg.self_comp_class)
 #define BT_LOG_OUTPUT_LEVEL         (ctx->log_cfg.log_level)
 #define BT_LOG_TAG                  "PLUGIN/CTF/META/IR-VISITOR"
+#include "logging.hpp"
 #include "logging/comp-logging.h"
 
-#include <stdio.h>
-#include <unistd.h>
-#include <string.h>
-#include <stdbool.h>
-#include <stdlib.h>
-#include <ctype.h>
 #include "common/assert.h"
-#include <glib.h>
-#include <inttypes.h>
-#include <errno.h>
 #include "common/common.h"
 #include "common/uuid.h"
 #include "compat/endian.h"
-#include <babeltrace2/babeltrace.h>
 
-#include "logging.hpp"
-#include "scanner.hpp"
 #include "ast.hpp"
-#include "decoder.hpp"
-#include "ctf-meta.hpp"
 #include "ctf-meta-visitors.hpp"
+#include "ctf-meta.hpp"
+#include "decoder.hpp"
+#include "scanner.hpp"
 
 /* Bit value (left shift) */
 #define _BV(_val) (1 << (_val))
@@ -262,26 +266,11 @@ end:
  * @param name         Name
  * @returns            Associated GQuark, or 0 on error
  */
-static GQuark get_prefixed_named_quark(struct ctf_visitor_generate_ir *ctx, char prefix,
-                                       const char *name)
+static GQuark get_prefixed_named_quark(char prefix, const char *name)
 {
-    GQuark qname = 0;
-
     BT_ASSERT(name);
-
-    /* Prefix character + original string + '\0' */
-    char *prname = g_new(char, strlen(name) + 2);
-    if (!prname) {
-        _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Failed to allocate a string.");
-        goto end;
-    }
-
-    sprintf(prname, "%c%s", prefix, name);
-    qname = g_quark_from_string(prname);
-    g_free(prname);
-
-end:
-    return qname;
+    std::string prname = std::string {prefix} + name;
+    return g_quark_from_string(prname.c_str());
 }
 
 /**
@@ -295,10 +284,9 @@ end:
  * @returns            Declaration (owned by caller if \p copy is true),
  *                     or NULL if not found
  */
-static struct ctf_field_class *
-ctx_decl_scope_lookup_prefix_alias(struct ctf_visitor_generate_ir *ctx,
-                                   struct ctx_decl_scope *scope, char prefix, const char *name,
-                                   int levels, bool copy)
+static struct ctf_field_class *ctx_decl_scope_lookup_prefix_alias(struct ctx_decl_scope *scope,
+                                                                  char prefix, const char *name,
+                                                                  int levels, bool copy)
 {
     GQuark qname = 0;
     int cur_levels = 0;
@@ -307,7 +295,7 @@ ctx_decl_scope_lookup_prefix_alias(struct ctf_visitor_generate_ir *ctx,
 
     BT_ASSERT(scope);
     BT_ASSERT(name);
-    qname = get_prefixed_named_quark(ctx, prefix, name);
+    qname = get_prefixed_named_quark(prefix, name);
     if (!qname) {
         goto end;
     }
@@ -347,11 +335,10 @@ end:
  * @returns            Declaration (owned by caller if \p copy is true),
  *                     or NULL if not found
  */
-static struct ctf_field_class *ctx_decl_scope_lookup_alias(struct ctf_visitor_generate_ir *ctx,
-                                                           struct ctx_decl_scope *scope,
+static struct ctf_field_class *ctx_decl_scope_lookup_alias(struct ctx_decl_scope *scope,
                                                            const char *name, int levels, bool copy)
 {
-    return ctx_decl_scope_lookup_prefix_alias(ctx, scope, _PREFIX_ALIAS, name, levels, copy);
+    return ctx_decl_scope_lookup_prefix_alias(scope, _PREFIX_ALIAS, name, levels, copy);
 }
 
 /**
@@ -364,13 +351,11 @@ static struct ctf_field_class *ctx_decl_scope_lookup_alias(struct ctf_visitor_ge
  * @returns            Declaration (owned by caller if \p copy is true),
  *                     or NULL if not found
  */
-static struct ctf_field_class_enum *ctx_decl_scope_lookup_enum(struct ctf_visitor_generate_ir *ctx,
-                                                               struct ctx_decl_scope *scope,
-                                                               const char *name, int levels,
-                                                               bool copy)
+static struct ctf_field_class_enum *
+ctx_decl_scope_lookup_enum(struct ctx_decl_scope *scope, const char *name, int levels, bool copy)
 {
     return ctf_field_class_as_enum(
-        ctx_decl_scope_lookup_prefix_alias(ctx, scope, _PREFIX_ENUM, name, levels, copy));
+        ctx_decl_scope_lookup_prefix_alias(scope, _PREFIX_ENUM, name, levels, copy));
 }
 
 /**
@@ -384,11 +369,10 @@ static struct ctf_field_class_enum *ctx_decl_scope_lookup_enum(struct ctf_visito
  *                     or NULL if not found
  */
 static struct ctf_field_class_struct *
-ctx_decl_scope_lookup_struct(struct ctf_visitor_generate_ir *ctx, struct ctx_decl_scope *scope,
-                             const char *name, int levels, bool copy)
+ctx_decl_scope_lookup_struct(struct ctx_decl_scope *scope, const char *name, int levels, bool copy)
 {
     return ctf_field_class_as_struct(
-        ctx_decl_scope_lookup_prefix_alias(ctx, scope, _PREFIX_STRUCT, name, levels, copy));
+        ctx_decl_scope_lookup_prefix_alias(scope, _PREFIX_STRUCT, name, levels, copy));
 }
 
 /**
@@ -402,11 +386,10 @@ ctx_decl_scope_lookup_struct(struct ctf_visitor_generate_ir *ctx, struct ctx_dec
  *                     or NULL if not found
  */
 static struct ctf_field_class_variant *
-ctx_decl_scope_lookup_variant(struct ctf_visitor_generate_ir *ctx, struct ctx_decl_scope *scope,
-                              const char *name, int levels, bool copy)
+ctx_decl_scope_lookup_variant(struct ctx_decl_scope *scope, const char *name, int levels, bool copy)
 {
     return ctf_field_class_as_variant(
-        ctx_decl_scope_lookup_prefix_alias(ctx, scope, _PREFIX_VARIANT, name, levels, copy));
+        ctx_decl_scope_lookup_prefix_alias(scope, _PREFIX_VARIANT, name, levels, copy));
 }
 
 /**
@@ -418,8 +401,7 @@ ctx_decl_scope_lookup_variant(struct ctf_visitor_generate_ir *ctx, struct ctx_de
  * @param decl         Field class to register (copied)
  * @returns            0 if registration went okay, negative value otherwise
  */
-static int ctx_decl_scope_register_prefix_alias(struct ctf_visitor_generate_ir *ctx,
-                                                struct ctx_decl_scope *scope, char prefix,
+static int ctx_decl_scope_register_prefix_alias(struct ctx_decl_scope *scope, char prefix,
                                                 const char *name, struct ctf_field_class *decl)
 {
     int ret = 0;
@@ -428,14 +410,14 @@ static int ctx_decl_scope_register_prefix_alias(struct ctf_visitor_generate_ir *
     BT_ASSERT(scope);
     BT_ASSERT(name);
     BT_ASSERT(decl);
-    qname = get_prefixed_named_quark(ctx, prefix, name);
+    qname = get_prefixed_named_quark(prefix, name);
     if (!qname) {
         ret = -ENOMEM;
         goto end;
     }
 
     /* Make sure alias does not exist in local scope */
-    if (ctx_decl_scope_lookup_prefix_alias(ctx, scope, prefix, name, 1, false)) {
+    if (ctx_decl_scope_lookup_prefix_alias(scope, prefix, name, 1, false)) {
         ret = -EEXIST;
         goto end;
     }
@@ -456,11 +438,10 @@ end:
  * @param decl Field class to register (copied)
  * @returns    0 if registration went okay, negative value otherwise
  */
-static int ctx_decl_scope_register_alias(struct ctf_visitor_generate_ir *ctx,
-                                         struct ctx_decl_scope *scope, const char *name,
+static int ctx_decl_scope_register_alias(struct ctx_decl_scope *scope, const char *name,
                                          struct ctf_field_class *decl)
 {
-    return ctx_decl_scope_register_prefix_alias(ctx, scope, _PREFIX_ALIAS, name, decl);
+    return ctx_decl_scope_register_prefix_alias(scope, _PREFIX_ALIAS, name, decl);
 }
 
 /**
@@ -471,12 +452,10 @@ static int ctx_decl_scope_register_alias(struct ctf_visitor_generate_ir *ctx,
  * @param decl Enumeration field class to register (copied)
  * @returns    0 if registration went okay, negative value otherwise
  */
-static int ctx_decl_scope_register_enum(struct ctf_visitor_generate_ir *ctx,
-                                        struct ctx_decl_scope *scope, const char *name,
+static int ctx_decl_scope_register_enum(struct ctx_decl_scope *scope, const char *name,
                                         struct ctf_field_class_enum *decl)
 {
-    return ctx_decl_scope_register_prefix_alias(ctx, scope, _PREFIX_ENUM, name,
-                                                &decl->base.base.base);
+    return ctx_decl_scope_register_prefix_alias(scope, _PREFIX_ENUM, name, &decl->base.base.base);
 }
 
 /**
@@ -487,11 +466,10 @@ static int ctx_decl_scope_register_enum(struct ctf_visitor_generate_ir *ctx,
  * @param decl Structure field class to register (copied)
  * @returns    0 if registration went okay, negative value otherwise
  */
-static int ctx_decl_scope_register_struct(struct ctf_visitor_generate_ir *ctx,
-                                          struct ctx_decl_scope *scope, const char *name,
+static int ctx_decl_scope_register_struct(struct ctx_decl_scope *scope, const char *name,
                                           struct ctf_field_class_struct *decl)
 {
-    return ctx_decl_scope_register_prefix_alias(ctx, scope, _PREFIX_STRUCT, name, &decl->base);
+    return ctx_decl_scope_register_prefix_alias(scope, _PREFIX_STRUCT, name, &decl->base);
 }
 
 /**
@@ -502,11 +480,10 @@ static int ctx_decl_scope_register_struct(struct ctf_visitor_generate_ir *ctx,
  * @param decl Variant field class to register
  * @returns    0 if registration went okay, negative value otherwise
  */
-static int ctx_decl_scope_register_variant(struct ctf_visitor_generate_ir *ctx,
-                                           struct ctx_decl_scope *scope, const char *name,
+static int ctx_decl_scope_register_variant(struct ctx_decl_scope *scope, const char *name,
                                            struct ctf_field_class_variant *decl)
 {
-    return ctx_decl_scope_register_prefix_alias(ctx, scope, _PREFIX_VARIANT, name, &decl->base);
+    return ctx_decl_scope_register_prefix_alias(scope, _PREFIX_VARIANT, name, &decl->base);
 }
 
 /**
@@ -1140,10 +1117,8 @@ static int visit_field_class_declarator(struct ctf_visitor_generate_ir *ctx,
 
     /* Find the right nested declaration if not provided */
     if (!nested_decl) {
-        struct bt_list_head *pointers =
-            &node_field_class_declarator->u.field_class_declarator.pointers;
-
-        if (node_field_class_declarator && !bt_list_empty(pointers)) {
+        if (node_field_class_declarator &&
+            !bt_list_empty(&node_field_class_declarator->u.field_class_declarator.pointers)) {
             GQuark qalias;
 
             /*
@@ -1153,8 +1128,8 @@ static int visit_field_class_declarator(struct ctf_visitor_generate_ir *ctx,
              */
             qalias =
                 create_class_alias_identifier(ctx, cls_specifier_list, node_field_class_declarator);
-            nested_decl = ctx_decl_scope_lookup_alias(ctx, ctx->current_scope,
-                                                      g_quark_to_string(qalias), -1, true);
+            nested_decl = ctx_decl_scope_lookup_alias(ctx->current_scope, g_quark_to_string(qalias),
+                                                      -1, true);
             if (!nested_decl) {
                 _BT_COMP_LOGE_APPEND_CAUSE_NODE(node_field_class_declarator,
                                                 "Cannot find class alias: name=\"%s\"",
@@ -1479,7 +1454,7 @@ static int visit_field_class_def(struct ctf_visitor_generate_ir *ctx,
             }
         }
 
-        ret = ctx_decl_scope_register_alias(ctx, ctx->current_scope, g_quark_to_string(qidentifier),
+        ret = ctx_decl_scope_register_alias(ctx->current_scope, g_quark_to_string(qidentifier),
                                             class_decl);
         if (ret) {
             _BT_COMP_LOGE_APPEND_CAUSE_NODE(iter, "Cannot register field class alias: name=\"%s\"",
@@ -1548,8 +1523,7 @@ static int visit_field_class_alias(struct ctf_visitor_generate_ir *ctx, struct c
                                 struct ctf_node, siblings);
     qalias = create_class_alias_identifier(
         ctx, alias->u.field_class_alias_name.field_class_specifier_list, node);
-    ret = ctx_decl_scope_register_alias(ctx, ctx->current_scope, g_quark_to_string(qalias),
-                                        class_decl);
+    ret = ctx_decl_scope_register_alias(ctx->current_scope, g_quark_to_string(qalias), class_decl);
     if (ret) {
         _BT_COMP_LOGE_APPEND_CAUSE_NODE(node, "Cannot register class alias: name=\"%s\"",
                                         g_quark_to_string(qalias));
@@ -1674,7 +1648,7 @@ static int visit_struct_decl(struct ctf_visitor_generate_ir *ctx, const char *na
             goto error;
         }
 
-        *struct_decl = ctx_decl_scope_lookup_struct(ctx, ctx->current_scope, name, -1, true);
+        *struct_decl = ctx_decl_scope_lookup_struct(ctx->current_scope, name, -1, true);
         if (!*struct_decl) {
             _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
                 "Cannot find structure field class: name=\"struct %s\"", name);
@@ -1686,7 +1660,7 @@ static int visit_struct_decl(struct ctf_visitor_generate_ir *ctx, const char *na
         uint64_t min_align_value = 0;
 
         if (name) {
-            if (ctx_decl_scope_lookup_struct(ctx, ctx->current_scope, name, 1, false)) {
+            if (ctx_decl_scope_lookup_struct(ctx->current_scope, name, 1, false)) {
                 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
                     "Structure field class already declared in local scope: "
                     "name=\"struct %s\"",
@@ -1731,7 +1705,7 @@ static int visit_struct_decl(struct ctf_visitor_generate_ir *ctx, const char *na
         ctx_pop_scope(ctx);
 
         if (name) {
-            ret = ctx_decl_scope_register_struct(ctx, ctx->current_scope, name, *struct_decl);
+            ret = ctx_decl_scope_register_struct(ctx->current_scope, name, *struct_decl);
             if (ret) {
                 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
                     "Cannot register structure field class in declaration scope: "
@@ -1768,8 +1742,7 @@ static int visit_variant_decl(struct ctf_visitor_generate_ir *ctx, const char *n
             goto error;
         }
 
-        untagged_variant_decl =
-            ctx_decl_scope_lookup_variant(ctx, ctx->current_scope, name, -1, true);
+        untagged_variant_decl = ctx_decl_scope_lookup_variant(ctx->current_scope, name, -1, true);
         if (!untagged_variant_decl) {
             _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
                 "Cannot find variant field class: name=\"variant %s\"", name);
@@ -1780,7 +1753,7 @@ static int visit_variant_decl(struct ctf_visitor_generate_ir *ctx, const char *n
         struct ctf_node *entry_node;
 
         if (name) {
-            if (ctx_decl_scope_lookup_variant(ctx, ctx->current_scope, name, 1, false)) {
+            if (ctx_decl_scope_lookup_variant(ctx->current_scope, name, 1, false)) {
                 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
                     "Variant field class already declared in local scope: "
                     "name=\"variant %s\"",
@@ -1809,8 +1782,7 @@ static int visit_variant_decl(struct ctf_visitor_generate_ir *ctx, const char *n
         ctx_pop_scope(ctx);
 
         if (name) {
-            ret = ctx_decl_scope_register_variant(ctx, ctx->current_scope, name,
-                                                  untagged_variant_decl);
+            ret = ctx_decl_scope_register_variant(ctx->current_scope, name, untagged_variant_decl);
             if (ret) {
                 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
                     "Cannot register variant field class in declaration scope: "
@@ -1971,7 +1943,7 @@ static int visit_enum_decl(struct ctf_visitor_generate_ir *ctx, const char *name
             goto error;
         }
 
-        *enum_decl = ctx_decl_scope_lookup_enum(ctx, ctx->current_scope, name, -1, true);
+        *enum_decl = ctx_decl_scope_lookup_enum(ctx->current_scope, name, -1, true);
         if (!*enum_decl) {
             _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot find enumeration field class: "
                                                      "name=\"enum %s\"",
@@ -1990,7 +1962,7 @@ static int visit_enum_decl(struct ctf_visitor_generate_ir *ctx, const char *name
         };
 
         if (name) {
-            if (ctx_decl_scope_lookup_enum(ctx, ctx->current_scope, name, 1, false)) {
+            if (ctx_decl_scope_lookup_enum(ctx->current_scope, name, 1, false)) {
                 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
                     "Enumeration field class already declared in local scope: "
                     "name=\"enum %s\"",
@@ -2002,7 +1974,7 @@ static int visit_enum_decl(struct ctf_visitor_generate_ir *ctx, const char *name
 
         if (!container_cls) {
             integer_decl = ctf_field_class_as_int(
-                ctx_decl_scope_lookup_alias(ctx, ctx->current_scope, "int", -1, true));
+                ctx_decl_scope_lookup_alias(ctx->current_scope, "int", -1, true));
             if (!integer_decl) {
                 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
                     "Cannot find implicit `int` field class alias for enumeration field class.");
@@ -2051,7 +2023,7 @@ static int visit_enum_decl(struct ctf_visitor_generate_ir *ctx, const char *name
         }
 
         if (name) {
-            ret = ctx_decl_scope_register_enum(ctx, ctx->current_scope, name, *enum_decl);
+            ret = ctx_decl_scope_register_enum(ctx->current_scope, name, *enum_decl);
             if (ret) {
                 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
                     "Cannot register enumeration field class in declaration scope: "
@@ -2090,7 +2062,7 @@ static int visit_field_class_specifier(struct ctf_visitor_generate_ir *ctx,
         goto error;
     }
 
-    *decl = ctx_decl_scope_lookup_alias(ctx, ctx->current_scope, str->str, -1, true);
+    *decl = ctx_decl_scope_lookup_alias(ctx->current_scope, str->str, -1, true);
     if (!*decl) {
         _BT_COMP_LOGE_APPEND_CAUSE_NODE(cls_specifier_list,
                                         "Cannot find field class alias: name=\"%s\"", str->str);
@@ -3313,21 +3285,21 @@ static int auto_map_field_to_trace_clock_class(struct ctf_visitor_generate_ir *c
                                                struct ctf_field_class *fc)
 {
     struct ctf_clock_class *clock_class_to_map_to = NULL;
-    struct ctf_field_class_int *int_fc = ctf_field_class_as_int(fc);
-    int ret = 0;
     uint64_t clock_class_count;
 
     if (!fc) {
-        goto end;
+        return 0;
     }
 
     if (fc->type != CTF_FIELD_CLASS_TYPE_INT && fc->type != CTF_FIELD_CLASS_TYPE_ENUM) {
-        goto end;
+        return 0;
     }
 
+    ctf_field_class_int *int_fc = ctf_field_class_as_int(fc);
+
     if (int_fc->mapped_clock_class) {
         /* Already mapped */
-        goto end;
+        return 0;
     }
 
     clock_class_count = ctx->ctf_tc->clock_classes->len;
@@ -3343,7 +3315,6 @@ static int auto_map_field_to_trace_clock_class(struct ctf_visitor_generate_ir *c
         BT_ASSERT(clock_class_to_map_to);
         clock_class_to_map_to->frequency = UINT64_C(1000000000);
         g_string_assign(clock_class_to_map_to->name, "default");
-        BT_ASSERT(ret == 0);
         g_ptr_array_add(ctx->ctf_tc->clock_classes, clock_class_to_map_to);
         break;
     case 1:
@@ -3361,15 +3332,13 @@ static int auto_map_field_to_trace_clock_class(struct ctf_visitor_generate_ir *c
         _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
             "Timestamp field found with no mapped clock class, "
             "but there's more than one clock class in the trace at this point.");
-        ret = -1;
-        goto end;
+        return -1;
     }
 
     BT_ASSERT(clock_class_to_map_to);
     int_fc->mapped_clock_class = clock_class_to_map_to;
 
-end:
-    return ret;
+    return 0;
 }
 
 static int auto_map_fields_to_trace_clock_class(struct ctf_visitor_generate_ir *ctx,
@@ -4499,7 +4468,6 @@ end:
     return ret;
 }
 
-BT_HIDDEN
 struct ctf_visitor_generate_ir *
 ctf_visitor_generate_ir_create(const struct ctf_metadata_decoder_config *decoder_config)
 {
@@ -4523,13 +4491,11 @@ end:
     return ctx;
 }
 
-BT_HIDDEN
 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir *visitor)
 {
     ctx_destroy(visitor);
 }
 
-BT_HIDDEN
 bt_trace_class *ctf_visitor_generate_ir_get_ir_trace_class(struct ctf_visitor_generate_ir *ctx)
 {
     BT_ASSERT_DBG(ctx);
@@ -4541,7 +4507,6 @@ bt_trace_class *ctf_visitor_generate_ir_get_ir_trace_class(struct ctf_visitor_ge
     return ctx->trace_class;
 }
 
-BT_HIDDEN
 struct ctf_trace_class *
 ctf_visitor_generate_ir_borrow_ctf_trace_class(struct ctf_visitor_generate_ir *ctx)
 {
@@ -4550,7 +4515,6 @@ ctf_visitor_generate_ir_borrow_ctf_trace_class(struct ctf_visitor_generate_ir *c
     return ctx->ctf_tc;
 }
 
-BT_HIDDEN
 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir *ctx, struct ctf_node *node)
 {
     int ret = 0;
This page took 0.030344 seconds and 4 git commands to generate.