GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 76.8% 205 / 1 / 268
Functions: 92.9% 13 / 0 / 14
Branches: 44.3% 219 / 4 / 498

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