GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 76.7% 211 / 1 / 276
Functions: 92.9% 13 / 0 / 14
Branches: 44.6% 229 / 4 / 518

src/irgenerator/DebugInfoGenerator.cpp
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #include "DebugInfoGenerator.h"
4
5 #include <ast/ASTNodes.h>
6 #include <driver/Driver.h>
7 #include <irgenerator/IRGenerator.h>
8 #include <irgenerator/NameMangling.h>
9 #include <model/Function.h>
10 #include <model/Struct.h>
11 #include <util/CustomHashFunctions.h>
12 #include <util/FileUtil.h>
13
14 #include <llvm/BinaryFormat/Dwarf.h>
15 #include <llvm/IR/Module.h>
16
17 namespace spice::compiler {
18
19
1/2
✓ Branch 3 → 4 taken 2163 times.
✗ Branch 3 → 6 not taken.
2163 DebugInfoGenerator::DebugInfoGenerator(IRGenerator *irGenerator) : irGenerator(irGenerator) {}
20
21 27 void DebugInfoGenerator::initialize(const std::string &sourceFileName, std::filesystem::path sourceFileDir) {
22 27 llvm::Module *module = irGenerator->module;
23 27 llvm::LLVMContext &context = irGenerator->context;
24
25 // Create DIBuilder
26
1/2
✓ Branch 2 → 3 taken 27 times.
✗ Branch 2 → 131 not taken.
27 diBuilder = std::make_unique<llvm::DIBuilder>(*module);
27
28 // Create compilation unit
29
3/6
✓ Branch 5 → 6 taken 27 times.
✗ Branch 5 → 136 not taken.
✓ Branch 6 → 7 taken 27 times.
✗ Branch 6 → 134 not taken.
✓ Branch 7 → 8 taken 27 times.
✗ Branch 7 → 132 not taken.
27 std::filesystem::path absolutePath = absolute(sourceFileDir / sourceFileName);
30 27 absolutePath.make_preferred();
31 27 sourceFileDir.make_preferred();
32
3/6
✓ Branch 15 → 16 taken 27 times.
✗ Branch 15 → 144 not taken.
✓ Branch 17 → 18 taken 27 times.
✗ Branch 17 → 140 not taken.
✓ Branch 19 → 20 taken 27 times.
✗ Branch 19 → 138 not taken.
54 llvm::DIFile *cuDiFile = diBuilder->createFile(absolutePath.string(), sourceFileDir.string());
33
1/2
✓ Branch 29 → 30 taken 27 times.
✗ Branch 29 → 148 not taken.
54 compileUnit = diBuilder->createCompileUnit(
34
2/4
✓ Branch 25 → 26 taken 27 times.
✗ Branch 25 → 151 not taken.
✓ Branch 26 → 27 taken 27 times.
✗ Branch 26 → 150 not taken.
27 llvm::dwarf::DW_LANG_C_plus_plus_14, cuDiFile, PRODUCER_STRING, irGenerator->cliOptions.optLevel > OptLevel::O0, "", 0, "",
35 llvm::DICompileUnit::FullDebug, 0, false, false, llvm::DICompileUnit::DebugNameTableKind::None);
36
37
2/4
✓ Branch 30 → 31 taken 27 times.
✗ Branch 30 → 154 not taken.
✓ Branch 31 → 32 taken 27 times.
✗ Branch 31 → 154 not taken.
27 module->addModuleFlag(llvm::Module::Max, "Dwarf Version", 5);
38
2/4
✓ Branch 32 → 33 taken 27 times.
✗ Branch 32 → 155 not taken.
✓ Branch 33 → 34 taken 27 times.
✗ Branch 33 → 155 not taken.
27 module->addModuleFlag(llvm::Module::Warning, "Debug Info Version", llvm::DEBUG_METADATA_VERSION);
39
40 // Create another DIFile as scope for subprograms
41
2/4
✓ Branch 37 → 38 taken 27 times.
✗ Branch 37 → 159 not taken.
✓ Branch 40 → 41 taken 27 times.
✗ Branch 40 → 156 not taken.
54 diFile = diBuilder->createFile(sourceFileName, sourceFileDir.string());
42
43
1/2
✓ Branch 43 → 44 taken 27 times.
✗ Branch 43 → 200 not taken.
27 pointerWidth = irGenerator->module->getDataLayout().getPointerSizeInBits();
44
45 // Initialize primitive debug types
46
2/4
✓ Branch 45 → 46 taken 27 times.
✗ Branch 45 → 163 not taken.
✓ Branch 46 → 47 taken 27 times.
✗ Branch 46 → 163 not taken.
27 doubleTy = diBuilder->createBasicType("double", 64, llvm::dwarf::DW_ATE_float);
47
2/4
✓ Branch 48 → 49 taken 27 times.
✗ Branch 48 → 164 not taken.
✓ Branch 49 → 50 taken 27 times.
✗ Branch 49 → 164 not taken.
27 intTy = diBuilder->createBasicType("int", 32, llvm::dwarf::DW_ATE_signed);
48
2/4
✓ Branch 51 → 52 taken 27 times.
✗ Branch 51 → 165 not taken.
✓ Branch 52 → 53 taken 27 times.
✗ Branch 52 → 165 not taken.
27 uIntTy = diBuilder->createBasicType("unsigned int", 32, llvm::dwarf::DW_ATE_unsigned);
49
2/4
✓ Branch 54 → 55 taken 27 times.
✗ Branch 54 → 166 not taken.
✓ Branch 55 → 56 taken 27 times.
✗ Branch 55 → 166 not taken.
27 shortTy = diBuilder->createBasicType("short", 16, llvm::dwarf::DW_ATE_signed);
50
2/4
✓ Branch 57 → 58 taken 27 times.
✗ Branch 57 → 167 not taken.
✓ Branch 58 → 59 taken 27 times.
✗ Branch 58 → 167 not taken.
27 uShortTy = diBuilder->createBasicType("unsigned short", 16, llvm::dwarf::DW_ATE_unsigned);
51
2/4
✓ Branch 60 → 61 taken 27 times.
✗ Branch 60 → 168 not taken.
✓ Branch 61 → 62 taken 27 times.
✗ Branch 61 → 168 not taken.
27 longTy = diBuilder->createBasicType("long", 64, llvm::dwarf::DW_ATE_signed);
52
2/4
✓ Branch 63 → 64 taken 27 times.
✗ Branch 63 → 169 not taken.
✓ Branch 64 → 65 taken 27 times.
✗ Branch 64 → 169 not taken.
27 uLongTy = diBuilder->createBasicType("unsigned long", 64, llvm::dwarf::DW_ATE_unsigned);
53
2/4
✓ Branch 66 → 67 taken 27 times.
✗ Branch 66 → 170 not taken.
✓ Branch 67 → 68 taken 27 times.
✗ Branch 67 → 170 not taken.
27 byteTy = diBuilder->createBasicType("byte", 8, llvm::dwarf::DW_ATE_unsigned);
54
2/4
✓ Branch 69 → 70 taken 27 times.
✗ Branch 69 → 171 not taken.
✓ Branch 70 → 71 taken 27 times.
✗ Branch 70 → 171 not taken.
27 charTy = diBuilder->createBasicType("char", 8, llvm::dwarf::DW_ATE_unsigned_char);
55
1/2
✓ Branch 75 → 76 taken 27 times.
✗ Branch 75 → 172 not taken.
54 stringTy = diBuilder->createPointerType(charTy, pointerWidth);
56
2/4
✓ Branch 77 → 78 taken 27 times.
✗ Branch 77 → 175 not taken.
✓ Branch 78 → 79 taken 27 times.
✗ Branch 78 → 175 not taken.
27 boolTy = diBuilder->createBasicType("bool", 8, llvm::dwarf::DW_ATE_boolean);
57
2/4
✓ Branch 80 → 81 taken 27 times.
✗ Branch 80 → 176 not taken.
✓ Branch 81 → 82 taken 27 times.
✗ Branch 81 → 176 not taken.
27 voidTy = diBuilder->createBasicType("void", 0, llvm::dwarf::DW_ATE_unsigned);
58
59 // Initialize lambda fat ptr type
60
1/2
✓ Branch 82 → 83 taken 27 times.
✗ Branch 82 → 200 not taken.
27 llvm::PointerType *ptrTy = irGenerator->builder.getPtrTy();
61
1/2
✓ Branch 83 → 84 taken 27 times.
✗ Branch 83 → 200 not taken.
27 llvm::IntegerType *int64Ty = irGenerator->builder.getInt64Ty();
62 27 const llvm::DataLayout &dataLayout = module->getDataLayout();
63
1/2
✓ Branch 85 → 86 taken 27 times.
✗ Branch 85 → 200 not taken.
27 const llvm::StructLayout *structLayout = dataLayout.getStructLayout(irGenerator->llvmTypes.lambdaFatPtrType);
64
1/2
✓ Branch 86 → 87 taken 27 times.
✗ Branch 86 → 177 not taken.
27 const uint32_t alignInBits = dataLayout.getABITypeAlign(irGenerator->llvmTypes.lambdaFatPtrType).value();
65
1/2
✓ Branch 88 → 89 taken 27 times.
✗ Branch 88 → 178 not taken.
27 const uint32_t ptrAlignInBits = dataLayout.getABITypeAlign(ptrTy).value();
66
2/4
✓ Branch 90 → 91 taken 27 times.
✗ Branch 90 → 179 not taken.
✓ Branch 91 → 92 taken 27 times.
✗ Branch 91 → 179 not taken.
27 const uint32_t int64Width = dataLayout.getTypeSizeInBits(int64Ty);
67
1/2
✓ Branch 92 → 93 taken 27 times.
✗ Branch 92 → 180 not taken.
27 const uint32_t int64AlignInBits = dataLayout.getABITypeAlign(int64Ty).value();
68
2/4
✓ Branch 94 → 95 taken 27 times.
✗ Branch 94 → 181 not taken.
✓ Branch 95 → 96 taken 27 times.
✗ Branch 95 → 181 not taken.
27 const uint64_t fctPtrOffset = structLayout->getElementOffsetInBits(0);
69
2/4
✓ Branch 96 → 97 taken 27 times.
✗ Branch 96 → 182 not taken.
✓ Branch 97 → 98 taken 27 times.
✗ Branch 97 → 182 not taken.
27 const uint64_t capturesOffset = structLayout->getElementOffsetInBits(1);
70
2/4
✓ Branch 98 → 99 taken 27 times.
✗ Branch 98 → 183 not taken.
✓ Branch 99 → 100 taken 27 times.
✗ Branch 99 → 183 not taken.
27 const uint64_t captureSizeOffset = structLayout->getElementOffsetInBits(2);
71
72
1/2
✓ Branch 104 → 105 taken 27 times.
✗ Branch 104 → 184 not taken.
54 llvm::DIType *voidPtrDIType = diBuilder->createPointerType(voidTy, pointerWidth, ptrAlignInBits);
73
5/10
✓ Branch 108 → 109 taken 27 times.
✗ Branch 108 → 189 not taken.
✓ Branch 109 → 110 taken 27 times.
✗ Branch 109 → 188 not taken.
✓ Branch 110 → 111 taken 27 times.
✗ Branch 110 → 188 not taken.
✓ Branch 111 → 112 taken 27 times.
✗ Branch 111 → 187 not taken.
✓ Branch 112 → 113 taken 27 times.
✗ Branch 112 → 187 not taken.
54 fatPtrTy = diBuilder->createStructType(diFile, "_lambda", diFile, 0, structLayout->getSizeInBits(), alignInBits,
74 llvm::DINode::FlagTypePassByValue | llvm::DINode::FlagNonTrivial, nullptr, {}, 0,
75
1/2
✓ Branch 106 → 107 taken 27 times.
✗ Branch 106 → 190 not taken.
27 nullptr, "_lambda");
76
2/4
✓ Branch 115 → 116 taken 27 times.
✗ Branch 115 → 191 not taken.
✓ Branch 116 → 117 taken 27 times.
✗ Branch 116 → 191 not taken.
27 const auto firstType = diBuilder->createMemberType(fatPtrTy, "fct", diFile, 0, pointerWidth, ptrAlignInBits, fctPtrOffset,
77 llvm::DINode::FlagZero, voidPtrDIType);
78
2/4
✓ Branch 119 → 120 taken 27 times.
✗ Branch 119 → 193 not taken.
✓ Branch 120 → 121 taken 27 times.
✗ Branch 120 → 193 not taken.
27 const auto secondType = diBuilder->createMemberType(fatPtrTy, "captures", diFile, 0, pointerWidth, ptrAlignInBits,
79 capturesOffset, llvm::DINode::FlagZero, voidPtrDIType);
80
2/4
✓ Branch 123 → 124 taken 27 times.
✗ Branch 123 → 195 not taken.
✓ Branch 124 → 125 taken 27 times.
✗ Branch 124 → 195 not taken.
27 const auto thirdType = diBuilder->createMemberType(fatPtrTy, "captureSize", diFile, 0, int64Width, int64AlignInBits,
81 captureSizeOffset, llvm::DINode::FlagZero, uLongTy);
82
2/4
✓ Branch 126 → 127 taken 27 times.
✗ Branch 126 → 197 not taken.
✓ Branch 128 → 129 taken 27 times.
✗ Branch 128 → 197 not taken.
27 fatPtrTy->replaceElements(llvm::MDTuple::get(context, {firstType, secondType, thirdType}));
83 27 }
84
85 33917 void DebugInfoGenerator::generateFunctionDebugInfo(llvm::Function *llvmFunction, const Function *spiceFunc, bool isLambda) {
86
2/2
✓ Branch 2 → 3 taken 33690 times.
✓ Branch 2 → 4 taken 227 times.
33917 if (!irGenerator->cliOptions.instrumentation.generateDebugInfo)
87 33690 return;
88
89 227 const ASTNode *node = spiceFunc->declNode;
90
1/2
✓ Branch 4 → 5 taken 227 times.
✗ Branch 4 → 125 not taken.
227 const uint32_t lineNo = spiceFunc->getDeclCodeLoc().line;
91
92 // Prepare flags
93 227 llvm::DIScope *scope = diFile;
94 227 llvm::DINode::DIFlags flags = llvm::DINode::FlagPrototyped;
95
8/10
✓ Branch 5 → 6 taken 221 times.
✓ Branch 5 → 10 taken 6 times.
✓ Branch 6 → 7 taken 221 times.
✗ Branch 6 → 125 not taken.
✓ Branch 7 → 8 taken 221 times.
✗ Branch 7 → 125 not taken.
✓ Branch 8 → 9 taken 212 times.
✓ Branch 8 → 10 taken 9 times.
✓ Branch 11 → 12 taken 212 times.
✓ Branch 11 → 13 taken 15 times.
227 if (spiceFunc->entry && spiceFunc->entry->getQualType().isPublic())
96
1/2
✓ Branch 12 → 13 taken 212 times.
✗ Branch 12 → 125 not taken.
212 flags |= llvm::DINode::FlagPublic;
97
98 // Prepare spFlags
99 227 llvm::DISubprogram::DISPFlags spFlags = llvm::DISubprogram::SPFlagDefinition;
100
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 227 times.
227 if (isLambda)
101 spFlags |= llvm::DISubprogram::SPFlagLocalToUnit;
102
2/2
✓ Branch 15 → 16 taken 5 times.
✓ Branch 15 → 20 taken 222 times.
227 if (spiceFunc->isVirtual) {
103
2/4
✓ Branch 16 → 17 taken 5 times.
✗ Branch 16 → 125 not taken.
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 19 taken 5 times.
5 if (spiceFunc->thisType.is(TY_INTERFACE))
104 spFlags |= llvm::DISubprogram::SPFlagPureVirtual;
105 else
106
1/2
✓ Branch 19 → 20 taken 5 times.
✗ Branch 19 → 125 not taken.
5 spFlags |= llvm::DISubprogram::SPFlagVirtual;
107 }
108
109 // Collect arguments
110
1/2
✓ Branch 20 → 21 taken 227 times.
✗ Branch 20 → 123 not taken.
227 std::vector<llvm::Metadata *> argTypes;
111
2/2
✓ Branch 23 → 24 taken 83 times.
✓ Branch 23 → 26 taken 144 times.
227 if (spiceFunc->isProcedure())
112
1/2
✓ Branch 24 → 25 taken 83 times.
✗ Branch 24 → 97 not taken.
83 argTypes.push_back(voidTy);
113 else
114
2/4
✓ Branch 26 → 27 taken 144 times.
✗ Branch 26 → 98 not taken.
✓ Branch 27 → 28 taken 144 times.
✗ Branch 27 → 98 not taken.
144 argTypes.push_back(getDITypeForQualType(node, spiceFunc->returnType)); // Add result type
115
2/2
✓ Branch 32 → 33 taken 134 times.
✓ Branch 32 → 36 taken 93 times.
227 if (spiceFunc->isMethod())
116
2/4
✓ Branch 33 → 34 taken 134 times.
✗ Branch 33 → 99 not taken.
✓ Branch 34 → 35 taken 134 times.
✗ Branch 34 → 99 not taken.
134 argTypes.push_back(getDITypeForQualType(node, spiceFunc->thisType)); // Add this type
117
1/2
✗ Branch 36 → 37 not taken.
✓ Branch 36 → 45 taken 227 times.
227 if (isLambda) {
118 llvm::DICompositeType *captureStructType = generateCaptureStructDebugInfo(spiceFunc);
119 scope = captureStructType;
120 llvm::DIType *captureStructPtr = diBuilder->createPointerType(captureStructType, pointerWidth);
121 argTypes.push_back(captureStructPtr); // Add this type
122 }
123
3/4
✓ Branch 45 → 46 taken 227 times.
✗ Branch 45 → 107 not taken.
✓ Branch 61 → 48 taken 244 times.
✓ Branch 61 → 62 taken 227 times.
698 for (const QualType &argType : spiceFunc->getParamTypes()) // Add arg types
124
2/4
✓ Branch 50 → 51 taken 244 times.
✗ Branch 50 → 104 not taken.
✓ Branch 51 → 52 taken 244 times.
✗ Branch 51 → 104 not taken.
471 argTypes.push_back(getDITypeForQualType(node, argType));
125
126 // Create function type
127
2/4
✓ Branch 66 → 67 taken 227 times.
✗ Branch 66 → 108 not taken.
✓ Branch 67 → 68 taken 227 times.
✗ Branch 67 → 108 not taken.
227 llvm::DISubroutineType *functionTy = diBuilder->createSubroutineType(diBuilder->getOrCreateTypeArray(argTypes));
128
129
1/2
✓ Branch 68 → 69 taken 227 times.
✗ Branch 68 → 123 not taken.
227 const std::string mangledName = spiceFunc->getMangledName();
130 llvm::DISubprogram *subprogram;
131
1/2
✓ Branch 69 → 70 taken 227 times.
✗ Branch 69 → 121 not taken.
227 const std::string &name = spiceFunc->name;
132
2/2
✓ Branch 72 → 73 taken 134 times.
✓ Branch 72 → 80 taken 93 times.
227 if (spiceFunc->isMethod()) {
133
1/2
✓ Branch 78 → 79 taken 134 times.
✗ Branch 78 → 109 not taken.
134 subprogram = diBuilder->createMethod(scope, name, mangledName, diFile, lineNo, functionTy, 0, 0, nullptr, flags, spFlags);
134 } else {
135
1/2
✓ Branch 87 → 88 taken 93 times.
✗ Branch 87 → 113 not taken.
186 subprogram = diBuilder->createFunction(scope, name, mangledName, diFile, lineNo, functionTy, lineNo, flags, spFlags);
136 }
137
1/2
✓ Branch 90 → 91 taken 227 times.
✗ Branch 90 → 119 not taken.
227 subprogram->replaceRetainedNodes({});
138
139 // Add debug info to LLVM function
140
1/2
✓ Branch 91 → 92 taken 227 times.
✗ Branch 91 → 121 not taken.
227 llvmFunction->setSubprogram(subprogram);
141 // Add scope to lexicalBlocks
142
1/2
✓ Branch 92 → 93 taken 227 times.
✗ Branch 92 → 120 not taken.
227 lexicalBlocks.push(subprogram);
143 227 }
144
145 34265 void DebugInfoGenerator::concludeFunctionDebugInfo() {
146
2/2
✓ Branch 2 → 3 taken 34038 times.
✓ Branch 2 → 4 taken 227 times.
34265 if (!irGenerator->cliOptions.instrumentation.generateDebugInfo)
147 34038 return;
148
149
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 227 times.
227 assert(!lexicalBlocks.empty());
150 227 lexicalBlocks.pop();
151 }
152
153 23407 void DebugInfoGenerator::pushLexicalBlock(const ASTNode *node) {
154
2/2
✓ Branch 2 → 3 taken 23209 times.
✓ Branch 2 → 4 taken 198 times.
23407 if (!irGenerator->cliOptions.instrumentation.generateDebugInfo)
155 23209 return;
156
157 198 const uint32_t line = node->codeLoc.line;
158 198 const uint32_t col = node->codeLoc.col;
159 198 llvm::DILexicalBlock *lexicalBlock = diBuilder->createLexicalBlock(lexicalBlocks.top(), diFile, line, col);
160
1/2
✓ Branch 7 → 8 taken 198 times.
✗ Branch 7 → 10 not taken.
198 lexicalBlocks.push(lexicalBlock);
161 }
162
163 23407 void DebugInfoGenerator::popLexicalBlock() {
164
2/2
✓ Branch 2 → 3 taken 23209 times.
✓ Branch 2 → 4 taken 198 times.
23407 if (!irGenerator->cliOptions.instrumentation.generateDebugInfo)
165 23209 return;
166
167
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 198 times.
198 assert(!lexicalBlocks.empty());
168 198 lexicalBlocks.pop();
169 }
170
171 llvm::DICompositeType *DebugInfoGenerator::generateCaptureStructDebugInfo(const Function *spiceFunc) {
172 const CaptureMap &captures = spiceFunc->bodyScope->symbolTable.captures;
173 const ASTNode *node = spiceFunc->declNode;
174 const uint32_t lineNo = node->codeLoc.line;
175
176 // Get LLVM type for struct
177 std::vector<llvm::Type *> fieldTypes;
178 std::vector<SymbolTableEntry *> fieldEntries;
179 QualTypeList fieldSymbolTypes;
180 for (const auto &capture : captures | std::views::values) {
181 QualType captureType = capture.capturedSymbol->getQualType();
182
183 // Capture by reference
184 if (capture.getMode() == BY_REFERENCE)
185 captureType = captureType.toRef(node);
186
187 fieldEntries.push_back(capture.capturedSymbol);
188 fieldSymbolTypes.push_back(captureType);
189 fieldTypes.push_back(captureType.toLLVMType(irGenerator->sourceFile));
190 }
191 llvm::StructType *structType = llvm::StructType::get(irGenerator->context, fieldTypes);
192 const llvm::StructLayout *structLayout = irGenerator->module->getDataLayout().getStructLayout(structType);
193 const size_t alignInBits = irGenerator->module->getDataLayout().getABITypeAlign(structType).value();
194
195 llvm::DIScope *scope = lexicalBlocks.top();
196 llvm::DICompositeType *structDiType =
197 diBuilder->createClassType(scope, "", diFile, lineNo, structLayout->getSizeInBits(), alignInBits, 0,
198 llvm::DINode::FlagTypePassByValue | llvm::DINode::FlagNonTrivial, nullptr, {});
199
200 std::vector<llvm::Metadata *> fieldDITypes;
201 for (size_t i = 0; i < fieldEntries.size(); i++) {
202 llvm::DIType *fieldDiType = getDITypeForQualType(node, fieldSymbolTypes.at(i));
203 const std::string &fieldName = fieldEntries.at(i)->name;
204 const size_t offsetInBits = structLayout->getElementOffsetInBits(i);
205 const size_t fieldSize = fieldDiType->getSizeInBits();
206 const size_t fieldAlign = fieldDiType->getAlignInBits();
207 llvm::DIDerivedType *fieldDiDerivedType = diBuilder->createMemberType(
208 structDiType, fieldName, diFile, lineNo, fieldSize, fieldAlign, offsetInBits, llvm::DINode::FlagZero, fieldDiType);
209 fieldDITypes.push_back(fieldDiDerivedType);
210 }
211 structDiType->replaceElements(llvm::MDTuple::get(irGenerator->context, fieldDITypes));
212
213 return structDiType;
214 }
215
216 2350 void DebugInfoGenerator::generateGlobalVarDebugInfo(llvm::GlobalVariable *global, const SymbolTableEntry *globalEntry) {
217
2/2
✓ Branch 2 → 3 taken 2343 times.
✓ Branch 2 → 4 taken 7 times.
2350 if (!irGenerator->cliOptions.instrumentation.generateDebugInfo)
218 2343 return;
219
220
1/2
✓ Branch 4 → 5 taken 7 times.
✗ Branch 4 → 17 not taken.
7 const uint32_t lineNo = globalEntry->getDeclCodeLoc().line;
221
1/2
✓ Branch 5 → 6 taken 7 times.
✗ Branch 5 → 17 not taken.
7 const llvm::StringRef name = global->getName();
222
2/4
✓ Branch 6 → 7 taken 7 times.
✗ Branch 6 → 17 not taken.
✓ Branch 7 → 8 taken 7 times.
✗ Branch 7 → 17 not taken.
7 llvm::DIType *type = getDITypeForQualType(globalEntry->declNode, globalEntry->getQualType());
223
2/4
✓ Branch 8 → 9 taken 7 times.
✗ Branch 8 → 17 not taken.
✓ Branch 9 → 10 taken 7 times.
✗ Branch 9 → 17 not taken.
7 const bool isLocal = globalEntry->getQualType().isPublic();
224
225
2/4
✓ Branch 12 → 13 taken 7 times.
✗ Branch 12 → 16 not taken.
✓ Branch 13 → 14 taken 7 times.
✗ Branch 13 → 16 not taken.
7 global->addDebugInfo(diBuilder->createGlobalVariableExpression(compileUnit, name, name, diFile, lineNo, type, isLocal));
226 }
227
228 52 void DebugInfoGenerator::generateGlobalStringDebugInfo(llvm::GlobalVariable *global, const std::string &name, size_t length,
229 const CodeLoc &codeLoc) const {
230 52 const uint32_t lineNo = codeLoc.line;
231 52 const size_t sizeInBits = (length + 1) * 8; // +1 because of null-terminator
232
233
1/2
✓ Branch 4 → 5 taken 52 times.
✗ Branch 4 → 12 not taken.
52 llvm::DIStringType *stringType = diBuilder->createStringType(name, sizeInBits);
234
2/4
✓ Branch 9 → 10 taken 52 times.
✗ Branch 9 → 13 not taken.
✓ Branch 10 → 11 taken 52 times.
✗ Branch 10 → 13 not taken.
52 global->addDebugInfo(diBuilder->createGlobalVariableExpression(compileUnit, name, name, diFile, lineNo, stringType, true));
235 52 }
236
237 104498 void DebugInfoGenerator::generateLocalVarDebugInfo(const std::string &varName, llvm::Value *address, size_t argNumber) {
238
2/2
✓ Branch 2 → 3 taken 103800 times.
✓ Branch 2 → 4 taken 698 times.
104498 if (!irGenerator->cliOptions.instrumentation.generateDebugInfo)
239 103800 return;
240
241 // Get symbol table entry
242 698 const SymbolTableEntry *variableEntry = irGenerator->currentScope->lookupStrict(varName);
243
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 698 times.
698 assert(variableEntry != nullptr);
244 // Build debug info
245 698 llvm::DIScope *scope = lexicalBlocks.top();
246 698 llvm::DIType *diType = getDITypeForQualType(variableEntry->declNode, variableEntry->getQualType());
247 698 const uint32_t lineNo = variableEntry->declNode->codeLoc.line;
248
249 llvm::DILocalVariable *varInfo;
250
2/2
✓ Branch 12 → 13 taken 378 times.
✓ Branch 12 → 18 taken 320 times.
698 if (argNumber != SIZE_MAX)
251
1/2
✓ Branch 16 → 17 taken 378 times.
✗ Branch 16 → 36 not taken.
378 varInfo = diBuilder->createParameterVariable(scope, varName, argNumber, diFile, lineNo, diType);
252 else
253
1/2
✓ Branch 20 → 21 taken 320 times.
✗ Branch 20 → 38 not taken.
320 varInfo = diBuilder->createAutoVariable(scope, varName, diFile, lineNo, diType);
254
1/2
✓ Branch 24 → 25 taken 698 times.
✗ Branch 24 → 39 not taken.
698 llvm::DIExpression *expr = diBuilder->createExpression();
255
2/4
✓ Branch 25 → 26 taken 698 times.
✗ Branch 25 → 42 not taken.
✓ Branch 26 → 27 taken 698 times.
✗ Branch 26 → 40 not taken.
698 const llvm::DILocation *debugLocation = irGenerator->builder.getCurrentDebugLocation();
256
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 698 times.
698 assert(debugLocation != nullptr);
257
1/2
✓ Branch 33 → 34 taken 698 times.
✗ Branch 33 → 43 not taken.
698 diBuilder->insertDeclare(address, varInfo, expr, debugLocation, irGenerator->builder.GetInsertPoint());
258 }
259
260 797948 void DebugInfoGenerator::setSourceLocation(const CodeLoc &codeLoc) {
261
2/2
✓ Branch 2 → 3 taken 792095 times.
✓ Branch 2 → 4 taken 5853 times.
797948 if (!irGenerator->cliOptions.instrumentation.generateDebugInfo)
262 792095 return;
263
264
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 5853 times.
5853 assert(!lexicalBlocks.empty());
265 5853 llvm::DIScope *scope = lexicalBlocks.top();
266 5853 const llvm::DILocation *diCodeLoc = llvm::DILocation::get(scope->getContext(), codeLoc.line, codeLoc.col, scope);
267
2/4
✓ Branch 10 → 11 taken 5853 times.
✗ Branch 10 → 17 not taken.
✓ Branch 11 → 12 taken 5853 times.
✗ Branch 11 → 15 not taken.
5853 irGenerator->builder.SetCurrentDebugLocation(diCodeLoc);
268 }
269
270 711473 void DebugInfoGenerator::setSourceLocation(const ASTNode *node) { setSourceLocation(node->codeLoc); }
271
272 2163 void DebugInfoGenerator::finalize() const {
273
2/2
✓ Branch 2 → 3 taken 27 times.
✓ Branch 2 → 5 taken 2136 times.
2163 if (irGenerator->cliOptions.instrumentation.generateDebugInfo)
274 27 diBuilder->finalize();
275 2163 }
276
277 1742 llvm::DIType *DebugInfoGenerator::getDITypeForQualType(const ASTNode *node, const QualType &ty) { // NOLINT(*-no-recursion)
278 // Pointer type
279
2/2
✓ Branch 3 → 4 taken 267 times.
✓ Branch 3 → 13 taken 1475 times.
1742 if (ty.isPtr()) {
280
2/4
✓ Branch 4 → 5 taken 267 times.
✗ Branch 4 → 178 not taken.
✓ Branch 5 → 6 taken 267 times.
✗ Branch 5 → 178 not taken.
267 llvm::DIType *pointeeTy = getDITypeForQualType(node, ty.getContained());
281
1/2
✓ Branch 10 → 11 taken 267 times.
✗ Branch 10 → 179 not taken.
534 return diBuilder->createPointerType(pointeeTy, pointerWidth);
282 }
283
284 // Reference type
285
2/2
✓ Branch 14 → 15 taken 149 times.
✓ Branch 14 → 22 taken 1326 times.
1475 if (ty.isRef()) {
286
2/4
✓ Branch 15 → 16 taken 149 times.
✗ Branch 15 → 182 not taken.
✓ Branch 16 → 17 taken 149 times.
✗ Branch 16 → 182 not taken.
149 llvm::DIType *referencedType = getDITypeForQualType(node, ty.getContained());
287
1/2
✓ Branch 19 → 20 taken 149 times.
✗ Branch 19 → 183 not taken.
298 return diBuilder->createReferenceType(llvm::dwarf::DW_TAG_reference_type, referencedType, pointerWidth);
288 }
289
290 // Array type
291
2/2
✓ Branch 23 → 24 taken 2 times.
✓ Branch 23 → 38 taken 1324 times.
1326 if (ty.isArray()) {
292
2/4
✓ Branch 24 → 25 taken 2 times.
✗ Branch 24 → 184 not taken.
✓ Branch 25 → 26 taken 2 times.
✗ Branch 25 → 184 not taken.
2 llvm::DIType *itemTy = getDITypeForQualType(node, ty.getContained());
293
1/2
✓ Branch 26 → 27 taken 2 times.
✗ Branch 26 → 190 not taken.
2 const size_t size = ty.getArraySize();
294
1/2
✓ Branch 29 → 30 taken 2 times.
✗ Branch 29 → 185 not taken.
2 const llvm::DINodeArray subscripts = diBuilder->getOrCreateArray({});
295
5/10
✓ Branch 31 → 32 taken 2 times.
✗ Branch 31 → 189 not taken.
✓ Branch 32 → 33 taken 2 times.
✗ Branch 32 → 188 not taken.
✓ Branch 33 → 34 taken 2 times.
✗ Branch 33 → 187 not taken.
✓ Branch 34 → 35 taken 2 times.
✗ Branch 34 → 186 not taken.
✓ Branch 35 → 36 taken 2 times.
✗ Branch 35 → 186 not taken.
2 return diBuilder->createArrayType(size, 0, itemTy, subscripts);
296 }
297
298 // Primitive types
299 llvm::DIType *baseDiType;
300
9/12
✓ Branch 39 → 40 taken 1 time.
✓ Branch 39 → 41 taken 86 times.
✗ Branch 39 → 46 not taken.
✓ Branch 39 → 51 taken 291 times.
✓ Branch 39 → 56 taken 103 times.
✓ Branch 39 → 57 taken 131 times.
✓ Branch 39 → 58 taken 146 times.
✓ Branch 39 → 59 taken 120 times.
✓ Branch 39 → 60 taken 435 times.
✗ Branch 39 → 126 not taken.
✓ Branch 39 → 162 taken 11 times.
✗ Branch 39 → 163 not taken.
1324 switch (ty.getSuperType()) {
301 1 case TY_DOUBLE:
302 1 baseDiType = doubleTy;
303 1 break;
304 86 case TY_INT:
305
2/2
✓ Branch 42 → 43 taken 69 times.
✓ Branch 42 → 44 taken 17 times.
86 baseDiType = ty.isSigned() ? intTy : uIntTy;
306 86 break;
307 case TY_SHORT:
308 baseDiType = ty.isSigned() ? shortTy : uShortTy;
309 break;
310 291 case TY_LONG:
311
2/2
✓ Branch 52 → 53 taken 56 times.
✓ Branch 52 → 54 taken 235 times.
291 baseDiType = ty.isSigned() ? longTy : uLongTy;
312 291 break;
313 103 case TY_BYTE:
314 103 baseDiType = byteTy;
315 103 break;
316 131 case TY_CHAR:
317 131 baseDiType = charTy;
318 131 break;
319 146 case TY_STRING:
320 146 baseDiType = stringTy;
321 146 break;
322 120 case TY_BOOL:
323 120 baseDiType = boolTy;
324 120 break;
325 435 case TY_STRUCT: {
326 // Do cache lookup
327 435 const size_t hashKey = std::hash<QualType>{}(ty);
328
1/2
✓ Branch 61 → 62 taken 435 times.
✗ Branch 61 → 206 not taken.
435 const auto it = structTypeCache.find(hashKey);
329
2/2
✓ Branch 64 → 65 taken 392 times.
✓ Branch 64 → 67 taken 43 times.
435 if (it != structTypeCache.end())
330 392 return it->second;
331
332 // Cache miss, generate struct type
333
1/2
✓ Branch 67 → 68 taken 43 times.
✗ Branch 67 → 206 not taken.
43 const Struct *spiceStruct = ty.getStruct(node);
334
1/2
✗ Branch 68 → 69 not taken.
✓ Branch 68 → 70 taken 43 times.
43 assert(spiceStruct != nullptr);
335
336 // Retrieve information about the struct
337
1/2
✓ Branch 70 → 71 taken 43 times.
✗ Branch 70 → 206 not taken.
43 const uint32_t lineNo = spiceStruct->getDeclCodeLoc().line;
338
2/4
✓ Branch 71 → 72 taken 43 times.
✗ Branch 71 → 206 not taken.
✓ Branch 72 → 73 taken 43 times.
✗ Branch 72 → 206 not taken.
43 llvm::Type *structType = spiceStruct->entry->getQualType().toLLVMType(irGenerator->sourceFile);
339
1/2
✗ Branch 73 → 74 not taken.
✓ Branch 73 → 75 taken 43 times.
43 assert(structType != nullptr);
340 43 const llvm::DataLayout &dataLayout = irGenerator->module->getDataLayout();
341
2/4
✓ Branch 76 → 77 taken 43 times.
✗ Branch 76 → 206 not taken.
✓ Branch 77 → 78 taken 43 times.
✗ Branch 77 → 206 not taken.
43 const llvm::StructLayout *structLayout = dataLayout.getStructLayout(llvm::cast<llvm::StructType>(structType));
342
1/2
✓ Branch 78 → 79 taken 43 times.
✗ Branch 78 → 191 not taken.
43 const uint32_t alignInBits = dataLayout.getABITypeAlign(structType).value();
343
344 // Create struct ty
345
1/2
✓ Branch 80 → 81 taken 43 times.
✗ Branch 80 → 206 not taken.
43 const std::string mangledName = NameMangling::mangleStruct(*spiceStruct);
346
2/4
✓ Branch 84 → 85 taken 43 times.
✗ Branch 84 → 194 not taken.
✓ Branch 86 → 87 taken 43 times.
✗ Branch 86 → 193 not taken.
129 llvm::DICompositeType *structDiType = diBuilder->createStructType(
347
2/4
✓ Branch 85 → 86 taken 43 times.
✗ Branch 85 → 193 not taken.
✓ Branch 88 → 89 taken 43 times.
✗ Branch 88 → 192 not taken.
86 diFile, spiceStruct->name, diFile, lineNo, structLayout->getSizeInBits(), alignInBits,
348 43 llvm::DINode::FlagTypePassByReference | llvm::DINode::FlagNonTrivial, nullptr, {}, 0, nullptr, mangledName);
349 43 baseDiType = structDiType;
350
351 // Insert into cache
352
1/2
✓ Branch 89 → 90 taken 43 times.
✗ Branch 89 → 204 not taken.
43 structTypeCache.emplace(hashKey, structDiType);
353
354 // Collect DI types for fields
355 43 std::vector<llvm::Metadata *> fieldTypes;
356
3/4
✓ Branch 116 → 117 taken 144 times.
✗ Branch 116 → 202 not taken.
✓ Branch 117 → 91 taken 101 times.
✓ Branch 117 → 118 taken 43 times.
144 for (size_t i = 0; i < spiceStruct->scope->getFieldCount(); i++) {
357 // Get field entry
358
1/2
✗ Branch 91 → 92 not taken.
✓ Branch 91 → 93 taken 101 times.
101 const SymbolTableEntry *fieldEntry = spiceStruct->scope->lookupField(i);
359
3/6
✓ Branch 96 → 97 taken 101 times.
✗ Branch 96 → 100 not taken.
✓ Branch 97 → 98 taken 101 times.
✗ Branch 97 → 202 not taken.
✓ Branch 98 → 99 taken 101 times.
✗ Branch 98 → 100 not taken.
101 assert(fieldEntry != nullptr && fieldEntry->isField());
360
2/2
✓ Branch 101 → 102 taken 4 times.
✓ Branch 101 → 103 taken 97 times.
101 if (fieldEntry->isImplicitField)
361 4 continue;
362
363
1/2
✓ Branch 103 → 104 taken 97 times.
✗ Branch 103 → 202 not taken.
97 const QualType &fieldType = fieldEntry->getQualType();
364 97 const uint32_t fieldLineNo = fieldEntry->declNode->codeLoc.line;
365
2/4
✓ Branch 104 → 105 taken 97 times.
✗ Branch 104 → 196 not taken.
✓ Branch 105 → 106 taken 97 times.
✗ Branch 105 → 196 not taken.
97 const size_t offsetInBits = structLayout->getElementOffsetInBits(i);
366
367
1/2
✓ Branch 106 → 107 taken 97 times.
✗ Branch 106 → 202 not taken.
97 llvm::DIType *fieldDiType = getDITypeForQualType(node, fieldType);
368 llvm::DIDerivedType *fieldDiDerivedType =
369
3/6
✓ Branch 109 → 110 taken 97 times.
✗ Branch 109 → 198 not taken.
✓ Branch 110 → 111 taken 97 times.
✗ Branch 110 → 198 not taken.
✓ Branch 112 → 113 taken 97 times.
✗ Branch 112 → 197 not taken.
97 diBuilder->createMemberType(structDiType, fieldEntry->name, diFile, fieldLineNo, fieldDiType->getSizeInBits(),
370 fieldDiType->getAlignInBits(), offsetInBits, llvm::DINode::FlagZero, fieldDiType);
371
372
1/2
✓ Branch 113 → 114 taken 97 times.
✗ Branch 113 → 199 not taken.
97 fieldTypes.push_back(fieldDiDerivedType);
373 }
374
375
2/4
✓ Branch 119 → 120 taken 43 times.
✗ Branch 119 → 200 not taken.
✓ Branch 121 → 122 taken 43 times.
✗ Branch 121 → 200 not taken.
43 structDiType->replaceElements(llvm::MDTuple::get(irGenerator->context, fieldTypes));
376 43 break;
377 43 }
378 case TY_INTERFACE: {
379 // Do cache lookup
380 const size_t hashKey = std::hash<QualType>{}(ty);
381 const auto it = structTypeCache.find(hashKey);
382 if (it != structTypeCache.end())
383 return it->second;
384
385 // Cache miss, generate interface type
386 const Interface *spiceInterface = ty.getInterface(node);
387 assert(spiceInterface != nullptr);
388
389 // Retrieve information about the interface
390 const uint32_t lineNo = spiceInterface->getDeclCodeLoc().line;
391 llvm::Type *interfaceType = spiceInterface->entry->getQualType().toLLVMType(irGenerator->sourceFile);
392 assert(interfaceType != nullptr);
393 const llvm::DataLayout dataLayout = irGenerator->module->getDataLayout();
394 const llvm::StructLayout *structLayout = dataLayout.getStructLayout(llvm::cast<llvm::StructType>(interfaceType));
395 const uint32_t alignInBits = dataLayout.getABITypeAlign(interfaceType).value();
396
397 // Create interface ty
398 const std::string mangledName = NameMangling::mangleInterface(*spiceInterface);
399 llvm::DICompositeType *interfaceDiType = diBuilder->createStructType(
400 diFile, spiceInterface->name, diFile, lineNo, structLayout->getSizeInBits(), alignInBits,
401 llvm::DINode::FlagTypePassByReference | llvm::DINode::FlagNonTrivial, nullptr, {}, 0, nullptr, mangledName);
402
403 // Set vtable holder to itself for interfaces
404 interfaceDiType->replaceVTableHolder(interfaceDiType);
405 baseDiType = interfaceDiType;
406
407 // Insert into cache
408 structTypeCache.emplace(hashKey, interfaceDiType);
409
410 break;
411 }
412 11 case TY_FUNCTION: // fall-through
413 case TY_PROCEDURE:
414 11 baseDiType = fatPtrTy;
415 11 break;
416 default:
417 throw CompilerError(UNHANDLED_BRANCH, "Debug Info Type fallthrough"); // GCOV_EXCL_LINE
418 }
419
420
2/2
✓ Branch 172 → 173 taken 96 times.
✓ Branch 172 → 176 taken 836 times.
932 if (ty.isConst())
421 96 baseDiType = diBuilder->createQualifiedType(llvm::dwarf::DW_TAG_const_type, baseDiType);
422
423 932 return baseDiType;
424 }
425
426 } // namespace spice::compiler
427