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

tudasc / TypeART / 13091819531

01 Feb 2025 07:39PM UTC coverage: 88.36%. First build
13091819531

Pull #155

github

web-flow
Merge 9129593c8 into 904382c9c
Pull Request #155: LLVM 18 testing

83 of 89 new or added lines in 8 files covered. (93.26%)

4031 of 4562 relevant lines covered (88.36%)

112157.1 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,015✔
69
}
2,015✔
70
#undef SIZE
71
#undef TYPENAME
72

73
}  // namespace builtins
74

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

84
using namespace builtins;
85

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

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

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

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

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

106
bool TypeDB::isUnknown(int type_id) const {
54✔
107
  return BuiltInQuery::is_unknown_type(type_id);
54✔
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 {
413✔
115
  const auto* structInfo = getStructInfo(type_id);
413✔
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) &&
826✔
119
         (static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::USER_DEFINED));
413✔
120
}
121

122
bool TypeDB::isVectorType(int type_id) const {
30✔
123
  const auto* structInfo = getStructInfo(type_id);
30✔
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) &&
60✔
127
         (static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::LLVM_VECTOR));
30✔
128
}
129

130
bool TypeDB::isValid(int type_id) const {
116,256✔
131
  if (isBuiltinType(type_id)) {
116,256✔
132
    return true;
105,902✔
133
  }
134
  return typeid_to_list_index.find(type_id) != typeid_to_list_index.end();
10,354✔
135
}
116,256✔
136

137
void TypeDB::registerStruct(const StructTypeInfo& struct_type, bool overwrite) {
4,764✔
138
  if (isValid(struct_type.type_id) || !isStructType(struct_type.type_id)) {
4,764✔
139
    if (isBuiltinType(struct_type.type_id)) {
54✔
140
      LOG_ERROR("Built-in type ID used for struct " << struct_type.name);
6✔
141
    } else if (isReservedType(struct_type.type_id)) {
54✔
142
      LOG_ERROR("Type ID is reserved for builtin types. Struct: " << struct_type.name);
12✔
143
    } else if (isUnknown(struct_type.type_id)) {
48✔
144
      LOG_ERROR("Type ID is reserved for unknown types. Struct: " << struct_type.name);
×
145
    } else {
×
146
      if (!overwrite) {
36✔
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);
36✔
153
      info       = struct_type;
36✔
154
    }
155
    return;
54✔
156
  }
157

158
  struct_info_vec.push_back(struct_type);
4,710✔
159
  typeid_to_list_index.insert({struct_type.type_id, struct_info_vec.size() - 1});
4,710✔
160
}
4,764✔
161

162
const std::string& TypeDB::getTypeName(int type_id) const {
223,952✔
163
  if (isBuiltinType(type_id)) {
223,952✔
164
    return builtins.get_name(type_id);
211,853✔
165
  }
166
  if (isStructType(type_id)) {
12,099✔
167
    const auto* structInfo = getStructInfo(type_id);
12,063✔
168
    if (structInfo != nullptr) {
12,063✔
169
      return structInfo->name;
12,027✔
170
    }
171
  }
36✔
172

173
  return unknown_struck_name;
72✔
174
}
223,952✔
175

176
size_t TypeDB::getTypeSize(int type_id) const {
232,174✔
177
  if (isReservedType(type_id)) {
232,174✔
178
    if (isBuiltinType(type_id)) {
218,942✔
179
      return builtins.get_size(type_id);
218,924✔
180
    }
181
    return 0;
18✔
182
  }
183

184
  const auto* structInfo = getStructInfo(type_id);
13,232✔
185
  if (structInfo != nullptr) {
13,232✔
186
    return structInfo->extent;
13,208✔
187
  }
188
  return 0;
24✔
189
}
232,174✔
190

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

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

207
const std::vector<StructTypeInfo>& TypeDB::getStructList() const {
2,817✔
208
  return struct_info_vec;
2,817✔
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