• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tudasc / TypeART / 19336558931

13 Nov 2025 03:24PM UTC coverage: 90.337% (+1.4%) from 88.924%
19336558931

push

github

web-flow
Global variables as type descriptors (#173)

597 of 629 new or added lines in 25 files covered. (94.91%)

2 existing lines in 2 files now uncovered.

4768 of 5278 relevant lines covered (90.34%)

208224.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.75
/lib/runtime/GlobalTypeDefCallbacks.cpp
1
#include "GlobalTypeDefCallbacks.h"
2

3
#include "CallbackInterface.h"
4
#include "Runtime.h"
5
#include "RuntimeData.h"
6
#include "TypeInterface.h"
7
#include "support/Logger.h"
8
#include "typelib/TypeDatabase.h"
9

10
#include <cassert>
11
#include <cstdint>
12
#include <sys/types.h>
13
#include <vector>
14

15
namespace typeart {
16

17
#define unlikely(x)   __builtin_expect(!!(x), 0)
18
#define CONCAT_(x, y) x##y
19
#define CONCAT(x, y)  CONCAT_(x, y)
20
#define GUARDNAME     CONCAT(typeart_guard_, __LINE__)
21
#define TYPEART_RUNTIME_GUARD     \
22
  typeart::RTGuard GUARDNAME;     \
23
  if (!GUARDNAME.shouldTrack()) { \
24
    return;                       \
25
  }
26

27
class GlobalTypeTranslator::Impl {
28
  TypeDatabase& type_db_;
29
  RuntimeT::TypeLookupMapT& translator_map_;
30
  int struct_count{0};
818✔
31

32
 public:
33
  explicit Impl(TypeDatabase& db, RuntimeT::TypeLookupMapT& translator_map)
818✔
34
      : type_db_(db), translator_map_(translator_map) {
818✔
35
  }
818✔
36

37
  int next_type_id(const GlobalTypeInfo* type) {
908✔
38
    // a fwd_decl and the decl must have the same type_id:
39
    {
40
      const auto& struct_list = type_db_.getStructList();
908✔
41
      for (const auto& type_in_db : struct_list) {
9,216✔
42
        if (type_in_db.name == type->name) {
8,308✔
43
          return type_in_db.type_id;
8✔
44
        }
45
      }
8,308✔
46
    }
908✔
47
    const int id = static_cast<int>(TYPEART_NUM_RESERVED_IDS) + struct_count;
900✔
48
    ++struct_count;
900✔
49
    return id;
900✔
50
  }
908✔
51

52
  int register_t(const GlobalTypeInfo* type) {  // NOLINT(misc-no-recursion)
3,432✔
53
    if (unlikely(type == nullptr)) {
3,432✔
NEW
54
      LOG_ERROR("Type descriptor is NULL, is it a weak extern global due to fwd decl?");
×
NEW
55
      return TYPEART_UNKNOWN_TYPE;
×
56
    }
57

58
    if (auto element = translator_map_.find(type); element != translator_map_.end()) {
5,452✔
59
      return element->second;
2,020✔
60
    }
61

62
    const bool built_in = builtins::BuiltInQuery::is_builtin_type(type->type_id);
1,412✔
63
    if (built_in) {
1,412✔
64
      translator_map_.try_emplace(type, type->type_id);
504✔
65
      return type->type_id;
504✔
66
    }
67

68
    StructTypeInfo type_descriptor;
908✔
69
    type_descriptor.type_id     = next_type_id(type);
908✔
70
    type_descriptor.name        = type->name;
908✔
71
    type_descriptor.extent      = type->extent;
908✔
72
    type_descriptor.num_members = type->num_members;
908✔
73
    type_descriptor.flag        = static_cast<StructTypeFlag>(type->flag);
908✔
74

420✔
75
    type_descriptor.array_sizes.reserve(type->num_members);
908✔
76
    type_descriptor.offsets.reserve(type->num_members);
908✔
77
    type_descriptor.member_types.reserve(type->num_members);
908✔
78
    for (uint32_t i = 0; i < type->num_members; ++i) {
2,922✔
79
      const auto member_id  = register_t(type->member_types[i]);
2,014✔
80
      const auto array_size = type->array_sizes[i];
2,014✔
81
      const auto offset     = type->offsets[i];
2,014✔
82
      type_descriptor.array_sizes.emplace_back(array_size);
2,014✔
83
      type_descriptor.offsets.emplace_back(offset);
2,014✔
84
      type_descriptor.member_types.emplace_back(member_id);
2,014✔
85
    }
2,014✔
86

87
    const bool fwd_decl = type_descriptor.flag == StructTypeFlag::FWD_DECL;
908✔
88
    type_db_.registerStruct(type_descriptor, not fwd_decl);
908✔
89
    translator_map_.try_emplace(type, type_descriptor.type_id);
908✔
90

91
    return type_descriptor.type_id;
908✔
92
  }
3,432✔
93
};
94

95
GlobalTypeTranslator::GlobalTypeTranslator(TypeDatabase& db) : pImpl(std::make_unique<Impl>(db, translator_map)) {
818✔
96
}
818✔
97

98
GlobalTypeTranslator::~GlobalTypeTranslator() = default;
818✔
99

100
void GlobalTypeTranslator::register_type(const void* type) {
1,418✔
101
  const auto* info_struct = reinterpret_cast<const GlobalTypeInfo*>(type);
1,418✔
102
  const auto type_id      = pImpl->register_t(info_struct);
1,418✔
103
  LOG_DEBUG("Type id reset: " << info_struct->name << " " << info_struct->type_id << " vs. " << type_id)
1,418✔
104
  const_cast<GlobalTypeInfo*>(info_struct)->type_id = type_id;
1,418✔
105
}
1,418✔
106

107
}  // namespace typeart
108

109
void __typeart_register_type(const void* type_ptr) {
1,418✔
110
  TYPEART_RUNTIME_GUARD;
1,418✔
111
  if (unlikely(type_ptr == nullptr)) {
1,418✔
NEW
112
    LOG_FATAL("type_ptr is NULL\n");
×
NEW
113
    return;
×
114
  }
115
  typeart::RuntimeSystem::get().type_translator().register_type(type_ptr);
1,418✔
116
}
1,418✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc