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.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 <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 2176 times.
✗ Branch 3 → 6 not taken.
2176 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 → 131 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 → 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);
29 27 absolutePath.make_preferred();
30 27 sourceFileDir.make_preferred();
31
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());
32
1/2
✓ Branch 29 → 30 taken 27 times.
✗ Branch 29 → 148 not taken.
54 compileUnit = diBuilder->createCompileUnit(
33
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, "",
34 llvm::DICompileUnit::FullDebug, 0, false, false, llvm::DICompileUnit::DebugNameTableKind::None);
35
36
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);
37
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);
38
39 // Create another DIFile as scope for subprograms
40
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());
41
42
1/2
✓ Branch 43 → 44 taken 27 times.
✗ Branch 43 → 200 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 → 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);
46
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);
47
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);
48
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);
49
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);
50
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);
51
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);
52
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);
53
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);
54
1/2
✓ Branch 75 → 76 taken 27 times.
✗ Branch 75 → 172 not taken.
54 stringTy = diBuilder->createPointerType(charTy, pointerWidth);
55
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);
56
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);
57
58 // Initialize lambda fat ptr type
59
1/2
✓ Branch 82 → 83 taken 27 times.
✗ Branch 82 → 200 not taken.
27 llvm::PointerType *ptrTy = irGenerator->builder.getPtrTy();
60
1/2
✓ Branch 83 → 84 taken 27 times.
✗ Branch 83 → 200 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 → 200 not taken.
27 const llvm::StructLayout *structLayout = dataLayout.getStructLayout(irGenerator->llvmTypes.lambdaFatPtrType);
63
1/2
✓ Branch 86 → 87 taken 27 times.
✗ Branch 86 → 177 not taken.
27 const uint32_t alignInBits = dataLayout.getABITypeAlign(irGenerator->llvmTypes.lambdaFatPtrType).value();
64
1/2
✓ Branch 88 → 89 taken 27 times.
✗ Branch 88 → 178 not taken.
27 const uint32_t ptrAlignInBits = dataLayout.getABITypeAlign(ptrTy).value();
65
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);
66
1/2
✓ Branch 92 → 93 taken 27 times.
✗ Branch 92 → 180 not taken.
27 const uint32_t int64AlignInBits = dataLayout.getABITypeAlign(int64Ty).value();
67
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);
68
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);
69
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);
70
71
1/2
✓ Branch 104 → 105 taken 27 times.
✗ Branch 104 → 184 not taken.
54 llvm::DIType *voidPtrDIType = diBuilder->createPointerType(voidTy, pointerWidth, ptrAlignInBits);
72
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,
73 llvm::DINode::FlagTypePassByValue | llvm::DINode::FlagNonTrivial, nullptr, {}, 0,
74
1/2
✓ Branch 106 → 107 taken 27 times.
✗ Branch 106 → 190 not taken.
27 nullptr, "_lambda");
75
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,
76 llvm::DINode::FlagZero, voidPtrDIType);
77
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,
78 capturesOffset, llvm::DINode::FlagZero, voidPtrDIType);
79
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,
80 captureSizeOffset, llvm::DINode::FlagZero, uLongTy);
81
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}));
82 27 }
83
84 34469 void DebugInfoGenerator::generateFunctionDebugInfo(llvm::Function *llvmFunction, const Function *spiceFunc, bool isLambda) {
85
2/2
✓ Branch 2 → 3 taken 34242 times.
✓ Branch 2 → 4 taken 227 times.
34469 if (!irGenerator->cliOptions.instrumentation.generateDebugInfo)
86 34242 return;
87
88 227 const ASTNode *node = spiceFunc->declNode;
89
1/2
✓ Branch 4 → 5 taken 227 times.
✗ Branch 4 → 125 not taken.
227 const uint32_t lineNo = spiceFunc->getDeclCodeLoc().line;
90
91 // Prepare flags
92 227 llvm::DIScope *scope = diFile;
93 227 llvm::DINode::DIFlags flags = llvm::DINode::FlagPrototyped;
94
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())
95
1/2
✓ Branch 12 → 13 taken 212 times.
✗ Branch 12 → 125 not taken.
212 flags |= llvm::DINode::FlagPublic;
96
97 // Prepare spFlags
98 227 llvm::DISubprogram::DISPFlags spFlags = llvm::DISubprogram::SPFlagDefinition;
99
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 227 times.
227 if (isLambda)
100 spFlags |= llvm::DISubprogram::SPFlagLocalToUnit;
101
2/2
✓ Branch 15 → 16 taken 5 times.
✓ Branch 15 → 20 taken 222 times.
227 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 227 times.
✗ Branch 20 → 123 not taken.
227 std::vector<llvm::Metadata *> argTypes;
110
2/2
✓ Branch 23 → 24 taken 83 times.
✓ Branch 23 → 26 taken 144 times.
227 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 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
114
2/2
✓ Branch 32 → 33 taken 134 times.
✓ Branch 32 → 36 taken 93 times.
227 if (spiceFunc->isMethod())
115
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
116
1/2
✗ Branch 36 → 37 not taken.
✓ Branch 36 → 45 taken 227 times.
227 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 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
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.
471 argTypes.push_back(getDITypeForQualType(node, argType));
124
125 // Create function type
126
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));
127
128
1/2
✓ Branch 68 → 69 taken 227 times.
✗ Branch 68 → 123 not taken.
227 const std::string mangledName = spiceFunc->getMangledName();
129 llvm::DISubprogram *subprogram;
130
1/2
✓ Branch 69 → 70 taken 227 times.
✗ Branch 69 → 121 not taken.
227 const std::string &name = spiceFunc->name;
131
2/2
✓ Branch 72 → 73 taken 134 times.
✓ Branch 72 → 80 taken 93 times.
227 if (spiceFunc->isMethod()) {
132
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);
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 227 times.
✗ Branch 90 → 119 not taken.
227 subprogram->replaceRetainedNodes({});
137
138 // Add debug info to LLVM function
139
1/2
✓ Branch 91 → 92 taken 227 times.
✗ Branch 91 → 121 not taken.
227 llvmFunction->setSubprogram(subprogram);
140 // Add scope to lexicalBlocks
141
1/2
✓ Branch 92 → 93 taken 227 times.
✗ Branch 92 → 120 not taken.
227 lexicalBlocks.push(subprogram);
142 227 }
143
144 34826 void DebugInfoGenerator::concludeFunctionDebugInfo() {
145
2/2
✓ Branch 2 → 3 taken 34599 times.
✓ Branch 2 → 4 taken 227 times.
34826 if (!irGenerator->cliOptions.instrumentation.generateDebugInfo)
146 34599 return;
147
148
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 227 times.
227 assert(!lexicalBlocks.empty());
149 227 lexicalBlocks.pop();
150 }
151
152 23528 void DebugInfoGenerator::pushLexicalBlock(const ASTNode *node) {
153
2/2
✓ Branch 2 → 3 taken 23330 times.
✓ Branch 2 → 4 taken 198 times.
23528 if (!irGenerator->cliOptions.instrumentation.generateDebugInfo)
154 23330 return;
155
156 198 const uint32_t line = node->codeLoc.line;
157 198 const uint32_t col = node->codeLoc.col;
158 198 llvm::DILexicalBlock *lexicalBlock = diBuilder->createLexicalBlock(lexicalBlocks.top(), diFile, line, col);
159
1/2
✓ Branch 7 → 8 taken 198 times.
✗ Branch 7 → 10 not taken.
198 lexicalBlocks.push(lexicalBlock);
160 }
161
162 23528 void DebugInfoGenerator::popLexicalBlock() {
163
2/2
✓ Branch 2 → 3 taken 23330 times.
✓ Branch 2 → 4 taken 198 times.
23528 if (!irGenerator->cliOptions.instrumentation.generateDebugInfo)
164 23330 return;
165
166
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 198 times.
198 assert(!lexicalBlocks.empty());
167 198 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 2348 void DebugInfoGenerator::generateGlobalVarDebugInfo(llvm::GlobalVariable *global, const SymbolTableEntry *globalEntry) {
216
2/2
✓ Branch 2 → 3 taken 2341 times.
✓ Branch 2 → 4 taken 7 times.
2348 if (!irGenerator->cliOptions.instrumentation.generateDebugInfo)
217 2341 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 52 void DebugInfoGenerator::generateGlobalStringDebugInfo(llvm::GlobalVariable *global, const std::string &name, size_t length,
228 const CodeLoc &codeLoc) const {
229 52 const uint32_t lineNo = codeLoc.line;
230 52 const size_t sizeInBits = (length + 1) * 8; // +1 because of null-terminator
231
232
1/2
✓ Branch 4 → 5 taken 52 times.
✗ Branch 4 → 12 not taken.
52 llvm::DIStringType *stringType = diBuilder->createStringType(name, sizeInBits);
233
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));
234 52 }
235
236 106352 void DebugInfoGenerator::generateLocalVarDebugInfo(const std::string &varName, llvm::Value *address, size_t argNumber) {
237
2/2
✓ Branch 2 → 3 taken 105652 times.
✓ Branch 2 → 4 taken 700 times.
106352 if (!irGenerator->cliOptions.instrumentation.generateDebugInfo)
238 105652 return;
239
240 // Get symbol table entry
241 700 const SymbolTableEntry *variableEntry = irGenerator->currentScope->lookupStrict(varName);
242
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 700 times.
700 assert(variableEntry != nullptr);
243 // Build debug info
244 700 llvm::DIScope *scope = lexicalBlocks.top();
245 700 llvm::DIType *diType = getDITypeForQualType(variableEntry->declNode, variableEntry->getQualType());
246 700 const uint32_t lineNo = variableEntry->declNode->codeLoc.line;
247
248 llvm::DILocalVariable *varInfo;
249
2/2
✓ Branch 12 → 13 taken 378 times.
✓ Branch 12 → 18 taken 322 times.
700 if (argNumber != SIZE_MAX)
250
1/2
✓ Branch 16 → 17 taken 378 times.
✗ Branch 16 → 36 not taken.
378 varInfo = diBuilder->createParameterVariable(scope, varName, argNumber, diFile, lineNo, diType);
251 else
252
1/2
✓ Branch 20 → 21 taken 322 times.
✗ Branch 20 → 38 not taken.
322 varInfo = diBuilder->createAutoVariable(scope, varName, diFile, lineNo, diType);
253
1/2
✓ Branch 24 → 25 taken 700 times.
✗ Branch 24 → 39 not taken.
700 llvm::DIExpression *expr = diBuilder->createExpression();
254
2/4
✓ Branch 25 → 26 taken 700 times.
✗ Branch 25 → 42 not taken.
✓ Branch 26 → 27 taken 700 times.
✗ Branch 26 → 40 not taken.
700 const llvm::DILocation *debugLocation = irGenerator->builder.getCurrentDebugLocation();
255
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 700 times.
700 assert(debugLocation != nullptr);
256
1/2
✓ Branch 33 → 34 taken 700 times.
✗ Branch 33 → 43 not taken.
700 diBuilder->insertDeclare(address, varInfo, expr, debugLocation, irGenerator->builder.GetInsertPoint());
257 }
258
259 684634 void DebugInfoGenerator::setSourceLocation(const CodeLoc &codeLoc) {
260
2/2
✓ Branch 2 → 3 taken 679606 times.
✓ Branch 2 → 4 taken 5028 times.
684634 if (!irGenerator->cliOptions.instrumentation.generateDebugInfo)
261 679606 return;
262
263
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 5028 times.
5028 assert(!lexicalBlocks.empty());
264 5028 llvm::DIScope *scope = lexicalBlocks.top();
265 5028 const llvm::DILocation *diCodeLoc = llvm::DILocation::get(scope->getContext(), codeLoc.line, codeLoc.col, scope);
266
2/4
✓ Branch 10 → 11 taken 5028 times.
✗ Branch 10 → 17 not taken.
✓ Branch 11 → 12 taken 5028 times.
✗ Branch 11 → 15 not taken.
5028 irGenerator->builder.SetCurrentDebugLocation(diCodeLoc);
267 }
268
269 2176 void DebugInfoGenerator::finalize() const {
270
2/2
✓ Branch 2 → 3 taken 27 times.
✓ Branch 2 → 5 taken 2149 times.
2176 if (irGenerator->cliOptions.instrumentation.generateDebugInfo)
271 27 diBuilder->finalize();
272 2176 }
273
274 1746 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 1479 times.
1746 if (ty.isPtr()) {
277
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());
278
1/2
✓ Branch 10 → 11 taken 267 times.
✗ Branch 10 → 179 not taken.
534 return diBuilder->createPointerType(pointeeTy, pointerWidth);
279 }
280
281 // Reference type
282
2/2
✓ Branch 14 → 15 taken 151 times.
✓ Branch 14 → 22 taken 1328 times.
1479 if (ty.isRef()) {
283
2/4
✓ Branch 15 → 16 taken 151 times.
✗ Branch 15 → 182 not taken.
✓ Branch 16 → 17 taken 151 times.
✗ Branch 16 → 182 not taken.
151 llvm::DIType *referencedType = getDITypeForQualType(node, ty.getContained());
284
1/2
✓ Branch 19 → 20 taken 151 times.
✗ Branch 19 → 183 not taken.
302 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 1326 times.
1328 if (ty.isArray()) {
289
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());
290
1/2
✓ Branch 26 → 27 taken 2 times.
✗ Branch 26 → 190 not taken.
2 const size_t size = ty.getArraySize();
291
1/2
✓ Branch 29 → 30 taken 2 times.
✗ Branch 29 → 185 not taken.
2 const llvm::DINodeArray subscripts = diBuilder->getOrCreateArray({});
292
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);
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 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.
1326 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 291 case TY_LONG:
308
2/2
✓ Branch 52 → 53 taken 56 times.
✓ Branch 52 → 54 taken 235 times.
291 baseDiType = ty.isSigned() ? longTy : uLongTy;
309 291 break;
310 103 case TY_BYTE:
311 103 baseDiType = byteTy;
312 103 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 120 case TY_BOOL:
320 120 baseDiType = boolTy;
321 120 break;
322 435 case TY_STRUCT: {
323 // Do cache lookup
324 435 const size_t hashKey = std::hash<QualType>{}(ty);
325
1/2
✓ Branch 61 → 62 taken 435 times.
✗ Branch 61 → 206 not taken.
435 const auto it = structTypeCache.find(hashKey);
326
2/2
✓ Branch 64 → 65 taken 392 times.
✓ Branch 64 → 67 taken 43 times.
435 if (it != structTypeCache.end())
327 392 return it->second;
328
329 // Cache miss, generate struct type
330
1/2
✓ Branch 67 → 68 taken 43 times.
✗ Branch 67 → 206 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 → 206 not taken.
43 const uint32_t lineNo = spiceStruct->getDeclCodeLoc().line;
335
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);
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 → 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));
339
1/2
✓ Branch 78 → 79 taken 43 times.
✗ Branch 78 → 191 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 → 206 not taken.
43 const std::string mangledName = NameMangling::mangleStruct(*spiceStruct);
343
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(
344
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,
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 89 → 90 taken 43 times.
✗ Branch 89 → 204 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 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++) {
354 // Get field entry
355
1/2
✗ Branch 91 → 92 not taken.
✓ Branch 91 → 93 taken 101 times.
101 const SymbolTableEntry *fieldEntry = spiceStruct->scope->lookupField(i);
356
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());
357
2/2
✓ Branch 101 → 102 taken 4 times.
✓ Branch 101 → 103 taken 97 times.
101 if (fieldEntry->isImplicitField)
358 4 continue;
359
360
1/2
✓ Branch 103 → 104 taken 97 times.
✗ Branch 103 → 202 not taken.
97 const QualType &fieldType = fieldEntry->getQualType();
361 97 const uint32_t fieldLineNo = fieldEntry->declNode->codeLoc.line;
362
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);
363
364
1/2
✓ Branch 106 → 107 taken 97 times.
✗ Branch 106 → 202 not taken.
97 llvm::DIType *fieldDiType = getDITypeForQualType(node, fieldType);
365 llvm::DIDerivedType *fieldDiDerivedType =
366
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(),
367 fieldDiType->getAlignInBits(), offsetInBits, llvm::DINode::FlagZero, fieldDiType);
368
369
1/2
✓ Branch 113 → 114 taken 97 times.
✗ Branch 113 → 199 not taken.
97 fieldTypes.push_back(fieldDiDerivedType);
370 }
371
372
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));
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 172 → 173 taken 96 times.
✓ Branch 172 → 176 taken 838 times.
934 if (ty.isConst())
418 96 baseDiType = diBuilder->createQualifiedType(llvm::dwarf::DW_TAG_const_type, baseDiType);
419
420 934 return baseDiType;
421 }
422
423 } // namespace spice::compiler
424