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

tudasc / TypeART / 23807897687

31 Mar 2026 04:21PM UTC coverage: 90.228% (-0.02%) from 90.245%
23807897687

Pull #184

github

web-flow
Merge ab63ddc36 into 8ae76f97d
Pull Request #184: Fix fwd declared types registering multiple times

28 of 28 new or added lines in 1 file covered. (100.0%)

2 existing lines in 2 files now uncovered.

4866 of 5393 relevant lines covered (90.23%)

33944.39 hits per line

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

78.35
/lib/typelib/TypeDB.cpp
1
// TypeART library
2
//
3
// Copyright (c) 2017-2025 TypeART Authors
4
// Distributed under the BSD 3-Clause license.
5
// (See accompanying file LICENSE.txt or copy at
6
// https://opensource.org/licenses/BSD-3-Clause)
7
//
8
// Project home: https://github.com/tudasc/TypeART
9
//
10
// SPDX-License-Identifier: BSD-3-Clause
11
//
12

13
#include "TypeDB.h"
14

15
#include "TypeIO.h"
16
#include "support/Logger.h"
17
#include "typelib/TypeInterface.h"
18

19
#include <ccomplex>
20
#include <complex>
21
#include <cstddef>
22
#include <iostream>
23
#include <string_view>
24
#include <utility>
25

26
namespace typeart {
27

28
namespace builtins {
29

30
namespace {
31
inline constexpr auto size_complex_float       = sizeof(std::complex<float>);
32
inline constexpr auto size_complex_double      = sizeof(std::complex<double>);
33
inline constexpr auto size_complex_long_double = sizeof(std::complex<long double>);
34
}  // namespace
35

36
#define FOR_EACH_TYPEART_BUILTIN(X)                                       \
37
  X(TYPEART_UNKNOWN_TYPE, "typeart_unknown_type", 0)                      \
38
  X(TYPEART_POINTER, "ptr", sizeof(void*))                                \
39
  X(TYPEART_VTABLE_POINTER, "vtable_ptr", sizeof(void*))                  \
40
  X(TYPEART_VOID, "void*", sizeof(void*))                                 \
41
  X(TYPEART_NULLPOINTER, "nullptr_t", sizeof(void*))                      \
42
  X(TYPEART_BOOL, "bool", sizeof(bool))                                   \
43
  X(TYPEART_CHAR_8, "char", sizeof(char))                                 \
44
  X(TYPEART_UCHAR_8, "unsigned char", sizeof(unsigned char))              \
45
  X(TYPEART_UTF_CHAR_8, "char8_t", sizeof(char))                          \
46
  X(TYPEART_UTF_CHAR_16, "char16_t", sizeof(char16_t))                    \
47
  X(TYPEART_UTF_CHAR_32, "char32_t", sizeof(char32_t))                    \
48
  X(TYPEART_INT_8, "int8_t", sizeof(int8_t))                              \
49
  X(TYPEART_INT_16, "short", sizeof(int16_t))                             \
50
  X(TYPEART_INT_32, "int", sizeof(int32_t))                               \
51
  X(TYPEART_INT_64, "long int", sizeof(int64_t))                          \
52
  X(TYPEART_INT_128, "int128_t", 16)                                      \
53
  X(TYPEART_UINT_8, "uint8_t", sizeof(uint8_t))                           \
54
  X(TYPEART_UINT_16, "unsigned short", sizeof(uint16_t))                  \
55
  X(TYPEART_UINT_32, "unsigned int", sizeof(uint32_t))                    \
56
  X(TYPEART_UINT_64, "unsigned long int", sizeof(uint64_t))               \
57
  X(TYPEART_UINT_128, "uint128_t", 16)                                    \
58
  X(TYPEART_FLOAT_8, "float8_t", 1)                                       \
59
  X(TYPEART_FLOAT_16, "float16_t", 2)                                     \
60
  X(TYPEART_FLOAT_32, "float", sizeof(float))                             \
61
  X(TYPEART_FLOAT_64, "double", sizeof(double))                           \
62
  X(TYPEART_FLOAT_128, "long double", sizeof(long double))                \
63
  X(TYPEART_COMPLEX_64, "float complex", size_complex_float)              \
64
  X(TYPEART_COMPLEX_128, "double complex", size_complex_double)           \
65
  X(TYPEART_COMPLEX_256, "long double complex", size_complex_long_double) \
66
  X(TYPEART_WCHAR, "wchar_t", sizeof(wchar_t))
67

68
#define TYPENAME(enum_name, str_name, size) std::string{str_name},
69
#define SIZE(enum_name, str_name, size)     (size),
70
BuiltInQuery::BuiltInQuery() : names{FOR_EACH_TYPEART_BUILTIN(TYPENAME)}, sizes{FOR_EACH_TYPEART_BUILTIN(SIZE)} {
6,140✔
71
}
6,140✔
72
#undef SIZE
73
#undef TYPENAME
74

1,970✔
75
}  // namespace builtins
76

77
std::pair<std::unique_ptr<TypeDatabase>, std::error_code> make_database(std::string_view file) {
×
78
  auto type_db = std::make_unique<TypeDB>();
×
79
  auto loaded  = io::load(type_db.get(), file.data());
×
80
  if (!loaded) {
×
81
    LOG_DEBUG("Database file not found: " << file)
82
  }
×
83
  return {std::move(type_db), loaded.getError()};
×
84
}
×
85

86
using namespace builtins;
87

88
const std::string unknown_struct_name{"typeart_unknown_struct"};
5,678✔
89

90
void TypeDB::clear() {
1,710✔
91
  struct_info_vec.clear();
1,710✔
92
  typeid_to_list_index.clear();
1,710✔
93
  // reverseTypeMap.clear();
94
}
1,710✔
95

96
bool TypeDB::isBuiltinType(int type_id) const {
1,782,914✔
97
  return BuiltInQuery::is_builtin_type(type_id);
1,782,914✔
98
}
99

100
bool TypeDB::isReservedType(int type_id) const {
726,982✔
101
  return BuiltInQuery::is_reserved_type(type_id);
726,982✔
102
}
103

104
bool TypeDB::isStructType(int type_id) const {
122,175✔
105
  return BuiltInQuery::is_userdef_type(type_id);
122,175✔
106
}
107

108
bool TypeDB::isUnknown(int type_id) const {
48✔
109
  return BuiltInQuery::is_unknown_type(type_id);
48✔
110
}
111

112
bool TypeDB::isPointerType(int type_id) const {
×
113
  return type_id == TYPEART_VOID || type_id == TYPEART_POINTER;
×
114
}
115

116
bool TypeDB::isUserDefinedType(int type_id) const {
502✔
117
  const auto* structInfo = getStructInfo(type_id);
502✔
118
  if (structInfo != nullptr) {
502✔
119
    LOG_DEBUG(structInfo->name << " " << static_cast<int>(structInfo->flag) << " "
120
                               << (static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::USER_DEFINED))
121
                               << " "
122
                               << (static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::UNION)))
123
  }
502✔
124
  return (structInfo != nullptr) &&
1,004✔
125
         (static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::USER_DEFINED) ||
502✔
126
          static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::UNION));
×
127
}
128

129
bool TypeDB::isVectorType(int type_id) const {
54✔
130
  const auto* structInfo = getStructInfo(type_id);
54✔
131
  LOG_DEBUG(structInfo->name << " " << static_cast<int>(structInfo->flag) << " "
132
                             << (static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::LLVM_VECTOR)))
133
  return (structInfo != nullptr) &&
108✔
134
         (static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::LLVM_VECTOR));
54✔
135
}
136

137
bool TypeDB::isUnion(int type_id) const {
×
138
  const auto* structInfo = getStructInfo(type_id);
×
139
  if (structInfo != nullptr) {
×
140
    LOG_DEBUG(structInfo->name << " " << static_cast<int>(structInfo->flag) << " "
141
                               << (static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::UNION)))
142
  }
×
143
  return (structInfo != nullptr) && (static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::UNION));
×
144
}
145

146
bool TypeDB::isValid(int type_id) const {
366,610✔
147
  if (isBuiltinType(type_id)) {
366,610✔
148
    return true;
307,842✔
149
  }
150
  return typeid_to_list_index.find(type_id) != typeid_to_list_index.end();
58,768✔
151
}
366,610✔
152

153
void TypeDB::registerStruct(const StructTypeInfo& struct_type, bool overwrite) {
13,220✔
154
  if (isValid(struct_type.type_id) || !isStructType(struct_type.type_id)) {
13,220✔
155
    if (isBuiltinType(struct_type.type_id)) {
48✔
156
      LOG_ERROR("Built-in type ID used for struct " << struct_type.name);
×
157
    } else if (isReservedType(struct_type.type_id)) {
48✔
158
      LOG_ERROR("Type ID is reserved for builtin types. Struct: " << struct_type.name);
×
159
    } else if (isUnknown(struct_type.type_id)) {
48✔
160
      LOG_ERROR("Type ID is reserved for unknown types. Struct: " << struct_type.name);
×
161
    } else {
×
162
      if (!overwrite) {
48✔
163
        LOG_DEBUG("Struct type ID already registered for " << struct_type.name << ". Conflicting struct is "
164
                                                           << getStructInfo(struct_type.type_id)->name);
UNCOV
165
        return;
×
166
      }
167
      LOG_DEBUG("Overwrite struct " << struct_type.name)
168
      auto& info = *getStructInfo(struct_type.type_id);
48✔
169
      info       = struct_type;
48✔
170
    }
171
    return;
48✔
172
  }
173

174
  struct_info_vec.push_back(struct_type);
13,172✔
175
  typeid_to_list_index.insert({struct_type.type_id, struct_info_vec.size() - 1});
13,172✔
176
}
13,220✔
177

178
const std::string& TypeDB::getTypeName(int type_id) const {
741,364✔
179
  if (isBuiltinType(type_id)) {
741,364✔
180
    return builtins.get_name(type_id);
632,937✔
181
  }
182
  if (isStructType(type_id)) {
108,427✔
183
    const auto* structInfo = getStructInfo(type_id);
108,355✔
184
    if (structInfo != nullptr) {
108,355✔
185
      return structInfo->name;
108,235✔
186
    }
187
  }
120✔
188

189
  return unknown_struct_name;
192✔
190
}
741,364✔
191

192
size_t TypeDB::getTypeSize(int type_id) const {
726,865✔
193
  if (isReservedType(type_id)) {
726,865✔
194
    if (isBuiltinType(type_id)) {
638,399✔
195
      return builtins.get_size(type_id);
638,345✔
196
    }
197
    return 0;
54✔
198
  }
199

200
  const auto* structInfo = getStructInfo(type_id);
88,466✔
201
  if (structInfo != nullptr) {
88,466✔
202
    return structInfo->extent;
88,394✔
203
  }
204
  return 0;
72✔
205
}
726,865✔
206

207
const StructTypeInfo* TypeDB::getStructInfo(int type_id) const {
223,080✔
208
  const auto index_iter = typeid_to_list_index.find(type_id);
223,080✔
209
  if (index_iter != typeid_to_list_index.end()) {
223,080✔
210
    return &struct_info_vec[index_iter->second];
222,852✔
211
  }
212
  return nullptr;
228✔
213
}
223,080✔
214

215
StructTypeInfo* TypeDB::getStructInfo(int type_id) {
6,840✔
216
  const auto index_iter = typeid_to_list_index.find(type_id);
6,840✔
217
  if (index_iter != typeid_to_list_index.end()) {
6,840✔
218
    return &struct_info_vec[index_iter->second];
6,840✔
219
  }
220
  return nullptr;
×
221
}
6,840✔
222

223
const std::vector<StructTypeInfo>& TypeDB::getStructList() const {
6,372✔
224
  return struct_info_vec;
6,372✔
225
}
226

227
}  // namespace typeart
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