GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 98.4% 188 / 4 / 195
Functions: 100.0% 22 / 0 / 22
Branches: 65.2% 253 / 0 / 388

src/symboltablebuilder/Scope.cpp
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #include "Scope.h"
4
5 #include <SourceFile.h>
6 #include <ast/ASTNodes.h>
7 #include <exception/SemanticError.h>
8 #include <symboltablebuilder/SymbolTableBuilder.h>
9
10 namespace spice::compiler {
11
12 26756 Scope::Scope(Scope *parent, SourceFile *sourceFile, ScopeType scopeType, const CodeLoc *codeLoc)
13
2/2
✓ Branch 3 → 4 taken 25512 times.
✓ Branch 3 → 5 taken 1244 times.
26756 : parent(parent), sourceFile(sourceFile), codeLoc(codeLoc), type(scopeType) {}
14
15 /**
16 * Create a child scope and return it
17 *
18 * @param scopeName Name of the child scope
19 * @param scopeType Type of the child scope
20 * @param declCodeLoc Code location of the scope
21 * @return Child scope (heap allocated)
22 */
23 25512 Scope *Scope::createChildScope(const std::string &scopeName, ScopeType scopeType, const CodeLoc *declCodeLoc) {
24
2/4
✓ Branch 2 → 3 taken 25512 times.
✗ Branch 2 → 15 not taken.
✓ Branch 3 → 4 taken 25512 times.
✗ Branch 3 → 13 not taken.
25512 const auto &[scope, inserted] = children.emplace(scopeName, std::make_shared<Scope>(this, sourceFile, scopeType, declCodeLoc));
25
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 25512 times.
25512 assert(inserted);
26 51024 return scope->second.get();
27 }
28
29 /**
30 * Rename the child scope. This is useful for realizing function overloading by storing a function with not
31 * only its name, but also its signature
32 *
33 * @param oldName Old name of the child table
34 * @param newName New name of the child table
35 */
36 13823 void Scope::renameChildScope(const std::string &oldName, const std::string &newName) {
37
4/8
✓ Branch 2 → 3 taken 13823 times.
✗ Branch 2 → 19 not taken.
✓ Branch 3 → 4 taken 13823 times.
✗ Branch 3 → 7 not taken.
✓ Branch 4 → 5 taken 13823 times.
✗ Branch 4 → 19 not taken.
✓ Branch 5 → 6 taken 13823 times.
✗ Branch 5 → 7 not taken.
13823 assert(children.contains(oldName) && !children.contains(newName));
38
1/2
✓ Branch 8 → 9 taken 13823 times.
✗ Branch 8 → 19 not taken.
13823 auto nodeHandler = children.extract(oldName);
39
1/2
✓ Branch 10 → 11 taken 13823 times.
✗ Branch 10 → 17 not taken.
13823 nodeHandler.key() = newName;
40
1/2
✓ Branch 12 → 13 taken 13823 times.
✗ Branch 12 → 16 not taken.
13823 children.insert(std::move(nodeHandler));
41 13823 }
42
43 /**
44 * Duplicates the child scope by copying it. The duplicated symbols point to the original ones.
45 *
46 * @param oldName Old name of the child block
47 * @param newName New block name
48 */
49 4664 Scope *Scope::copyChildScope(const std::string &oldName, const std::string &newName) {
50
4/8
✓ Branch 2 → 3 taken 4664 times.
✗ Branch 2 → 18 not taken.
✓ Branch 3 → 4 taken 4664 times.
✗ Branch 3 → 7 not taken.
✓ Branch 4 → 5 taken 4664 times.
✗ Branch 4 → 18 not taken.
✓ Branch 5 → 6 taken 4664 times.
✗ Branch 5 → 7 not taken.
4664 assert(children.contains(oldName) && !children.contains(newName));
51 // Create copy
52
2/4
✓ Branch 8 → 9 taken 4664 times.
✗ Branch 8 → 18 not taken.
✓ Branch 10 → 11 taken 4664 times.
✗ Branch 10 → 18 not taken.
4664 const std::shared_ptr<Scope> newScope = children.at(oldName)->deepCopyScope();
53 // Save copy under new name
54
1/2
✓ Branch 11 → 12 taken 4664 times.
✗ Branch 11 → 16 not taken.
4664 children.emplace(newName, newScope);
55 9328 return newScope.get();
56 4664 }
57
58 /**
59 * Deep copy the current scope and all its children
60 *
61 * @return Deep copy of the current scope
62 */
63 14700 std::shared_ptr<Scope> Scope::deepCopyScope() { // NOLINT(misc-no-recursion)
64 14700 const auto newScope = std::make_shared<Scope>(*this);
65
2/2
✓ Branch 30 → 5 taken 10036 times.
✓ Branch 30 → 31 taken 14700 times.
24736 for (const auto &[childName, oldChild] : children) {
66
2/4
✓ Branch 9 → 10 taken 10036 times.
✗ Branch 9 → 37 not taken.
✓ Branch 11 → 12 taken 10036 times.
✗ Branch 11 → 35 not taken.
10036 newScope->children[childName] = oldChild->deepCopyScope();
67
1/2
✓ Branch 16 → 17 taken 10036 times.
✗ Branch 16 → 38 not taken.
10036 newScope->children[childName]->parent = newScope.get();
68
2/4
✓ Branch 19 → 20 taken 10036 times.
✗ Branch 19 → 38 not taken.
✓ Branch 22 → 23 taken 10036 times.
✗ Branch 22 → 38 not taken.
10036 newScope->children[childName]->symbolTable.scope = newScope->children[childName].get();
69
1/2
✓ Branch 26 → 27 taken 10036 times.
✗ Branch 26 → 38 not taken.
10036 newScope->children[childName]->symbolTable.parent = &newScope->symbolTable;
70 }
71 14700 newScope->symbolTable.scope = newScope.get();
72 14700 return newScope;
73 } // LCOV_EXCL_LINE - false positive
74
75 /**
76 * Get a child scope of the current scope by its name
77 *
78 * @param scopeName Child scope name
79 * @return Child scope
80 */
81 50917 Scope *Scope::getChildScope(const std::string &scopeName) const {
82
1/2
✓ Branch 2 → 3 taken 50917 times.
✗ Branch 2 → 12 not taken.
50917 const auto it = children.find(scopeName);
83
2/2
✓ Branch 5 → 6 taken 50916 times.
✓ Branch 5 → 8 taken 1 time.
50917 return it != children.end() ? it->second.get() : nullptr;
84 }
85
86 /**
87 * Retrieve all variables in the current scope, that have reached the end of their lifetime at the end of this scope
88 *
89 * @return Collection of EOL variables
90 */
91 22252 std::vector<SymbolTableEntry *> Scope::getVarsGoingOutOfScope() { // NOLINT(misc-no-recursion)
92
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 22252 times.
22252 assert(!isRootScope()); // Should not be called in root scope
93 22252 std::vector<SymbolTableEntry *> varsGoingOutOfScope;
94
95 // Collect all variables in this scope
96
2/2
✓ Branch 26 → 8 taken 39859 times.
✓ Branch 26 → 27 taken 22252 times.
62111 for (const auto &[name, entry] : symbolTable.symbols) {
97 // Skip 'this' and result variables
98
8/10
✓ Branch 11 → 12 taken 39859 times.
✗ Branch 11 → 52 not taken.
✓ Branch 12 → 13 taken 33125 times.
✓ Branch 12 → 15 taken 6734 times.
✓ Branch 13 → 14 taken 33125 times.
✗ Branch 13 → 52 not taken.
✓ Branch 14 → 15 taken 8377 times.
✓ Branch 14 → 16 taken 24748 times.
✓ Branch 17 → 18 taken 15111 times.
✓ Branch 17 → 19 taken 24748 times.
39859 if (name == THIS_VARIABLE_NAME || name == RETURN_VARIABLE_NAME)
99 15111 continue;
100 // Skip parameters (ToDo: Remove when copy constructors work for by-value argument passing)
101
2/2
✓ Branch 19 → 20 taken 15931 times.
✓ Branch 19 → 21 taken 8817 times.
24748 if (entry.isParam)
102 15931 continue;
103 // Found variable, that goes out of scope
104
2/4
✓ Branch 21 → 22 taken 8817 times.
✗ Branch 21 → 51 not taken.
✓ Branch 22 → 23 taken 8817 times.
✗ Branch 22 → 51 not taken.
8817 varsGoingOutOfScope.push_back(&symbolTable.symbols.at(name));
105 }
106
107 // If this is the scope of a dtor, also return all fields of the struct
108
2/2
✓ Branch 27 → 28 taken 126 times.
✓ Branch 27 → 49 taken 22126 times.
22252 if (isDtorScope) {
109
2/4
✓ Branch 30 → 31 taken 126 times.
✗ Branch 30 → 33 not taken.
✓ Branch 31 → 32 taken 126 times.
✗ Branch 31 → 33 not taken.
126 assert(!isRootScope() && parent->type == ScopeType::STRUCT);
110 // Get all fields of the struct
111
2/2
✓ Branch 47 → 36 taken 4167 times.
✓ Branch 47 → 48 taken 126 times.
4293 for (const auto &[name, entry] : parent->symbolTable.symbols)
112
4/6
✓ Branch 39 → 40 taken 4167 times.
✗ Branch 39 → 55 not taken.
✓ Branch 40 → 41 taken 4167 times.
✗ Branch 40 → 53 not taken.
✓ Branch 41 → 42 taken 332 times.
✓ Branch 41 → 45 taken 3835 times.
4167 if (!entry.getQualType().isOneOf({TY_FUNCTION, TY_PROCEDURE}))
113
2/4
✓ Branch 42 → 43 taken 332 times.
✗ Branch 42 → 54 not taken.
✓ Branch 43 → 44 taken 332 times.
✗ Branch 43 → 54 not taken.
332 varsGoingOutOfScope.push_back(&parent->symbolTable.symbols.at(name));
114 }
115
116 22252 return varsGoingOutOfScope;
117 } // LCOV_EXCL_LINE - false positive
118
119 /**
120 * Insert a new generic type in this scope
121 *
122 * @param typeName Generic type name
123 * @param genericType Generic type itself
124 */
125 5260 void Scope::insertGenericType(const std::string &typeName, const GenericType &genericType) {
126
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 5260 times.
5260 assert(!genericTypes.contains(typeName));
127 5260 genericTypes.emplace(typeName, genericType);
128 5260 }
129
130 /**
131 * Search for a generic type by its name in this scope.
132 * If the generic type is not found, a nullptr is returned.
133 *
134 * @param typeName Name of the generic type
135 * @return Generic type
136 */
137 40214 GenericType *Scope::lookupGenericTypeStrict(const std::string &typeName) {
138
1/2
✓ Branch 2 → 3 taken 40214 times.
✗ Branch 2 → 12 not taken.
40214 const auto it = genericTypes.find(typeName);
139
2/2
✓ Branch 5 → 6 taken 16955 times.
✓ Branch 5 → 8 taken 23259 times.
40214 return it != genericTypes.end() ? &it->second : nullptr;
140 }
141
142 /**
143 * Collect all warnings, produced within this scope
144 *
145 * @param warnings List of warnings
146 * @return Collection of warnings
147 */
148 2324 void Scope::collectWarnings(std::vector<CompilerWarning> &warnings) const { // NOLINT(misc-no-recursion)
149 // Visit own symbols
150
5/8
✓ Branch 2 → 3 taken 2324 times.
✗ Branch 2 → 215 not taken.
✓ Branch 3 → 4 taken 2324 times.
✗ Branch 3 → 215 not taken.
✓ Branch 4 → 5 taken 2324 times.
✗ Branch 4 → 215 not taken.
✓ Branch 143 → 6 taken 7271 times.
✓ Branch 143 → 144 taken 2324 times.
9595 for (const SymbolTableEntry &entry : symbolTable.symbols | std::views::values) {
151 // Do not produce a warning if the symbol is used or has a special name
152 7271 const std::string &name = entry.name;
153
6/6
✓ Branch 7 → 8 taken 278 times.
✓ Branch 7 → 10 taken 6993 times.
✓ Branch 9 → 10 taken 103 times.
✓ Branch 9 → 11 taken 175 times.
✓ Branch 12 → 13 taken 7096 times.
✓ Branch 12 → 14 taken 175 times.
7271 if (entry.used || name.starts_with(UNUSED_VARIABLE_NAME))
154 7124 continue;
155
156
1/2
✓ Branch 14 → 15 taken 175 times.
✗ Branch 14 → 214 not taken.
175 const QualType entryType = entry.getQualType();
157
158 // Determine warning type and message by the scope type and the symbol type
159 175 CompilerWarningType warningType = UNUSED_VARIABLE;
160 175 std::string warningMessage;
161
4/4
✓ Branch 16 → 17 taken 28 times.
✓ Branch 16 → 92 taken 50 times.
✓ Branch 16 → 121 taken 27 times.
✓ Branch 16 → 127 taken 70 times.
175 switch (type) {
162 28 case ScopeType::GLOBAL: {
163 // Skip generic function/procedure/struct/interface entries
164
6/10
✓ Branch 17 → 18 taken 28 times.
✗ Branch 17 → 157 not taken.
✓ Branch 18 → 19 taken 18 times.
✓ Branch 18 → 23 taken 10 times.
✓ Branch 19 → 20 taken 18 times.
✗ Branch 19 → 157 not taken.
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 18 times.
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 28 times.
28 if (entryType.isOneOf({TY_FUNCTION, TY_PROCEDURE, TY_STRUCT, TY_INTERFACE}) && !entryType.getTemplateTypes().empty())
165 continue;
166
167
3/4
✓ Branch 26 → 27 taken 28 times.
✗ Branch 26 → 212 not taken.
✓ Branch 27 → 28 taken 14 times.
✓ Branch 27 → 38 taken 14 times.
28 if (entryType.is(TY_FUNCTION)) {
168
1/2
✓ Branch 28 → 29 taken 14 times.
✗ Branch 28 → 212 not taken.
14 const std::vector<Function *> *fctManifestations = entry.declNode->getFctManifestations(name);
169 14 warningType = UNUSED_FUNCTION;
170
3/6
✓ Branch 30 → 31 taken 14 times.
✗ Branch 30 → 162 not taken.
✓ Branch 31 → 32 taken 14 times.
✗ Branch 31 → 160 not taken.
✓ Branch 32 → 33 taken 14 times.
✗ Branch 32 → 158 not taken.
14 warningMessage = "'" + fctManifestations->front()->getSignature() + "' is unused";
171
3/4
✓ Branch 38 → 39 taken 14 times.
✗ Branch 38 → 212 not taken.
✓ Branch 39 → 40 taken 2 times.
✓ Branch 39 → 50 taken 12 times.
14 } else if (entryType.is(TY_PROCEDURE)) {
172
1/2
✓ Branch 40 → 41 taken 2 times.
✗ Branch 40 → 212 not taken.
2 const std::vector<Function *> *fctManifestations = entry.declNode->getFctManifestations(name);
173 2 warningType = UNUSED_PROCEDURE;
174
3/6
✓ Branch 42 → 43 taken 2 times.
✗ Branch 42 → 169 not taken.
✓ Branch 43 → 44 taken 2 times.
✗ Branch 43 → 167 not taken.
✓ Branch 44 → 45 taken 2 times.
✗ Branch 44 → 165 not taken.
2 warningMessage = "'" + fctManifestations->front()->getSignature() + "' is unused";
175
3/4
✓ Branch 50 → 51 taken 12 times.
✗ Branch 50 → 212 not taken.
✓ Branch 51 → 52 taken 1 time.
✓ Branch 51 → 58 taken 11 times.
12 } else if (entryType.is(TY_STRUCT)) {
176 1 warningType = UNUSED_STRUCT;
177
2/4
✓ Branch 52 → 53 taken 1 time.
✗ Branch 52 → 174 not taken.
✓ Branch 53 → 54 taken 1 time.
✗ Branch 53 → 172 not taken.
1 warningMessage = "The struct '" + entry.name + "' is unused";
178
3/4
✓ Branch 58 → 59 taken 11 times.
✗ Branch 58 → 212 not taken.
✓ Branch 59 → 60 taken 1 time.
✓ Branch 59 → 66 taken 10 times.
11 } else if (entryType.is(TY_INTERFACE)) {
179 1 warningType = UNUSED_INTERFACE;
180
2/4
✓ Branch 60 → 61 taken 1 time.
✗ Branch 60 → 178 not taken.
✓ Branch 61 → 62 taken 1 time.
✗ Branch 61 → 176 not taken.
1 warningMessage = "The interface '" + entry.name + "' is unused";
181
3/4
✓ Branch 66 → 67 taken 10 times.
✗ Branch 66 → 212 not taken.
✓ Branch 67 → 68 taken 2 times.
✓ Branch 67 → 69 taken 8 times.
10 } else if (entryType.is(TY_ENUM)) {
182 2 continue; // Do not report unused enums. Only unused enum items are reported
183
3/4
✓ Branch 69 → 70 taken 8 times.
✗ Branch 69 → 212 not taken.
✓ Branch 70 → 71 taken 2 times.
✓ Branch 70 → 77 taken 6 times.
8 } else if (entryType.is(TY_IMPORT)) {
184 2 warningType = UNUSED_IMPORT;
185
2/4
✓ Branch 71 → 72 taken 2 times.
✗ Branch 71 → 182 not taken.
✓ Branch 72 → 73 taken 2 times.
✗ Branch 72 → 180 not taken.
2 warningMessage = "The import '" + entry.name + "' is unused";
186
3/4
✓ Branch 77 → 78 taken 6 times.
✗ Branch 77 → 212 not taken.
✓ Branch 78 → 79 taken 1 time.
✓ Branch 78 → 85 taken 5 times.
6 } else if (entryType.is(TY_ALIAS)) {
187 1 warningType = UNUSED_ALIAS;
188
2/4
✓ Branch 79 → 80 taken 1 time.
✗ Branch 79 → 186 not taken.
✓ Branch 80 → 81 taken 1 time.
✗ Branch 80 → 184 not taken.
1 warningMessage = "The type alias '" + entry.name + "' is unused";
189 } else {
190 5 warningType = UNUSED_VARIABLE;
191
2/4
✓ Branch 85 → 86 taken 5 times.
✗ Branch 85 → 190 not taken.
✓ Branch 86 → 87 taken 5 times.
✗ Branch 86 → 188 not taken.
5 warningMessage = "The variable '" + entry.name + "' is unused";
192 }
193
194 26 break;
195 }
196 50 case ScopeType::STRUCT: // fall-through
197 case ScopeType::INTERFACE: {
198
3/4
✓ Branch 92 → 93 taken 50 times.
✗ Branch 92 → 212 not taken.
✓ Branch 93 → 94 taken 5 times.
✓ Branch 93 → 100 taken 45 times.
50 if (entry.isField()) {
199 5 warningType = UNUSED_FIELD;
200
2/4
✓ Branch 94 → 95 taken 5 times.
✗ Branch 94 → 194 not taken.
✓ Branch 95 → 96 taken 5 times.
✗ Branch 95 → 192 not taken.
5 warningMessage = "The field '" + entry.name + "' is unused";
201
2/4
✓ Branch 100 → 101 taken 45 times.
✗ Branch 100 → 196 not taken.
✓ Branch 101 → 102 taken 45 times.
✗ Branch 101 → 120 not taken.
45 } else if (entryType.isOneOf({TY_FUNCTION, TY_PROCEDURE})) {
202 // Skip implicit method entries and generic templates
203
1/2
✓ Branch 102 → 103 taken 45 times.
✗ Branch 102 → 212 not taken.
45 const std::vector<Function *> *fctManifestations = entry.declNode->getFctManifestations(name);
204
5/6
✓ Branch 104 → 105 taken 19 times.
✓ Branch 104 → 107 taken 26 times.
✗ Branch 106 → 107 not taken.
✓ Branch 106 → 108 taken 19 times.
✓ Branch 109 → 110 taken 26 times.
✓ Branch 109 → 111 taken 19 times.
45 if (fctManifestations->empty() || fctManifestations->front()->implicitDefault)
205 26 continue;
206
207 19 warningType = UNUSED_METHOD;
208
3/6
✓ Branch 112 → 113 taken 19 times.
✗ Branch 112 → 201 not taken.
✓ Branch 113 → 114 taken 19 times.
✗ Branch 113 → 199 not taken.
✓ Branch 114 → 115 taken 19 times.
✗ Branch 114 → 197 not taken.
19 warningMessage = "The method '" + fctManifestations->front()->getSignature() + "' is unused";
209 }
210 24 break;
211 }
212 27 case ScopeType::ENUM: {
213 27 warningType = UNUSED_ENUM_ITEM;
214
2/4
✓ Branch 121 → 122 taken 27 times.
✗ Branch 121 → 206 not taken.
✓ Branch 122 → 123 taken 27 times.
✗ Branch 122 → 204 not taken.
27 warningMessage = "The enum item '" + entry.name + "' is unused";
215 27 break;
216 }
217 70 default: {
218 70 warningType = UNUSED_VARIABLE;
219
2/4
✓ Branch 127 → 128 taken 70 times.
✗ Branch 127 → 210 not taken.
✓ Branch 128 → 129 taken 70 times.
✗ Branch 128 → 208 not taken.
70 warningMessage = "The variable '" + entry.name + "' is unused";
220 70 break;
221 }
222 }
223
224 // Add warning
225
2/4
✓ Branch 133 → 134 taken 147 times.
✗ Branch 133 → 212 not taken.
✓ Branch 134 → 135 taken 147 times.
✗ Branch 134 → 212 not taken.
147 warnings.emplace_back(entry.getDeclCodeLoc(), warningType, warningMessage);
226
2/2
✓ Branch 137 → 138 taken 147 times.
✓ Branch 137 → 140 taken 28 times.
175 }
227
228 // Visit children
229
5/8
✓ Branch 144 → 145 taken 2324 times.
✗ Branch 144 → 216 not taken.
✓ Branch 145 → 146 taken 2324 times.
✗ Branch 145 → 216 not taken.
✓ Branch 146 → 147 taken 2324 times.
✗ Branch 146 → 216 not taken.
✓ Branch 155 → 148 taken 2133 times.
✓ Branch 155 → 156 taken 2324 times.
4457 for (const auto &childScope : children | std::views::values)
230
2/2
✓ Branch 150 → 151 taken 2024 times.
✓ Branch 150 → 153 taken 109 times.
2133 if (!childScope->isGenericScope)
231
1/2
✓ Branch 152 → 153 taken 2024 times.
✗ Branch 152 → 216 not taken.
2024 childScope->collectWarnings(warnings);
232 2324 }
233
234 /**
235 * Checks if all variables of this and all child scopes are of an explicit type.
236 * This is executed after type inference to check that all variables could be inferred correctly.
237 */
238 81825 void Scope::ensureSuccessfulTypeInference() const { // NOLINT(misc-no-recursion)
239 // Check symbols in this scope
240
2/2
✓ Branch 19 → 4 taken 185372 times.
✓ Branch 19 → 20 taken 81824 times.
267196 for (auto &[name, entry] : symbolTable.symbols)
241
4/6
✓ Branch 7 → 8 taken 185372 times.
✗ Branch 7 → 40 not taken.
✓ Branch 8 → 9 taken 185372 times.
✗ Branch 8 → 40 not taken.
✓ Branch 9 → 10 taken 1 time.
✓ Branch 9 → 17 taken 185371 times.
185372 if (entry.getQualType().is(TY_DYN))
242
3/6
✓ Branch 11 → 12 taken 1 time.
✗ Branch 11 → 36 not taken.
✓ Branch 12 → 13 taken 1 time.
✗ Branch 12 → 34 not taken.
✓ Branch 13 → 14 taken 1 time.
✗ Branch 13 → 31 not taken.
1 throw SemanticError(entry.declNode, UNEXPECTED_DYN_TYPE, "For the variable '" + name + "' no type could be inferred");
243
244 // Check child scopes
245
5/8
✓ Branch 20 → 21 taken 81824 times.
✗ Branch 20 → 41 not taken.
✓ Branch 21 → 22 taken 81824 times.
✗ Branch 21 → 41 not taken.
✓ Branch 22 → 23 taken 81824 times.
✗ Branch 22 → 41 not taken.
✓ Branch 29 → 24 taken 79553 times.
✓ Branch 29 → 30 taken 81823 times.
161376 for (const auto &scope : children | std::views::values)
246
2/2
✓ Branch 26 → 27 taken 79552 times.
✓ Branch 26 → 41 taken 1 time.
79553 scope->ensureSuccessfulTypeInference();
247 81823 }
248
249 /**
250 * Get the number of fields if this is a struct scope
251 *
252 * @return Number of fields
253 */
254 49880 size_t Scope::getFieldCount() const {
255
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 49880 times.
49880 assert(type == ScopeType::STRUCT);
256 49880 size_t fieldCount = 0;
257
5/8
✓ Branch 4 → 5 taken 49880 times.
✗ Branch 4 → 29 not taken.
✓ Branch 5 → 6 taken 49880 times.
✗ Branch 5 → 29 not taken.
✓ Branch 6 → 7 taken 49880 times.
✗ Branch 6 → 29 not taken.
✓ Branch 26 → 8 taken 910059 times.
✓ Branch 26 → 27 taken 49880 times.
959939 for (const auto &symbol : symbolTable.symbols | std::views::values) {
258
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 910059 times.
910059 if (symbol.anonymous)
259 continue;
260
1/2
✓ Branch 11 → 12 taken 910059 times.
✗ Branch 11 → 29 not taken.
910059 const QualType &symbolType = symbol.getQualType();
261
2/4
✓ Branch 12 → 13 taken 910059 times.
✗ Branch 12 → 29 not taken.
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 910059 times.
910059 if (symbolType.is(TY_IMPORT))
262 continue;
263 910059 const ASTNode *declNode = symbol.declNode;
264
8/10
✓ Branch 15 → 16 taken 910059 times.
✗ Branch 15 → 29 not taken.
✓ Branch 16 → 17 taken 187829 times.
✓ Branch 16 → 19 taken 722230 times.
✓ Branch 17 → 18 taken 187829 times.
✗ Branch 17 → 29 not taken.
✓ Branch 18 → 19 taken 30873 times.
✓ Branch 18 → 20 taken 156956 times.
✓ Branch 21 → 22 taken 753103 times.
✓ Branch 21 → 23 taken 156956 times.
910059 if (declNode->isFctOrProcDef() || declNode->isStructDef())
265 753103 continue;
266 156956 fieldCount++;
267 }
268 49880 return fieldCount;
269 }
270
271 /**
272 * Get all virtual methods in this scope, sorted by declaration code location
273 *
274 * @return List of virtual method pointers
275 */
276 788 std::vector<Function *> Scope::getVirtualMethods() {
277
3/4
✓ Branch 2 → 3 taken 356 times.
✓ Branch 2 → 5 taken 432 times.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 356 times.
788 assert(type == ScopeType::STRUCT || type == ScopeType::INTERFACE);
278
279 // Collect all virtual methods
280 788 std::vector<Function *> methods;
281
2/2
✓ Branch 37 → 7 taken 5018 times.
✓ Branch 37 → 38 taken 788 times.
5806 for (auto &[fctId, manifestationList] : functions) {
282
1/2
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 5018 times.
5018 assert(!manifestationList.empty());
283
2/2
✓ Branch 34 → 15 taken 7282 times.
✓ Branch 34 → 35 taken 5018 times.
12300 for (auto &[mangledName, function] : manifestationList)
284
2/2
✓ Branch 27 → 28 taken 1758 times.
✓ Branch 27 → 32 taken 5524 times.
7282 if (function.isVirtualMethod())
285
3/6
✓ Branch 28 → 29 taken 1758 times.
✗ Branch 28 → 41 not taken.
✓ Branch 29 → 30 taken 1758 times.
✗ Branch 29 → 41 not taken.
✓ Branch 30 → 31 taken 1758 times.
✗ Branch 30 → 41 not taken.
1758 methods.push_back(&functions.at(fctId).at(mangledName));
286 }
287
288 // Sort the list
289
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 2140 times.
4280 const auto pred = [](const Function *a, const Function *b) { return a->getDeclCodeLoc() < b->getDeclCodeLoc(); };
290
1/2
✓ Branch 38 → 39 taken 788 times.
✗ Branch 38 → 44 not taken.
788 std::ranges::sort(methods, pred);
291
292 788 return methods;
293 } // LCOV_EXCL_LINE - false positive
294
295 /**
296 * Retrieve all struct manifestations in this scope in the order of their declaration
297 *
298 * @return All struct manifestations in declaration order
299 */
300 1203 std::vector<const Struct *> Scope::getAllStructManifestationsInDeclarationOrder() const {
301 // Retrieve all struct manifestations in this scope
302 1203 std::vector<const Struct *> manifestations;
303
1/2
✓ Branch 3 → 4 taken 1203 times.
✗ Branch 3 → 27 not taken.
1203 manifestations.reserve(structs.size()); // Reserve at least the size of individual generic structs
304
5/8
✓ Branch 4 → 5 taken 1203 times.
✗ Branch 4 → 26 not taken.
✓ Branch 5 → 6 taken 1203 times.
✗ Branch 5 → 26 not taken.
✓ Branch 6 → 7 taken 1203 times.
✗ Branch 6 → 26 not taken.
✓ Branch 20 → 8 taken 745 times.
✓ Branch 20 → 21 taken 1203 times.
1948 for (const auto &structManifestations : structs | std::views::values)
305
5/8
✓ Branch 9 → 10 taken 745 times.
✗ Branch 9 → 25 not taken.
✓ Branch 10 → 11 taken 745 times.
✗ Branch 10 → 25 not taken.
✓ Branch 11 → 12 taken 745 times.
✗ Branch 11 → 25 not taken.
✓ Branch 17 → 13 taken 748 times.
✓ Branch 17 → 18 taken 745 times.
1493 for (const auto &manifestation : structManifestations | std::views::values)
306
1/2
✓ Branch 14 → 15 taken 748 times.
✗ Branch 14 → 24 not taken.
748 manifestations.push_back(&manifestation);
307
308 // Sort manifestations by declaration code location
309
2/2
✓ Branch 4 → 5 taken 4 times.
✓ Branch 4 → 6 taken 425 times.
858 auto sortLambda = [](const Struct *lhs, const Struct *rhs) { return lhs->getDeclCodeLoc() < rhs->getDeclCodeLoc(); };
310
1/2
✓ Branch 21 → 22 taken 1203 times.
✗ Branch 21 → 27 not taken.
1203 std::ranges::sort(manifestations, sortLambda);
311 1203 return manifestations;
312 } // LCOV_EXCL_LINE - false positive
313
314 /**
315 * Get the current number of nested loops
316 *
317 * @return Number of loops
318 */
319 2996 unsigned int Scope::getLoopNestingDepth() const { // NOLINT(misc-no-recursion)
320
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 2996 times.
2996 assert(!isRootScope());
321
2/2
✓ Branch 6 → 7 taken 551 times.
✓ Branch 6 → 8 taken 2445 times.
2996 if (parent->parent == nullptr)
322 551 return 0;
323 2445 unsigned int loopCount = parent->getLoopNestingDepth();
324
6/6
✓ Branch 9 → 10 taken 2321 times.
✓ Branch 9 → 12 taken 124 times.
✓ Branch 10 → 11 taken 1495 times.
✓ Branch 10 → 12 taken 826 times.
✓ Branch 11 → 12 taken 24 times.
✓ Branch 11 → 13 taken 1471 times.
2445 if (type == ScopeType::WHILE_BODY || type == ScopeType::FOR_BODY || type == ScopeType::FOREACH_BODY)
325 974 loopCount++;
326 2445 return loopCount;
327 }
328
329 /**
330 * Check if this scope is one of the child scopes of a switch statement
331 *
332 * @return Child scope of switch statement or not
333 */
334 7 bool Scope::isInCaseBranch() const { // NOLINT(misc-no-recursion)
335
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 7 times.
7 assert(!isRootScope());
336
2/2
✓ Branch 6 → 7 taken 2 times.
✓ Branch 6 → 8 taken 5 times.
7 if (parent->parent == nullptr)
337 2 return false;
338
2/2
✓ Branch 8 → 9 taken 4 times.
✓ Branch 8 → 10 taken 1 time.
5 if (type == ScopeType::CASE_BODY)
339 4 return true;
340 1 return parent->isInCaseBranch();
341 }
342
343 /**
344 * Check if this scope is within an async scope
345 *
346 * @return Within async scope or not
347 */
348 71 bool Scope::isInAsyncScope() const { // NOLINT(misc-no-recursion)
349
2/2
✓ Branch 2 → 3 taken 7 times.
✓ Branch 2 → 4 taken 64 times.
71 if (isAsyncScope)
350 7 return true;
351
3/4
✓ Branch 6 → 7 taken 41 times.
✓ Branch 6 → 10 taken 23 times.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 41 times.
64 return !isRootScope() && parent->isInAsyncScope();
352 }
353
354 /**
355 * Check if unsafe operations are allowed in this scope
356 *
357 * @return Allowed or not
358 */
359 5570 bool Scope::doesAllowUnsafeOperations() const { // NOLINT(misc-no-recursion)
360
2/2
✓ Branch 2 → 3 taken 3488 times.
✓ Branch 2 → 4 taken 2082 times.
5570 if (type == ScopeType::UNSAFE_BODY)
361 3488 return true;
362
4/4
✓ Branch 6 → 7 taken 2081 times.
✓ Branch 6 → 10 taken 1 time.
✓ Branch 8 → 9 taken 2080 times.
✓ Branch 8 → 10 taken 1 time.
2082 return !isRootScope() && parent->doesAllowUnsafeOperations();
363 }
364
365 /**
366 * Checks if this scope is imported
367 *
368 * @param askingScope Scope, which asks whether the current one is imported from its point of view or not
369 *
370 * @return Imported / not imported
371 */
372 103705 bool Scope::isImportedBy(const Scope *askingScope) const { return askingScope->sourceFile->imports(sourceFile); }
373
374 /**
375 * Get JSON representation of the symbol table
376 *
377 * @return Symbol table as JSON object
378 */
379 81889 nlohmann::json Scope::getSymbolTableJSON() const { // NOLINT(misc-no-recursion)
380
1/2
✓ Branch 2 → 3 taken 81889 times.
✗ Branch 2 → 42 not taken.
81889 nlohmann::json result = symbolTable.toJSON();
381
382 // Collect all children
383 81889 std::vector<nlohmann::json> jsonChildren;
384
1/2
✓ Branch 4 → 5 taken 81889 times.
✗ Branch 4 → 38 not taken.
81889 jsonChildren.reserve(children.size());
385
2/2
✓ Branch 20 → 7 taken 79609 times.
✓ Branch 20 → 21 taken 81889 times.
161498 for (const auto &[name, childScope] : children) {
386
1/2
✓ Branch 11 → 12 taken 79609 times.
✗ Branch 11 → 33 not taken.
79609 nlohmann::json c = childScope->getSymbolTableJSON();
387
2/4
✓ Branch 12 → 13 taken 79609 times.
✗ Branch 12 → 30 not taken.
✓ Branch 13 → 14 taken 79609 times.
✗ Branch 13 → 28 not taken.
79609 c["name"] = name; // Inject symbol table name into JSON object
388
1/2
✓ Branch 16 → 17 taken 79609 times.
✗ Branch 16 → 31 not taken.
79609 jsonChildren.emplace_back(c);
389 79609 }
390
2/4
✓ Branch 21 → 22 taken 81889 times.
✗ Branch 21 → 37 not taken.
✓ Branch 22 → 23 taken 81889 times.
✗ Branch 22 → 35 not taken.
81889 result["children"] = jsonChildren;
391
392 81889 return result;
393 81889 }
394
395 } // namespace spice::compiler
396