GCC Code Coverage Report


Directory: ../
File: src/irgenerator/GenImplicit.cpp
Date: 2025-12-29 16:28:41
Coverage Exec Excl Total
Lines: 91.7% 386 0 421
Functions: 95.7% 22 0 23
Branches: 49.7% 506 0 1018

Line Branch Exec Source
1 // Copyright (c) 2021-2025 ChilliBits. All rights reserved.
2
3 #include "IRGenerator.h"
4
5 #include <SourceFile.h>
6 #include <ast/ASTNodes.h>
7 #include <ast/Attributes.h>
8 #include <driver/Driver.h>
9 #include <global/GlobalResourceManager.h>
10 #include <model/Function.h>
11 #include <symboltablebuilder/SymbolTableBuilder.h>
12
13 #include <llvm/IR/Module.h>
14
15 namespace spice::compiler {
16
17 // String placeholders for builtin testing output
18 static const char *const TEST_ALL_START_MSG = "[==========] Running %d test(s) from %d source file(s)\n";
19 static const char *const TEST_ALL_END_MSG = "[==========] Ran %d test(s) from %d source file(s)\n";
20 static const char *const TEST_FILE_START_MSG = "[----------] Running %d test(s) from %s\n";
21 static const char *const TEST_FILE_END_MSG = "[----------] Ran %d test(s) from %s\n\n";
22 static const char *const TEST_CASE_RUN_MSG = "[ RUN ] %s\n";
23 static const char *const TEST_CASE_SUCCESS_MSG = "\033[1m\033[32m[ PASSED ]\033[0m\033[22m %s\n";
24 static const char *const TEST_CASE_FAILED_MSG = "\033[1m\033[31m[ FAILED ]\033[0m\033[22m %s\n";
25 static const char *const TEST_CASE_SKIPPED_MSG = "\033[1m\033[33m[ SKIPPED ]\033[0m\033[22m %s\n";
26
27 13 llvm::Value *IRGenerator::doImplicitCast(llvm::Value *src, QualType dstSTy, QualType srcSTy) {
28
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 13 times.
13 assert(srcSTy != dstSTy); // We only need to cast implicitly, if the types do not match exactly
29
30 // Unpack the pointers until a pointer of another type is met
31 13 size_t loadCounter = 0;
32
1/2
✗ Branch 16 → 6 not taken.
✓ Branch 16 → 17 taken 13 times.
13 while (srcSTy.isPtr()) {
33 src = insertLoad(srcSTy, src);
34 srcSTy = srcSTy.getContained();
35 dstSTy = dstSTy.getContained();
36 loadCounter++;
37 }
38 // GEP or bit-cast
39
3/6
✓ Branch 18 → 19 taken 13 times.
✗ Branch 18 → 22 not taken.
✓ Branch 20 → 21 taken 13 times.
✗ Branch 20 → 22 not taken.
✓ Branch 23 → 24 taken 13 times.
✗ Branch 23 → 35 not taken.
13 if (dstSTy.isArray() && srcSTy.isArray()) { // Special case that is used for passing arrays as pointer to functions
40
2/4
✓ Branch 24 → 25 taken 13 times.
✗ Branch 24 → 72 not taken.
✓ Branch 25 → 26 taken 13 times.
✗ Branch 25 → 72 not taken.
13 llvm::Value *indices[2] = {builder.getInt64(0), builder.getInt32(0)};
41
2/4
✓ Branch 30 → 31 taken 13 times.
✗ Branch 30 → 65 not taken.
✓ Branch 31 → 32 taken 13 times.
✗ Branch 31 → 65 not taken.
13 src = insertInBoundsGEP(srcSTy.toLLVMType(sourceFile), src, indices);
42 } else {
43 src = insertLoad(srcSTy, src);
44 src = builder.CreateBitCast(src, dstSTy.toLLVMType(sourceFile));
45 }
46 // Pack the pointers together again
47
1/2
✗ Branch 54 → 46 not taken.
✓ Branch 54 → 55 taken 13 times.
13 for (; loadCounter > 0; loadCounter--) {
48 llvm::Value *newActualArg = insertAlloca(srcSTy);
49 insertStore(src, newActualArg);
50 src = newActualArg;
51 }
52 13 return src;
53 }
54
55 30488 void IRGenerator::generateScopeCleanup(const StmtLstNode *node) const {
56 // Do not clean up if the block is already terminated
57
2/2
✓ Branch 2 → 3 taken 10976 times.
✓ Branch 2 → 4 taken 19512 times.
30488 if (blockAlreadyTerminated)
58 10976 return;
59
60 // Call all dtor functions
61 19512 const auto &[dtorFunctionsToCall, heapVarsToFree] = node->resourcesToCleanup.at(manIdx);
62
2/2
✓ Branch 15 → 7 taken 1007 times.
✓ Branch 15 → 16 taken 19512 times.
20519 for (auto [entry, dtor] : dtorFunctionsToCall)
63
1/2
✓ Branch 11 → 12 taken 1007 times.
✗ Branch 11 → 45 not taken.
1007 generateCtorOrDtorCall(entry, dtor, {});
64
65 // Deallocate all heap variables that go out of scope and are currently owned
66
2/2
✓ Branch 23 → 18 taken 4 times.
✓ Branch 23 → 24 taken 19512 times.
19516 for (const SymbolTableEntry *entry : heapVarsToFree)
67
2/4
✓ Branch 19 → 20 taken 4 times.
✗ Branch 19 → 49 not taken.
✓ Branch 20 → 21 taken 4 times.
✗ Branch 20 → 49 not taken.
4 generateDeallocCall(entry->getAddress());
68
69 // Generate lifetime end markers
70
2/2
✓ Branch 24 → 25 taken 33 times.
✓ Branch 24 → 44 taken 19479 times.
19512 if (cliOptions.useLifetimeMarkers) {
71
3/4
✓ Branch 25 → 26 taken 33 times.
✗ Branch 25 → 53 not taken.
✓ Branch 41 → 28 taken 9 times.
✓ Branch 41 → 42 taken 33 times.
42 for (const SymbolTableEntry *var : currentScope->getVarsGoingOutOfScope()) {
72
1/2
✓ Branch 29 → 30 taken 9 times.
✗ Branch 29 → 51 not taken.
9 llvm::Value *address = var->getAddress();
73
1/2
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 32 taken 9 times.
9 if (address == nullptr)
74 continue;
75
4/8
✓ Branch 33 → 34 taken 9 times.
✗ Branch 33 → 50 not taken.
✓ Branch 34 → 35 taken 9 times.
✗ Branch 34 → 50 not taken.
✓ Branch 35 → 36 taken 9 times.
✗ Branch 35 → 50 not taken.
✓ Branch 36 → 37 taken 9 times.
✗ Branch 36 → 50 not taken.
9 const uint64_t sizeInBytes = module->getDataLayout().getTypeAllocSize(var->getQualType().toLLVMType(sourceFile));
76
2/4
✓ Branch 37 → 38 taken 9 times.
✗ Branch 37 → 51 not taken.
✓ Branch 38 → 39 taken 9 times.
✗ Branch 38 → 51 not taken.
9 builder.CreateLifetimeEnd(address, builder.getInt64(sizeInBytes));
77 33 }
78 }
79 }
80
81 1719 void IRGenerator::generateFctDecl(const Function *fct, const std::vector<llvm::Value *> &args) const {
82 // Retrieve metadata for the function
83
1/2
✓ Branch 2 → 3 taken 1719 times.
✗ Branch 2 → 82 not taken.
1719 const std::string mangledName = fct->getMangledName();
84
85 // Function is not defined in the current module -> declare it
86
3/4
✓ Branch 4 → 5 taken 1719 times.
✗ Branch 4 → 63 not taken.
✓ Branch 5 → 6 taken 429 times.
✓ Branch 5 → 61 taken 1290 times.
1719 if (!module->getFunction(mangledName)) {
87 429 std::vector<llvm::Type *> paramTypes;
88
2/2
✓ Branch 13 → 8 taken 514 times.
✓ Branch 13 → 14 taken 429 times.
943 for (const llvm::Value *argValue : args)
89
1/2
✓ Branch 10 → 11 taken 514 times.
✗ Branch 10 → 64 not taken.
514 paramTypes.push_back(argValue->getType());
90
2/6
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 20 taken 429 times.
✗ Branch 18 → 19 not taken.
✗ Branch 18 → 77 not taken.
✓ Branch 20 → 21 taken 429 times.
✗ Branch 20 → 77 not taken.
429 llvm::Type *returnType = fct->isFunction() ? fct->returnType.toLLVMType(sourceFile) : builder.getVoidTy();
91
1/2
✓ Branch 23 → 24 taken 429 times.
✗ Branch 23 → 66 not taken.
429 llvm::FunctionType *fctType = llvm::FunctionType::get(returnType, paramTypes, false);
92
2/4
✓ Branch 25 → 26 taken 429 times.
✗ Branch 25 → 67 not taken.
✓ Branch 26 → 27 taken 429 times.
✗ Branch 26 → 77 not taken.
429 module->getOrInsertFunction(mangledName, fctType);
93
94
1/2
✓ Branch 29 → 30 taken 429 times.
✗ Branch 29 → 59 not taken.
429 if (fct->isMethod()) {
95 // Get callee function
96
1/2
✓ Branch 31 → 32 taken 429 times.
✗ Branch 31 → 68 not taken.
429 llvm::Function *callee = module->getFunction(mangledName);
97
1/2
✗ Branch 32 → 33 not taken.
✓ Branch 32 → 34 taken 429 times.
429 assert(callee != nullptr);
98
99 // Set attributes to 'this' param
100 // Get 'this' entry
101
1/2
✓ Branch 36 → 37 taken 429 times.
✗ Branch 36 → 71 not taken.
1287 const SymbolTableEntry *thisEntry = fct->bodyScope->lookupStrict(THIS_VARIABLE_NAME);
102
1/2
✗ Branch 42 → 43 not taken.
✓ Branch 42 → 44 taken 429 times.
429 assert(thisEntry != nullptr);
103
3/6
✓ Branch 44 → 45 taken 429 times.
✗ Branch 44 → 75 not taken.
✓ Branch 45 → 46 taken 429 times.
✗ Branch 45 → 75 not taken.
✓ Branch 46 → 47 taken 429 times.
✗ Branch 46 → 75 not taken.
429 llvm::Type *structType = thisEntry->getQualType().getContained().toLLVMType(sourceFile);
104
1/2
✗ Branch 47 → 48 not taken.
✓ Branch 47 → 49 taken 429 times.
429 assert(structType != nullptr);
105
1/2
✓ Branch 49 → 50 taken 429 times.
✗ Branch 49 → 77 not taken.
429 callee->addParamAttr(0, llvm::Attribute::NoUndef);
106
1/2
✓ Branch 50 → 51 taken 429 times.
✗ Branch 50 → 77 not taken.
429 callee->addParamAttr(0, llvm::Attribute::NonNull);
107
3/6
✓ Branch 52 → 53 taken 429 times.
✗ Branch 52 → 76 not taken.
✓ Branch 53 → 54 taken 429 times.
✗ Branch 53 → 76 not taken.
✓ Branch 54 → 55 taken 429 times.
✗ Branch 54 → 76 not taken.
429 callee->addDereferenceableParamAttr(0, module->getDataLayout().getTypeStoreSize(structType));
108
3/6
✓ Branch 56 → 57 taken 429 times.
✗ Branch 56 → 77 not taken.
✓ Branch 57 → 58 taken 429 times.
✗ Branch 57 → 77 not taken.
✓ Branch 58 → 59 taken 429 times.
✗ Branch 58 → 77 not taken.
429 callee->addParamAttr(0, llvm::Attribute::getWithAlignment(context, module->getDataLayout().getABITypeAlign(structType)));
109 }
110 429 }
111 1719 }
112
113 1719 llvm::CallInst *IRGenerator::generateFctCall(const Function *fct, const std::vector<llvm::Value *> &args) const {
114 // Retrieve metadata for the function
115
1/2
✓ Branch 2 → 3 taken 1719 times.
✗ Branch 2 → 57 not taken.
1719 const std::string mangledName = fct->getMangledName();
116
117 // Get callee function
118
1/2
✓ Branch 4 → 5 taken 1719 times.
✗ Branch 4 → 43 not taken.
1719 llvm::Function *callee = module->getFunction(mangledName);
119
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 1719 times.
1719 assert(callee != nullptr);
120
121 // Generate function call
122
4/8
✓ Branch 7 → 8 taken 1719 times.
✗ Branch 7 → 46 not taken.
✓ Branch 9 → 10 taken 1719 times.
✗ Branch 9 → 44 not taken.
✓ Branch 10 → 11 taken 1719 times.
✗ Branch 10 → 44 not taken.
✓ Branch 11 → 12 taken 1719 times.
✗ Branch 11 → 55 not taken.
1719 llvm::CallInst *callInst = builder.CreateCall(callee, args);
123
124 // Set attributes to 'this' param
125
1/2
✓ Branch 14 → 15 taken 1719 times.
✗ Branch 14 → 40 not taken.
1719 if (fct->isMethod()) {
126 // Get 'this' entry
127
1/2
✓ Branch 17 → 18 taken 1719 times.
✗ Branch 17 → 49 not taken.
5157 const SymbolTableEntry *thisEntry = fct->bodyScope->lookupStrict(THIS_VARIABLE_NAME);
128
1/2
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 1719 times.
1719 assert(thisEntry != nullptr);
129
3/6
✓ Branch 25 → 26 taken 1719 times.
✗ Branch 25 → 53 not taken.
✓ Branch 26 → 27 taken 1719 times.
✗ Branch 26 → 53 not taken.
✓ Branch 27 → 28 taken 1719 times.
✗ Branch 27 → 53 not taken.
1719 llvm::Type *structType = thisEntry->getQualType().getContained().toLLVMType(sourceFile);
130
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 1719 times.
1719 assert(structType != nullptr);
131
1/2
✓ Branch 30 → 31 taken 1719 times.
✗ Branch 30 → 55 not taken.
1719 callInst->addParamAttr(0, llvm::Attribute::NoUndef);
132
1/2
✓ Branch 31 → 32 taken 1719 times.
✗ Branch 31 → 55 not taken.
1719 callInst->addParamAttr(0, llvm::Attribute::NonNull);
133
3/6
✓ Branch 33 → 34 taken 1719 times.
✗ Branch 33 → 54 not taken.
✓ Branch 34 → 35 taken 1719 times.
✗ Branch 34 → 54 not taken.
✓ Branch 35 → 36 taken 1719 times.
✗ Branch 35 → 54 not taken.
1719 callInst->addDereferenceableParamAttr(0, module->getDataLayout().getTypeStoreSize(structType));
134
3/6
✓ Branch 37 → 38 taken 1719 times.
✗ Branch 37 → 55 not taken.
✓ Branch 38 → 39 taken 1719 times.
✗ Branch 38 → 55 not taken.
✓ Branch 39 → 40 taken 1719 times.
✗ Branch 39 → 55 not taken.
1719 callInst->addParamAttr(0, llvm::Attribute::getWithAlignment(context, module->getDataLayout().getABITypeAlign(structType)));
135 }
136
137 1719 return callInst;
138 1719 }
139
140 llvm::Value *IRGenerator::generateFctDeclAndCall(const Function *fct, const std::vector<llvm::Value *> &args) const {
141 generateFctDecl(fct, args);
142 return generateFctCall(fct, args);
143 }
144
145 1719 void IRGenerator::generateProcDeclAndCall(const Function *proc, const std::vector<llvm::Value *> &args) const {
146 1719 generateFctDecl(proc, args);
147 1719 (void)generateFctCall(proc, args);
148 1719 }
149
150 1288 void IRGenerator::generateCtorOrDtorCall(const SymbolTableEntry *entry, const Function *ctorOrDtor,
151 const std::vector<llvm::Value *> &args) const {
152 // Retrieve address of the struct variable. For fields this is the 'this' variable, otherwise use the normal address
153 llvm::Value *structAddr;
154
2/2
✓ Branch 3 → 4 taken 113 times.
✓ Branch 3 → 41 taken 1175 times.
1288 if (entry->isField()) {
155 // Take 'this' var as base pointer
156
1/2
✓ Branch 6 → 7 taken 113 times.
✗ Branch 6 → 50 not taken.
339 const SymbolTableEntry *thisVar = currentScope->lookupStrict(THIS_VARIABLE_NAME);
157
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 113 times.
113 assert(thisVar != nullptr);
158
7/14
✓ Branch 14 → 15 taken 113 times.
✗ Branch 14 → 54 not taken.
✓ Branch 15 → 16 taken 113 times.
✗ Branch 15 → 54 not taken.
✓ Branch 16 → 17 taken 113 times.
✗ Branch 16 → 22 not taken.
✓ Branch 17 → 18 taken 113 times.
✗ Branch 17 → 54 not taken.
✓ Branch 18 → 19 taken 113 times.
✗ Branch 18 → 54 not taken.
✓ Branch 19 → 20 taken 113 times.
✗ Branch 19 → 54 not taken.
✓ Branch 20 → 21 taken 113 times.
✗ Branch 20 → 22 not taken.
113 assert(thisVar->getQualType().isPtr() && thisVar->getQualType().getContained().is(TY_STRUCT));
159
3/6
✓ Branch 23 → 24 taken 113 times.
✗ Branch 23 → 55 not taken.
✓ Branch 24 → 25 taken 113 times.
✗ Branch 24 → 55 not taken.
✓ Branch 25 → 26 taken 113 times.
✗ Branch 25 → 55 not taken.
113 llvm::Type *thisType = thisVar->getQualType().getContained().toLLVMType(sourceFile);
160
4/8
✓ Branch 28 → 29 taken 113 times.
✗ Branch 28 → 58 not taken.
✓ Branch 29 → 30 taken 113 times.
✗ Branch 29 → 56 not taken.
✓ Branch 30 → 31 taken 113 times.
✗ Branch 30 → 56 not taken.
✓ Branch 31 → 32 taken 113 times.
✗ Branch 31 → 56 not taken.
226 llvm::Value *thisPtr = insertLoad(builder.getPtrTy(), thisVar->getAddress());
161 // Add field offset
162
1/2
✓ Branch 37 → 38 taken 113 times.
✗ Branch 37 → 62 not taken.
113 structAddr = insertStructGEP(thisType, thisPtr, entry->orderIndex);
163 } else {
164 1175 structAddr = entry->getAddress();
165 // For optional parameter initializers we need this exception
166
2/2
✓ Branch 42 → 43 taken 5 times.
✓ Branch 42 → 44 taken 1170 times.
1175 if (!structAddr)
167 5 return;
168 }
169
1/2
✗ Branch 44 → 45 not taken.
✓ Branch 44 → 46 taken 1283 times.
1283 assert(structAddr != nullptr);
170 1283 generateCtorOrDtorCall(structAddr, ctorOrDtor, args);
171 }
172
173 1719 void IRGenerator::generateCtorOrDtorCall(llvm::Value *structAddr, const Function *ctorOrDtor,
174 const std::vector<llvm::Value *> &args) const {
175 // Build parameter list
176
1/2
✓ Branch 4 → 5 taken 1719 times.
✗ Branch 4 → 14 not taken.
3438 std::vector argValues = {structAddr};
177
1/2
✓ Branch 10 → 11 taken 1719 times.
✗ Branch 10 → 18 not taken.
1719 argValues.insert(argValues.end(), args.begin(), args.end());
178
179 // Generate function call
180
1/2
✓ Branch 11 → 12 taken 1719 times.
✗ Branch 11 → 20 not taken.
1719 generateProcDeclAndCall(ctorOrDtor, argValues);
181 1719 }
182
183 78 void IRGenerator::generateDeallocCall(llvm::Value *variableAddress) const {
184 // Abort if the address is not set. This can happen when leaving the scope of a dtor, which already freed the heap memory
185
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 77 times.
78 if (!variableAddress)
186 1 return;
187
188 // In case of string runtime, call free manually. Otherwise, use the memory_rt implementation of sDealloc()
189
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 14 taken 77 times.
154 if (sourceFile->isStringRT()) {
190 llvm::Function *freeFct = stdFunctionManager.getFreeFct();
191 builder.CreateCall(freeFct, variableAddress);
192 } else {
193 77 llvm::Function *deallocFct = stdFunctionManager.getDeallocBytePtrRefFct();
194
3/6
✓ Branch 15 → 16 taken 77 times.
✗ Branch 15 → 26 not taken.
✓ Branch 17 → 18 taken 77 times.
✗ Branch 17 → 24 not taken.
✓ Branch 18 → 19 taken 77 times.
✗ Branch 18 → 24 not taken.
77 builder.CreateCall(deallocFct, variableAddress);
195 }
196 }
197
198 4 llvm::Function *IRGenerator::generateImplicitFunction(const std::function<void()> &generateBody, const Function *spiceFunc) {
199 // Only focus on method procedures
200
1/2
✓ Branch 2 → 3 taken 4 times.
✗ Branch 2 → 155 not taken.
4 const ASTNode *node = spiceFunc->entry->declNode;
201
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 4 times.
4 assert(spiceFunc->isFunction());
202
203 // Only generate if used
204
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 4 times.
4 if (!spiceFunc->used)
205 return nullptr;
206
207 // Retrieve return type
208
1/2
✓ Branch 9 → 10 taken 4 times.
✗ Branch 9 → 155 not taken.
4 llvm::Type *returnType = spiceFunc->returnType.toLLVMType(sourceFile);
209
210 // Get 'this' entry
211 4 std::vector<llvm::Type *> paramTypes;
212
1/2
✓ Branch 10 → 11 taken 4 times.
✗ Branch 10 → 153 not taken.
4 SymbolTableEntry *thisEntry = nullptr;
213
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 27 taken 4 times.
4 if (spiceFunc->isMethod()) {
214 thisEntry = spiceFunc->bodyScope->lookupStrict(THIS_VARIABLE_NAME);
215 assert(thisEntry != nullptr);
216 paramTypes.push_back(builder.getPtrTy());
217 }
218
219 // Get parameter types
220
1/2
✗ Branch 36 → 29 not taken.
✓ Branch 36 → 37 taken 4 times.
4 for (const auto &[qualType, isOptional] : spiceFunc->paramList) {
221 assert(!isOptional);
222 paramTypes.push_back(qualType.toLLVMType(sourceFile));
223 }
224
225 // Get function linkage
226
2/4
✓ Branch 37 → 38 taken 4 times.
✗ Branch 37 → 153 not taken.
✓ Branch 38 → 39 taken 4 times.
✗ Branch 38 → 153 not taken.
4 const bool isPublic = spiceFunc->entry->getQualType().isPublic();
227
1/2
✓ Branch 39 → 40 taken 4 times.
✗ Branch 39 → 41 not taken.
4 const llvm::GlobalValue::LinkageTypes linkage = isPublic ? llvm::Function::ExternalLinkage : llvm::Function::PrivateLinkage;
228
229 // Create function
230
1/2
✓ Branch 42 → 43 taken 4 times.
✗ Branch 42 → 153 not taken.
4 const std::string mangledName = spiceFunc->getMangledName();
231
1/2
✓ Branch 44 → 45 taken 4 times.
✗ Branch 44 → 126 not taken.
4 llvm::FunctionType *fctType = llvm::FunctionType::get(returnType, paramTypes, false);
232
2/4
✓ Branch 45 → 46 taken 4 times.
✗ Branch 45 → 127 not taken.
✓ Branch 46 → 47 taken 4 times.
✗ Branch 46 → 127 not taken.
4 llvm::Function *fct = llvm::Function::Create(fctType, llvm::Function::ExternalLinkage, mangledName, module);
233
1/2
✓ Branch 47 → 48 taken 4 times.
✗ Branch 47 → 151 not taken.
4 fct->setLinkage(linkage);
234
1/2
✓ Branch 48 → 49 taken 4 times.
✗ Branch 48 → 151 not taken.
4 fct->setDoesNotRecurse();
235
236 // Set attributes to 'this' param
237
1/2
✗ Branch 52 → 53 not taken.
✓ Branch 52 → 70 taken 4 times.
4 if (spiceFunc->isMethod()) {
238 fct->addParamAttr(0, llvm::Attribute::NoUndef);
239 fct->addParamAttr(0, llvm::Attribute::NonNull);
240 assert(thisEntry != nullptr);
241 llvm::Type *structType = thisEntry->getQualType().getContained().toLLVMType(sourceFile);
242 assert(structType != nullptr);
243 fct->addDereferenceableParamAttr(0, module->getDataLayout().getTypeStoreSize(structType));
244 fct->addParamAttr(0, llvm::Attribute::getWithAlignment(context, module->getDataLayout().getABITypeAlign(structType)));
245 }
246
247 // Add debug info
248
1/2
✓ Branch 70 → 71 taken 4 times.
✗ Branch 70 → 151 not taken.
4 diGenerator.generateFunctionDebugInfo(fct, spiceFunc);
249
1/2
✗ Branch 71 → 72 not taken.
✓ Branch 71 → 73 taken 4 times.
4 if (node != nullptr)
250 diGenerator.setSourceLocation(node);
251
252 // Change to body scope
253
2/4
✓ Branch 73 → 74 taken 4 times.
✗ Branch 73 → 132 not taken.
✓ Branch 74 → 75 taken 4 times.
✗ Branch 74 → 130 not taken.
4 changeToScope(spiceFunc->getScopeName(), ScopeType::FUNC_PROC_BODY);
254
255 // Create entry block
256
2/4
✓ Branch 78 → 79 taken 4 times.
✗ Branch 78 → 135 not taken.
✓ Branch 79 → 80 taken 4 times.
✗ Branch 79 → 133 not taken.
4 llvm::BasicBlock *bEntry = createBlock();
257
1/2
✓ Branch 82 → 83 taken 4 times.
✗ Branch 82 → 151 not taken.
4 switchToBlock(bEntry, fct);
258
259 // Reset alloca insert markers to this block
260 4 allocaInsertBlock = bEntry;
261
1/2
✓ Branch 83 → 84 taken 4 times.
✗ Branch 83 → 151 not taken.
4 allocaInsertInst = nullptr;
262
263 // Store first argument to 'this' symbol
264
1/2
✗ Branch 86 → 87 not taken.
✓ Branch 86 → 106 taken 4 times.
4 if (spiceFunc->isMethod()) {
265 assert(thisEntry != nullptr);
266 // Allocate space for the parameter
267 llvm::Value *thisAddress = insertAlloca(paramTypes.front(), THIS_VARIABLE_NAME);
268 // Update the symbol table entry
269 thisEntry->updateAddress(thisAddress);
270 // Store the value at the new address
271 insertStore(fct->arg_begin(), thisAddress);
272 // Generate debug info
273 diGenerator.generateLocalVarDebugInfo(THIS_VARIABLE_NAME, thisAddress, 1);
274 }
275
276 // Generate body
277
1/2
✓ Branch 106 → 107 taken 4 times.
✗ Branch 106 → 151 not taken.
4 generateBody();
278
279 // Conclude debug info for function
280
1/2
✓ Branch 107 → 108 taken 4 times.
✗ Branch 107 → 151 not taken.
4 diGenerator.concludeFunctionDebugInfo();
281
282 // Verify function
283 // Use the code location of the declaration node if available. Otherwise, (e.g. in case of test main) use an artificial code loc
284
2/4
✗ Branch 108 → 109 not taken.
✓ Branch 108 → 110 taken 4 times.
✓ Branch 110 → 111 taken 4 times.
✗ Branch 110 → 151 not taken.
4 const CodeLoc codeLoc = node != nullptr ? node->codeLoc : CodeLoc(1, 1, sourceFile);
285
1/2
✓ Branch 111 → 112 taken 4 times.
✗ Branch 111 → 151 not taken.
4 verifyFunction(fct, codeLoc);
286
287 // Change to parent scope
288
1/2
✓ Branch 112 → 113 taken 4 times.
✗ Branch 112 → 151 not taken.
4 changeToParentScope(ScopeType::FUNC_PROC_BODY);
289
290 4 return fct;
291 4 }
292
293 249 llvm::Function *IRGenerator::generateImplicitProcedure(const std::function<void()> &generateBody, const Function *spiceProc) {
294 // Only focus on method procedures
295 249 const ASTNode *node = spiceProc->entry->declNode;
296
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 249 times.
249 assert(node != nullptr);
297
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 249 times.
249 assert(spiceProc->isProcedure());
298
299 // Only generate if used
300
2/2
✓ Branch 9 → 10 taken 79 times.
✓ Branch 9 → 11 taken 170 times.
249 if (!spiceProc->used)
301 79 return nullptr;
302
303 // Get 'this' entry
304 170 std::vector<llvm::Type *> paramTypes;
305
1/2
✓ Branch 11 → 12 taken 170 times.
✗ Branch 11 → 152 not taken.
170 SymbolTableEntry *thisEntry = nullptr;
306
1/2
✓ Branch 14 → 15 taken 170 times.
✗ Branch 14 → 28 not taken.
170 if (spiceProc->isMethod()) {
307
1/2
✓ Branch 17 → 18 taken 170 times.
✗ Branch 17 → 118 not taken.
510 thisEntry = spiceProc->bodyScope->lookupStrict(THIS_VARIABLE_NAME);
308
1/2
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 170 times.
170 assert(thisEntry != nullptr);
309
2/4
✓ Branch 25 → 26 taken 170 times.
✗ Branch 25 → 122 not taken.
✓ Branch 26 → 27 taken 170 times.
✗ Branch 26 → 122 not taken.
170 paramTypes.push_back(builder.getPtrTy());
310 }
311
312 // Get parameter types
313
2/2
✓ Branch 37 → 30 taken 24 times.
✓ Branch 37 → 38 taken 170 times.
194 for (const auto &[qualType, isOptional] : spiceProc->paramList) {
314
1/2
✗ Branch 31 → 32 not taken.
✓ Branch 31 → 33 taken 24 times.
24 assert(!isOptional);
315
2/4
✓ Branch 33 → 34 taken 24 times.
✗ Branch 33 → 123 not taken.
✓ Branch 34 → 35 taken 24 times.
✗ Branch 34 → 123 not taken.
24 paramTypes.push_back(qualType.toLLVMType(sourceFile));
316 }
317
318 // Get function linkage
319
2/4
✓ Branch 38 → 39 taken 170 times.
✗ Branch 38 → 152 not taken.
✓ Branch 39 → 40 taken 170 times.
✗ Branch 39 → 152 not taken.
170 const bool isPublic = spiceProc->entry->getQualType().isPublic();
320
1/2
✓ Branch 40 → 41 taken 170 times.
✗ Branch 40 → 42 not taken.
170 const llvm::GlobalValue::LinkageTypes linkage = isPublic ? llvm::Function::ExternalLinkage : llvm::Function::PrivateLinkage;
321
322 // Create function
323
1/2
✓ Branch 43 → 44 taken 170 times.
✗ Branch 43 → 152 not taken.
170 const std::string mangledName = spiceProc->getMangledName();
324
2/4
✓ Branch 45 → 46 taken 170 times.
✗ Branch 45 → 125 not taken.
✓ Branch 46 → 47 taken 170 times.
✗ Branch 46 → 125 not taken.
170 llvm::FunctionType *fctType = llvm::FunctionType::get(builder.getVoidTy(), paramTypes, false);
325
2/4
✓ Branch 47 → 48 taken 170 times.
✗ Branch 47 → 126 not taken.
✓ Branch 48 → 49 taken 170 times.
✗ Branch 48 → 126 not taken.
170 llvm::Function *fct = llvm::Function::Create(fctType, llvm::Function::ExternalLinkage, mangledName, module);
326
1/2
✓ Branch 49 → 50 taken 170 times.
✗ Branch 49 → 150 not taken.
170 fct->setLinkage(linkage);
327
1/2
✓ Branch 50 → 51 taken 170 times.
✗ Branch 50 → 150 not taken.
170 fct->setDoesNotRecurse();
328
329 // Set attributes to 'this' param
330
1/2
✓ Branch 54 → 55 taken 170 times.
✗ Branch 54 → 72 not taken.
170 if (spiceProc->isMethod()) {
331
1/2
✓ Branch 55 → 56 taken 170 times.
✗ Branch 55 → 150 not taken.
170 fct->addParamAttr(0, llvm::Attribute::NoUndef);
332
1/2
✓ Branch 56 → 57 taken 170 times.
✗ Branch 56 → 150 not taken.
170 fct->addParamAttr(0, llvm::Attribute::NonNull);
333
1/2
✗ Branch 57 → 58 not taken.
✓ Branch 57 → 59 taken 170 times.
170 assert(thisEntry != nullptr);
334
3/6
✓ Branch 59 → 60 taken 170 times.
✗ Branch 59 → 127 not taken.
✓ Branch 60 → 61 taken 170 times.
✗ Branch 60 → 127 not taken.
✓ Branch 61 → 62 taken 170 times.
✗ Branch 61 → 127 not taken.
170 llvm::Type *structType = thisEntry->getQualType().getContained().toLLVMType(sourceFile);
335
1/2
✗ Branch 62 → 63 not taken.
✓ Branch 62 → 64 taken 170 times.
170 assert(structType != nullptr);
336
3/6
✓ Branch 65 → 66 taken 170 times.
✗ Branch 65 → 128 not taken.
✓ Branch 66 → 67 taken 170 times.
✗ Branch 66 → 128 not taken.
✓ Branch 67 → 68 taken 170 times.
✗ Branch 67 → 128 not taken.
170 fct->addDereferenceableParamAttr(0, module->getDataLayout().getTypeStoreSize(structType));
337
3/6
✓ Branch 69 → 70 taken 170 times.
✗ Branch 69 → 150 not taken.
✓ Branch 70 → 71 taken 170 times.
✗ Branch 70 → 150 not taken.
✓ Branch 71 → 72 taken 170 times.
✗ Branch 71 → 150 not taken.
170 fct->addParamAttr(0, llvm::Attribute::getWithAlignment(context, module->getDataLayout().getABITypeAlign(structType)));
338 }
339
340 // Add debug info
341
1/2
✓ Branch 72 → 73 taken 170 times.
✗ Branch 72 → 150 not taken.
170 diGenerator.generateFunctionDebugInfo(fct, spiceProc);
342
1/2
✓ Branch 73 → 74 taken 170 times.
✗ Branch 73 → 150 not taken.
170 diGenerator.setSourceLocation(node);
343
344 // Change to body scope
345
2/4
✓ Branch 74 → 75 taken 170 times.
✗ Branch 74 → 131 not taken.
✓ Branch 75 → 76 taken 170 times.
✗ Branch 75 → 129 not taken.
170 changeToScope(spiceProc->getScopeName(), ScopeType::FUNC_PROC_BODY);
346
347 // Create entry block
348
2/4
✓ Branch 79 → 80 taken 170 times.
✗ Branch 79 → 134 not taken.
✓ Branch 80 → 81 taken 170 times.
✗ Branch 80 → 132 not taken.
170 llvm::BasicBlock *bEntry = createBlock();
349
1/2
✓ Branch 83 → 84 taken 170 times.
✗ Branch 83 → 150 not taken.
170 switchToBlock(bEntry, fct);
350
351 // Reset alloca insert markers to this block
352 170 allocaInsertBlock = bEntry;
353
1/2
✓ Branch 84 → 85 taken 170 times.
✗ Branch 84 → 150 not taken.
170 allocaInsertInst = nullptr;
354
355 // Store first argument to 'this' symbol
356
1/2
✓ Branch 87 → 88 taken 170 times.
✗ Branch 87 → 107 not taken.
170 if (spiceProc->isMethod()) {
357
1/2
✗ Branch 88 → 89 not taken.
✓ Branch 88 → 90 taken 170 times.
170 assert(thisEntry != nullptr);
358 // Allocate space for the parameter
359
2/4
✓ Branch 92 → 93 taken 170 times.
✗ Branch 92 → 140 not taken.
✓ Branch 94 → 95 taken 170 times.
✗ Branch 94 → 138 not taken.
170 llvm::Value *thisAddress = insertAlloca(paramTypes.front(), THIS_VARIABLE_NAME);
360 // Update the symbol table entry
361
1/2
✓ Branch 97 → 98 taken 170 times.
✗ Branch 97 → 150 not taken.
170 thisEntry->updateAddress(thisAddress);
362 // Store the value at the new address
363
2/4
✓ Branch 98 → 99 taken 170 times.
✗ Branch 98 → 150 not taken.
✓ Branch 99 → 100 taken 170 times.
✗ Branch 99 → 150 not taken.
170 insertStore(fct->arg_begin(), thisAddress);
364 // Generate debug info
365
2/4
✓ Branch 102 → 103 taken 170 times.
✗ Branch 102 → 146 not taken.
✓ Branch 103 → 104 taken 170 times.
✗ Branch 103 → 144 not taken.
510 diGenerator.generateLocalVarDebugInfo(THIS_VARIABLE_NAME, thisAddress, 1);
366 }
367
368 // Generate body
369
1/2
✓ Branch 107 → 108 taken 170 times.
✗ Branch 107 → 150 not taken.
170 generateBody();
370
371 // Create return instruction
372
1/2
✓ Branch 108 → 109 taken 170 times.
✗ Branch 108 → 150 not taken.
170 builder.CreateRetVoid();
373
374 // Conclude debug info for function
375
1/2
✓ Branch 109 → 110 taken 170 times.
✗ Branch 109 → 150 not taken.
170 diGenerator.concludeFunctionDebugInfo();
376
377 // Verify function
378
1/2
✓ Branch 110 → 111 taken 170 times.
✗ Branch 110 → 150 not taken.
170 verifyFunction(fct, node->codeLoc);
379
380 // Change to parent scope
381
1/2
✓ Branch 111 → 112 taken 170 times.
✗ Branch 111 → 150 not taken.
170 changeToParentScope(ScopeType::FUNC_PROC_BODY);
382
383 170 return fct;
384 170 }
385
386 1323 void IRGenerator::generateCtorBodyPreamble(Scope *bodyScope) {
387 // Retrieve struct scope
388 1323 Scope *structScope = bodyScope->parent;
389
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 1323 times.
1323 assert(structScope != nullptr);
390
391 // Get struct address
392
1/2
✓ Branch 6 → 7 taken 1323 times.
✗ Branch 6 → 130 not taken.
3969 const SymbolTableEntry *thisEntry = bodyScope->lookupStrict(THIS_VARIABLE_NAME);
393
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 1323 times.
1323 assert(thisEntry != nullptr);
394
1/2
✓ Branch 14 → 15 taken 1323 times.
✗ Branch 14 → 184 not taken.
1323 llvm::Value *thisPtrPtr = thisEntry->getAddress();
395
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 1323 times.
1323 assert(thisPtrPtr != nullptr);
396 1323 llvm::Value *thisPtr = nullptr;
397
2/4
✓ Branch 17 → 18 taken 1323 times.
✗ Branch 17 → 184 not taken.
✓ Branch 18 → 19 taken 1323 times.
✗ Branch 18 → 184 not taken.
1323 const QualType structSymbolType = thisEntry->getQualType().getBase();
398
1/2
✓ Branch 19 → 20 taken 1323 times.
✗ Branch 19 → 184 not taken.
1323 llvm::Type *structType = structSymbolType.toLLVMType(sourceFile);
399
400 // Store VTable to first struct field if required
401
1/2
✓ Branch 20 → 21 taken 1323 times.
✗ Branch 20 → 184 not taken.
1323 const Struct *spiceStruct = structSymbolType.getStruct(nullptr);
402
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 1323 times.
1323 assert(spiceStruct != nullptr);
403
2/2
✓ Branch 23 → 24 taken 252 times.
✓ Branch 23 → 45 taken 1071 times.
1323 if (spiceStruct->vTableData.vtable != nullptr) {
404
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 252 times.
252 assert(spiceStruct->vTableData.vtableType != nullptr);
405 // Store VTable to field address at index 0
406
3/6
✓ Branch 28 → 29 taken 252 times.
✗ Branch 28 → 136 not taken.
✓ Branch 29 → 30 taken 252 times.
✗ Branch 29 → 134 not taken.
✓ Branch 30 → 31 taken 252 times.
✗ Branch 30 → 134 not taken.
252 thisPtr = insertLoad(builder.getPtrTy(), thisPtrPtr);
407
3/6
✓ Branch 33 → 34 taken 252 times.
✗ Branch 33 → 147 not taken.
✓ Branch 34 → 35 taken 252 times.
✗ Branch 34 → 147 not taken.
✓ Branch 35 → 36 taken 252 times.
✗ Branch 35 → 147 not taken.
252 llvm::Value *indices[3] = {builder.getInt64(0), builder.getInt32(0), builder.getInt32(2)};
408
1/2
✓ Branch 40 → 41 taken 252 times.
✗ Branch 40 → 140 not taken.
252 llvm::Value *gepResult = insertInBoundsGEP(spiceStruct->vTableData.vtableType, spiceStruct->vTableData.vtable, indices);
409
1/2
✓ Branch 43 → 44 taken 252 times.
✗ Branch 43 → 147 not taken.
252 insertStore(gepResult, thisPtr);
410 }
411
412
1/2
✓ Branch 45 → 46 taken 1323 times.
✗ Branch 45 → 184 not taken.
1323 const size_t fieldCount = structScope->getFieldCount();
413
2/2
✓ Branch 126 → 47 taken 3318 times.
✓ Branch 126 → 127 taken 1323 times.
4641 for (size_t i = 0; i < fieldCount; i++) {
414
1/2
✗ Branch 47 → 48 not taken.
✓ Branch 47 → 49 taken 3318 times.
3318 const SymbolTableEntry *fieldSymbol = structScope->lookupField(i);
415
3/6
✓ Branch 52 → 53 taken 3318 times.
✗ Branch 52 → 56 not taken.
✓ Branch 53 → 54 taken 3318 times.
✗ Branch 53 → 184 not taken.
✓ Branch 54 → 55 taken 3318 times.
✗ Branch 54 → 56 not taken.
3318 assert(fieldSymbol != nullptr && fieldSymbol->isField());
416
2/2
✓ Branch 57 → 58 taken 204 times.
✓ Branch 57 → 59 taken 3114 times.
3318 if (fieldSymbol->isImplicitField)
417 204 continue;
418
419 // Call ctor for struct fields
420
1/2
✓ Branch 59 → 60 taken 3114 times.
✗ Branch 59 → 184 not taken.
3114 const QualType &fieldType = fieldSymbol->getQualType();
421
1/2
✓ Branch 60 → 61 taken 3114 times.
✗ Branch 60 → 62 not taken.
3114 const auto fieldNode = spice_pointer_cast<FieldNode *>(fieldSymbol->declNode);
422
3/4
✓ Branch 67 → 68 taken 3114 times.
✗ Branch 67 → 184 not taken.
✓ Branch 68 → 69 taken 386 times.
✓ Branch 68 → 99 taken 2728 times.
3114 if (fieldType.is(TY_STRUCT)) {
423 // Lookup ctor function and call if available
424
1/2
✓ Branch 69 → 70 taken 386 times.
✗ Branch 69 → 184 not taken.
386 Scope *matchScope = fieldType.getBodyScope();
425
4/6
✓ Branch 73 → 74 taken 386 times.
✗ Branch 73 → 150 not taken.
✓ Branch 74 → 75 taken 386 times.
✗ Branch 74 → 148 not taken.
✓ Branch 78 → 79 taken 326 times.
✓ Branch 78 → 98 taken 60 times.
1158 if (const Function *ctorFunction = FunctionManager::lookup(matchScope, CTOR_FUNCTION_NAME, fieldType, {}, false)) {
426
2/2
✓ Branch 79 → 80 taken 90 times.
✓ Branch 79 → 88 taken 236 times.
326 if (!thisPtr)
427
3/6
✓ Branch 82 → 83 taken 90 times.
✗ Branch 82 → 159 not taken.
✓ Branch 83 → 84 taken 90 times.
✗ Branch 83 → 157 not taken.
✓ Branch 84 → 85 taken 90 times.
✗ Branch 84 → 157 not taken.
180 thisPtr = insertLoad(builder.getPtrTy(), thisPtrPtr);
428
1/2
✓ Branch 91 → 92 taken 326 times.
✗ Branch 91 → 163 not taken.
326 llvm::Value *fieldAddress = insertStructGEP(structType, thisPtr, i);
429
1/2
✓ Branch 95 → 96 taken 326 times.
✗ Branch 95 → 169 not taken.
326 generateCtorOrDtorCall(fieldAddress, ctorFunction, {});
430 }
431 386 continue;
432 386 }
433
434 // Store default field values
435
3/4
✓ Branch 99 → 100 taken 2436 times.
✓ Branch 99 → 101 taken 292 times.
✓ Branch 100 → 101 taken 2436 times.
✗ Branch 100 → 125 not taken.
2728 if (fieldNode->defaultValue != nullptr || cliOptions.buildMode != BuildMode::RELEASE) {
436 // Retrieve field address
437
2/2
✓ Branch 101 → 102 taken 971 times.
✓ Branch 101 → 110 taken 1757 times.
2728 if (!thisPtr)
438
3/6
✓ Branch 104 → 105 taken 971 times.
✗ Branch 104 → 174 not taken.
✓ Branch 105 → 106 taken 971 times.
✗ Branch 105 → 172 not taken.
✓ Branch 106 → 107 taken 971 times.
✗ Branch 106 → 172 not taken.
1942 thisPtr = insertLoad(builder.getPtrTy(), thisPtrPtr);
439
1/2
✓ Branch 113 → 114 taken 2728 times.
✗ Branch 113 → 178 not taken.
2728 llvm::Value *fieldAddress = insertStructGEP(structType, thisPtr, i);
440 // Retrieve default value
441 llvm::Value *value;
442
2/2
✓ Branch 116 → 117 taken 292 times.
✓ Branch 116 → 119 taken 2436 times.
2728 if (fieldNode->defaultValue != nullptr) {
443 // To resolve the default value, we need to temporarily change to the manifestation of the current struct instantiation
444 292 const size_t oldManIdx = manIdx; // Save manifestation index
445 292 manIdx = spiceStruct->manifestationIndex;
446
1/2
✓ Branch 117 → 118 taken 292 times.
✗ Branch 117 → 184 not taken.
292 value = resolveValue(fieldNode->defaultValue);
447 292 manIdx = oldManIdx; // Restore manifestation index
448 } else {
449
1/4
✗ Branch 119 → 120 not taken.
✓ Branch 119 → 122 taken 2436 times.
✗ Branch 120 → 121 not taken.
✗ Branch 120 → 122 not taken.
2436 assert(cliOptions.buildMode == BuildMode::DEBUG || cliOptions.buildMode == BuildMode::TEST);
450
1/2
✓ Branch 122 → 123 taken 2436 times.
✗ Branch 122 → 184 not taken.
2436 value = getDefaultValueForSymbolType(fieldType);
451 }
452 // Store default value
453
1/2
✓ Branch 124 → 125 taken 2728 times.
✗ Branch 124 → 184 not taken.
2728 insertStore(value, fieldAddress);
454 }
455 }
456 1323 }
457
458 39 void IRGenerator::generateDefaultCtor(const Function *ctorFunction) {
459
3/6
✓ Branch 2 → 3 taken 39 times.
✗ Branch 2 → 6 not taken.
✓ Branch 3 → 4 taken 39 times.
✗ Branch 3 → 13 not taken.
✓ Branch 4 → 5 taken 39 times.
✗ Branch 4 → 6 not taken.
39 assert(ctorFunction->implicitDefault && ctorFunction->name == CTOR_FUNCTION_NAME);
460 74 const std::function<void()> generateBody = [&] { generateCtorBodyPreamble(ctorFunction->bodyScope); };
461
1/2
✓ Branch 8 → 9 taken 39 times.
✗ Branch 8 → 11 not taken.
39 generateImplicitProcedure(generateBody, ctorFunction);
462 39 }
463
464 24 void IRGenerator::generateCopyCtorBodyPreamble(const Function *copyCtorFunction) {
465 // Retrieve struct scope
466 24 Scope *structScope = copyCtorFunction->bodyScope->parent;
467
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 24 times.
24 assert(structScope != nullptr);
468
469 // Get struct address
470
1/2
✓ Branch 6 → 7 taken 24 times.
✗ Branch 6 → 141 not taken.
72 const SymbolTableEntry *thisEntry = copyCtorFunction->bodyScope->lookupStrict(THIS_VARIABLE_NAME);
471
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 24 times.
24 assert(thisEntry != nullptr);
472 24 llvm::Value *thisPtrPtr = thisEntry->getAddress();
473
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 24 times.
24 assert(thisPtrPtr != nullptr);
474 24 llvm::Value *thisPtr = nullptr;
475
3/6
✓ Branch 17 → 18 taken 24 times.
✗ Branch 17 → 145 not taken.
✓ Branch 18 → 19 taken 24 times.
✗ Branch 18 → 145 not taken.
✓ Branch 19 → 20 taken 24 times.
✗ Branch 19 → 145 not taken.
24 llvm::Type *structType = thisEntry->getQualType().getBase().toLLVMType(sourceFile);
476
477 // Retrieve the value of the original struct, which is the only function parameter
478 24 llvm::Value *originalThisPtr = builder.GetInsertBlock()->getParent()->getArg(1);
479
480 24 const size_t fieldCount = structScope->getFieldCount();
481
2/2
✓ Branch 137 → 25 taken 84 times.
✓ Branch 137 → 138 taken 24 times.
108 for (size_t i = 0; i < fieldCount; i++) {
482
1/2
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 27 taken 84 times.
84 const SymbolTableEntry *fieldSymbol = structScope->lookupField(i);
483
2/4
✓ Branch 30 → 31 taken 84 times.
✗ Branch 30 → 34 not taken.
✓ Branch 32 → 33 taken 84 times.
✗ Branch 32 → 34 not taken.
84 assert(fieldSymbol != nullptr && fieldSymbol->isField());
484
485 // Retrieve the address of the original field (copy source)
486
1/2
✓ Branch 38 → 39 taken 84 times.
✗ Branch 38 → 146 not taken.
84 llvm::Value *originalFieldAddress = insertStructGEP(structType, originalThisPtr, i);
487
488 84 const QualType &fieldType = fieldSymbol->getQualType();
489
490 // Call copy ctor for struct fields
491
6/6
✓ Branch 43 → 44 taken 43 times.
✓ Branch 43 → 47 taken 41 times.
✓ Branch 45 → 46 taken 40 times.
✓ Branch 45 → 47 taken 3 times.
✓ Branch 48 → 49 taken 40 times.
✓ Branch 48 → 73 taken 44 times.
84 if (fieldType.is(TY_STRUCT) && !fieldType.isTriviallyCopyable(nullptr)) {
492 // Lookup copy ctor function and call if available
493
1/2
✓ Branch 49 → 50 taken 40 times.
✗ Branch 49 → 173 not taken.
40 Scope *matchScope = fieldType.getBodyScope();
494
2/4
✓ Branch 50 → 51 taken 40 times.
✗ Branch 50 → 156 not taken.
✓ Branch 54 → 55 taken 40 times.
✗ Branch 54 → 152 not taken.
120 const ArgList args = {{fieldType.toConstRef(nullptr), false /* we have the field as storage */}};
495
2/4
✓ Branch 58 → 59 taken 40 times.
✗ Branch 58 → 160 not taken.
✓ Branch 59 → 60 taken 40 times.
✗ Branch 59 → 158 not taken.
40 const Function *copyCtor = FunctionManager::lookup(matchScope, CTOR_FUNCTION_NAME, fieldType, args, false);
496
1/2
✗ Branch 62 → 63 not taken.
✓ Branch 62 → 64 taken 40 times.
40 assert(copyCtor != nullptr);
497
2/4
✓ Branch 66 → 67 taken 40 times.
✗ Branch 66 → 166 not taken.
✓ Branch 67 → 68 taken 40 times.
✗ Branch 67 → 164 not taken.
80 generateCtorOrDtorCall(fieldSymbol, copyCtor, {originalFieldAddress});
498 40 continue;
499 40 }
500
501 // Retrieve the address of the new field (copy dest)
502
2/2
✓ Branch 73 → 74 taken 14 times.
✓ Branch 73 → 82 taken 30 times.
44 if (!thisPtr)
503
3/6
✓ Branch 76 → 77 taken 14 times.
✗ Branch 76 → 176 not taken.
✓ Branch 77 → 78 taken 14 times.
✗ Branch 77 → 174 not taken.
✓ Branch 78 → 79 taken 14 times.
✗ Branch 78 → 174 not taken.
28 thisPtr = insertLoad(builder.getPtrTy(), thisPtrPtr);
504
1/2
✓ Branch 85 → 86 taken 44 times.
✗ Branch 85 → 180 not taken.
44 llvm::Value *fieldAddress = insertStructGEP(structType, thisPtr, i);
505
506 // For owning heap fields, copy the underlying heap storage
507
2/2
✓ Branch 89 → 90 taken 7 times.
✓ Branch 89 → 134 taken 37 times.
44 if (fieldType.isHeap()) {
508
1/2
✗ Branch 91 → 92 not taken.
✓ Branch 91 → 93 taken 7 times.
7 assert(fieldType.isPtr());
509
2/4
✓ Branch 93 → 94 taken 7 times.
✗ Branch 93 → 186 not taken.
✓ Branch 94 → 95 taken 7 times.
✗ Branch 94 → 186 not taken.
7 llvm::Type *pointeeType = fieldType.getContained().toLLVMType(sourceFile);
510
511 // Retrieve original heap address
512
3/6
✓ Branch 97 → 98 taken 7 times.
✗ Branch 97 → 189 not taken.
✓ Branch 98 → 99 taken 7 times.
✗ Branch 98 → 187 not taken.
✓ Branch 99 → 100 taken 7 times.
✗ Branch 99 → 187 not taken.
14 llvm::Value *originalHeapAddress = insertLoad(builder.getPtrTy(), originalFieldAddress);
513
514 // Insert check for nullptr
515
2/4
✓ Branch 104 → 105 taken 7 times.
✗ Branch 104 → 195 not taken.
✓ Branch 105 → 106 taken 7 times.
✗ Branch 105 → 193 not taken.
14 llvm::BasicBlock *bThen = createBlock("nullptrcheck.then");
516
2/4
✓ Branch 110 → 111 taken 7 times.
✗ Branch 110 → 201 not taken.
✓ Branch 111 → 112 taken 7 times.
✗ Branch 111 → 199 not taken.
7 llvm::BasicBlock *bExit = createBlock("nullptrcheck.exit");
517
4/8
✓ Branch 114 → 115 taken 7 times.
✗ Branch 114 → 205 not taken.
✓ Branch 115 → 116 taken 7 times.
✗ Branch 115 → 205 not taken.
✓ Branch 116 → 117 taken 7 times.
✗ Branch 116 → 205 not taken.
✓ Branch 117 → 118 taken 7 times.
✗ Branch 117 → 205 not taken.
7 llvm::Value *condValue = builder.CreateICmpNE(originalHeapAddress, llvm::Constant::getNullValue(builder.getPtrTy()));
518 7 insertCondJump(condValue, bThen, bExit);
519
520 // Fill then block
521 7 switchToBlock(bThen);
522
523 // Allocate new space on the heap
524 7 llvm::Function *unsafeAllocFct = stdFunctionManager.getAllocUnsafeLongFct();
525
2/4
✓ Branch 122 → 123 taken 7 times.
✗ Branch 122 → 206 not taken.
✓ Branch 123 → 124 taken 7 times.
✗ Branch 123 → 206 not taken.
7 const size_t typeSizeInBytes = module->getDataLayout().getTypeSizeInBits(pointeeType) / 8;
526 7 llvm::ConstantInt *typeSize = builder.getInt64(typeSizeInBytes);
527
3/6
✓ Branch 125 → 126 taken 7 times.
✗ Branch 125 → 210 not taken.
✓ Branch 127 → 128 taken 7 times.
✗ Branch 127 → 207 not taken.
✓ Branch 128 → 129 taken 7 times.
✗ Branch 128 → 207 not taken.
7 llvm::Value *newHeapAddress = builder.CreateCall(unsafeAllocFct, {typeSize});
528 7 insertStore(newHeapAddress, fieldAddress);
529
530 // Copy data from the old heap storage to the new one
531 7 generateShallowCopy(originalHeapAddress, pointeeType, newHeapAddress, false);
532 7 insertJump(bExit);
533
534 // Switch to exit block
535 7 switchToBlock(bExit);
536
537 7 continue;
538 7 }
539
540 // Shallow copy
541 37 llvm::Type *type = fieldType.toLLVMType(sourceFile);
542 37 generateShallowCopy(originalFieldAddress, type, fieldAddress, false);
543 }
544 24 }
545
546 97 void IRGenerator::generateDefaultCopyCtor(const Function *copyCtorFunction) {
547
3/6
✓ Branch 2 → 3 taken 97 times.
✗ Branch 2 → 6 not taken.
✓ Branch 3 → 4 taken 97 times.
✗ Branch 3 → 13 not taken.
✓ Branch 4 → 5 taken 97 times.
✗ Branch 4 → 6 not taken.
97 assert(copyCtorFunction->implicitDefault && copyCtorFunction->name == CTOR_FUNCTION_NAME);
548 121 const std::function<void()> generateBody = [&] { generateCopyCtorBodyPreamble(copyCtorFunction); };
549
1/2
✓ Branch 8 → 9 taken 97 times.
✗ Branch 8 → 11 not taken.
97 generateImplicitProcedure(generateBody, copyCtorFunction);
550 97 }
551
552 111 void IRGenerator::generateDtorBodyPreamble(const Function *dtorFunction) const {
553 // Retrieve struct scope
554 111 Scope *structScope = dtorFunction->bodyScope->parent;
555
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 111 times.
111 assert(structScope != nullptr);
556
557 // Get struct address
558
1/2
✓ Branch 6 → 7 taken 111 times.
✗ Branch 6 → 73 not taken.
333 const SymbolTableEntry *thisEntry = dtorFunction->bodyScope->lookupStrict(THIS_VARIABLE_NAME);
559
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 111 times.
111 assert(thisEntry != nullptr);
560 111 llvm::Value *thisPtrPtr = thisEntry->getAddress();
561
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 111 times.
111 assert(thisPtrPtr != nullptr);
562 111 llvm::Value *thisPtr = nullptr;
563
3/6
✓ Branch 17 → 18 taken 111 times.
✗ Branch 17 → 77 not taken.
✓ Branch 18 → 19 taken 111 times.
✗ Branch 18 → 77 not taken.
✓ Branch 19 → 20 taken 111 times.
✗ Branch 19 → 77 not taken.
111 llvm::Type *structType = thisEntry->getQualType().getBase().toLLVMType(sourceFile);
564
565 111 const size_t fieldCount = structScope->getFieldCount();
566
2/2
✓ Branch 69 → 22 taken 382 times.
✓ Branch 69 → 70 taken 111 times.
493 for (size_t i = 0; i < fieldCount; i++) {
567
1/2
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 382 times.
382 const SymbolTableEntry *fieldSymbol = structScope->lookupField(i);
568
2/4
✓ Branch 27 → 28 taken 382 times.
✗ Branch 27 → 31 not taken.
✓ Branch 29 → 30 taken 382 times.
✗ Branch 29 → 31 not taken.
382 assert(fieldSymbol != nullptr && fieldSymbol->isField());
569
570 // Call dtor for struct fields
571 382 const QualType &fieldType = fieldSymbol->getQualType();
572
2/2
✓ Branch 34 → 35 taken 82 times.
✓ Branch 34 → 50 taken 300 times.
382 if (fieldType.is(TY_STRUCT)) {
573 // Lookup dtor function and generate call if found
574
5/8
✓ Branch 38 → 39 taken 82 times.
✗ Branch 38 → 80 not taken.
✓ Branch 39 → 40 taken 82 times.
✗ Branch 39 → 78 not taken.
✓ Branch 40 → 41 taken 82 times.
✗ Branch 40 → 78 not taken.
✓ Branch 44 → 45 taken 73 times.
✓ Branch 44 → 49 taken 9 times.
246 if (const Function *dtorFct = FunctionManager::lookup(fieldType.getBodyScope(), DTOR_FUNCTION_NAME, fieldType, {}, false))
575
1/2
✓ Branch 46 → 47 taken 73 times.
✗ Branch 46 → 87 not taken.
73 generateCtorOrDtorCall(fieldSymbol, dtorFct, {});
576 82 continue;
577 82 }
578
579 // Deallocate fields, that are stored on the heap
580
2/2
✓ Branch 51 → 52 taken 74 times.
✓ Branch 51 → 68 taken 226 times.
300 if (fieldType.isHeap()) {
581 // Retrieve field address
582
2/2
✓ Branch 52 → 53 taken 68 times.
✓ Branch 52 → 61 taken 6 times.
74 if (!thisPtr)
583
3/6
✓ Branch 55 → 56 taken 68 times.
✗ Branch 55 → 92 not taken.
✓ Branch 56 → 57 taken 68 times.
✗ Branch 56 → 90 not taken.
✓ Branch 57 → 58 taken 68 times.
✗ Branch 57 → 90 not taken.
136 thisPtr = insertLoad(builder.getPtrTy(), thisPtrPtr);
584
1/2
✓ Branch 64 → 65 taken 74 times.
✗ Branch 64 → 96 not taken.
74 llvm::Value *fieldAddress = insertStructGEP(structType, thisPtr, i);
585 // Call dealloc function
586 74 generateDeallocCall(fieldAddress);
587 }
588 }
589 111 }
590
591 113 void IRGenerator::generateDefaultDtor(const Function *dtorFunction) {
592
3/6
✓ Branch 2 → 3 taken 113 times.
✗ Branch 2 → 6 not taken.
✓ Branch 3 → 4 taken 113 times.
✗ Branch 3 → 13 not taken.
✓ Branch 4 → 5 taken 113 times.
✗ Branch 4 → 6 not taken.
113 assert(dtorFunction->implicitDefault && dtorFunction->name == DTOR_FUNCTION_NAME);
593 224 const std::function<void()> generateBody = [&] { generateDtorBodyPreamble(dtorFunction); };
594
1/2
✓ Branch 8 → 9 taken 113 times.
✗ Branch 8 → 11 not taken.
113 generateImplicitProcedure(generateBody, dtorFunction);
595 113 }
596
597 4 void IRGenerator::generateTestMain() {
598 // Collect all test functions
599 4 std::vector<const std::vector<const Function *> *> tests;
600
5/8
✓ Branch 2 → 3 taken 4 times.
✗ Branch 2 → 138 not taken.
✓ Branch 3 → 4 taken 4 times.
✗ Branch 3 → 138 not taken.
✓ Branch 4 → 5 taken 4 times.
✗ Branch 4 → 138 not taken.
✓ Branch 15 → 6 taken 5 times.
✓ Branch 15 → 16 taken 4 times.
9 for (const auto &sourceFile : resourceManager.sourceFiles | std::views::values)
601
1/2
✓ Branch 9 → 10 taken 5 times.
✗ Branch 9 → 13 not taken.
5 if (!sourceFile->testFunctions.empty())
602
1/2
✓ Branch 11 → 12 taken 5 times.
✗ Branch 11 → 137 not taken.
5 tests.push_back(&sourceFile->testFunctions);
603
604 // Prepare printf function
605
1/2
✓ Branch 16 → 17 taken 4 times.
✗ Branch 16 → 263 not taken.
4 llvm::Function *printfFct = stdFunctionManager.getPrintfFct();
606
607 // Prepare success and error messages
608
3/6
✓ Branch 19 → 20 taken 4 times.
✗ Branch 19 → 147 not taken.
✓ Branch 22 → 23 taken 4 times.
✗ Branch 22 → 141 not taken.
✓ Branch 23 → 24 taken 4 times.
✗ Branch 23 → 139 not taken.
16 llvm::Constant *allStartMsg = createGlobalStringConst("allStartMsg", TEST_ALL_START_MSG, *rootScope->codeLoc);
609
3/6
✓ Branch 30 → 31 taken 4 times.
✗ Branch 30 → 159 not taken.
✓ Branch 33 → 34 taken 4 times.
✗ Branch 33 → 153 not taken.
✓ Branch 34 → 35 taken 4 times.
✗ Branch 34 → 151 not taken.
16 llvm::Constant *allEndMsg = createGlobalStringConst("allEndMsg", TEST_ALL_END_MSG, *rootScope->codeLoc);
610
3/6
✓ Branch 41 → 42 taken 4 times.
✗ Branch 41 → 171 not taken.
✓ Branch 44 → 45 taken 4 times.
✗ Branch 44 → 165 not taken.
✓ Branch 45 → 46 taken 4 times.
✗ Branch 45 → 163 not taken.
16 llvm::Constant *fileStartMsg = createGlobalStringConst("fileStartMsg", TEST_FILE_START_MSG, *rootScope->codeLoc);
611
3/6
✓ Branch 52 → 53 taken 4 times.
✗ Branch 52 → 183 not taken.
✓ Branch 55 → 56 taken 4 times.
✗ Branch 55 → 177 not taken.
✓ Branch 56 → 57 taken 4 times.
✗ Branch 56 → 175 not taken.
16 llvm::Constant *fileEndMsg = createGlobalStringConst("fileEndMsg", TEST_FILE_END_MSG, *rootScope->codeLoc);
612
3/6
✓ Branch 63 → 64 taken 4 times.
✗ Branch 63 → 195 not taken.
✓ Branch 66 → 67 taken 4 times.
✗ Branch 66 → 189 not taken.
✓ Branch 67 → 68 taken 4 times.
✗ Branch 67 → 187 not taken.
16 llvm::Constant *runMsg = createGlobalStringConst("runMsg", TEST_CASE_RUN_MSG, *rootScope->codeLoc);
613
3/6
✓ Branch 74 → 75 taken 4 times.
✗ Branch 74 → 207 not taken.
✓ Branch 77 → 78 taken 4 times.
✗ Branch 77 → 201 not taken.
✓ Branch 78 → 79 taken 4 times.
✗ Branch 78 → 199 not taken.
16 llvm::Constant *successMsg = createGlobalStringConst("successMsg", TEST_CASE_SUCCESS_MSG, *rootScope->codeLoc);
614
3/6
✓ Branch 85 → 86 taken 4 times.
✗ Branch 85 → 219 not taken.
✓ Branch 88 → 89 taken 4 times.
✗ Branch 88 → 213 not taken.
✓ Branch 89 → 90 taken 4 times.
✗ Branch 89 → 211 not taken.
16 llvm::Constant *errorMsg = createGlobalStringConst("errorMsg", TEST_CASE_FAILED_MSG, *rootScope->codeLoc);
615
3/6
✓ Branch 96 → 97 taken 4 times.
✗ Branch 96 → 231 not taken.
✓ Branch 99 → 100 taken 4 times.
✗ Branch 99 → 225 not taken.
✓ Branch 100 → 101 taken 4 times.
✗ Branch 100 → 223 not taken.
16 llvm::Constant *skippedMsg = createGlobalStringConst("skippedMsg", TEST_CASE_SKIPPED_MSG, *rootScope->codeLoc);
616
617 // Prepare entry for test main
618
1/2
✓ Branch 105 → 106 taken 4 times.
✗ Branch 105 → 263 not taken.
4 QualType functionType(TY_FUNCTION);
619
1/2
✓ Branch 106 → 107 taken 4 times.
✗ Branch 106 → 263 not taken.
4 functionType.setQualifiers(TypeQualifiers::of(TY_FUNCTION));
620
1/2
✓ Branch 108 → 109 taken 4 times.
✗ Branch 108 → 263 not taken.
4 functionType.makePublic();
621
2/4
✓ Branch 111 → 112 taken 4 times.
✗ Branch 111 → 237 not taken.
✓ Branch 112 → 113 taken 4 times.
✗ Branch 112 → 235 not taken.
8 SymbolTableEntry entry(MAIN_FUNCTION_NAME, functionType, rootScope, nullptr, 0, false);
622
623 // Prepare test main function
624
3/6
✓ Branch 117 → 118 taken 4 times.
✗ Branch 117 → 246 not taken.
✓ Branch 118 → 119 taken 4 times.
✗ Branch 118 → 245 not taken.
✓ Branch 121 → 122 taken 4 times.
✗ Branch 121 → 241 not taken.
12 Function testMain(MAIN_FUNCTION_NAME, &entry, QualType(TY_DYN), QualType(TY_INT), {}, {}, nullptr);
625 4 testMain.used = true; // Mark as used to prevent removal
626 4 testMain.implicitDefault = true;
627 4 testMain.mangleFunctionName = false;
628
629 // Prepare scope
630
2/4
✓ Branch 127 → 128 taken 4 times.
✗ Branch 127 → 255 not taken.
✓ Branch 128 → 129 taken 4 times.
✗ Branch 128 → 253 not taken.
4 rootScope->createChildScope(testMain.getScopeName(), ScopeType::FUNC_PROC_BODY, nullptr);
631
632 // Generate
633 const std::function<void()> generateBody = [&] {
634 // Prepare result variable
635 4 std::vector<llvm::Value *> testCaseResults;
636
1/2
✓ Branch 3 → 4 taken 4 times.
✗ Branch 3 → 273 not taken.
4 testCaseResults.reserve(tests.size());
637
638 // Print start message
639 5 const auto accFct = [&](size_t sum, const std::vector<const Function *> *innerVector) { return sum + innerVector->size(); };
640 4 const size_t totalTestCount = std::accumulate(tests.begin(), tests.end(), 0, accFct);
641
5/10
✓ Branch 7 → 8 taken 4 times.
✗ Branch 7 → 193 not taken.
✓ Branch 8 → 9 taken 4 times.
✗ Branch 8 → 191 not taken.
✓ Branch 10 → 11 taken 4 times.
✗ Branch 10 → 191 not taken.
✓ Branch 12 → 13 taken 4 times.
✗ Branch 12 → 190 not taken.
✓ Branch 13 → 14 taken 4 times.
✗ Branch 13 → 190 not taken.
4 builder.CreateCall(printfFct, {allStartMsg, builder.getInt32(totalTestCount), builder.getInt32(tests.size())});
642
643 // Generate a call to each test function
644
2/2
✓ Branch 165 → 16 taken 5 times.
✓ Branch 165 → 166 taken 4 times.
9 for (const std::vector<const Function *> *testSuite : tests) {
645 // Print test suite prologue
646
1/2
✓ Branch 18 → 19 taken 5 times.
✗ Branch 18 → 263 not taken.
5 const std::string fileName = testSuite->front()->bodyScope->sourceFile->fileName;
647
3/6
✓ Branch 20 → 21 taken 5 times.
✗ Branch 20 → 261 not taken.
✓ Branch 23 → 24 taken 5 times.
✗ Branch 23 → 196 not taken.
✓ Branch 24 → 25 taken 5 times.
✗ Branch 24 → 194 not taken.
10 llvm::Constant *fileNameValue = createGlobalStringConst("fileName", fileName, testSuite->front()->getDeclCodeLoc());
648
4/8
✓ Branch 27 → 28 taken 5 times.
✗ Branch 27 → 203 not taken.
✓ Branch 29 → 30 taken 5 times.
✗ Branch 29 → 201 not taken.
✓ Branch 31 → 32 taken 5 times.
✗ Branch 31 → 200 not taken.
✓ Branch 32 → 33 taken 5 times.
✗ Branch 32 → 200 not taken.
5 builder.CreateCall(printfFct, {fileStartMsg, builder.getInt32(testSuite->size()), fileNameValue});
649
650
3/4
✓ Branch 36 → 37 taken 10 times.
✗ Branch 36 → 255 not taken.
✓ Branch 155 → 35 taken 10 times.
✓ Branch 155 → 156 taken 5 times.
15 for (const Function *testFunction : *testSuite) {
651
1/2
✗ Branch 48 → 49 not taken.
✓ Branch 48 → 50 taken 10 times.
10 assert(testFunction->isNormalFunction());
652
1/2
✗ Branch 51 → 52 not taken.
✓ Branch 51 → 53 taken 10 times.
10 assert(testFunction->paramList.empty());
653
654 // Retrieve attribute list for the test function
655
2/4
✓ Branch 53 → 54 taken 10 times.
✗ Branch 53 → 255 not taken.
✗ Branch 54 → 55 not taken.
✓ Branch 54 → 56 taken 10 times.
10 assert(testFunction->declNode->isFctOrProcDef());
656
1/2
✓ Branch 56 → 57 taken 10 times.
✗ Branch 56 → 58 not taken.
10 const auto fctDefNode = spice_pointer_cast<FctDefBaseNode *>(testFunction->declNode);
657
1/2
✗ Branch 63 → 64 not taken.
✓ Branch 63 → 65 taken 10 times.
10 assert(fctDefNode->attrs != nullptr);
658 10 const AttrLstNode *attrs = fctDefNode->attrs->attrLst;
659
3/6
✓ Branch 67 → 68 taken 10 times.
✗ Branch 67 → 206 not taken.
✓ Branch 68 → 69 taken 10 times.
✗ Branch 68 → 204 not taken.
✗ Branch 69 → 70 not taken.
✓ Branch 69 → 71 taken 10 times.
20 assert(attrs->getAttrValueByName(ATTR_TEST)->boolValue); // The test attribute must be present
660
2/4
✓ Branch 75 → 76 taken 10 times.
✗ Branch 75 → 212 not taken.
✓ Branch 76 → 77 taken 10 times.
✗ Branch 76 → 210 not taken.
20 const CompileTimeValue *testSkipAttr = attrs->getAttrValueByName(ATTR_TEST_SKIP);
661
4/4
✓ Branch 79 → 80 taken 3 times.
✓ Branch 79 → 82 taken 7 times.
✓ Branch 80 → 81 taken 1 time.
✓ Branch 80 → 82 taken 2 times.
10 const bool skipTest = testSkipAttr && testSkipAttr->boolValue;
662
2/4
✓ Branch 85 → 86 taken 10 times.
✗ Branch 85 → 218 not taken.
✓ Branch 86 → 87 taken 10 times.
✗ Branch 86 → 216 not taken.
20 const CompileTimeValue *testNameAttr = attrs->getAttrValueByName(ATTR_TEST_NAME);
663
664 // Prepare test name
665
1/2
✓ Branch 89 → 90 taken 10 times.
✗ Branch 89 → 255 not taken.
10 std::stringstream testName;
666
1/2
✓ Branch 90 → 91 taken 10 times.
✗ Branch 90 → 253 not taken.
10 testName << testFunction->name;
667
2/2
✓ Branch 91 → 92 taken 1 time.
✓ Branch 91 → 96 taken 9 times.
10 if (testNameAttr)
668
4/8
✓ Branch 92 → 93 taken 1 time.
✗ Branch 92 → 253 not taken.
✓ Branch 93 → 94 taken 1 time.
✗ Branch 93 → 253 not taken.
✓ Branch 94 → 95 taken 1 time.
✗ Branch 94 → 253 not taken.
✓ Branch 95 → 96 taken 1 time.
✗ Branch 95 → 253 not taken.
1 testName << " (" << resourceManager.compileTimeStringValues.at(testNameAttr->stringValueOffset) << ")";
669
670 // Print test case run message
671
4/8
✓ Branch 96 → 97 taken 10 times.
✗ Branch 96 → 253 not taken.
✓ Branch 97 → 98 taken 10 times.
✗ Branch 97 → 230 not taken.
✓ Branch 100 → 101 taken 10 times.
✗ Branch 100 → 224 not taken.
✓ Branch 101 → 102 taken 10 times.
✗ Branch 101 → 222 not taken.
30 llvm::Constant *testNameValue = createGlobalStringConst("testName", testName.str(), testFunction->getDeclCodeLoc());
672
3/6
✓ Branch 105 → 106 taken 10 times.
✗ Branch 105 → 234 not taken.
✓ Branch 107 → 108 taken 10 times.
✗ Branch 107 → 231 not taken.
✓ Branch 108 → 109 taken 10 times.
✗ Branch 108 → 231 not taken.
10 builder.CreateCall(printfFct, {runMsg, testNameValue});
673
674
2/2
✓ Branch 109 → 110 taken 1 time.
✓ Branch 109 → 115 taken 9 times.
10 if (skipTest) {
675 // Print test case skip message
676
3/6
✓ Branch 110 → 111 taken 1 time.
✗ Branch 110 → 238 not taken.
✓ Branch 112 → 113 taken 1 time.
✗ Branch 112 → 235 not taken.
✓ Branch 113 → 114 taken 1 time.
✗ Branch 113 → 235 not taken.
1 builder.CreateCall(printfFct, {skippedMsg, testNameValue});
677 1 continue;
678 }
679
680 // Test function is not defined in the current module -> declare it
681
1/2
✓ Branch 115 → 116 taken 9 times.
✗ Branch 115 → 253 not taken.
9 const std::string mangledName = testFunction->getMangledName();
682
3/4
✓ Branch 117 → 118 taken 9 times.
✗ Branch 117 → 239 not taken.
✓ Branch 118 → 119 taken 2 times.
✓ Branch 118 → 131 taken 7 times.
9 if (!module->getFunction(mangledName)) {
683
2/4
✓ Branch 119 → 120 taken 2 times.
✗ Branch 119 → 251 not taken.
✗ Branch 120 → 121 not taken.
✓ Branch 120 → 122 taken 2 times.
2 assert(testFunction->returnType.is(TY_BOOL));
684
1/2
✗ Branch 123 → 124 not taken.
✓ Branch 123 → 125 taken 2 times.
2 assert(testFunction->paramList.empty());
685
2/4
✓ Branch 126 → 127 taken 2 times.
✗ Branch 126 → 240 not taken.
✓ Branch 127 → 128 taken 2 times.
✗ Branch 127 → 240 not taken.
2 llvm::FunctionType *fctType = llvm::FunctionType::get(builder.getInt1Ty(), {}, false);
686
1/2
✓ Branch 129 → 130 taken 2 times.
✗ Branch 129 → 241 not taken.
2 module->getOrInsertFunction(mangledName, fctType);
687 }
688
689 // Call test function
690
1/2
✓ Branch 132 → 133 taken 9 times.
✗ Branch 132 → 242 not taken.
9 llvm::Function *callee = module->getFunction(mangledName);
691
1/2
✗ Branch 133 → 134 not taken.
✓ Branch 133 → 135 taken 9 times.
9 assert(callee != nullptr);
692
3/6
✓ Branch 135 → 136 taken 9 times.
✗ Branch 135 → 245 not taken.
✓ Branch 137 → 138 taken 9 times.
✗ Branch 137 → 243 not taken.
✓ Branch 138 → 139 taken 9 times.
✗ Branch 138 → 243 not taken.
9 llvm::Value *testCaseResult = builder.CreateCall(callee);
693
1/2
✓ Branch 139 → 140 taken 9 times.
✗ Branch 139 → 251 not taken.
9 testCaseResults.push_back(testCaseResult);
694
695 // Print test case result message
696
2/4
✓ Branch 140 → 141 taken 9 times.
✗ Branch 140 → 246 not taken.
✓ Branch 141 → 142 taken 9 times.
✗ Branch 141 → 246 not taken.
9 llvm::Value *message = builder.CreateSelect(testCaseResult, successMsg, errorMsg);
697
3/6
✓ Branch 142 → 143 taken 9 times.
✗ Branch 142 → 250 not taken.
✓ Branch 144 → 145 taken 9 times.
✗ Branch 144 → 247 not taken.
✓ Branch 145 → 146 taken 9 times.
✗ Branch 145 → 247 not taken.
9 builder.CreateCall(printfFct, {message, testNameValue});
698
2/2
✓ Branch 149 → 150 taken 9 times.
✓ Branch 149 → 152 taken 1 time.
10 }
699
700 // Print test suite epilogue
701
4/8
✓ Branch 156 → 157 taken 5 times.
✗ Branch 156 → 260 not taken.
✓ Branch 158 → 159 taken 5 times.
✗ Branch 158 → 258 not taken.
✓ Branch 160 → 161 taken 5 times.
✗ Branch 160 → 257 not taken.
✓ Branch 161 → 162 taken 5 times.
✗ Branch 161 → 257 not taken.
5 builder.CreateCall(printfFct, {fileEndMsg, builder.getInt32(testSuite->size()), fileNameValue});
702 5 }
703
704 // Print end message
705
5/10
✓ Branch 166 → 167 taken 4 times.
✗ Branch 166 → 268 not taken.
✓ Branch 167 → 168 taken 4 times.
✗ Branch 167 → 266 not taken.
✓ Branch 169 → 170 taken 4 times.
✗ Branch 169 → 266 not taken.
✓ Branch 171 → 172 taken 4 times.
✗ Branch 171 → 265 not taken.
✓ Branch 172 → 173 taken 4 times.
✗ Branch 172 → 265 not taken.
4 builder.CreateCall(printfFct, {allEndMsg, builder.getInt32(totalTestCount), builder.getInt32(tests.size())});
706
707 // Compute overall result
708
1/2
✓ Branch 173 → 174 taken 4 times.
✗ Branch 173 → 273 not taken.
4 llvm::Value *overallResult = builder.getTrue();
709
2/2
✓ Branch 181 → 176 taken 9 times.
✓ Branch 181 → 182 taken 4 times.
13 for (llvm::Value *testCaseResult : testCaseResults)
710
2/4
✓ Branch 177 → 178 taken 9 times.
✗ Branch 177 → 269 not taken.
✓ Branch 178 → 179 taken 9 times.
✗ Branch 178 → 269 not taken.
9 overallResult = builder.CreateAnd(overallResult, testCaseResult);
711
712 // Return code must be 0 for success and 1 for failure, so we need to invert the result and zero extend to 32 bit
713
2/4
✓ Branch 182 → 183 taken 4 times.
✗ Branch 182 → 271 not taken.
✓ Branch 183 → 184 taken 4 times.
✗ Branch 183 → 271 not taken.
4 llvm::Value *overallResultNegated = builder.CreateNot(overallResult);
714
3/6
✓ Branch 184 → 185 taken 4 times.
✗ Branch 184 → 272 not taken.
✓ Branch 185 → 186 taken 4 times.
✗ Branch 185 → 272 not taken.
✓ Branch 186 → 187 taken 4 times.
✗ Branch 186 → 272 not taken.
4 llvm::Value *exitCode = builder.CreateZExt(overallResultNegated, builder.getInt32Ty());
715
1/2
✓ Branch 187 → 188 taken 4 times.
✗ Branch 187 → 273 not taken.
4 builder.CreateRet(exitCode);
716
1/2
✓ Branch 130 → 131 taken 4 times.
✗ Branch 130 → 256 not taken.
8 };
717
1/2
✓ Branch 131 → 132 taken 4 times.
✗ Branch 131 → 257 not taken.
4 generateImplicitFunction(generateBody, &testMain);
718 4 }
719
720 } // namespace spice::compiler
721