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

tudasc / TypeART / 13528988609

25 Feb 2025 07:06PM UTC coverage: 88.854% (-1.9%) from 90.735%
13528988609

Pull #163

github

web-flow
Merge e4a2d80f6 into d2e14acc5
Pull Request #163: LLVM 18 support

974 of 1122 new or added lines in 38 files covered. (86.81%)

30 existing lines in 6 files now uncovered.

4201 of 4728 relevant lines covered (88.85%)

190054.62 hits per line

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

93.33
/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_VOID, "void*", sizeof(void*))                       \
40
  X(TYPEART_NULLPOINTER, "nullptr_t", sizeof(void*))            \
41
  X(TYPEART_BOOL, "bool", sizeof(bool))                         \
42
  X(TYPEART_CHAR_8, "char", sizeof(char))                       \
43
  X(TYPEART_UCHAR_8, "unsigned char", sizeof(unsigned char))    \
44
  X(TYPEART_UTF_CHAR_8, "char8_t", sizeof(char))                \
45
  X(TYPEART_UTF_CHAR_16, "char16_t", sizeof(char16_t))          \
46
  X(TYPEART_UTF_CHAR_32, "char32_t", sizeof(char32_t))          \
47
  X(TYPEART_INT_8, "int8_t", sizeof(int8_t))                    \
48
  X(TYPEART_INT_16, "short", sizeof(int16_t))                   \
49
  X(TYPEART_INT_32, "int", sizeof(int32_t))                     \
50
  X(TYPEART_INT_64, "long int", sizeof(int64_t))                \
51
  X(TYPEART_INT_128, "int128_t", 16)                            \
52
  X(TYPEART_UINT_8, "uint8_t", sizeof(uint8_t))                 \
53
  X(TYPEART_UINT_16, "unsigned short", sizeof(uint16_t))        \
54
  X(TYPEART_UINT_32, "unsigned int", sizeof(uint32_t))          \
55
  X(TYPEART_UINT_64, "unsigned long int", sizeof(uint64_t))     \
56
  X(TYPEART_UINT_128, "uint128_t", 16)                          \
57
  X(TYPEART_FLOAT_8, "float8_t", 1)                             \
58
  X(TYPEART_FLOAT_16, "float16_t", 2)                           \
59
  X(TYPEART_FLOAT_32, "float", sizeof(float))                   \
60
  X(TYPEART_FLOAT_64, "double", sizeof(double))                 \
61
  X(TYPEART_FLOAT_128, "long double", sizeof(long double))      \
62
  X(TYPEART_COMPLEX_64, "float complex", size_complex_float)    \
63
  X(TYPEART_COMPLEX_128, "double complex", size_complex_double) \
64
  X(TYPEART_COMPLEX_256, "long double complex", size_complex_long_double)
65

66
#define TYPENAME(enum_name, str_name, size) std::string{str_name},
67
#define SIZE(enum_name, str_name, size)     (size),
68
BuiltInQuery::BuiltInQuery() : names{FOR_EACH_TYPEART_BUILTIN(TYPENAME)}, sizes{FOR_EACH_TYPEART_BUILTIN(SIZE)} {
2,957✔
69
}
2,957✔
70
#undef SIZE
71
#undef TYPENAME
72

73
}  // namespace builtins
74

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

84
using namespace builtins;
85

86
const std::string unknown_struck_name{"typeart_unknown_struct"};
2,756✔
87

88
void TypeDB::clear() {
1,664✔
89
  struct_info_vec.clear();
1,664✔
90
  typeid_to_list_index.clear();
1,664✔
91
  // reverseTypeMap.clear();
92
}
1,664✔
93

94
bool TypeDB::isBuiltinType(int type_id) const {
823,777✔
95
  return BuiltInQuery::is_builtin_type(type_id);
823,777✔
96
}
97

98
bool TypeDB::isReservedType(int type_id) const {
343,411✔
99
  return BuiltInQuery::is_reserved_type(type_id);
343,411✔
100
}
101

102
bool TypeDB::isStructType(int type_id) const {
30,447✔
103
  return BuiltInQuery::is_userdef_type(type_id);
30,447✔
104
}
105

106
bool TypeDB::isUnknown(int type_id) const {
67✔
107
  return BuiltInQuery::is_unknown_type(type_id);
67✔
108
}
109

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

114
bool TypeDB::isUserDefinedType(int type_id) const {
419✔
115
  const auto* structInfo = getStructInfo(type_id);
419✔
116
  LOG_DEBUG(structInfo->name << " " << static_cast<int>(structInfo->flag) << " "
117
                             << (static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::USER_DEFINED)))
118
  return (structInfo != nullptr) &&
838✔
119
         (static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::USER_DEFINED));
419✔
120
}
121

122
bool TypeDB::isVectorType(int type_id) const {
36✔
123
  const auto* structInfo = getStructInfo(type_id);
36✔
124
  LOG_DEBUG(structInfo->name << " " << static_cast<int>(structInfo->flag) << " "
125
                             << (static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::LLVM_VECTOR)))
126
  return (structInfo != nullptr) &&
72✔
127
         (static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::LLVM_VECTOR));
36✔
128
}
129

130
bool TypeDB::isValid(int type_id) const {
172,809✔
131
  if (isBuiltinType(type_id)) {
172,809✔
132
    return true;
154,529✔
133
  }
134
  return typeid_to_list_index.find(type_id) != typeid_to_list_index.end();
18,280✔
135
}
172,809✔
136

137
void TypeDB::registerStruct(const StructTypeInfo& struct_type, bool overwrite) {
7,696✔
138
  if (isValid(struct_type.type_id) || !isStructType(struct_type.type_id)) {
7,696✔
139
    if (isBuiltinType(struct_type.type_id)) {
67✔
140
      LOG_ERROR("Built-in type ID used for struct " << struct_type.name);
9✔
141
    } else if (isReservedType(struct_type.type_id)) {
67✔
142
      LOG_ERROR("Type ID is reserved for builtin types. Struct: " << struct_type.name);
18✔
143
    } else if (isUnknown(struct_type.type_id)) {
58✔
144
      LOG_ERROR("Type ID is reserved for unknown types. Struct: " << struct_type.name);
×
145
    } else {
×
146
      if (!overwrite) {
40✔
147
        LOG_ERROR("Struct type ID already registered for " << struct_type.name << ". Conflicting struct is "
×
148
                                                           << getStructInfo(struct_type.type_id)->name);
149
        return;
×
150
      }
151
      LOG_DEBUG("Overwrite struct " << struct_type.name)
152
      auto& info = *getStructInfo(struct_type.type_id);
40✔
153
      info       = struct_type;
40✔
154
    }
155
    return;
67✔
156
  }
157

158
  struct_info_vec.push_back(struct_type);
7,629✔
159
  typeid_to_list_index.insert({struct_type.type_id, struct_info_vec.size() - 1});
7,629✔
160
}
7,696✔
161

162
const std::string& TypeDB::getTypeName(int type_id) const {
331,525✔
163
  if (isBuiltinType(type_id)) {
331,525✔
164
    return builtins.get_name(type_id);
308,959✔
165
  }
166
  if (isStructType(type_id)) {
22,566✔
167
    const auto* structInfo = getStructInfo(type_id);
22,512✔
168
    if (structInfo != nullptr) {
22,512✔
169
      return structInfo->name;
22,458✔
170
    }
171
  }
54✔
172

173
  return unknown_struck_name;
108✔
174
}
331,525✔
175

176
size_t TypeDB::getTypeSize(int type_id) const {
343,365✔
177
  if (isReservedType(type_id)) {
343,365✔
178
    if (isBuiltinType(type_id)) {
318,977✔
179
      return builtins.get_size(type_id);
318,950✔
180
    }
181
    return 0;
27✔
182
  }
183

184
  const auto* structInfo = getStructInfo(type_id);
24,388✔
185
  if (structInfo != nullptr) {
24,388✔
186
    return structInfo->extent;
24,352✔
187
  }
188
  return 0;
36✔
189
}
343,365✔
190

191
const StructTypeInfo* TypeDB::getStructInfo(int type_id) const {
47,792✔
192
  const auto index_iter = typeid_to_list_index.find(type_id);
47,792✔
193
  if (index_iter != typeid_to_list_index.end()) {
47,792✔
194
    return &struct_info_vec[index_iter->second];
47,684✔
195
  }
196
  return nullptr;
108✔
197
}
47,792✔
198

199
StructTypeInfo* TypeDB::getStructInfo(int type_id) {
3,686✔
200
  const auto index_iter = typeid_to_list_index.find(type_id);
3,686✔
201
  if (index_iter != typeid_to_list_index.end()) {
3,686✔
202
    return &struct_info_vec[index_iter->second];
3,677✔
203
  }
204
  return nullptr;
9✔
205
}
3,686✔
206

207
const std::vector<StructTypeInfo>& TypeDB::getStructList() const {
4,095✔
208
  return struct_info_vec;
4,095✔
209
}
210

211
}  // 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