GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 97.5% 193 / 4 / 202
Functions: 100.0% 23 / 0 / 23
Branches: 65.1% 267 / 0 / 410

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 180803 Scope::Scope(Scope *parent, SourceFile *sourceFile, ScopeType scopeType, const CodeLoc *codeLoc)
13
2/2
✓ Branch 3 → 4 taken 174756 times.
✓ Branch 3 → 5 taken 6047 times.
180803 : 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 174756 Scope *Scope::createChildScope(const std::string &scopeName, ScopeType scopeType, const CodeLoc *declCodeLoc) {
24
2/4
✓ Branch 2 → 3 taken 174756 times.
✗ Branch 2 → 15 not taken.
✓ Branch 3 → 4 taken 174756 times.
✗ Branch 3 → 13 not taken.
174756 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 174756 times.
174756 assert(inserted);
26 349512 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 86821 void Scope::renameChildScope(const std::string &oldName, const std::string &newName) {
37
4/8
✓ Branch 2 → 3 taken 86821 times.
✗ Branch 2 → 20 not taken.
✓ Branch 3 → 4 taken 86821 times.
✗ Branch 3 → 7 not taken.
✓ Branch 4 → 5 taken 86821 times.
✗ Branch 4 → 20 not taken.
✓ Branch 5 → 6 taken 86821 times.
✗ Branch 5 → 7 not taken.
86821 assert(children.contains(oldName) && !children.contains(newName));
38
1/2
✓ Branch 8 → 9 taken 86821 times.
✗ Branch 8 → 20 not taken.
86821 auto nodeHandler = children.extract(oldName);
39
1/2
✓ Branch 10 → 11 taken 86821 times.
✗ Branch 10 → 18 not taken.
86821 nodeHandler.key() = newName;
40
1/2
✓ Branch 13 → 14 taken 86821 times.
✗ Branch 13 → 17 not taken.
173642 children.insert(std::move(nodeHandler));
41 86821 }
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 38635 Scope *Scope::copyChildScope(const std::string &oldName, const std::string &newName) {
50
4/8
✓ Branch 2 → 3 taken 38635 times.
✗ Branch 2 → 18 not taken.
✓ Branch 3 → 4 taken 38635 times.
✗ Branch 3 → 7 not taken.
✓ Branch 4 → 5 taken 38635 times.
✗ Branch 4 → 18 not taken.
✓ Branch 5 → 6 taken 38635 times.
✗ Branch 5 → 7 not taken.
38635 assert(children.contains(oldName) && !children.contains(newName));
51 // Create copy
52
2/4
✓ Branch 8 → 9 taken 38635 times.
✗ Branch 8 → 18 not taken.
✓ Branch 10 → 11 taken 38635 times.
✗ Branch 10 → 18 not taken.
38635 const std::shared_ptr<Scope> newScope = children.at(oldName)->deepCopyScope();
53 // Save copy under new name
54
1/2
✓ Branch 11 → 12 taken 38635 times.
✗ Branch 11 → 16 not taken.
38635 children.emplace(newName, newScope);
55 77270 return newScope.get();
56 38635 }
57
58 /**
59 * Deep copy the current scope and all its children
60 *
61 * @return Deep copy of the current scope
62 */
63 156347 std::shared_ptr<Scope> Scope::deepCopyScope() { // NOLINT(misc-no-recursion)
64 156347 const auto newScope = std::make_shared<Scope>(*this);
65
2/2
✓ Branch 30 → 5 taken 117712 times.
✓ Branch 30 → 31 taken 156347 times.
274059 for (const auto &[childName, oldChild] : children) {
66
2/4
✓ Branch 9 → 10 taken 117712 times.
✗ Branch 9 → 37 not taken.
✓ Branch 11 → 12 taken 117712 times.
✗ Branch 11 → 35 not taken.
117712 newScope->children[childName] = oldChild->deepCopyScope();
67
1/2
✓ Branch 16 → 17 taken 117712 times.
✗ Branch 16 → 38 not taken.
117712 newScope->children[childName]->parent = newScope.get();
68
2/4
✓ Branch 19 → 20 taken 117712 times.
✗ Branch 19 → 38 not taken.
✓ Branch 22 → 23 taken 117712 times.
✗ Branch 22 → 38 not taken.
117712 newScope->children[childName]->symbolTable.scope = newScope->children[childName].get();
69
1/2
✓ Branch 26 → 27 taken 117712 times.
✗ Branch 26 → 38 not taken.
117712 newScope->children[childName]->symbolTable.parent = &newScope->symbolTable;
70 }
71 156347 newScope->symbolTable.scope = newScope.get();
72 156347 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 407957 Scope *Scope::getChildScope(const std::string &scopeName) const {
82
1/2
✓ Branch 2 → 3 taken 407957 times.
✗ Branch 2 → 12 not taken.
407957 const auto it = children.find(scopeName);
83
2/2
✓ Branch 5 → 6 taken 407955 times.
✓ Branch 5 → 8 taken 2 times.
815914 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 167985 std::vector<SymbolTableEntry *> Scope::getVarsGoingOutOfScope() { // NOLINT(misc-no-recursion)
92
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 167985 times.
167985 assert(!isRootScope()); // Should not be called in root scope
93 167985 std::vector<SymbolTableEntry *> varsGoingOutOfScope;
94
95 // Collect all variables in this scope
96
2/2
✓ Branch 26 → 8 taken 273565 times.
✓ Branch 26 → 27 taken 167985 times.
441550 for (const auto &[name, entry] : symbolTable.symbols) {
97 // Skip 'this' and result variables
98
8/10
✓ Branch 11 → 12 taken 273565 times.
✗ Branch 11 → 52 not taken.
✓ Branch 12 → 13 taken 215887 times.
✓ Branch 12 → 15 taken 57678 times.
✓ Branch 13 → 14 taken 215887 times.
✗ Branch 13 → 52 not taken.
✓ Branch 14 → 15 taken 54013 times.
✓ Branch 14 → 16 taken 161874 times.
✓ Branch 17 → 18 taken 111691 times.
✓ Branch 17 → 19 taken 161874 times.
273565 if (name == THIS_VARIABLE_NAME || name == RETURN_VARIABLE_NAME)
99 111691 continue;
100 // Skip parameters (ToDo: Remove when copy constructors work for by-value argument passing)
101
2/2
✓ Branch 19 → 20 taken 96597 times.
✓ Branch 19 → 21 taken 65277 times.
161874 if (entry.isParam)
102 96597 continue;
103 // Found variable, that goes out of scope
104
2/4
✓ Branch 21 → 22 taken 65277 times.
✗ Branch 21 → 51 not taken.
✓ Branch 22 → 23 taken 65277 times.
✗ Branch 22 → 51 not taken.
65277 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 1382 times.
✓ Branch 27 → 49 taken 166603 times.
167985 if (isDtorScope) {
109
2/4
✓ Branch 30 → 31 taken 1382 times.
✗ Branch 30 → 33 not taken.
✓ Branch 31 → 32 taken 1382 times.
✗ Branch 31 → 33 not taken.
1382 assert(!isRootScope() && parent->type == ScopeType::STRUCT);
110 // Get all fields of the struct
111
2/2
✓ Branch 47 → 36 taken 42127 times.
✓ Branch 47 → 48 taken 1382 times.
43509 for (const auto &[name, entry] : parent->symbolTable.symbols)
112
4/6
✓ Branch 39 → 40 taken 42127 times.
✗ Branch 39 → 55 not taken.
✓ Branch 40 → 41 taken 42127 times.
✗ Branch 40 → 53 not taken.
✓ Branch 41 → 42 taken 4422 times.
✓ Branch 41 → 45 taken 37705 times.
42127 if (!entry.getQualType().isOneOf({TY_FUNCTION, TY_PROCEDURE}))
113
2/4
✓ Branch 42 → 43 taken 4422 times.
✗ Branch 42 → 54 not taken.
✓ Branch 43 → 44 taken 4422 times.
✗ Branch 43 → 54 not taken.
4422 varsGoingOutOfScope.push_back(&parent->symbolTable.symbols.at(name));
114 }
115
116 167985 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 37261 void Scope::insertGenericType(const std::string &typeName, const GenericType &genericType) {
126
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 37261 times.
37261 assert(!genericTypes.contains(typeName));
127 37261 genericTypes.emplace(typeName, genericType);
128 37261 }
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 289171 GenericType *Scope::lookupGenericTypeStrict(const std::string &typeName) {
138
1/2
✓ Branch 2 → 3 taken 289171 times.
✗ Branch 2 → 12 not taken.
289171 const auto it = genericTypes.find(typeName);
139
2/2
✓ Branch 5 → 6 taken 96639 times.
✓ Branch 5 → 8 taken 192532 times.
578342 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 5886 void Scope::collectWarnings(std::vector<CompilerWarning> &warnings) const { // NOLINT(misc-no-recursion)
149 // Visit own symbols
150
5/8
✓ Branch 2 → 3 taken 5886 times.
✗ Branch 2 → 227 not taken.
✓ Branch 3 → 4 taken 5886 times.
✗ Branch 3 → 227 not taken.
✓ Branch 4 → 5 taken 5886 times.
✗ Branch 4 → 227 not taken.
✓ Branch 155 → 6 taken 21117 times.
✓ Branch 155 → 156 taken 5886 times.
27003 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 21117 const std::string &name = entry.name;
153
6/6
✓ Branch 7 → 8 taken 659 times.
✓ Branch 7 → 10 taken 20458 times.
✓ Branch 9 → 10 taken 242 times.
✓ Branch 9 → 11 taken 417 times.
✓ Branch 12 → 13 taken 20700 times.
✓ Branch 12 → 14 taken 417 times.
21117 if (entry.used || name.starts_with(UNUSED_VARIABLE_NAME))
154 20808 continue;
155
156
1/2
✓ Branch 14 → 15 taken 417 times.
✗ Branch 14 → 226 not taken.
417 const QualType entryType = entry.getQualType();
157
158 // When compiling a static or shared library, publicly accessible symbols form part of the library's exported
159 // API. They may legitimately go unused within the library itself, since external consumers are expected to
160 // use them, so do not report them as unused. isPublic() is only defined for the type categories checked here
161 // (see the assertion in QualType::isPublic()), so guard the call accordingly.
162
12/18
✓ Branch 15 → 16 taken 417 times.
✗ Branch 15 → 226 not taken.
✓ Branch 16 → 17 taken 172 times.
✓ Branch 16 → 19 taken 245 times.
✓ Branch 17 → 18 taken 172 times.
✗ Branch 17 → 226 not taken.
✓ Branch 18 → 19 taken 4 times.
✓ Branch 18 → 24 taken 168 times.
✓ Branch 19 → 20 taken 249 times.
✗ Branch 19 → 226 not taken.
✓ Branch 20 → 21 taken 119 times.
✓ Branch 20 → 24 taken 130 times.
✓ Branch 21 → 22 taken 119 times.
✗ Branch 21 → 226 not taken.
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 119 times.
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 27 taken 417 times.
417 if ((entryType.isExtendedPrimitive() || entryType.is(TY_ENUM)) && entryType.isPublic() && sourceFile->isLibraryOutput())
163 continue;
164
165 // Determine warning type and message by the scope type and the symbol type
166 417 CompilerWarningType warningType = UNUSED_VARIABLE;
167 417 std::string warningMessage;
168
4/4
✓ Branch 28 → 29 taken 32 times.
✓ Branch 28 → 104 taken 172 times.
✓ Branch 28 → 133 taken 6 times.
✓ Branch 28 → 139 taken 207 times.
417 switch (type) {
169 32 case ScopeType::GLOBAL: {
170 // Skip generic function/procedure/struct/interface entries
171
6/10
✓ Branch 29 → 30 taken 32 times.
✗ Branch 29 → 169 not taken.
✓ Branch 30 → 31 taken 18 times.
✓ Branch 30 → 35 taken 14 times.
✓ Branch 31 → 32 taken 18 times.
✗ Branch 31 → 169 not taken.
✗ Branch 33 → 34 not taken.
✓ Branch 33 → 35 taken 18 times.
✗ Branch 36 → 37 not taken.
✓ Branch 36 → 38 taken 32 times.
32 if (entryType.isOneOf({TY_FUNCTION, TY_PROCEDURE, TY_STRUCT, TY_INTERFACE}) && !entryType.getTemplateTypes().empty())
172 continue;
173
174
3/4
✓ Branch 38 → 39 taken 32 times.
✗ Branch 38 → 224 not taken.
✓ Branch 39 → 40 taken 10 times.
✓ Branch 39 → 50 taken 22 times.
32 if (entryType.is(TY_FUNCTION)) {
175
1/2
✓ Branch 40 → 41 taken 10 times.
✗ Branch 40 → 224 not taken.
10 const std::vector<Function *> *fctManifestations = entry.declNode->getFctManifestations(name);
176 10 warningType = UNUSED_FUNCTION;
177
3/6
✓ Branch 42 → 43 taken 10 times.
✗ Branch 42 → 174 not taken.
✓ Branch 43 → 44 taken 10 times.
✗ Branch 43 → 172 not taken.
✓ Branch 44 → 45 taken 10 times.
✗ Branch 44 → 170 not taken.
10 warningMessage = "'" + fctManifestations->front()->getSignature() + "' is unused";
178
3/4
✓ Branch 50 → 51 taken 22 times.
✗ Branch 50 → 224 not taken.
✓ Branch 51 → 52 taken 4 times.
✓ Branch 51 → 62 taken 18 times.
22 } else if (entryType.is(TY_PROCEDURE)) {
179
1/2
✓ Branch 52 → 53 taken 4 times.
✗ Branch 52 → 224 not taken.
4 const std::vector<Function *> *fctManifestations = entry.declNode->getFctManifestations(name);
180 4 warningType = UNUSED_PROCEDURE;
181
3/6
✓ Branch 54 → 55 taken 4 times.
✗ Branch 54 → 181 not taken.
✓ Branch 55 → 56 taken 4 times.
✗ Branch 55 → 179 not taken.
✓ Branch 56 → 57 taken 4 times.
✗ Branch 56 → 177 not taken.
4 warningMessage = "'" + fctManifestations->front()->getSignature() + "' is unused";
182
3/4
✓ Branch 62 → 63 taken 18 times.
✗ Branch 62 → 224 not taken.
✓ Branch 63 → 64 taken 2 times.
✓ Branch 63 → 70 taken 16 times.
18 } else if (entryType.is(TY_STRUCT)) {
183 2 warningType = UNUSED_STRUCT;
184
2/4
✓ Branch 64 → 65 taken 2 times.
✗ Branch 64 → 186 not taken.
✓ Branch 65 → 66 taken 2 times.
✗ Branch 65 → 184 not taken.
2 warningMessage = "The struct '" + entry.name + "' is unused";
185
3/4
✓ Branch 70 → 71 taken 16 times.
✗ Branch 70 → 224 not taken.
✓ Branch 71 → 72 taken 2 times.
✓ Branch 71 → 78 taken 14 times.
16 } else if (entryType.is(TY_INTERFACE)) {
186 2 warningType = UNUSED_INTERFACE;
187
2/4
✓ Branch 72 → 73 taken 2 times.
✗ Branch 72 → 190 not taken.
✓ Branch 73 → 74 taken 2 times.
✗ Branch 73 → 188 not taken.
2 warningMessage = "The interface '" + entry.name + "' is unused";
188
3/4
✓ Branch 78 → 79 taken 14 times.
✗ Branch 78 → 224 not taken.
✓ Branch 79 → 80 taken 4 times.
✓ Branch 79 → 81 taken 10 times.
14 } else if (entryType.is(TY_ENUM)) {
189 4 continue; // Do not report unused enums. Only unused enum items are reported
190
3/4
✓ Branch 81 → 82 taken 10 times.
✗ Branch 81 → 224 not taken.
✓ Branch 82 → 83 taken 8 times.
✓ Branch 82 → 89 taken 2 times.
10 } else if (entryType.is(TY_IMPORT)) {
191 8 warningType = UNUSED_IMPORT;
192
2/4
✓ Branch 83 → 84 taken 8 times.
✗ Branch 83 → 194 not taken.
✓ Branch 84 → 85 taken 8 times.
✗ Branch 84 → 192 not taken.
8 warningMessage = "The import '" + entry.name + "' is unused";
193
2/4
✓ Branch 89 → 90 taken 2 times.
✗ Branch 89 → 224 not taken.
✓ Branch 90 → 91 taken 2 times.
✗ Branch 90 → 97 not taken.
2 } else if (entryType.is(TY_ALIAS)) {
194 2 warningType = UNUSED_ALIAS;
195
2/4
✓ Branch 91 → 92 taken 2 times.
✗ Branch 91 → 198 not taken.
✓ Branch 92 → 93 taken 2 times.
✗ Branch 92 → 196 not taken.
2 warningMessage = "The type alias '" + entry.name + "' is unused";
196 } else {
197 warningType = UNUSED_VARIABLE;
198 warningMessage = "The variable '" + entry.name + "' is unused";
199 }
200
201 28 break;
202 }
203 172 case ScopeType::STRUCT: // fall-through
204 case ScopeType::INTERFACE: {
205
3/4
✓ Branch 104 → 105 taken 172 times.
✗ Branch 104 → 224 not taken.
✓ Branch 105 → 106 taken 48 times.
✓ Branch 105 → 112 taken 124 times.
172 if (entry.isField()) {
206 48 warningType = UNUSED_FIELD;
207
2/4
✓ Branch 106 → 107 taken 48 times.
✗ Branch 106 → 206 not taken.
✓ Branch 107 → 108 taken 48 times.
✗ Branch 107 → 204 not taken.
48 warningMessage = "The field '" + entry.name + "' is unused";
208
2/4
✓ Branch 112 → 113 taken 124 times.
✗ Branch 112 → 208 not taken.
✓ Branch 113 → 114 taken 124 times.
✗ Branch 113 → 132 not taken.
124 } else if (entryType.isOneOf({TY_FUNCTION, TY_PROCEDURE})) {
209 // Skip implicit method entries and generic templates
210
1/2
✓ Branch 114 → 115 taken 124 times.
✗ Branch 114 → 224 not taken.
124 const std::vector<Function *> *fctManifestations = entry.declNode->getFctManifestations(name);
211
5/6
✓ Branch 116 → 117 taken 20 times.
✓ Branch 116 → 119 taken 104 times.
✗ Branch 118 → 119 not taken.
✓ Branch 118 → 120 taken 20 times.
✓ Branch 121 → 122 taken 104 times.
✓ Branch 121 → 123 taken 20 times.
124 if (fctManifestations->empty() || fctManifestations->front()->implicitDefault)
212 104 continue;
213
214 20 warningType = UNUSED_METHOD;
215
3/6
✓ Branch 124 → 125 taken 20 times.
✗ Branch 124 → 213 not taken.
✓ Branch 125 → 126 taken 20 times.
✗ Branch 125 → 211 not taken.
✓ Branch 126 → 127 taken 20 times.
✗ Branch 126 → 209 not taken.
20 warningMessage = "The method '" + fctManifestations->front()->getSignature() + "' is unused";
216 }
217 68 break;
218 }
219 6 case ScopeType::ENUM: {
220 6 warningType = UNUSED_ENUM_ITEM;
221
2/4
✓ Branch 133 → 134 taken 6 times.
✗ Branch 133 → 218 not taken.
✓ Branch 134 → 135 taken 6 times.
✗ Branch 134 → 216 not taken.
6 warningMessage = "The enum item '" + entry.name + "' is unused";
222 6 break;
223 }
224 207 default: {
225 207 warningType = UNUSED_VARIABLE;
226
2/4
✓ Branch 139 → 140 taken 207 times.
✗ Branch 139 → 222 not taken.
✓ Branch 140 → 141 taken 207 times.
✗ Branch 140 → 220 not taken.
207 warningMessage = "The variable '" + entry.name + "' is unused";
227 207 break;
228 }
229 }
230
231 // Add warning
232
2/4
✓ Branch 145 → 146 taken 309 times.
✗ Branch 145 → 224 not taken.
✓ Branch 146 → 147 taken 309 times.
✗ Branch 146 → 224 not taken.
309 warnings.emplace_back(entry.getDeclCodeLoc(), warningType, warningMessage);
233
2/2
✓ Branch 149 → 150 taken 309 times.
✓ Branch 149 → 152 taken 108 times.
417 }
234
235 // Visit children
236
5/8
✓ Branch 156 → 157 taken 5886 times.
✗ Branch 156 → 228 not taken.
✓ Branch 157 → 158 taken 5886 times.
✗ Branch 157 → 228 not taken.
✓ Branch 158 → 159 taken 5886 times.
✗ Branch 158 → 228 not taken.
✓ Branch 167 → 160 taken 5433 times.
✓ Branch 167 → 168 taken 5886 times.
11319 for (const auto &childScope : children | std::views::values)
237
2/2
✓ Branch 162 → 163 taken 5043 times.
✓ Branch 162 → 165 taken 390 times.
5433 if (!childScope->isGenericScope)
238
1/2
✓ Branch 164 → 165 taken 5043 times.
✗ Branch 164 → 228 not taken.
5043 childScope->collectWarnings(warnings);
239 5886 }
240
241 /**
242 * Checks if all variables of this and all child scopes are of an explicit type.
243 * This is executed after type inference to check that all variables could be inferred correctly.
244 */
245 1127267 void Scope::ensureSuccessfulTypeInference() const { // NOLINT(misc-no-recursion)
246 // Check symbols in this scope
247
2/2
✓ Branch 19 → 4 taken 2357054 times.
✓ Branch 19 → 20 taken 1127265 times.
3484319 for (auto &[name, entry] : symbolTable.symbols)
248
4/6
✓ Branch 7 → 8 taken 2357054 times.
✗ Branch 7 → 40 not taken.
✓ Branch 8 → 9 taken 2357054 times.
✗ Branch 8 → 40 not taken.
✓ Branch 9 → 10 taken 2 times.
✓ Branch 9 → 17 taken 2357052 times.
2357054 if (entry.getQualType().is(TY_DYN))
249
3/6
✓ Branch 11 → 12 taken 2 times.
✗ Branch 11 → 36 not taken.
✓ Branch 12 → 13 taken 2 times.
✗ Branch 12 → 34 not taken.
✓ Branch 13 → 14 taken 2 times.
✗ Branch 13 → 31 not taken.
2 throw SemanticError(entry.declNode, UNEXPECTED_DYN_TYPE, "For the variable '" + name + "' no type could be inferred");
250
251 // Check child scopes
252
5/8
✓ Branch 20 → 21 taken 1127265 times.
✗ Branch 20 → 41 not taken.
✓ Branch 21 → 22 taken 1127265 times.
✗ Branch 21 → 41 not taken.
✓ Branch 22 → 23 taken 1127265 times.
✗ Branch 22 → 41 not taken.
✓ Branch 29 → 24 taken 1110739 times.
✓ Branch 29 → 30 taken 1127263 times.
2238002 for (const auto &scope : children | std::views::values)
253
2/2
✓ Branch 26 → 27 taken 1110737 times.
✓ Branch 26 → 41 taken 2 times.
1110739 scope->ensureSuccessfulTypeInference();
254 1127263 }
255
256 /**
257 * Get the number of fields if this is a struct scope
258 *
259 * @return Number of fields
260 */
261 678294 size_t Scope::getFieldCount() const {
262
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 678294 times.
678294 assert(type == ScopeType::STRUCT);
263 678294 size_t fieldCount = 0;
264
5/8
✓ Branch 4 → 5 taken 678294 times.
✗ Branch 4 → 29 not taken.
✓ Branch 5 → 6 taken 678294 times.
✗ Branch 5 → 29 not taken.
✓ Branch 6 → 7 taken 678294 times.
✗ Branch 6 → 29 not taken.
✓ Branch 26 → 8 taken 12577556 times.
✓ Branch 26 → 27 taken 678294 times.
13255850 for (const auto &symbol : symbolTable.symbols | std::views::values) {
265
2/2
✓ Branch 9 → 10 taken 4296 times.
✓ Branch 9 → 11 taken 12573260 times.
12577556 if (symbol.anonymous)
266 4296 continue;
267
1/2
✓ Branch 11 → 12 taken 12573260 times.
✗ Branch 11 → 29 not taken.
12573260 const QualType &symbolType = symbol.getQualType();
268
2/4
✓ Branch 12 → 13 taken 12573260 times.
✗ Branch 12 → 29 not taken.
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 12573260 times.
12573260 if (symbolType.is(TY_IMPORT))
269 continue;
270 12573260 const ASTNode *declNode = symbol.declNode;
271
8/10
✓ Branch 15 → 16 taken 12573260 times.
✗ Branch 15 → 29 not taken.
✓ Branch 16 → 17 taken 2696159 times.
✓ Branch 16 → 19 taken 9877101 times.
✓ Branch 17 → 18 taken 2696159 times.
✗ Branch 17 → 29 not taken.
✓ Branch 18 → 19 taken 397850 times.
✓ Branch 18 → 20 taken 2298309 times.
✓ Branch 21 → 22 taken 10274951 times.
✓ Branch 21 → 23 taken 2298309 times.
12573260 if (declNode->isFctOrProcDef() || declNode->isStructDef())
272 10274951 continue;
273 2298309 fieldCount++;
274 }
275 678294 return fieldCount;
276 }
277
278 /**
279 * Get all virtual methods in this scope, sorted by their VTable index
280 *
281 * The VTable index is assigned based on the declaration order of the methods in the implemented interface(s). This must match the
282 * order the call sites use to index into the VTable, which may differ from the order the methods are defined in within the struct.
283 *
284 * @return List of virtual method pointers
285 */
286 9044 std::vector<const Function *> Scope::getVirtualMethods() {
287
3/4
✓ Branch 2 → 3 taken 4520 times.
✓ Branch 2 → 5 taken 4524 times.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 4520 times.
9044 assert(type == ScopeType::STRUCT || type == ScopeType::INTERFACE);
288
289 // Collect all virtual methods
290 9044 std::vector<const Function *> methods;
291
2/2
✓ Branch 37 → 7 taken 75658 times.
✓ Branch 37 → 38 taken 9044 times.
84702 for (auto &[fctId, manifestationList] : functions) {
292
1/2
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 75658 times.
75658 assert(!manifestationList.empty());
293
2/2
✓ Branch 34 → 15 taken 103994 times.
✓ Branch 34 → 35 taken 75658 times.
179652 for (auto &[mangledName, function] : manifestationList)
294
2/2
✓ Branch 27 → 28 taken 29268 times.
✓ Branch 27 → 32 taken 74726 times.
103994 if (function.isVirtualMethod())
295
3/6
✓ Branch 28 → 29 taken 29268 times.
✗ Branch 28 → 41 not taken.
✓ Branch 29 → 30 taken 29268 times.
✗ Branch 29 → 41 not taken.
✓ Branch 30 → 31 taken 29268 times.
✗ Branch 30 → 41 not taken.
29268 methods.push_back(&functions.at(fctId).at(mangledName));
296 }
297
298 // Sort the list by VTable index, so that the VTable layout matches the slot indices used at the call sites
299 84112 const auto pred = [](const Function *a, const Function *b) { return a->vtableIndex < b->vtableIndex; };
300
1/2
✓ Branch 38 → 39 taken 9044 times.
✗ Branch 38 → 44 not taken.
9044 std::ranges::sort(methods, pred);
301
302 9044 return methods;
303 } // LCOV_EXCL_LINE - false positive
304
305 /**
306 * Retrieve all struct manifestations in this scope in the order of their declaration
307 *
308 * @return All struct manifestations in declaration order
309 */
310 5967 std::vector<Struct *> Scope::getAllStructManifestationsInDeclarationOrder() {
311 // Retrieve all struct manifestations in this scope
312 5967 std::vector<Struct *> manifestations;
313
1/2
✓ Branch 3 → 4 taken 5967 times.
✗ Branch 3 → 27 not taken.
5967 manifestations.reserve(structs.size()); // Reserve at least the size of individual generic structs
314
5/8
✓ Branch 4 → 5 taken 5967 times.
✗ Branch 4 → 26 not taken.
✓ Branch 5 → 6 taken 5967 times.
✗ Branch 5 → 26 not taken.
✓ Branch 6 → 7 taken 5967 times.
✗ Branch 6 → 26 not taken.
✓ Branch 20 → 8 taken 7467 times.
✓ Branch 20 → 21 taken 5967 times.
13434 for (auto &structManifestations : structs | std::views::values)
315
5/8
✓ Branch 9 → 10 taken 7467 times.
✗ Branch 9 → 25 not taken.
✓ Branch 10 → 11 taken 7467 times.
✗ Branch 10 → 25 not taken.
✓ Branch 11 → 12 taken 7467 times.
✗ Branch 11 → 25 not taken.
✓ Branch 17 → 13 taken 7479 times.
✓ Branch 17 → 18 taken 7467 times.
14946 for (auto &manifestation : structManifestations | std::views::values)
316
1/2
✓ Branch 14 → 15 taken 7479 times.
✗ Branch 14 → 24 not taken.
7479 manifestations.push_back(&manifestation);
317
318 // Sort manifestations by declaration code location
319
2/2
✓ Branch 4 → 5 taken 20 times.
✓ Branch 4 → 6 taken 23286 times.
46612 auto sortLambda = [](const Struct *lhs, const Struct *rhs) { return lhs->getDeclCodeLoc() < rhs->getDeclCodeLoc(); };
320
1/2
✓ Branch 21 → 22 taken 5967 times.
✗ Branch 21 → 27 not taken.
5967 std::ranges::sort(manifestations, sortLambda);
321 5967 return manifestations;
322 } // LCOV_EXCL_LINE - false positive
323
324 /**
325 * Get the current number of nested loops
326 *
327 * @return Number of loops
328 */
329 14782 unsigned int Scope::getLoopNestingDepth() const { // NOLINT(misc-no-recursion)
330
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 14782 times.
14782 assert(!isRootScope());
331
2/2
✓ Branch 6 → 7 taken 2826 times.
✓ Branch 6 → 8 taken 11956 times.
14782 if (parent->parent == nullptr)
332 2826 return 0;
333 11956 unsigned int loopCount = parent->getLoopNestingDepth();
334
6/6
✓ Branch 9 → 10 taken 10843 times.
✓ Branch 9 → 12 taken 1113 times.
✓ Branch 10 → 11 taken 7707 times.
✓ Branch 10 → 12 taken 3136 times.
✓ Branch 11 → 12 taken 192 times.
✓ Branch 11 → 13 taken 7515 times.
11956 if (type == ScopeType::WHILE_BODY || type == ScopeType::FOR_BODY || type == ScopeType::FOREACH_BODY)
335 4441 loopCount++;
336 11956 return loopCount;
337 }
338
339 /**
340 * Get the nearest enclosing function/procedure/lambda body scope, including this scope itself
341 *
342 * @return Nearest enclosing function/procedure/lambda body scope
343 */
344 108644 Scope *Scope::getFunctionScope() { // NOLINT(misc-no-recursion)
345
4/4
✓ Branch 2 → 3 taken 34335 times.
✓ Branch 2 → 4 taken 74309 times.
✓ Branch 3 → 4 taken 100 times.
✓ Branch 3 → 5 taken 34235 times.
108644 if (type == ScopeType::FUNC_PROC_BODY || type == ScopeType::LAMBDA_BODY)
346 74409 return this;
347
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 34235 times.
34235 assert(!isRootScope());
348 34235 return parent->getFunctionScope();
349 }
350
351 /**
352 * Check if this scope is one of the child scopes of a switch statement
353 *
354 * @return Child scope of switch statement or not
355 */
356 14 bool Scope::isInCaseBranch() const { // NOLINT(misc-no-recursion)
357
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 14 times.
14 assert(!isRootScope());
358
2/2
✓ Branch 6 → 7 taken 4 times.
✓ Branch 6 → 8 taken 10 times.
14 if (parent->parent == nullptr)
359 4 return false;
360
2/2
✓ Branch 8 → 9 taken 8 times.
✓ Branch 8 → 10 taken 2 times.
10 if (type == ScopeType::CASE_BODY)
361 8 return true;
362 2 return parent->isInCaseBranch();
363 }
364
365 /**
366 * Check if this scope is within an async scope
367 *
368 * @return Within async scope or not
369 */
370 454 bool Scope::isInAsyncScope() const { // NOLINT(misc-no-recursion)
371
2/2
✓ Branch 2 → 3 taken 22 times.
✓ Branch 2 → 4 taken 432 times.
454 if (isAsyncScope)
372 22 return true;
373
3/4
✓ Branch 6 → 7 taken 302 times.
✓ Branch 6 → 10 taken 130 times.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 302 times.
432 return !isRootScope() && parent->isInAsyncScope();
374 }
375
376 /**
377 * Check if unsafe operations are allowed in this scope
378 *
379 * @return Allowed or not
380 */
381 35116 bool Scope::doesAllowUnsafeOperations() const { // NOLINT(misc-no-recursion)
382
2/2
✓ Branch 2 → 3 taken 24043 times.
✓ Branch 2 → 4 taken 11073 times.
35116 if (type == ScopeType::UNSAFE_BODY)
383 24043 return true;
384
4/4
✓ Branch 6 → 7 taken 11009 times.
✓ Branch 6 → 10 taken 64 times.
✓ Branch 8 → 9 taken 10881 times.
✓ Branch 8 → 10 taken 128 times.
11073 return !isRootScope() && parent->doesAllowUnsafeOperations();
385 }
386
387 /**
388 * Checks if this scope is imported
389 *
390 * @param askingScope Scope, which asks whether the current one is imported from its point of view or not
391 *
392 * @return Imported / not imported
393 */
394 759366 bool Scope::isImportedBy(const Scope *askingScope) const { return askingScope->sourceFile->imports(sourceFile); }
395
396 /**
397 * Get JSON representation of the symbol table
398 *
399 * @return Symbol table as JSON object
400 */
401 1127311 nlohmann::json Scope::getSymbolTableJSON() const { // NOLINT(misc-no-recursion)
402
1/2
✓ Branch 2 → 3 taken 1127311 times.
✗ Branch 2 → 42 not taken.
1127311 nlohmann::json result = symbolTable.toJSON();
403
404 // Collect all children
405 1127311 std::vector<nlohmann::json> jsonChildren;
406
1/2
✓ Branch 4 → 5 taken 1127311 times.
✗ Branch 4 → 38 not taken.
1127311 jsonChildren.reserve(children.size());
407
2/2
✓ Branch 20 → 7 taken 1110783 times.
✓ Branch 20 → 21 taken 1127311 times.
2238094 for (const auto &[name, childScope] : children) {
408
1/2
✓ Branch 11 → 12 taken 1110783 times.
✗ Branch 11 → 33 not taken.
1110783 nlohmann::json c = childScope->getSymbolTableJSON();
409
2/4
✓ Branch 12 → 13 taken 1110783 times.
✗ Branch 12 → 30 not taken.
✓ Branch 13 → 14 taken 1110783 times.
✗ Branch 13 → 28 not taken.
1110783 c["name"] = name; // Inject symbol table name into JSON object
410
1/2
✓ Branch 16 → 17 taken 1110783 times.
✗ Branch 16 → 31 not taken.
1110783 jsonChildren.emplace_back(c);
411 1110783 }
412
2/4
✓ Branch 21 → 22 taken 1127311 times.
✗ Branch 21 → 37 not taken.
✓ Branch 22 → 23 taken 1127311 times.
✗ Branch 22 → 35 not taken.
1127311 result["children"] = jsonChildren;
413
414 1127311 return result;
415 1127311 }
416
417 } // namespace spice::compiler
418