GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 76.6% 210 / 1 / 275
Functions: 92.3% 12 / 0 / 13
Branches: 44.5% 226 / 4 / 512

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