GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 24 / 4 / 28
Functions: 100.0% 6 / 0 / 6
Branches: 58.7% 27 / 20 / 66

src/symboltablebuilder/SymbolTableEntry.cpp
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #include "SymbolTableEntry.h"
4
5 #include <ast/ASTNodes.h>
6 #include <exception/SemanticError.h>
7 #include <symboltablebuilder/Scope.h>
8 #include <util/CodeLoc.h>
9
10 namespace spice::compiler {
11
12 /**
13 * Retrieve the qualified type of this symbol
14 *
15 * @return Qualified type of this symbol
16 */
17 16215869 const QualType &SymbolTableEntry::getQualType() const { return qualType; }
18
19 /**
20 * Update the type of this symbol.
21 *
22 * @param newType New type of the current symbol
23 * @param overwriteExistingType Overwrites the existing type without throwing an error
24 */
25 405093 void SymbolTableEntry::updateType(const QualType &newType, [[maybe_unused]] bool overwriteExistingType) {
26
4/6
✓ Branch 2 → 3 taken 180553 times.
✓ Branch 2 → 6 taken 224540 times.
✓ Branch 3 → 4 taken 180553 times.
✗ Branch 3 → 7 not taken.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 180553 times.
405093 assert(overwriteExistingType || qualType.isOneOf({TY_INVALID, TY_DYN}));
27 405093 qualType = newType;
28 405093 }
29
30 /**
31 * Update the state of the current symbol
32 *
33 * @throws CompilerError When the state of the symbol is set to initialized before a concrete type was set
34 *
35 * @param newState New state of the current symbol
36 * @param node AST node where the update takes place
37 */
38 624700 void SymbolTableEntry::updateState(const LifecycleState &newState, const ASTNode *node) {
39 624700 const LifecycleState oldState = lifecycle.getCurrentState();
40 − if (newState == DEAD && oldState == DECLARED) // GCOV_EXCL_LINE
41 − throw CompilerError(INTERNAL_ERROR, "Cannot destroy uninitialized variable '" + name + "'"); // GCOV_EXCL_LINE
42 − if (newState == DEAD && oldState == DEAD) // GCOV_EXCL_LINE
43 − throw CompilerError(INTERNAL_ERROR, "Cannot destroy already destroyed variable '" + name + "'"); // GCOV_EXCL_LINE
44
1/2
✓ Branch 21 → 22 taken 624700 times.
✗ Branch 21 → 41 not taken.
624700 lifecycle.addEvent({newState, node});
45 624700 }
46
47 /**
48 * Retrieve the code location where the symbol was declared
49 *
50 * @return Declaration code location
51 */
52 434 const CodeLoc &SymbolTableEntry::getDeclCodeLoc() const { return declNode->codeLoc; }
53
54 /**
55 * Check if this symbol is a struct field
56 *
57 * @return Struct field or not
58 */
59 152572 bool SymbolTableEntry::isField() const {
60
6/6
✓ Branch 2 → 3 taken 26534 times.
✓ Branch 2 → 4 taken 126038 times.
✓ Branch 3 → 4 taken 114 times.
✓ Branch 3 → 8 taken 26420 times.
✓ Branch 5 → 6 taken 126006 times.
✓ Branch 5 → 8 taken 146 times.
278578 return (scope->type == ScopeType::STRUCT || scope->type == ScopeType::UNION) && orderIndex < scope->getFieldCount() &&
61
1/2
✓ Branch 6 → 7 taken 126006 times.
✗ Branch 6 → 8 not taken.
278578 !anonymous;
62 }
63
64 /**
65 * Stringify the current symbol to a human-readable form. Used to dump whole symbol tables with their contents.
66 *
67 * Example:
68 * {
69 * "name": "testIden",
70 * "type": "int[]*",
71 * "orderIndex": 4,
72 * "state": "initialized",
73 * "qualifiers: [
74 * "const": true,
75 * "signed": false
76 * ],
77 * "isGlobal": false,
78 * "isVolatile": false
79 * }
80 *
81 * @return Symbol table entry as a JSON object
82 */
83 1266367 nlohmann::ordered_json SymbolTableEntry::toJSON() const {
84 1266367 nlohmann::json result;
85
2/4
✓ Branch 3 → 4 taken 1266367 times.
✗ Branch 3 → 43 not taken.
✓ Branch 4 → 5 taken 1266367 times.
✗ Branch 4 → 41 not taken.
1266367 result["name"] = name;
86
3/6
✓ Branch 7 → 8 taken 1266367 times.
✗ Branch 7 → 48 not taken.
✓ Branch 8 → 9 taken 1266367 times.
✗ Branch 8 → 46 not taken.
✓ Branch 9 → 10 taken 1266367 times.
✗ Branch 9 → 44 not taken.
1266367 result["type"] = qualType.getName(true);
87
3/6
✓ Branch 13 → 14 taken 1266367 times.
✗ Branch 13 → 54 not taken.
✓ Branch 14 → 15 taken 1266367 times.
✗ Branch 14 → 52 not taken.
✓ Branch 15 → 16 taken 1266367 times.
✗ Branch 15 → 50 not taken.
1266367 result["codeLoc"] = declNode->codeLoc.toString();
88
1/2
✓ Branch 20 → 21 taken 1266367 times.
✗ Branch 20 → 56 not taken.
1266367 result["orderIndex"] = orderIndex;
89
3/6
✓ Branch 23 → 24 taken 1266367 times.
✗ Branch 23 → 61 not taken.
✓ Branch 24 → 25 taken 1266367 times.
✗ Branch 24 → 61 not taken.
✓ Branch 25 → 26 taken 1266367 times.
✗ Branch 25 → 59 not taken.
1266367 result["state"] = lifecycle.getCurrentStateName();
90
1/2
✓ Branch 29 → 30 taken 1266367 times.
✗ Branch 29 → 63 not taken.
1266367 result["isGlobal"] = global;
91
1/2
✓ Branch 33 → 34 taken 1266367 times.
✗ Branch 33 → 66 not taken.
1266367 result["isVolatile"] = isVolatile;
92
1/2
✓ Branch 36 → 37 taken 1266367 times.
✗ Branch 36 → 69 not taken.
2532734 return result;
93 1266367 }
94
95 } // namespace spice::compiler
96