GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 95.0% 19 / 0 / 20
Functions: 100.0% 7 / 0 / 7
Branches: 62.5% 20 / 0 / 32

src/model/Struct.cpp
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #include "Struct.h"
4
5 #include <ast/ASTNodes.h>
6
7 namespace spice::compiler {
8
9 static constexpr auto STRUCT_SCOPE_PREFIX = "struct:";
10
11 /**
12 * Retrieve the name of the scope, where members and methods are placed. This is used to navigate to the scope of the struct
13 * from the parent scope.
14 *
15 * @return Name of the struct scope
16 */
17 944 std::string Struct::getScopeName() const {
18
5/8
✓ Branch 2 → 3 taken 944 times.
✗ Branch 2 → 13 not taken.
✓ Branch 3 → 4 taken 472 times.
✓ Branch 3 → 5 taken 472 times.
✓ Branch 4 → 6 taken 472 times.
✗ Branch 4 → 13 not taken.
✓ Branch 5 → 6 taken 472 times.
✗ Branch 5 → 13 not taken.
944 const std::string &appendix = isGenericSubstantiation() ? getSignature() : name;
19
1/2
✓ Branch 6 → 7 taken 944 times.
✗ Branch 6 → 11 not taken.
1888 return STRUCT_SCOPE_PREFIX + appendix;
20 944 }
21
22 /**
23 * Retrieve the name of the scope, where members and methods are placed. This is used to navigate to the scope of the struct
24 * from the parent scope.
25 *
26 * @return Name of the struct scope
27 */
28 20303 std::string Struct::getScopeName(const std::string &name, const QualTypeList &concreteTemplateTypes) {
29
2/4
✓ Branch 2 → 3 taken 20303 times.
✗ Branch 2 → 10 not taken.
✓ Branch 3 → 4 taken 20303 times.
✗ Branch 3 → 8 not taken.
20303 return STRUCT_SCOPE_PREFIX + getSignature(name, concreteTemplateTypes);
30 }
31
32 /**
33 * Checks at least one field is a reference.
34 * This is used to prohibit constant instantiations.
35 *
36 * @return Has reference as field type or not
37 */
38 282 bool Struct::hasReferenceFields() const {
39 718 return std::ranges::any_of(fieldTypes, [](const QualType &fieldType) { return fieldType.isRef(); });
40 }
41
42 /**
43 * Check that all fields are in a certain lifecycle state.
44 *
45 * @param state Lifecycle state to check for
46 * @return nullptr if all fields are in this state, otherwise the first mismatched field symbol
47 */
48 253 const SymbolTableEntry *Struct::areAllFieldsInState(LifecycleState state) const {
49
2/2
✓ Branch 20 → 3 taken 476 times.
✓ Branch 20 → 21 taken 253 times.
729 for (size_t i = 0; i < fieldTypes.size(); i++) {
50
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 476 times.
476 const SymbolTableEntry *fieldSymbol = scope->lookupField(i);
51
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 476 times.
476 assert(fieldSymbol != nullptr);
52
4/6
✓ Branch 10 → 11 taken 393 times.
✓ Branch 10 → 15 taken 83 times.
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 393 times.
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 476 times.
476 if (!fieldSymbol->isImplicitField && fieldSymbol->getLifecycle().getCurrentState() != state)
53 return fieldSymbol;
54 }
55 253 return nullptr;
56 }
57
58 /**
59 * Check that all fields are initialized.
60 *
61 * @return nullptr if all fields are initialized, otherwise the first uninitialized field symbol
62 */
63 253 const SymbolTableEntry *Struct::areAllFieldsInitialized() const { return areAllFieldsInState(INITIALIZED); }
64
65 /**
66 * Reset the initialization state of all fields to DECLARED.
67 *
68 * @param node AST node to associate with the update
69 */
70 1438 void Struct::resetFieldSymbolsToDeclared(const ASTNode *node) const {
71
2/2
✓ Branch 11 → 3 taken 3389 times.
✓ Branch 11 → 12 taken 1438 times.
4827 for (size_t i = 0; i < fieldTypes.size(); i++)
72
2/4
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 3389 times.
✓ Branch 8 → 9 taken 3389 times.
✗ Branch 8 → 13 not taken.
6778 scope->lookupField(i)->updateState(DECLARED, node);
73 1438 }
74
75 } // namespace spice::compiler
76