GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 93.3% 97 / 0 / 104
Functions: 75.0% 9 / 0 / 12
Branches: 54.7% 105 / 0 / 192

src/irgenerator/GenStatements.cpp
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #include "IRGenerator.h"
4
5 #include <ast/ASTNodes.h>
6 #include <driver/Driver.h>
7 #include <symboltablebuilder/SymbolTableBuilder.h>
8
9 #include <llvm/IR/Module.h>
10
11 namespace spice::compiler {
12
13 57990 std::any IRGenerator::visitStmtLst(const StmtLstNode *node) {
14 // Generate instructions in the scope
15
2/2
✓ Branch 20 → 4 taken 113465 times.
✓ Branch 20 → 21 taken 57969 times.
229424 for (const StmtNode *stmt : node->statements) {
16 // Check if we can cancel generating instructions for this code branch
17
4/4
✓ Branch 6 → 7 taken 113464 times.
✓ Branch 6 → 21 taken 1 time.
✓ Branch 7 → 8 taken 113444 times.
✓ Branch 7 → 21 taken 20 times.
113465 if (blockAlreadyTerminated || stmt->unreachable)
18 break;
19
20 // Set source location for debug info
21
1/2
✓ Branch 8 → 9 taken 113444 times.
✗ Branch 8 → 27 not taken.
113444 diGenerator.setSourceLocation(stmt);
22
23 // Visit child
24
1/2
✓ Branch 9 → 10 taken 113444 times.
✗ Branch 9 → 26 not taken.
113444 visit(stmt);
25 }
26
27 // Generate cleanup code of this scope, e.g. dtor calls for struct instances
28 57990 generateScopeCleanup(node);
29
30
1/2
✓ Branch 22 → 23 taken 57990 times.
✗ Branch 22 → 28 not taken.
115980 return nullptr;
31 }
32
33 std::any IRGenerator::visitTypeAltsLst(const TypeAltsLstNode *node) {
34 return nullptr; // Noop
35 }
36
37 24622 std::any IRGenerator::visitDeclStmt(const DeclStmtNode *node) {
38 // Get variable entry
39
1/2
✓ Branch 2 → 3 taken 24622 times.
✗ Branch 2 → 88 not taken.
24622 const SymbolTableEntry *varEntry = node->entries.at(manIdx);
40
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 24622 times.
24622 assert(varEntry != nullptr);
41
1/2
✓ Branch 5 → 6 taken 24622 times.
✗ Branch 5 → 88 not taken.
24622 const QualType varSymbolType = varEntry->getQualType();
42
43 // Get LLVM type of variable
44
1/2
✓ Branch 6 → 7 taken 24622 times.
✗ Branch 6 → 88 not taken.
24622 llvm::Type *varTy = varSymbolType.toLLVMType(sourceFile);
45
46 // Check if the declaration is with an assignment or the default value
47 24622 llvm::Value *varAddress = nullptr;
48
2/2
✓ Branch 7 → 8 taken 22813 times.
✓ Branch 7 → 33 taken 1809 times.
24622 if (node->hasAssignment) { // Assignment
49
2/2
✓ Branch 8 → 9 taken 223 times.
✓ Branch 8 → 27 taken 22590 times.
22813 if (node->calledCopyCtor) {
50 // Allocate memory
51
1/2
✓ Branch 12 → 13 taken 223 times.
✗ Branch 12 → 63 not taken.
223 varAddress = insertAlloca(varTy);
52
1/2
✓ Branch 15 → 16 taken 223 times.
✗ Branch 15 → 88 not taken.
223 updateAddress(varEntry, varAddress);
53 // Generate debug info for variable declaration
54
1/2
✓ Branch 16 → 17 taken 223 times.
✗ Branch 16 → 88 not taken.
223 diGenerator.generateLocalVarDebugInfo(node->varName, varAddress);
55 // Call copy ctor
56
1/2
✓ Branch 17 → 18 taken 223 times.
✗ Branch 17 → 88 not taken.
223 llvm::Value *rhsAddress = resolveAddress(node->assignExpr);
57
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 223 times.
223 assert(rhsAddress != nullptr);
58
2/4
✓ Branch 22 → 23 taken 223 times.
✗ Branch 22 → 71 not taken.
✓ Branch 23 → 24 taken 223 times.
✗ Branch 23 → 69 not taken.
669 generateCtorOrDtorCall(varEntry, node->calledCopyCtor, {rhsAddress});
59 } else {
60 // Assign rhs to lhs
61
1/2
✓ Branch 27 → 28 taken 22590 times.
✗ Branch 27 → 76 not taken.
22590 [[maybe_unused]] const LLVMExprResult assignResult = doAssignment(varAddress, varEntry, node->assignExpr, node, true);
62
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 22590 times.
22590 assert(assignResult.entry == varEntry);
63
1/2
✓ Branch 30 → 31 taken 22590 times.
✗ Branch 30 → 76 not taken.
22590 varAddress = getAddress(varEntry);
64
1/2
✓ Branch 31 → 32 taken 22590 times.
✗ Branch 31 → 76 not taken.
22590 updateAddress(varEntry, varAddress);
65 }
66 } else { // Default value
67 // Allocate memory
68
1/2
✓ Branch 36 → 37 taken 1809 times.
✗ Branch 36 → 77 not taken.
1809 varAddress = insertAlloca(varTy);
69
1/2
✓ Branch 39 → 40 taken 1809 times.
✗ Branch 39 → 88 not taken.
1809 updateAddress(varEntry, varAddress);
70
71 // Generate debug info for variable declaration
72
1/2
✓ Branch 40 → 41 taken 1809 times.
✗ Branch 40 → 88 not taken.
1809 diGenerator.generateLocalVarDebugInfo(node->varName, varAddress);
73
74
2/2
✓ Branch 41 → 42 taken 761 times.
✓ Branch 41 → 46 taken 1048 times.
1809 if (node->calledInitCtor) {
75 // Call no-args constructor
76
1/2
✓ Branch 43 → 44 taken 761 times.
✗ Branch 43 → 83 not taken.
761 generateCtorOrDtorCall(varEntry, node->calledInitCtor, {});
77
4/4
✓ Branch 46 → 47 taken 460 times.
✓ Branch 46 → 55 taken 588 times.
✓ Branch 47 → 48 taken 458 times.
✓ Branch 47 → 55 taken 2 times.
1048 } else if (!node->isForEachItem && cliOptions.buildMode != BuildMode::RELEASE) {
78
1/2
✗ Branch 48 → 49 not taken.
✓ Branch 48 → 50 taken 458 times.
458 assert(!node->isCtorCallRequired);
79
1/4
✗ Branch 50 → 51 not taken.
✓ Branch 50 → 53 taken 458 times.
✗ Branch 51 → 52 not taken.
✗ Branch 51 → 53 not taken.
458 assert(cliOptions.buildMode == BuildMode::DEBUG || cliOptions.buildMode == BuildMode::TEST);
80 // Retrieve default value for lhs symbol type and store it
81
1/2
✓ Branch 53 → 54 taken 458 times.
✗ Branch 53 → 88 not taken.
458 llvm::Constant *defaultValue = getDefaultValueForSymbolType(varSymbolType);
82
1/2
✓ Branch 54 → 55 taken 458 times.
✗ Branch 54 → 88 not taken.
458 insertStore(defaultValue, varAddress);
83 }
84 }
85
1/2
✗ Branch 55 → 56 not taken.
✓ Branch 55 → 57 taken 24622 times.
24622 assert(varAddress != nullptr);
86
87 // Attach the variable name to the LLVM value.
88
2/4
✓ Branch 57 → 58 taken 24622 times.
✗ Branch 57 → 86 not taken.
✓ Branch 58 → 59 taken 24622 times.
✗ Branch 58 → 86 not taken.
24622 varAddress->setName(varEntry->name);
89
90
1/2
✓ Branch 59 → 60 taken 24622 times.
✗ Branch 59 → 87 not taken.
49244 return nullptr;
91 }
92
93 std::any IRGenerator::visitQualifierLst(const QualifierLstNode *node) {
94 return nullptr; // Noop
95 }
96
97 657 std::any IRGenerator::visitModAttr(const ModAttrNode *node) {
98
1/2
✓ Branch 2 → 3 taken 657 times.
✗ Branch 2 → 6 not taken.
1314 return nullptr; // Noop
99 }
100
101 std::any IRGenerator::visitTopLevelDefinitionAttr(const TopLevelDefAttrNode *node) {
102 return nullptr; // Noop
103 }
104
105 843 std::any IRGenerator::visitCaseConstant(const CaseConstantNode *node) {
106
2/2
✓ Branch 2 → 3 taken 15 times.
✓ Branch 2 → 4 taken 828 times.
843 if (node->constant)
107 15 return visit(node->constant);
108
109 828 const SymbolTableEntry *constantEntry = node->entry;
110
4/8
✓ Branch 4 → 5 taken 828 times.
✗ Branch 4 → 12 not taken.
✓ Branch 5 → 6 taken 828 times.
✗ Branch 5 → 11 not taken.
✓ Branch 6 → 7 taken 828 times.
✗ Branch 6 → 11 not taken.
✓ Branch 7 → 8 taken 828 times.
✗ Branch 7 → 11 not taken.
1656 return getConst(constantEntry->declNode->getCompileTimeValue(manIdx), node->getEvaluatedSymbolType(manIdx), node);
111 }
112
113 27290 std::any IRGenerator::visitReturnStmt(const ReturnStmtNode *node) {
114 27290 llvm::Value *returnValue = nullptr;
115
2/2
✓ Branch 2 → 3 taken 26983 times.
✓ Branch 2 → 31 taken 307 times.
27290 if (node->hasReturnValue) { // Return value is attached to the return statement
116 26983 const ExprNode *returnExpr = node->assignExpr;
117
2/2
✓ Branch 3 → 4 taken 139 times.
✓ Branch 3 → 25 taken 26844 times.
26983 if (node->calledCopyCtor) {
118 // Perform a copy
119 139 llvm::Value *originalAddress = resolveAddress(returnExpr);
120 139 llvm::Type *returnTy = node->returnType.toLLVMType(sourceFile);
121
1/2
✓ Branch 9 → 10 taken 139 times.
✗ Branch 9 → 59 not taken.
139 llvm::Value *newAddress = insertAlloca(returnTy);
122
2/4
✓ Branch 14 → 15 taken 139 times.
✗ Branch 14 → 67 not taken.
✓ Branch 15 → 16 taken 139 times.
✗ Branch 15 → 65 not taken.
417 generateCtorOrDtorCall(newAddress, node->calledCopyCtor, {originalAddress});
123
1/2
✓ Branch 21 → 22 taken 139 times.
✗ Branch 21 → 72 not taken.
139 returnValue = insertLoad(returnTy, newAddress);
124 } else {
125
2/2
✓ Branch 26 → 27 taken 1914 times.
✓ Branch 26 → 29 taken 24930 times.
26844 returnValue = node->returnType.isRef() ? resolveAddress(returnExpr) : resolveValue(returnExpr);
126 }
127 } else { // Try to load result variable value
128
1/2
✓ Branch 33 → 34 taken 307 times.
✗ Branch 33 → 80 not taken.
921 const SymbolTableEntry *resultEntry = currentScope->lookup(RETURN_VARIABLE_NAME);
129
2/2
✓ Branch 39 → 40 taken 7 times.
✓ Branch 39 → 50 taken 300 times.
307 if (resultEntry != nullptr) {
130 7 llvm::Type *resultSTy = resultEntry->getQualType().toLLVMType(sourceFile);
131 7 llvm::Value *returnValueAddr = getAddress(resultEntry);
132
1/2
✓ Branch 46 → 47 taken 7 times.
✗ Branch 46 → 84 not taken.
7 returnValue = insertLoad(resultSTy, returnValueAddr);
133 }
134 }
135
136 // Terminate the block (with scope cleanup)
137 27290 terminateBlock(node->getParentScopeNode());
138
139 // Create return instruction
140
2/2
✓ Branch 52 → 53 taken 26990 times.
✓ Branch 52 → 54 taken 300 times.
27290 if (returnValue != nullptr) {
141 // Return with value
142 26990 builder.CreateRet(returnValue);
143 } else {
144 // Return without value
145 300 builder.CreateRetVoid();
146 }
147
148
1/2
✓ Branch 55 → 56 taken 27290 times.
✗ Branch 55 → 90 not taken.
54580 return nullptr;
149 }
150
151 233 std::any IRGenerator::visitBreakStmt(const BreakStmtNode *node) {
152 // Jump to destination block
153 233 const size_t blockIdx = breakBlocks.size() - node->breakTimes;
154 233 insertJump(breakBlocks.at(blockIdx));
155
156
1/2
✓ Branch 5 → 6 taken 233 times.
✗ Branch 5 → 9 not taken.
466 return nullptr;
157 }
158
159 727 std::any IRGenerator::visitContinueStmt(const ContinueStmtNode *node) {
160 // Jump to destination block
161 727 const size_t blockIdx = continueBlocks.size() - node->continueTimes;
162 727 insertJump(continueBlocks.at(blockIdx));
163
164
1/2
✓ Branch 5 → 6 taken 727 times.
✗ Branch 5 → 9 not taken.
1454 return nullptr;
165 }
166
167 4 std::any IRGenerator::visitFallthroughStmt(const FallthroughStmtNode *node) {
168 // Jump to destination block
169 4 insertJump(fallthroughBlocks.top());
170
171
1/2
✓ Branch 4 → 5 taken 4 times.
✗ Branch 4 → 8 not taken.
8 return nullptr;
172 }
173
174 4741 std::any IRGenerator::visitAssertStmt(const AssertStmtNode *node) {
175 // Do not generate assertions in release mode
176
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 6 taken 4741 times.
4741 if (cliOptions.buildMode == BuildMode::RELEASE)
177 return nullptr;
178
179 // Create blocks
180
1/2
✓ Branch 6 → 7 taken 4741 times.
✗ Branch 6 → 87 not taken.
4741 const std::string &codeLine = node->codeLoc.toPrettyLine();
181
2/4
✓ Branch 7 → 8 taken 4741 times.
✗ Branch 7 → 55 not taken.
✓ Branch 8 → 9 taken 4741 times.
✗ Branch 8 → 53 not taken.
4741 llvm::BasicBlock *bThen = createBlock("assert.then." + codeLine);
182
2/4
✓ Branch 10 → 11 taken 4741 times.
✗ Branch 10 → 58 not taken.
✓ Branch 11 → 12 taken 4741 times.
✗ Branch 11 → 56 not taken.
4741 llvm::BasicBlock *bExit = createBlock("assert.exit." + codeLine);
183
184 // Visit the assignExpr
185
1/2
✓ Branch 13 → 14 taken 4741 times.
✗ Branch 13 → 85 not taken.
4741 llvm::Value *condValue = resolveValue(node->assignExpr);
186
187 // Create condition check
188
1/2
✓ Branch 14 → 15 taken 4741 times.
✗ Branch 14 → 85 not taken.
4741 insertCondJump(condValue, bExit, bThen, Likelihood::LIKELY);
189
190 // Switch to then block
191
1/2
✓ Branch 15 → 16 taken 4741 times.
✗ Branch 15 → 85 not taken.
4741 switchToBlock(bThen);
192 // Create constant for error message
193
2/4
✓ Branch 16 → 17 taken 4741 times.
✗ Branch 16 → 61 not taken.
✓ Branch 17 → 18 taken 4741 times.
✗ Branch 17 → 59 not taken.
4741 const std::string errorMsg = "Assertion failed: Condition '" + node->expressionString + "' evaluated to false.\n";
194
4/8
✓ Branch 21 → 22 taken 4741 times.
✗ Branch 21 → 67 not taken.
✓ Branch 22 → 23 taken 4741 times.
✗ Branch 22 → 65 not taken.
✓ Branch 23 → 24 taken 4741 times.
✗ Branch 23 → 63 not taken.
✓ Branch 25 → 26 taken 4741 times.
✗ Branch 25 → 62 not taken.
9482 llvm::GlobalVariable *globalString = builder.CreateGlobalString(errorMsg, getUnusedGlobalName(ANON_GLOBAL_STRING_NAME));
195 // If the output should be comparable, fix alignment to 4 bytes
196
1/2
✓ Branch 29 → 30 taken 4741 times.
✗ Branch 29 → 33 not taken.
4741 if (cliOptions.comparableOutput)
197
2/4
✓ Branch 30 → 31 taken 4741 times.
✗ Branch 30 → 73 not taken.
✓ Branch 31 → 32 taken 4741 times.
✗ Branch 31 → 73 not taken.
4741 globalString->setAlignment(llvm::Align(4));
198 // Print the error message
199
1/2
✓ Branch 33 → 34 taken 4741 times.
✗ Branch 33 → 83 not taken.
4741 llvm::Function *printfFct = stdFunctionManager.getPrintfFct();
200
4/8
✓ Branch 34 → 35 taken 4741 times.
✗ Branch 34 → 77 not taken.
✓ Branch 36 → 37 taken 4741 times.
✗ Branch 36 → 74 not taken.
✓ Branch 37 → 38 taken 4741 times.
✗ Branch 37 → 74 not taken.
✓ Branch 38 → 39 taken 4741 times.
✗ Branch 38 → 83 not taken.
4741 builder.CreateCall(printfFct, globalString);
201 // Generate call to exit()
202
1/2
✓ Branch 38 → 39 taken 4741 times.
✗ Branch 38 → 83 not taken.
4741 llvm::Function *exitFct = stdFunctionManager.getExitFct();
203
5/10
✓ Branch 39 → 40 taken 4741 times.
✗ Branch 39 → 81 not taken.
✓ Branch 40 → 41 taken 4741 times.
✗ Branch 40 → 79 not taken.
✓ Branch 42 → 43 taken 4741 times.
✗ Branch 42 → 78 not taken.
✓ Branch 43 → 44 taken 4741 times.
✗ Branch 43 → 78 not taken.
✓ Branch 44 → 45 taken 4741 times.
✗ Branch 44 → 83 not taken.
4741 builder.CreateCall(exitFct, builder.getInt32(EXIT_FAILURE));
204 // Create unreachable instruction
205
1/2
✓ Branch 44 → 45 taken 4741 times.
✗ Branch 44 → 83 not taken.
4741 builder.CreateUnreachable();
206
207 // Switch to exit block
208
1/2
✓ Branch 45 → 46 taken 4741 times.
✗ Branch 45 → 83 not taken.
4741 switchToBlock(bExit);
209
210
1/2
✓ Branch 46 → 47 taken 4741 times.
✗ Branch 46 → 82 not taken.
4741 return nullptr;
211 4741 }
212
213 } // namespace spice::compiler
214