GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 85.0% 442 / 3 / 523
Functions: 93.0% 40 / 0 / 43
Branches: 46.0% 455 / 12 / 1002

src/SourceFile.cpp
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #include "SourceFile.h"
4
5 #include <ast/ASTBuilder.h>
6 #include <driver/Driver.h>
7 #include <exception/AntlrThrowingErrorListener.h>
8 #include <exception/CompilerError.h>
9 #include <global/GlobalResourceManager.h>
10 #include <global/TypeRegistry.h>
11 #include <importcollector/ImportCollector.h>
12 #include <irgenerator/IRGenerator.h>
13 #include <iroptimizer/IROptimizer.h>
14 #include <linker/BitcodeLinker.h>
15 #include <objectemitter/ObjectEmitter.h>
16 #include <symboltablebuilder/SymbolTable.h>
17 #include <symboltablebuilder/SymbolTableBuilder.h>
18 #include <typechecker/FunctionManager.h>
19 #include <typechecker/InterfaceManager.h>
20 #include <typechecker/MacroDefs.h>
21 #include <typechecker/StructManager.h>
22 #include <typechecker/TypeChecker.h>
23 #include <util/CompilerWarning.h>
24 #include <util/FileUtil.h>
25 #include <util/SystemUtil.h>
26 #include <util/Timer.h>
27 #include <visualizer/ASTVisualizer.h>
28 #include <visualizer/CSTVisualizer.h>
29 #include <visualizer/DependencyGraphVisualizer.h>
30
31 #include <llvm/IR/Module.h>
32 #include <llvm/MC/TargetRegistry.h>
33
34 namespace spice::compiler {
35
36 1256 SourceFile::SourceFile(GlobalResourceManager &resourceManager, SourceFile *parent, std::string name,
37 1256 const std::filesystem::path &filePath, bool stdFile)
38
1/2
✓ Branch 5 → 6 taken 1256 times.
✗ Branch 5 → 118 not taken.
2512 : name(std::move(name)), filePath(filePath), isStdFile(stdFile), parent(parent),
39
3/4
✓ Branch 13 → 14 taken 2 times.
✓ Branch 13 → 15 taken 1254 times.
✓ Branch 16 → 17 taken 1256 times.
✗ Branch 16 → 58 not taken.
1256 builder(resourceManager.cliOptions.useLTO ? resourceManager.ltoContext : context), resourceManager(resourceManager),
40
1/2
✓ Branch 11 → 12 taken 1256 times.
✗ Branch 11 → 106 not taken.
3768 cliOptions(resourceManager.cliOptions) {
41 // Deduce fileName and fileDir
42
3/6
✓ Branch 24 → 25 taken 1256 times.
✗ Branch 24 → 63 not taken.
✓ Branch 25 → 26 taken 1256 times.
✗ Branch 25 → 61 not taken.
✓ Branch 26 → 27 taken 1256 times.
✗ Branch 26 → 59 not taken.
1256 fileName = std::filesystem::path(filePath).filename().string();
43
3/6
✓ Branch 31 → 32 taken 1256 times.
✗ Branch 31 → 70 not taken.
✓ Branch 32 → 33 taken 1256 times.
✗ Branch 32 → 68 not taken.
✓ Branch 33 → 34 taken 1256 times.
✗ Branch 33 → 66 not taken.
1256 fileDir = std::filesystem::path(filePath).parent_path().string();
44
45 // Search after the selected target
46 1256 std::string error;
47
1/2
✓ Branch 39 → 40 taken 1256 times.
✗ Branch 39 → 85 not taken.
1256 const llvm::Target *target = llvm::TargetRegistry::lookupTarget(cliOptions.targetTriple, error);
48
1/2
✗ Branch 40 → 41 not taken.
✓ Branch 40 → 46 taken 1256 times.
1256 if (!target)
49 throw CompilerError(TARGET_NOT_AVAILABLE, "Selected target was not found: " + error); // GCOV_EXCL_LINE
50
51 // Create the target machine
52
1/2
✓ Branch 46 → 47 taken 1256 times.
✗ Branch 46 → 85 not taken.
1256 llvm::TargetOptions opt;
53 1256 opt.MCOptions.AsmVerbose = true;
54 1256 opt.MCOptions.PreserveAsmComments = true;
55 1256 const std::string &cpuName = resourceManager.cpuName;
56 1256 const std::string &features = resourceManager.cpuFeatures;
57 1256 const llvm::Triple &targetTriple = cliOptions.targetTriple;
58 1256 constexpr llvm::Reloc::Model relocModel = llvm::Reloc::PIC_;
59
1/2
✓ Branch 51 → 52 taken 1256 times.
✗ Branch 51 → 79 not taken.
1256 llvm::TargetMachine *targetMachineRaw = target->createTargetMachine(targetTriple, cpuName, features, opt, relocModel);
60 1256 targetMachine = std::unique_ptr<llvm::TargetMachine>(targetMachineRaw);
61 1256 }
62
63 1465 void SourceFile::runLexer() {
64
2/2
✓ Branch 2 → 3 taken 458 times.
✓ Branch 2 → 4 taken 1007 times.
1465 if (isMainFile)
65
1/2
✓ Branch 3 → 4 taken 458 times.
✗ Branch 3 → 89 not taken.
458 resourceManager.totalTimer.start();
66
67 // Check if this stage has already been done
68
2/2
✓ Branch 4 → 5 taken 211 times.
✓ Branch 4 → 6 taken 1254 times.
1465 if (previousStage >= LEXER)
69 211 return;
70
71
1/2
✓ Branch 6 → 7 taken 1254 times.
✗ Branch 6 → 89 not taken.
1254 Timer timer(&compilerOutput.times.lexer);
72
1/2
✓ Branch 7 → 8 taken 1254 times.
✗ Branch 7 → 89 not taken.
1254 timer.start();
73
74 // Read from the input source file
75
1/2
✓ Branch 8 → 9 taken 1254 times.
✗ Branch 8 → 89 not taken.
1254 std::ifstream fileInputStream(filePath);
76
3/4
✓ Branch 9 → 10 taken 1254 times.
✗ Branch 9 → 87 not taken.
✓ Branch 10 → 11 taken 1 time.
✓ Branch 10 → 20 taken 1253 times.
1254 if (!fileInputStream)
77
4/8
✓ Branch 12 → 13 taken 1 time.
✗ Branch 12 → 65 not taken.
✓ Branch 13 → 14 taken 1 time.
✗ Branch 13 → 63 not taken.
✓ Branch 14 → 15 taken 1 time.
✗ Branch 14 → 61 not taken.
✓ Branch 15 → 16 taken 1 time.
✗ Branch 15 → 58 not taken.
1 throw CompilerError(SOURCE_FILE_NOT_FOUND, "Source file at path '" + filePath.string() + "' does not exist.");
78
79 // Tokenize input
80
1/2
✓ Branch 20 → 21 taken 1253 times.
✗ Branch 20 → 70 not taken.
1253 antlrCtx.inputStream = std::make_unique<antlr4::ANTLRInputStream>(fileInputStream);
81
1/2
✓ Branch 24 → 25 taken 1253 times.
✗ Branch 24 → 71 not taken.
1253 antlrCtx.lexer = std::make_unique<SpiceLexer>(antlrCtx.inputStream.get());
82
1/2
✓ Branch 28 → 29 taken 1253 times.
✗ Branch 28 → 87 not taken.
1253 antlrCtx.lexer->removeErrorListeners();
83
1/2
✓ Branch 29 → 30 taken 1253 times.
✗ Branch 29 → 73 not taken.
1253 antlrCtx.lexerErrorHandler = std::make_unique<AntlrThrowingErrorListener>(ThrowingErrorListenerMode::LEXER, this);
84
1/2
✓ Branch 34 → 35 taken 1253 times.
✗ Branch 34 → 87 not taken.
1253 antlrCtx.lexer->addErrorListener(antlrCtx.lexerErrorHandler.get());
85
1/2
✓ Branch 36 → 37 taken 1253 times.
✗ Branch 36 → 76 not taken.
1253 antlrCtx.tokenStream = std::make_unique<antlr4::CommonTokenStream>(antlrCtx.lexer.get());
86
87 // Calculate cache key
88
1/2
✓ Branch 39 → 40 taken 1253 times.
✗ Branch 39 → 87 not taken.
1253 std::stringstream cacheKeyString;
89
4/6
✓ Branch 40 → 41 taken 1253 times.
✗ Branch 40 → 85 not taken.
✓ Branch 42 → 43 taken 1252 times.
✓ Branch 42 → 80 taken 1 time.
✓ Branch 44 → 45 taken 1252 times.
✗ Branch 44 → 78 not taken.
1253 cacheKeyString << std::hex << std::hash<std::string>{}(antlrCtx.tokenStream->getText());
90
1/2
✓ Branch 46 → 47 taken 1252 times.
✗ Branch 46 → 82 not taken.
1252 cacheKey = cacheKeyString.str();
91
92 // Try to load from cache
93
1/2
✗ Branch 49 → 50 not taken.
✓ Branch 49 → 52 taken 1252 times.
1252 if (!cliOptions.ignoreCache)
94 restoredFromCache = resourceManager.cacheManager.lookupSourceFile(this);
95
96 1252 previousStage = LEXER;
97
1/2
✓ Branch 52 → 53 taken 1252 times.
✗ Branch 52 → 85 not taken.
1252 timer.stop();
98
1/2
✓ Branch 53 → 54 taken 1252 times.
✗ Branch 53 → 83 not taken.
1252 printStatusMessage("Lexer", IO_CODE, IO_TOKENS, compilerOutput.times.lexer);
99 1255 }
100
101 1463 void SourceFile::runParser() {
102 // Skip if restored from the cache or this stage has already been done
103
3/4
✓ Branch 2 → 3 taken 1463 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 211 times.
✓ Branch 3 → 5 taken 1252 times.
1463 if (restoredFromCache || previousStage >= PARSER)
104 211 return;
105
106
1/2
✓ Branch 5 → 6 taken 1252 times.
✗ Branch 5 → 32 not taken.
1252 Timer timer(&compilerOutput.times.parser);
107
1/2
✓ Branch 6 → 7 taken 1252 times.
✗ Branch 6 → 32 not taken.
1252 timer.start();
108
109 // Parse input
110
1/2
✓ Branch 8 → 9 taken 1252 times.
✗ Branch 8 → 25 not taken.
1252 antlrCtx.parser = std::make_unique<SpiceParser>(antlrCtx.tokenStream.get()); // Check for syntax errors
111
1/2
✓ Branch 12 → 13 taken 1252 times.
✗ Branch 12 → 32 not taken.
1252 antlrCtx.parser->removeErrorListeners();
112
1/2
✓ Branch 13 → 14 taken 1252 times.
✗ Branch 13 → 27 not taken.
1252 antlrCtx.parserErrorHandler = std::make_unique<AntlrThrowingErrorListener>(ThrowingErrorListenerMode::PARSER, this);
113
1/2
✓ Branch 18 → 19 taken 1252 times.
✗ Branch 18 → 32 not taken.
1252 antlrCtx.parser->addErrorListener(antlrCtx.parserErrorHandler.get());
114
1/2
✓ Branch 20 → 21 taken 1252 times.
✗ Branch 20 → 32 not taken.
1252 antlrCtx.parser->removeParseListeners();
115
116 1252 previousStage = PARSER;
117
1/2
✓ Branch 21 → 22 taken 1252 times.
✗ Branch 21 → 32 not taken.
1252 timer.stop();
118
1/2
✓ Branch 22 → 23 taken 1252 times.
✗ Branch 22 → 30 not taken.
1252 printStatusMessage("Parser", IO_TOKENS, IO_CST, compilerOutput.times.parser);
119 }
120
121 1015 void SourceFile::runCSTVisualizer() {
122 // Only execute if enabled
123
3/6
✓ Branch 2 → 3 taken 1015 times.
✗ Branch 2 → 5 not taken.
✓ Branch 3 → 4 taken 1015 times.
✗ Branch 3 → 6 not taken.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 1015 times.
1015 if (restoredFromCache || (!cliOptions.dump.dumpCST && !cliOptions.testMode))
124 211 return;
125 // Check if this stage has already been done
126
2/2
✓ Branch 6 → 7 taken 211 times.
✓ Branch 6 → 8 taken 804 times.
1015 if (previousStage >= CST_VISUALIZER)
127 211 return;
128
129
1/2
✓ Branch 8 → 9 taken 804 times.
✗ Branch 8 → 66 not taken.
804 Timer timer(&compilerOutput.times.cstVisualizer);
130
1/2
✓ Branch 9 → 10 taken 804 times.
✗ Branch 9 → 66 not taken.
804 timer.start();
131
132 // Generate dot code for this source file
133
1/2
✓ Branch 10 → 11 taken 804 times.
✗ Branch 10 → 66 not taken.
804 std::stringstream dotCode;
134
1/2
✓ Branch 11 → 12 taken 804 times.
✗ Branch 11 → 64 not taken.
804 visualizerPreamble(dotCode);
135
1/2
✓ Branch 14 → 15 taken 804 times.
✗ Branch 14 → 64 not taken.
804 CSTVisualizer cstVisualizer(resourceManager, this, antlrCtx.lexer.get(), antlrCtx.parser.get());
136
6/12
✓ Branch 15 → 16 taken 804 times.
✗ Branch 15 → 62 not taken.
✓ Branch 17 → 18 taken 804 times.
✗ Branch 17 → 51 not taken.
✓ Branch 18 → 19 taken 804 times.
✗ Branch 18 → 51 not taken.
✓ Branch 19 → 20 taken 804 times.
✗ Branch 19 → 49 not taken.
✓ Branch 20 → 21 taken 804 times.
✗ Branch 20 → 47 not taken.
✓ Branch 21 → 22 taken 804 times.
✗ Branch 21 → 47 not taken.
804 dotCode << " " << std::any_cast<std::string>(cstVisualizer.visit(antlrCtx.parser->entry())) << "}";
137
1/2
✓ Branch 25 → 26 taken 804 times.
✗ Branch 25 → 62 not taken.
804 antlrCtx.parser->reset();
138
139 // Dump the serialized CST string and the SVG file
140
2/4
✓ Branch 26 → 27 taken 804 times.
✗ Branch 26 → 28 not taken.
✓ Branch 27 → 28 taken 804 times.
✗ Branch 27 → 32 not taken.
804 if (cliOptions.dump.dumpCST || cliOptions.testMode)
141
1/2
✓ Branch 28 → 29 taken 804 times.
✗ Branch 28 → 53 not taken.
804 compilerOutput.cstString = dotCode.str();
142
143
1/2
✗ Branch 32 → 33 not taken.
✓ Branch 32 → 40 taken 804 times.
804 if (cliOptions.dump.dumpCST)
144 visualizerOutput("CST", compilerOutput.cstString);
145
146 804 previousStage = CST_VISUALIZER;
147
1/2
✓ Branch 40 → 41 taken 804 times.
✗ Branch 40 → 62 not taken.
804 timer.stop();
148
1/2
✓ Branch 41 → 42 taken 804 times.
✗ Branch 41 → 60 not taken.
804 printStatusMessage("CST Visualizer", IO_CST, IO_CST, compilerOutput.times.cstVisualizer);
149 804 }
150
151 1463 void SourceFile::runASTBuilder() {
152 // Skip if restored from the cache or this stage has already been done
153
3/4
✓ Branch 2 → 3 taken 1463 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 211 times.
✓ Branch 3 → 5 taken 1252 times.
1463 if (restoredFromCache || previousStage >= AST_BUILDER)
154 211 return;
155
156
1/2
✓ Branch 5 → 6 taken 1252 times.
✗ Branch 5 → 36 not taken.
1252 Timer timer(&compilerOutput.times.astBuilder);
157
1/2
✓ Branch 6 → 7 taken 1252 times.
✗ Branch 6 → 36 not taken.
1252 timer.start();
158
159 // Build AST for this source file
160
1/2
✓ Branch 8 → 9 taken 1252 times.
✗ Branch 8 → 36 not taken.
1252 ASTBuilder astBuilder(resourceManager, this, antlrCtx.inputStream.get());
161
5/6
✓ Branch 10 → 11 taken 1250 times.
✓ Branch 10 → 26 taken 2 times.
✓ Branch 11 → 12 taken 1244 times.
✓ Branch 11 → 26 taken 6 times.
✓ Branch 12 → 13 taken 1244 times.
✗ Branch 12 → 24 not taken.
1252 ast = std::any_cast<EntryNode *>(astBuilder.visit(antlrCtx.parser->entry()));
162
1/2
✓ Branch 15 → 16 taken 1244 times.
✗ Branch 15 → 34 not taken.
1244 antlrCtx.parser->reset();
163
164 // Create global scope
165
1/2
✓ Branch 16 → 17 taken 1244 times.
✗ Branch 16 → 27 not taken.
1244 globalScope = std::make_unique<Scope>(nullptr, this, ScopeType::GLOBAL, &ast->codeLoc);
166
167 1244 previousStage = AST_BUILDER;
168
1/2
✓ Branch 19 → 20 taken 1244 times.
✗ Branch 19 → 34 not taken.
1244 timer.stop();
169
1/2
✓ Branch 20 → 21 taken 1244 times.
✗ Branch 20 → 32 not taken.
1244 printStatusMessage("AST Builder", IO_CST, IO_AST, compilerOutput.times.astBuilder);
170 1252 }
171
172 1015 void SourceFile::runASTVisualizer() {
173 // Only execute if enabled
174
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 1015 times.
1015 if (restoredFromCache)
175 211 return;
176
2/4
✓ Branch 4 → 5 taken 1015 times.
✗ Branch 4 → 7 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 1015 times.
1015 if (!cliOptions.dump.dumpAST && !cliOptions.testMode)
177 return;
178 // Check if this stage has already been done
179
2/2
✓ Branch 7 → 8 taken 211 times.
✓ Branch 7 → 9 taken 804 times.
1015 if (previousStage >= AST_VISUALIZER)
180 211 return;
181
182
1/2
✓ Branch 9 → 10 taken 804 times.
✗ Branch 9 → 58 not taken.
804 Timer timer(&compilerOutput.times.astVisualizer);
183
1/2
✓ Branch 10 → 11 taken 804 times.
✗ Branch 10 → 58 not taken.
804 timer.start();
184
185 // Generate dot code for this source file
186
1/2
✓ Branch 11 → 12 taken 804 times.
✗ Branch 11 → 58 not taken.
804 std::stringstream dotCode;
187
1/2
✓ Branch 12 → 13 taken 804 times.
✗ Branch 12 → 56 not taken.
804 visualizerPreamble(dotCode);
188
1/2
✓ Branch 13 → 14 taken 804 times.
✗ Branch 13 → 56 not taken.
804 ASTVisualizer astVisualizer(resourceManager, this);
189
5/10
✓ Branch 14 → 15 taken 804 times.
✗ Branch 14 → 54 not taken.
✓ Branch 15 → 16 taken 804 times.
✗ Branch 15 → 43 not taken.
✓ Branch 16 → 17 taken 804 times.
✗ Branch 16 → 41 not taken.
✓ Branch 17 → 18 taken 804 times.
✗ Branch 17 → 39 not taken.
✓ Branch 18 → 19 taken 804 times.
✗ Branch 18 → 39 not taken.
804 dotCode << " " << std::any_cast<std::string>(astVisualizer.visit(ast)) << "}";
190
191 // Dump the serialized AST string and the SVG file
192
1/2
✓ Branch 21 → 22 taken 804 times.
✗ Branch 21 → 45 not taken.
804 compilerOutput.astString = dotCode.str();
193
194
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 32 taken 804 times.
804 if (cliOptions.dump.dumpAST)
195 visualizerOutput("AST", compilerOutput.astString);
196
197 804 previousStage = AST_VISUALIZER;
198
1/2
✓ Branch 32 → 33 taken 804 times.
✗ Branch 32 → 54 not taken.
804 timer.stop();
199
1/2
✓ Branch 33 → 34 taken 804 times.
✗ Branch 33 → 52 not taken.
804 printStatusMessage("AST Visualizer", IO_AST, IO_AST, compilerOutput.times.astVisualizer);
200 804 }
201
202 1455 void SourceFile::runImportCollector() { // NOLINT(misc-no-recursion)
203 // Skip if restored from the cache or this stage has already been done
204
3/4
✓ Branch 2 → 3 taken 1455 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 211 times.
✓ Branch 3 → 5 taken 1244 times.
1455 if (restoredFromCache || previousStage >= IMPORT_COLLECTOR)
205 211 return;
206
207
1/2
✓ Branch 5 → 6 taken 1244 times.
✗ Branch 5 → 30 not taken.
1244 Timer timer(&compilerOutput.times.importCollector);
208
1/2
✓ Branch 6 → 7 taken 1244 times.
✗ Branch 6 → 30 not taken.
1244 timer.start();
209
210 // Collect the imports for this source file
211
1/2
✓ Branch 7 → 8 taken 1244 times.
✗ Branch 7 → 30 not taken.
1244 ImportCollector importCollector(resourceManager, this);
212
2/2
✓ Branch 8 → 9 taken 1239 times.
✓ Branch 8 → 24 taken 5 times.
1244 importCollector.visit(ast);
213
214 1239 previousStage = IMPORT_COLLECTOR;
215
1/2
✓ Branch 10 → 11 taken 1239 times.
✗ Branch 10 → 28 not taken.
1239 timer.stop();
216
217 // Run first part of pipeline for the imported source file
218
5/8
✓ Branch 11 → 12 taken 1239 times.
✗ Branch 11 → 25 not taken.
✓ Branch 12 → 13 taken 1239 times.
✗ Branch 12 → 25 not taken.
✓ Branch 13 → 14 taken 1239 times.
✗ Branch 13 → 25 not taken.
✓ Branch 19 → 15 taken 643 times.
✓ Branch 19 → 20 taken 1237 times.
1880 for (SourceFile *sourceFile : dependencies | std::views::values)
219
2/2
✓ Branch 16 → 17 taken 641 times.
✓ Branch 16 → 25 taken 2 times.
643 sourceFile->runFrontEnd();
220
221
1/2
✓ Branch 20 → 21 taken 1237 times.
✗ Branch 20 → 26 not taken.
1237 printStatusMessage("Import Collector", IO_AST, IO_AST, compilerOutput.times.importCollector);
222 1244 }
223
224 1448 void SourceFile::runSymbolTableBuilder() {
225 // Skip if restored from the cache or this stage has already been done
226
3/4
✓ Branch 2 → 3 taken 1448 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 211 times.
✓ Branch 3 → 5 taken 1237 times.
1448 if (restoredFromCache || previousStage >= SYMBOL_TABLE_BUILDER)
227 211 return;
228
229
1/2
✓ Branch 5 → 6 taken 1237 times.
✗ Branch 5 → 30 not taken.
1237 Timer timer(&compilerOutput.times.symbolTableBuilder);
230
1/2
✓ Branch 6 → 7 taken 1237 times.
✗ Branch 6 → 30 not taken.
1237 timer.start();
231
232 // The symbol tables of all dependencies are present at this point, so we can merge the exported name registries in
233
2/2
✓ Branch 15 → 9 taken 641 times.
✓ Branch 15 → 16 taken 1237 times.
1878 for (const auto &[importName, sourceFile] : dependencies)
234
1/2
✓ Branch 12 → 13 taken 641 times.
✗ Branch 12 → 24 not taken.
641 mergeNameRegistries(*sourceFile, importName);
235
236 // Build symbol table of the current file
237
1/2
✓ Branch 16 → 17 taken 1237 times.
✗ Branch 16 → 30 not taken.
1237 SymbolTableBuilder symbolTableBuilder(resourceManager, this);
238
2/2
✓ Branch 17 → 18 taken 1218 times.
✓ Branch 17 → 25 taken 19 times.
1237 symbolTableBuilder.visit(ast);
239
240 1218 previousStage = SYMBOL_TABLE_BUILDER;
241
1/2
✓ Branch 19 → 20 taken 1218 times.
✗ Branch 19 → 28 not taken.
1218 timer.stop();
242
1/2
✓ Branch 20 → 21 taken 1218 times.
✗ Branch 20 → 26 not taken.
1218 printStatusMessage("Symbol Table Builder", IO_AST, IO_AST, compilerOutput.times.symbolTableBuilder);
243 1237 }
244
245 1428 void SourceFile::runTypeCheckerPre() { // NOLINT(misc-no-recursion)
246 // Skip if restored from the cache or this stage has already been done
247
3/4
✓ Branch 2 → 3 taken 1428 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 211 times.
✓ Branch 3 → 5 taken 1217 times.
1428 if (restoredFromCache || previousStage >= TYPE_CHECKER_PRE)
248 211 return;
249
250 // Type-check all dependencies first
251
5/8
✓ Branch 5 → 6 taken 1217 times.
✗ Branch 5 → 24 not taken.
✓ Branch 6 → 7 taken 1217 times.
✗ Branch 6 → 24 not taken.
✓ Branch 7 → 8 taken 1217 times.
✗ Branch 7 → 24 not taken.
✓ Branch 13 → 9 taken 640 times.
✓ Branch 13 → 14 taken 1217 times.
1857 for (SourceFile *sourceFile : dependencies | std::views::values)
252
1/2
✓ Branch 10 → 11 taken 640 times.
✗ Branch 10 → 24 not taken.
640 sourceFile->runTypeCheckerPre();
253
254
1/2
✓ Branch 14 → 15 taken 1217 times.
✗ Branch 14 → 30 not taken.
1217 Timer timer(&compilerOutput.times.typeCheckerPre);
255
1/2
✓ Branch 15 → 16 taken 1217 times.
✗ Branch 15 → 30 not taken.
1217 timer.start();
256
257 // Then type-check the current file
258
1/2
✓ Branch 16 → 17 taken 1217 times.
✗ Branch 16 → 30 not taken.
1217 TypeChecker typeChecker(resourceManager, this, TC_MODE_PRE);
259
2/2
✓ Branch 17 → 18 taken 1203 times.
✓ Branch 17 → 25 taken 14 times.
1217 typeChecker.visit(ast);
260
261 1203 previousStage = TYPE_CHECKER_PRE;
262
1/2
✓ Branch 19 → 20 taken 1203 times.
✗ Branch 19 → 28 not taken.
1203 timer.stop();
263
1/2
✓ Branch 20 → 21 taken 1203 times.
✗ Branch 20 → 26 not taken.
1203 printStatusMessage("Type Checker Pre", IO_AST, IO_AST, compilerOutput.times.typeCheckerPre);
264 1217 }
265
266 2993 void SourceFile::runTypeCheckerPost() { // NOLINT(misc-no-recursion)
267 // Skip if restored from cache, this stage has already been done, or not all dependants finished type checking
268
6/8
✓ Branch 2 → 3 taken 2993 times.
✗ Branch 2 → 5 not taken.
✓ Branch 3 → 4 taken 2993 times.
✗ Branch 3 → 80 not taken.
✓ Branch 4 → 5 taken 551 times.
✓ Branch 4 → 6 taken 2442 times.
✓ Branch 7 → 8 taken 551 times.
✓ Branch 7 → 9 taken 2442 times.
2993 if (restoredFromCache || !haveAllDependantsBeenTypeChecked())
269 551 return;
270
271
1/2
✓ Branch 9 → 10 taken 2442 times.
✗ Branch 9 → 80 not taken.
2442 Timer timer(&compilerOutput.times.typeCheckerPost);
272
1/2
✓ Branch 10 → 11 taken 2442 times.
✗ Branch 10 → 80 not taken.
2442 timer.start();
273
274 // Start type-checking loop. The type-checker can request a re-execution. The max number of type-checker runs is limited
275
1/2
✓ Branch 11 → 12 taken 2442 times.
✗ Branch 11 → 80 not taken.
2442 TypeChecker typeChecker(resourceManager, this, TC_MODE_POST);
276 2442 unsigned short typeCheckerRuns = 0;
277
2/2
✓ Branch 27 → 13 taken 1682 times.
✓ Branch 27 → 28 taken 2379 times.
4061 while (reVisitRequested) {
278 1682 typeCheckerRuns++;
279 1682 totalTypeCheckerRuns++;
280 1682 reVisitRequested = false;
281
282 // Type-check the current file first. Multiple times, if requested
283 1682 timer.resume();
284
2/2
✓ Branch 14 → 15 taken 1650 times.
✓ Branch 14 → 58 taken 32 times.
1682 typeChecker.visit(ast);
285
1/2
✓ Branch 16 → 17 taken 1650 times.
✗ Branch 16 → 78 not taken.
1650 timer.pause();
286
287 // Then type-check all dependencies
288
5/8
✓ Branch 17 → 18 taken 1650 times.
✗ Branch 17 → 59 not taken.
✓ Branch 18 → 19 taken 1650 times.
✗ Branch 18 → 59 not taken.
✓ Branch 19 → 20 taken 1650 times.
✗ Branch 19 → 59 not taken.
✓ Branch 25 → 21 taken 2583 times.
✓ Branch 25 → 26 taken 1619 times.
4202 for (SourceFile *sourceFile : dependencies | std::views::values)
289
2/2
✓ Branch 22 → 23 taken 2552 times.
✓ Branch 22 → 59 taken 31 times.
2583 sourceFile->runTypeCheckerPost();
290 }
291
292
2/2
✓ Branch 28 → 29 taken 2272 times.
✓ Branch 28 → 78 taken 107 times.
2379 checkForSoftErrors();
293
294 // Check if all dyn variables were type-inferred successfully
295
2/2
✓ Branch 30 → 31 taken 2271 times.
✓ Branch 30 → 78 taken 1 time.
2272 globalScope->ensureSuccessfulTypeInference();
296
297 2271 previousStage = TYPE_CHECKER_POST;
298
1/2
✓ Branch 31 → 32 taken 2271 times.
✗ Branch 31 → 78 not taken.
2271 timer.stop();
299
1/2
✓ Branch 32 → 33 taken 2271 times.
✗ Branch 32 → 60 not taken.
2271 printStatusMessage("Type Checker Post", IO_AST, IO_AST, compilerOutput.times.typeCheckerPost, typeCheckerRuns);
300
301 // Save the JSON version in the compiler output
302
2/4
✓ Branch 33 → 34 taken 2271 times.
✗ Branch 33 → 35 not taken.
✓ Branch 34 → 35 taken 2271 times.
✗ Branch 34 → 42 not taken.
2271 if (cliOptions.dump.dumpSymbolTable || cliOptions.testMode)
303
2/4
✓ Branch 36 → 37 taken 2271 times.
✗ Branch 36 → 64 not taken.
✓ Branch 37 → 38 taken 2271 times.
✗ Branch 37 → 62 not taken.
2271 compilerOutput.symbolTableString = globalScope->getSymbolTableJSON().dump(/*indent=*/2);
304
305 // Dump symbol table
306
1/2
✗ Branch 42 → 43 not taken.
✓ Branch 42 → 55 taken 2271 times.
2271 if (cliOptions.dump.dumpSymbolTable)
307 dumpOutput(compilerOutput.symbolTableString, "Symbol Table", "symbol-table.json");
308 2442 }
309
310 272 void SourceFile::runDependencyGraphVisualizer() {
311 // Only execute if enabled
312
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 272 times.
272 if (restoredFromCache)
313 2 return;
314
2/4
✓ Branch 4 → 5 taken 272 times.
✗ Branch 4 → 7 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 272 times.
272 if (!cliOptions.dump.dumpDependencyGraph && !cliOptions.testMode)
315 return;
316 // Check if this stage has already been done
317
2/2
✓ Branch 7 → 8 taken 2 times.
✓ Branch 7 → 9 taken 270 times.
272 if (previousStage >= DEP_GRAPH_VISUALIZER)
318 2 return;
319
320
1/2
✓ Branch 9 → 10 taken 270 times.
✗ Branch 9 → 47 not taken.
270 Timer timer(&compilerOutput.times.depGraphVisualizer);
321
1/2
✓ Branch 10 → 11 taken 270 times.
✗ Branch 10 → 47 not taken.
270 timer.start();
322
323 // Generate dot code for this source file
324
1/2
✓ Branch 11 → 12 taken 270 times.
✗ Branch 11 → 47 not taken.
270 std::stringstream dotCode;
325
1/2
✓ Branch 12 → 13 taken 270 times.
✗ Branch 12 → 45 not taken.
270 visualizerPreamble(dotCode);
326
1/2
✓ Branch 13 → 14 taken 270 times.
✗ Branch 13 → 45 not taken.
270 DependencyGraphVisualizer depGraphVisualizer(resourceManager, this);
327
1/2
✓ Branch 14 → 15 taken 270 times.
✗ Branch 14 → 43 not taken.
270 depGraphVisualizer.getDependencyGraph(dotCode);
328
1/2
✓ Branch 15 → 16 taken 270 times.
✗ Branch 15 → 43 not taken.
270 dotCode << "}";
329
330 // Dump the serialized AST string and the SVG file
331
1/2
✓ Branch 16 → 17 taken 270 times.
✗ Branch 16 → 34 not taken.
270 compilerOutput.depGraphString = dotCode.str();
332
333
1/2
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 27 taken 270 times.
270 if (cliOptions.dump.dumpDependencyGraph)
334 visualizerOutput("Dependency Graph", compilerOutput.depGraphString);
335
336 270 previousStage = DEP_GRAPH_VISUALIZER;
337
1/2
✓ Branch 27 → 28 taken 270 times.
✗ Branch 27 → 43 not taken.
270 timer.stop();
338
1/2
✓ Branch 28 → 29 taken 270 times.
✗ Branch 28 → 41 not taken.
270 printStatusMessage("AST Visualizer", IO_AST, IO_AST, compilerOutput.times.depGraphVisualizer);
339 270 }
340
341 3654 void SourceFile::runIRGenerator() {
342 // Skip if restored from the cache or this stage has already been done
343
3/4
✓ Branch 2 → 3 taken 3654 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 2646 times.
✓ Branch 3 → 5 taken 1008 times.
3654 if (restoredFromCache || previousStage >= IR_GENERATOR)
344 2646 return;
345
346
1/2
✓ Branch 5 → 6 taken 1008 times.
✗ Branch 5 → 60 not taken.
1008 Timer timer(&compilerOutput.times.irGenerator);
347
1/2
✓ Branch 6 → 7 taken 1008 times.
✗ Branch 6 → 60 not taken.
1008 timer.start();
348
349 // Create the LLVM module for this source file
350
2/2
✓ Branch 7 → 8 taken 2 times.
✓ Branch 7 → 9 taken 1006 times.
1008 llvm::LLVMContext &llvmContext = cliOptions.useLTO ? resourceManager.ltoContext : context;
351
1/2
✓ Branch 10 → 11 taken 1008 times.
✗ Branch 10 → 41 not taken.
1008 llvmModule = std::make_unique<llvm::Module>(fileName, llvmContext);
352
353 // Generate this source file
354
1/2
✓ Branch 13 → 14 taken 1008 times.
✗ Branch 13 → 60 not taken.
1008 IRGenerator irGenerator(resourceManager, this);
355
1/2
✓ Branch 14 → 15 taken 1008 times.
✗ Branch 14 → 42 not taken.
1008 irGenerator.visit(ast);
356
357 // Save the ir string in the compiler output
358
2/4
✓ Branch 16 → 17 taken 1008 times.
✗ Branch 16 → 18 not taken.
✓ Branch 17 → 18 taken 1008 times.
✗ Branch 17 → 23 not taken.
1008 if (cliOptions.dump.dumpIR || cliOptions.testMode)
359
1/2
✓ Branch 19 → 20 taken 1008 times.
✗ Branch 19 → 43 not taken.
1008 compilerOutput.irString = IRGenerator::getIRString(llvmModule.get(), cliOptions);
360
361 // Dump unoptimized IR code
362
1/2
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 36 taken 1008 times.
1008 if (cliOptions.dump.dumpIR)
363 dumpOutput(compilerOutput.irString, "Unoptimized IR Code", "ir-code.ll");
364
365 1008 previousStage = IR_GENERATOR;
366
1/2
✓ Branch 36 → 37 taken 1008 times.
✗ Branch 36 → 58 not taken.
1008 timer.stop();
367
1/2
✓ Branch 37 → 38 taken 1008 times.
✗ Branch 37 → 56 not taken.
1008 printStatusMessage("IR Generator", IO_AST, IO_IR, compilerOutput.times.irGenerator);
368 1008 }
369
370 3574 void SourceFile::runDefaultIROptimizer() {
371
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 3574 times.
3574 assert(!cliOptions.useLTO);
372
373 // Skip if restored from the cache or this stage has already been done
374
6/8
✓ Branch 4 → 5 taken 3574 times.
✗ Branch 4 → 8 not taken.
✓ Branch 5 → 6 taken 928 times.
✓ Branch 5 → 8 taken 2646 times.
✓ Branch 6 → 7 taken 29 times.
✓ Branch 6 → 9 taken 899 times.
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 29 times.
3574 if (restoredFromCache || previousStage > IR_OPTIMIZER || (previousStage == IR_OPTIMIZER && !cliOptions.testMode))
375 2646 return;
376
377
1/2
✓ Branch 9 → 10 taken 928 times.
✗ Branch 9 → 60 not taken.
928 Timer timer(&compilerOutput.times.irOptimizer);
378
1/2
✓ Branch 10 → 11 taken 928 times.
✗ Branch 10 → 60 not taken.
928 timer.start();
379
380 // Optimize this source file
381
1/2
✓ Branch 11 → 12 taken 928 times.
✗ Branch 11 → 60 not taken.
928 IROptimizer irOptimizer(resourceManager, this);
382
1/2
✓ Branch 12 → 13 taken 928 times.
✗ Branch 12 → 58 not taken.
928 irOptimizer.prepare();
383
1/2
✓ Branch 13 → 14 taken 928 times.
✗ Branch 13 → 58 not taken.
928 irOptimizer.optimizeDefault();
384
385 // Save the optimized ir string in the compiler output
386
2/4
✓ Branch 14 → 15 taken 928 times.
✗ Branch 14 → 16 not taken.
✓ Branch 15 → 16 taken 928 times.
✗ Branch 15 → 21 not taken.
928 if (cliOptions.dump.dumpIR || cliOptions.testMode)
387
1/2
✓ Branch 17 → 18 taken 928 times.
✗ Branch 17 → 40 not taken.
928 compilerOutput.irOptString = IRGenerator::getIRString(llvmModule.get(), cliOptions);
388
389 // Dump optimized IR code
390
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 35 taken 928 times.
928 if (cliOptions.dump.dumpIR)
391 dumpOutput(compilerOutput.irOptString, "Optimized IR Code",
392 "ir-code-O" + std::to_string(static_cast<uint8_t>(cliOptions.optLevel)) + ".ll");
393
394 928 previousStage = IR_OPTIMIZER;
395
1/2
✓ Branch 35 → 36 taken 928 times.
✗ Branch 35 → 58 not taken.
928 timer.stop();
396
1/2
✓ Branch 36 → 37 taken 928 times.
✗ Branch 36 → 56 not taken.
928 printStatusMessage("IR Optimizer", IO_IR, IO_IR, compilerOutput.times.irOptimizer);
397 928 }
398
399 2 void SourceFile::runPreLinkIROptimizer() {
400
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 2 times.
2 assert(cliOptions.useLTO);
401
402 // Skip if restored from the cache or this stage has already been done
403
2/4
✓ Branch 4 → 5 taken 2 times.
✗ Branch 4 → 6 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 2 times.
2 if (restoredFromCache || previousStage >= IR_OPTIMIZER)
404 return;
405
406
1/2
✓ Branch 7 → 8 taken 2 times.
✗ Branch 7 → 51 not taken.
2 Timer timer(&compilerOutput.times.irOptimizer);
407
1/2
✓ Branch 8 → 9 taken 2 times.
✗ Branch 8 → 51 not taken.
2 timer.start();
408
409 // Optimize this source file
410
1/2
✓ Branch 9 → 10 taken 2 times.
✗ Branch 9 → 51 not taken.
2 IROptimizer irOptimizer(resourceManager, this);
411
1/2
✓ Branch 10 → 11 taken 2 times.
✗ Branch 10 → 49 not taken.
2 irOptimizer.prepare();
412
1/2
✓ Branch 11 → 12 taken 2 times.
✗ Branch 11 → 49 not taken.
2 irOptimizer.optimizePreLink();
413
414 // Save the optimized ir string in the compiler output
415
2/4
✓ Branch 12 → 13 taken 2 times.
✗ Branch 12 → 14 not taken.
✓ Branch 13 → 14 taken 2 times.
✗ Branch 13 → 19 not taken.
2 if (cliOptions.dump.dumpIR || cliOptions.testMode)
416
1/2
✓ Branch 15 → 16 taken 2 times.
✗ Branch 15 → 36 not taken.
2 compilerOutput.irOptString = IRGenerator::getIRString(llvmModule.get(), cliOptions);
417
418 // Dump optimized IR code
419
1/2
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 32 taken 2 times.
2 if (cliOptions.dump.dumpIR)
420 dumpOutput(compilerOutput.irOptString, "Optimized IR Code (pre-link)", "ir-code-lto-pre-link.ll");
421
422
1/2
✓ Branch 32 → 33 taken 2 times.
✗ Branch 32 → 49 not taken.
2 timer.pause();
423 2 }
424
425 2 void SourceFile::runBitcodeLinker() {
426
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 2 times.
2 assert(cliOptions.useLTO);
427
428 // Skip if this is not the main source file
429
2/2
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 1 time.
2 if (!isMainFile)
430 1 return;
431
432 // Skip if restored from the cache or this stage has already been done
433
2/4
✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 8 not taken.
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 1 time.
1 if (restoredFromCache || previousStage >= IR_OPTIMIZER)
434 return;
435
436
1/2
✓ Branch 9 → 10 taken 1 time.
✗ Branch 9 → 20 not taken.
1 Timer timer(&compilerOutput.times.irOptimizer);
437 1 timer.resume();
438
439 // Link all source files together
440
1/2
✓ Branch 11 → 12 taken 1 time.
✗ Branch 11 → 20 not taken.
1 BitcodeLinker linker(resourceManager);
441
1/2
✓ Branch 12 → 13 taken 1 time.
✗ Branch 12 → 18 not taken.
1 linker.link();
442
443
1/2
✓ Branch 13 → 14 taken 1 time.
✗ Branch 13 → 18 not taken.
1 timer.pause();
444 1 }
445
446 2 void SourceFile::runPostLinkIROptimizer() {
447
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 2 times.
2 assert(cliOptions.useLTO);
448
449 // Skip if this is not the main source file
450
2/2
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 1 time.
2 if (!isMainFile)
451 1 return;
452
453 // Skip if restored from the cache or this stage has already been done
454
2/4
✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 8 not taken.
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 1 time.
1 if (restoredFromCache || previousStage >= IR_OPTIMIZER)
455 return;
456
457
1/2
✓ Branch 9 → 10 taken 1 time.
✗ Branch 9 → 57 not taken.
1 Timer timer(&compilerOutput.times.irOptimizer);
458 1 timer.resume();
459
460 // Optimize LTO module
461
1/2
✓ Branch 11 → 12 taken 1 time.
✗ Branch 11 → 57 not taken.
1 IROptimizer irOptimizer(resourceManager, this);
462
1/2
✓ Branch 12 → 13 taken 1 time.
✗ Branch 12 → 55 not taken.
1 irOptimizer.prepare();
463
1/2
✓ Branch 13 → 14 taken 1 time.
✗ Branch 13 → 55 not taken.
1 irOptimizer.optimizePostLink();
464
465 // Save the optimized ir string in the compiler output
466
2/4
✓ Branch 14 → 15 taken 1 time.
✗ Branch 14 → 16 not taken.
✓ Branch 15 → 16 taken 1 time.
✗ Branch 15 → 21 not taken.
1 if (cliOptions.dump.dumpIR || cliOptions.testMode) {
467 1 llvm::Module *module = resourceManager.ltoModule.get();
468
1/2
✓ Branch 17 → 18 taken 1 time.
✗ Branch 17 → 40 not taken.
1 compilerOutput.irOptString = IRGenerator::getIRString(module, cliOptions);
469 }
470
471 // Dump optimized IR code
472
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 34 taken 1 time.
1 if (cliOptions.dump.dumpIR)
473 dumpOutput(compilerOutput.irOptString, "Optimized IR Code (post-Link)", "ir-code-lto-post-link.ll");
474
475 1 previousStage = IR_OPTIMIZER;
476
1/2
✓ Branch 34 → 35 taken 1 time.
✗ Branch 34 → 55 not taken.
1 timer.stop();
477
1/2
✓ Branch 35 → 36 taken 1 time.
✗ Branch 35 → 53 not taken.
1 printStatusMessage("IR Optimizer", IO_IR, IO_IR, compilerOutput.times.irOptimizer);
478 1 }
479
480 3606 void SourceFile::runObjectEmitter() {
481 // Skip if restored from the cache or this stage has already been done
482
3/4
✓ Branch 2 → 3 taken 3606 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 2646 times.
✓ Branch 3 → 5 taken 960 times.
3606 if (restoredFromCache || previousStage >= OBJECT_EMITTER)
483 2647 return;
484
485 // Skip if LTO is enabled and this is not the main source file
486
4/4
✓ Branch 5 → 6 taken 2 times.
✓ Branch 5 → 8 taken 958 times.
✓ Branch 6 → 7 taken 1 time.
✓ Branch 6 → 8 taken 1 time.
960 if (cliOptions.useLTO && !isMainFile)
487 1 return;
488
489
1/2
✓ Branch 8 → 9 taken 959 times.
✗ Branch 8 → 67 not taken.
959 Timer timer(&compilerOutput.times.objectEmitter);
490
1/2
✓ Branch 9 → 10 taken 959 times.
✗ Branch 9 → 67 not taken.
959 timer.start();
491
492 // Deduce an object file path
493
2/4
✓ Branch 10 → 11 taken 959 times.
✗ Branch 10 → 45 not taken.
✓ Branch 11 → 12 taken 959 times.
✗ Branch 11 → 43 not taken.
959 std::filesystem::path objectFilePath = cliOptions.outputDir / filePath.filename();
494
2/4
✓ Branch 13 → 14 taken 959 times.
✗ Branch 13 → 48 not taken.
✓ Branch 14 → 15 taken 959 times.
✗ Branch 14 → 46 not taken.
959 objectFilePath.replace_extension("o");
495
496 // Emit object for this source file
497
1/2
✓ Branch 16 → 17 taken 959 times.
✗ Branch 16 → 65 not taken.
959 const ObjectEmitter objectEmitter(resourceManager, this);
498
1/2
✓ Branch 17 → 18 taken 959 times.
✗ Branch 17 → 63 not taken.
959 objectEmitter.emit(objectFilePath);
499
500 // Save assembly string in the compiler output
501
4/6
✓ Branch 18 → 19 taken 915 times.
✓ Branch 18 → 22 taken 44 times.
✓ Branch 19 → 20 taken 915 times.
✗ Branch 19 → 21 not taken.
✓ Branch 20 → 21 taken 915 times.
✗ Branch 20 → 22 not taken.
959 if (cliOptions.isNativeTarget && (cliOptions.dump.dumpAssembly || cliOptions.testMode))
502
1/2
✓ Branch 21 → 22 taken 915 times.
✗ Branch 21 → 63 not taken.
915 objectEmitter.getASMString(compilerOutput.asmString);
503
504 // Dump assembly code
505
1/2
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 35 taken 959 times.
959 if (cliOptions.dump.dumpAssembly)
506 dumpOutput(compilerOutput.asmString, "Assembly code", "assembly-code.s");
507
508 // Add the object file to the linker objects
509
1/2
✓ Branch 35 → 36 taken 959 times.
✗ Branch 35 → 63 not taken.
959 resourceManager.linker.addFileToLinkage(objectFilePath);
510
511 959 previousStage = OBJECT_EMITTER;
512
1/2
✓ Branch 36 → 37 taken 959 times.
✗ Branch 36 → 63 not taken.
959 timer.stop();
513
1/2
✓ Branch 37 → 38 taken 959 times.
✗ Branch 37 → 61 not taken.
959 printStatusMessage("Object Emitter", IO_IR, IO_OBJECT_FILE, compilerOutput.times.objectEmitter);
514 959 }
515
516 3606 void SourceFile::concludeCompilation() {
517 // Skip if restored from the cache or this stage has already been done
518
3/4
✓ Branch 2 → 3 taken 3606 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 2646 times.
✓ Branch 3 → 5 taken 960 times.
3606 if (restoredFromCache || previousStage >= FINISHED)
519 2646 return;
520
521 // Cache the source file
522
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 960 times.
960 if (!cliOptions.ignoreCache)
523 resourceManager.cacheManager.cacheSourceFile(this);
524
525 // Save type registry as string in the compiler output
526
4/6
✓ Branch 7 → 8 taken 222 times.
✓ Branch 7 → 14 taken 738 times.
✓ Branch 8 → 9 taken 222 times.
✗ Branch 8 → 10 not taken.
✓ Branch 9 → 10 taken 222 times.
✗ Branch 9 → 14 not taken.
960 if (isMainFile && (cliOptions.dump.dumpTypes || cliOptions.testMode))
527
1/2
✓ Branch 10 → 11 taken 222 times.
✗ Branch 10 → 52 not taken.
222 compilerOutput.typesString = TypeRegistry::dump();
528
529 // Dump type registry
530
3/4
✓ Branch 14 → 15 taken 222 times.
✓ Branch 14 → 28 taken 738 times.
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 28 taken 222 times.
960 if (isMainFile && cliOptions.dump.dumpTypes)
531 dumpOutput(compilerOutput.typesString, "Type Registry", "type-registry.out");
532
533 // Save cache statistics as string in the compiler output
534
4/6
✓ Branch 28 → 29 taken 222 times.
✓ Branch 28 → 32 taken 738 times.
✓ Branch 29 → 30 taken 222 times.
✗ Branch 29 → 31 not taken.
✓ Branch 30 → 31 taken 222 times.
✗ Branch 30 → 32 not taken.
960 if (isMainFile && (cliOptions.dump.dumpCacheStats || cliOptions.testMode))
535 222 dumpCacheStats();
536
537 // Dump lookup cache statistics
538
3/4
✓ Branch 32 → 33 taken 222 times.
✓ Branch 32 → 46 taken 738 times.
✗ Branch 33 → 34 not taken.
✓ Branch 33 → 46 taken 222 times.
960 if (isMainFile && cliOptions.dump.dumpCacheStats)
539 dumpOutput(compilerOutput.cacheStats, "Cache Statistics", "cache-stats.out");
540
541
1/2
✗ Branch 46 → 47 not taken.
✓ Branch 46 → 50 taken 960 times.
960 if (cliOptions.printDebugOutput)
542 std::cout << "Finished compiling " << fileName << std::endl;
543
544 960 previousStage = FINISHED;
545 }
546
547 1007 void SourceFile::runFrontEnd() { // NOLINT(misc-no-recursion)
548 1007 runLexer();
549
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 1007 times.
1007 CHECK_ABORT_FLAG_V()
550 1007 runParser();
551
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 1007 times.
1007 CHECK_ABORT_FLAG_V()
552 1007 runCSTVisualizer();
553
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 1007 times.
1007 CHECK_ABORT_FLAG_V()
554 1007 runASTBuilder();
555
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 1007 times.
1007 CHECK_ABORT_FLAG_V()
556 1007 runASTVisualizer();
557
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 1007 times.
1007 CHECK_ABORT_FLAG_V()
558 1007 runImportCollector();
559
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 1005 times.
1005 CHECK_ABORT_FLAG_V()
560 1005 runSymbolTableBuilder();
561
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 1005 times.
1005 CHECK_ABORT_FLAG_V()
562 }
563
564 424 void SourceFile::runMiddleEnd() {
565 // We need two runs here due to generics.
566 // The first run to determine all concrete function/struct/interface substantiations
567 424 runTypeCheckerPre(); // Visit the dependency tree from bottom to top
568
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 410 times.
410 CHECK_ABORT_FLAG_V()
569 // The second run to ensure, also generic scopes are type-checked properly
570 410 runTypeCheckerPost(); // Visit the dependency tree from top to bottom in topological order
571
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 270 times.
270 CHECK_ABORT_FLAG_V()
572 // Visualize dependency graph
573 270 runDependencyGraphVisualizer();
574
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 270 times.
270 CHECK_ABORT_FLAG_V()
575 }
576
577 3384 void SourceFile::runBackEnd() { // NOLINT(misc-no-recursion)
578 // Run backend for all dependencies first
579
5/8
✓ Branch 2 → 3 taken 3384 times.
✗ Branch 2 → 36 not taken.
✓ Branch 3 → 4 taken 3384 times.
✗ Branch 3 → 36 not taken.
✓ Branch 4 → 5 taken 3384 times.
✗ Branch 4 → 36 not taken.
✓ Branch 10 → 6 taken 3173 times.
✓ Branch 10 → 11 taken 3384 times.
6557 for (SourceFile *sourceFile : dependencies | std::views::values)
580
1/2
✓ Branch 7 → 8 taken 3173 times.
✗ Branch 7 → 36 not taken.
3173 sourceFile->runBackEnd();
581
582 3384 runIRGenerator();
583
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 3384 times.
3384 CHECK_ABORT_FLAG_V()
584
2/2
✓ Branch 14 → 15 taken 1 time.
✓ Branch 14 → 24 taken 3383 times.
3384 if (cliOptions.useLTO) {
585 1 runPreLinkIROptimizer();
586
1/2
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 1 time.
1 CHECK_ABORT_FLAG_V()
587 1 runBitcodeLinker();
588
1/2
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 21 taken 1 time.
1 CHECK_ABORT_FLAG_V()
589 1 runPostLinkIROptimizer();
590
1/2
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 27 taken 1 time.
1 CHECK_ABORT_FLAG_V()
591 } else {
592 3383 runDefaultIROptimizer();
593
1/2
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 27 taken 3383 times.
3383 CHECK_ABORT_FLAG_V()
594 }
595 3384 runObjectEmitter();
596
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 3384 times.
3384 CHECK_ABORT_FLAG_V()
597 3384 concludeCompilation();
598
599
1/2
✗ Branch 31 → 32 not taken.
✓ Branch 31 → 35 taken 3384 times.
3384 if (isMainFile) {
600 resourceManager.totalTimer.stop();
601 if (cliOptions.printDebugOutput)
602 dumpCompilationStats();
603 }
604 }
605
606 1470 void SourceFile::addDependency(SourceFile *sourceFile, const ASTNode *declNode, const std::string &dependencyName,
607 const std::string &path) {
608 // Check if this would cause a circular dependency
609
1/2
✓ Branch 2 → 3 taken 1470 times.
✗ Branch 2 → 36 not taken.
1470 std::stack<const SourceFile *> dependencyCircle;
610
3/4
✓ Branch 3 → 4 taken 1470 times.
✗ Branch 3 → 34 not taken.
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 17 taken 1469 times.
1470 if (isAlreadyImported(path, dependencyCircle)) {
611 // Build the error message
612
1/2
✓ Branch 5 → 6 taken 1 time.
✗ Branch 5 → 32 not taken.
1 std::stringstream errorMessage;
613
3/6
✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 30 not taken.
✓ Branch 7 → 8 taken 1 time.
✗ Branch 7 → 30 not taken.
✓ Branch 8 → 9 taken 1 time.
✗ Branch 8 → 30 not taken.
1 errorMessage << "Circular import detected while importing '" << sourceFile->fileName << "':\n\n";
614
2/4
✓ Branch 9 → 10 taken 1 time.
✗ Branch 9 → 23 not taken.
✓ Branch 10 → 11 taken 1 time.
✗ Branch 10 → 21 not taken.
1 errorMessage << CommonUtil::getCircularImportMessage(dependencyCircle);
615
2/4
✓ Branch 13 → 14 taken 1 time.
✗ Branch 13 → 27 not taken.
✓ Branch 14 → 15 taken 1 time.
✗ Branch 14 → 24 not taken.
1 throw SemanticError(declNode, CIRCULAR_DEPENDENCY, errorMessage.str());
616 1 }
617
618 // Add the dependency
619 1469 sourceFile->isMainFile = false;
620
1/2
✓ Branch 17 → 18 taken 1469 times.
✗ Branch 17 → 34 not taken.
1469 dependencies.emplace(dependencyName, sourceFile);
621
622 // Add the dependant
623
1/2
✓ Branch 18 → 19 taken 1469 times.
✗ Branch 18 → 33 not taken.
1469 sourceFile->dependants.push_back(this);
624 1470 }
625
626 103705 bool SourceFile::imports(const SourceFile *sourceFile) const {
627 318114 return std::ranges::any_of(dependencies, [=](const auto &dependency) { return dependency.second == sourceFile; });
628 }
629
630 6707 bool SourceFile::isAlreadyImported(const std::string &filePathSearch, // NOLINT(misc-no-recursion)
631 std::stack<const SourceFile *> &circle) const {
632
1/2
✓ Branch 2 → 3 taken 6707 times.
✗ Branch 2 → 20 not taken.
6707 circle.push(this);
633
634 // Check if the current source file corresponds to the path to search
635
4/6
✓ Branch 3 → 4 taken 6707 times.
✗ Branch 3 → 23 not taken.
✓ Branch 4 → 5 taken 6707 times.
✗ Branch 4 → 21 not taken.
✓ Branch 6 → 7 taken 1 time.
✓ Branch 6 → 8 taken 6706 times.
6707 if (std::filesystem::equivalent(filePath, filePathSearch))
636 1 return true;
637
638 // Check dependants recursively
639
2/2
✓ Branch 16 → 10 taken 5237 times.
✓ Branch 16 → 17 taken 6704 times.
11941 for (const SourceFile *dependant : dependants)
640
3/4
✓ Branch 11 → 12 taken 5237 times.
✗ Branch 11 → 24 not taken.
✓ Branch 12 → 13 taken 2 times.
✓ Branch 12 → 14 taken 5235 times.
5237 if (dependant->isAlreadyImported(filePathSearch, circle))
641 2 return true;
642
643 // If no dependant was found, remove the current source file from the circle to continue with the next sibling
644 6704 circle.pop();
645 6704 return false;
646 }
647
648 3748 SourceFile *SourceFile::requestRuntimeModule(RuntimeModule runtimeModule) {
649 // Check if the module was already imported
650
2/2
✓ Branch 3 → 4 taken 2924 times.
✓ Branch 3 → 6 taken 824 times.
3748 if (isRuntimeModuleAvailable(runtimeModule))
651 2924 return resourceManager.runtimeModuleManager.getModule(runtimeModule);
652 824 return resourceManager.runtimeModuleManager.requestModule(this, runtimeModule);
653 }
654
655 4092 bool SourceFile::isRuntimeModuleAvailable(RuntimeModule runtimeModule) const { return importedRuntimeModules & runtimeModule; }
656
657 39762 void SourceFile::addNameRegistryEntry(const std::string &symbolName, uint64_t typeId, SymbolTableEntry *entry, Scope *scope,
658 bool keepNewOnCollision, SymbolTableEntry *importEntry) {
659
6/6
✓ Branch 2 → 3 taken 9918 times.
✓ Branch 2 → 5 taken 29844 times.
✓ Branch 4 → 5 taken 9851 times.
✓ Branch 4 → 6 taken 67 times.
✓ Branch 7 → 8 taken 39695 times.
✓ Branch 7 → 13 taken 67 times.
39762 if (keepNewOnCollision || !exportedNameRegistry.contains(symbolName)) // Overwrite potential existing entry
660 39695 exportedNameRegistry[symbolName] = {symbolName, typeId, entry, scope, importEntry};
661 else // Name collision => we must remove the existing entry
662 67 exportedNameRegistry.erase(symbolName);
663
2/6
✓ Branch 8 → 9 taken 39695 times.
✗ Branch 8 → 20 not taken.
✓ Branch 9 → 10 taken 39695 times.
✗ Branch 9 → 15 not taken.
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 19 not taken.
79457 }
664
665 216195 const NameRegistryEntry *SourceFile::getNameRegistryEntry(const std::string &symbolName) const {
666
1/2
✓ Branch 2 → 3 taken 216195 times.
✗ Branch 2 → 13 not taken.
216195 const auto it = exportedNameRegistry.find(symbolName);
667
2/2
✓ Branch 5 → 6 taken 107915 times.
✓ Branch 5 → 7 taken 108280 times.
216195 if (it == exportedNameRegistry.end())
668 107915 return nullptr;
669
670 // Resolve registry entry for the given name
671 108280 const NameRegistryEntry *entry = &it->second;
672
673 // Mark the import entry as used
674
2/2
✓ Branch 8 → 9 taken 5318 times.
✓ Branch 8 → 10 taken 102962 times.
108280 if (entry->importEntry != nullptr)
675 5318 entry->importEntry->used = true;
676
677 108280 return entry;
678 }
679
680 372991 llvm::Type *SourceFile::getLLVMType(const Type *type) {
681 // Check if the type is already in the mapping
682
1/2
✓ Branch 2 → 3 taken 372991 times.
✗ Branch 2 → 13 not taken.
372991 const auto it = typeToLLVMTypeMapping.find(type);
683
2/2
✓ Branch 5 → 6 taken 364328 times.
✓ Branch 5 → 8 taken 8663 times.
372991 if (it != typeToLLVMTypeMapping.end())
684 364328 return it->second;
685
686 // If not, generate the LLVM type
687
1/2
✓ Branch 8 → 9 taken 8663 times.
✗ Branch 8 → 13 not taken.
8663 llvm::Type *llvmType = type->toLLVMType(this);
688
1/2
✓ Branch 9 → 10 taken 8663 times.
✗ Branch 9 → 13 not taken.
8663 typeToLLVMTypeMapping[type] = llvmType;
689 8663 return llvmType;
690 }
691
692 2381 void SourceFile::checkForSoftErrors() const {
693 // Check if there are any soft errors and if so, print them
694
2/2
✓ Branch 3 → 4 taken 109 times.
✓ Branch 3 → 19 taken 2272 times.
2381 if (!resourceManager.errorManager.softErrors.empty()) {
695
1/2
✓ Branch 4 → 5 taken 109 times.
✗ Branch 4 → 29 not taken.
109 std::stringstream errorStream;
696
1/2
✓ Branch 5 → 6 taken 109 times.
✗ Branch 5 → 27 not taken.
109 errorStream << "There are unresolved errors. Please fix them and recompile.";
697
2/2
✓ Branch 13 → 8 taken 168 times.
✓ Branch 13 → 14 taken 109 times.
277 for (const auto &[codeLoc, message] : resourceManager.errorManager.softErrors)
698
2/4
✓ Branch 9 → 10 taken 168 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 168 times.
✗ Branch 10 → 20 not taken.
168 errorStream << "\n\n" << message;
699
2/4
✓ Branch 15 → 16 taken 109 times.
✗ Branch 15 → 24 not taken.
✓ Branch 16 → 17 taken 109 times.
✗ Branch 16 → 21 not taken.
109 throw CompilerError(UNRESOLVED_SOFT_ERRORS, errorStream.str());
700 109 }
701 2272 }
702
703 300 void SourceFile::collectAndPrintWarnings() { // NOLINT(misc-no-recursion)
704 // Print warnings for all dependencies
705
5/8
✓ Branch 2 → 3 taken 300 times.
✗ Branch 2 → 23 not taken.
✓ Branch 3 → 4 taken 300 times.
✗ Branch 3 → 23 not taken.
✓ Branch 4 → 5 taken 300 times.
✗ Branch 4 → 23 not taken.
✓ Branch 11 → 6 taken 263 times.
✓ Branch 11 → 12 taken 300 times.
563 for (SourceFile *sourceFile : dependencies | std::views::values)
706
2/2
✓ Branch 7 → 8 taken 30 times.
✓ Branch 7 → 9 taken 233 times.
263 if (!sourceFile->isStdFile)
707
1/2
✓ Branch 8 → 9 taken 30 times.
✗ Branch 8 → 23 not taken.
30 sourceFile->collectAndPrintWarnings();
708 // Collect warnings for this file
709
1/2
✓ Branch 12 → 13 taken 300 times.
✗ Branch 12 → 15 not taken.
300 if (!ignoreWarnings)
710 300 globalScope->collectWarnings(compilerOutput.warnings);
711 // Print warnings for this file
712
2/2
✓ Branch 21 → 17 taken 201 times.
✓ Branch 21 → 22 taken 300 times.
501 for (const CompilerWarning &warning : compilerOutput.warnings)
713
1/2
✓ Branch 18 → 19 taken 201 times.
✗ Branch 18 → 24 not taken.
201 warning.print();
714 300 }
715
716 4856 const SourceFile *SourceFile::getRootSourceFile() const { // NOLINT(misc-no-recursion)
717
2/2
✓ Branch 2 → 3 taken 1757 times.
✓ Branch 2 → 4 taken 3099 times.
4856 return isMainFile ? this : parent->getRootSourceFile();
718 }
719
720 12009 bool SourceFile::isRT(RuntimeModule runtimeModule) const {
721
2/4
✓ Branch 2 → 3 taken 12009 times.
✗ Branch 2 → 39 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 12009 times.
12009 assert(IDENTIFYING_TOP_LEVEL_NAMES.contains(runtimeModule));
722
1/2
✓ Branch 5 → 6 taken 12009 times.
✗ Branch 5 → 39 not taken.
12009 const char *topLevelName = IDENTIFYING_TOP_LEVEL_NAMES.at(runtimeModule);
723
2/4
✓ Branch 8 → 9 taken 12009 times.
✗ Branch 8 → 29 not taken.
✓ Branch 9 → 10 taken 12009 times.
✗ Branch 9 → 27 not taken.
24018 const auto it = exportedNameRegistry.find(topLevelName);
724
2/2
✓ Branch 14 → 15 taken 1812 times.
✓ Branch 14 → 16 taken 10197 times.
12009 if (it == exportedNameRegistry.end())
725 1812 return false;
726
2/4
✓ Branch 18 → 19 taken 10197 times.
✗ Branch 18 → 35 not taken.
✓ Branch 19 → 20 taken 10197 times.
✗ Branch 19 → 33 not taken.
30591 return exportedNameRegistry.at(topLevelName).targetEntry->scope == globalScope.get();
727 }
728
729 2993 bool SourceFile::haveAllDependantsBeenTypeChecked() const {
730 8912 return std::ranges::all_of(dependants, [](const SourceFile *dependant) { return dependant->totalTypeCheckerRuns >= 1; });
731 }
732
733 /**
734 * Acquire all publicly visible symbols from the imported source file and put them in the name registry of the current one.
735 * But only do that for the symbols that are actually defined in the imported source file. Do not allow transitive dependencies.
736 * Here, we also register privately visible symbols to know that the symbol exist. The error handling regarding the visibility
737 * is issued later in the pipeline.
738 *
739 * @param importedSourceFile Imported source file
740 * @param importName First fragment of all fully qualified symbol names from that import
741 */
742 1465 void SourceFile::mergeNameRegistries(const SourceFile &importedSourceFile, const std::string &importName) {
743 // Retrieve import entry
744 1465 SymbolTableEntry *importEntry = globalScope->lookupStrict(importName);
745
3/4
✓ Branch 6 → 7 taken 824 times.
✓ Branch 6 → 10 taken 641 times.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 824 times.
1465 assert(importEntry != nullptr || importName.starts_with("__")); // Runtime imports start with two underscores
746
747
2/2
✓ Branch 29 → 12 taken 41758 times.
✓ Branch 29 → 30 taken 1465 times.
43223 for (const auto &[originalName, entry] : importedSourceFile.exportedNameRegistry) {
748 // Skip if we introduce a transitive dependency
749
2/2
✓ Branch 16 → 17 taken 20076 times.
✓ Branch 16 → 18 taken 21682 times.
41758 if (entry.targetScope->sourceFile->globalScope != importedSourceFile.globalScope)
750 20076 continue;
751 // Add the fully qualified name
752
1/2
✓ Branch 18 → 19 taken 21682 times.
✗ Branch 18 → 39 not taken.
21682 std::string newName = importName;
753
1/2
✓ Branch 19 → 20 taken 21682 times.
✗ Branch 19 → 37 not taken.
21682 newName += SCOPE_ACCESS_TOKEN;
754
1/2
✓ Branch 20 → 21 taken 21682 times.
✗ Branch 20 → 37 not taken.
21682 newName += originalName;
755
2/6
✓ Branch 21 → 22 taken 21682 times.
✗ Branch 21 → 36 not taken.
✓ Branch 22 → 23 taken 21682 times.
✗ Branch 22 → 31 not taken.
✗ Branch 33 → 34 not taken.
✗ Branch 33 → 35 not taken.
21682 exportedNameRegistry.emplace(newName, NameRegistryEntry{newName, entry.typeId, entry.targetEntry, entry.targetScope, importEntry});
756 // Add the shortened name, considering the name collision
757 21682 const bool keepOnCollision = importedSourceFile.alwaysKeepSymbolsOnNameCollision;
758
1/2
✓ Branch 24 → 25 taken 21682 times.
✗ Branch 24 → 37 not taken.
21682 addNameRegistryEntry(originalName, entry.typeId, entry.targetEntry, entry.targetScope, keepOnCollision, importEntry);
759 21682 }
760 1465 }
761
762 222 void SourceFile::dumpCacheStats() {
763
1/2
✓ Branch 2 → 3 taken 222 times.
✗ Branch 2 → 32 not taken.
222 std::stringstream cacheStats;
764
3/6
✓ Branch 3 → 4 taken 222 times.
✗ Branch 3 → 22 not taken.
✓ Branch 4 → 5 taken 222 times.
✗ Branch 4 → 20 not taken.
✓ Branch 5 → 6 taken 222 times.
✗ Branch 5 → 20 not taken.
222 cacheStats << FunctionManager::dumpLookupCacheStatistics() << std::endl;
765
3/6
✓ Branch 7 → 8 taken 222 times.
✗ Branch 7 → 25 not taken.
✓ Branch 8 → 9 taken 222 times.
✗ Branch 8 → 23 not taken.
✓ Branch 9 → 10 taken 222 times.
✗ Branch 9 → 23 not taken.
222 cacheStats << StructManager::dumpLookupCacheStatistics() << std::endl;
766
3/6
✓ Branch 11 → 12 taken 222 times.
✗ Branch 11 → 28 not taken.
✓ Branch 12 → 13 taken 222 times.
✗ Branch 12 → 26 not taken.
✓ Branch 13 → 14 taken 222 times.
✗ Branch 13 → 26 not taken.
222 cacheStats << InterfaceManager::dumpLookupCacheStatistics() << std::endl;
767
1/2
✓ Branch 15 → 16 taken 222 times.
✗ Branch 15 → 29 not taken.
222 compilerOutput.cacheStats = cacheStats.str();
768 222 }
769
770 void SourceFile::dumpCompilationStats() const {
771 const size_t sourceFileCount = resourceManager.sourceFiles.size();
772 const size_t totalLineCount = resourceManager.getTotalLineCount();
773 const size_t totalTypeCount = TypeRegistry::getTypeCount();
774 const size_t allocatedBytes = resourceManager.astNodeAlloc.getTotalAllocatedSize();
775 const size_t allocationCount = resourceManager.astNodeAlloc.getAllocationCount();
776 const size_t totalDuration = resourceManager.totalTimer.getDurationMilliseconds();
777 std::cout << "\nSuccessfully compiled " << std::to_string(sourceFileCount) << " source file(s)";
778 std::cout << " or " << std::to_string(totalLineCount) << " lines in total.\n";
779 std::cout << "Total number of blocks allocated via BlockAllocator: " << CommonUtil::formatBytes(allocatedBytes);
780 std::cout << " in " << std::to_string(allocationCount) << " allocations.\n";
781 #ifndef NDEBUG
782 resourceManager.astNodeAlloc.printAllocatedClassStatistic();
783 #endif
784 std::cout << "Total number of types: " << std::to_string(totalTypeCount) << "\n";
785 std::cout << "Total compile time: " << std::to_string(totalDuration) << " ms\n";
786 }
787
788 void SourceFile::dumpOutput(const std::string &content, const std::string &caption, const std::string &fileSuffix) const {
789 if (cliOptions.dump.dumpToFiles) {
790 // Dump to file
791 const std::string dumpFileName = filePath.stem().string() + "-" + fileSuffix;
792 std::filesystem::path dumpFilePath = cliOptions.outputDir / dumpFileName;
793 dumpFilePath.make_preferred();
794 FileUtil::writeToFile(dumpFilePath, content);
795 } else {
796 // Dump to console
797 std::cout << "\n" << caption << ":\n" << content;
798 }
799
800 // If the abort after dump is requested, set the abort compilation flag
801 if (cliOptions.dump.abortAfterDump) {
802 // If this is an IR dump whilst having optimization enabled, we may not abort when dumping unoptimized IR,
803 // because we also have to dump the optimized IR
804 if (cliOptions.dump.dumpIR && fileSuffix == "ir-code.ll") {
805 resourceManager.abortCompilation = cliOptions.optLevel == OptLevel::O0;
806 } else {
807 resourceManager.abortCompilation = true;
808 }
809 }
810 }
811
812 1878 void SourceFile::visualizerPreamble(std::stringstream &output) const {
813
2/2
✓ Branch 2 → 3 taken 286 times.
✓ Branch 2 → 4 taken 1592 times.
1878 if (isMainFile)
814 286 output << "digraph {\n rankdir=\"TB\";\n";
815 else
816 1592 output << "subgraph {\n";
817
3/6
✓ Branch 6 → 7 taken 1878 times.
✗ Branch 6 → 13 not taken.
✓ Branch 7 → 8 taken 1878 times.
✗ Branch 7 → 11 not taken.
✓ Branch 8 → 9 taken 1878 times.
✗ Branch 8 → 11 not taken.
1878 output << " label=\"" << filePath.generic_string() << "\";\n";
818 1878 }
819
820 void SourceFile::visualizerOutput(std::string outputName, const std::string &output) const {
821 if (cliOptions.dump.dumpToFiles) {
822 // Check if graphviz is installed
823 // GCOV_EXCL_START
824 if (!SystemUtil::isGraphvizInstalled())
825 throw CompilerError(IO_ERROR, "Please check if you have installed Graphviz and added it to the PATH variable");
826 // GCOV_EXCL_STOP
827
828 // Write to a dot file
829 std::ranges::transform(outputName, outputName.begin(), ::tolower);
830 dumpOutput(output, outputName, outputName + ".dot");
831
832 // Generate SVG. This only works if the dot code was dumped into a file
833 std::cout << "\nGenerating SVG file ... ";
834 const std::string dotFileName = filePath.stem().string() + "-" + outputName + ".dot";
835 std::filesystem::path dotFilePath = cliOptions.outputDir / dotFileName;
836 std::filesystem::path svgFilePath = dotFilePath;
837 svgFilePath.replace_extension("svg");
838 dotFilePath.make_preferred();
839 svgFilePath.make_preferred();
840 SystemUtil::exec("dot -T svg -o" + svgFilePath.string() + " " + dotFilePath.string());
841 std::cout << "done.\nSVG file can be found at: " << svgFilePath << "\n";
842 } else {
843 // Dump to console
844 std::cout << "\nSerialized " << outputName << ":\n\n" << output << "\n";
845 }
846
847 // If the abort after dump is requested, set the abort compilation flag
848 if (cliOptions.dump.abortAfterDump)
849 resourceManager.abortCompilation = true;
850 }
851
852 14451 void SourceFile::printStatusMessage(const char *stage, const CompileStageIOType &in, const CompileStageIOType &out,
853 uint64_t stageRuntime, unsigned short stageRuns) const {
854
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 30 taken 14451 times.
14451 if (cliOptions.printDebugOutput) {
855 static constexpr const char *const compilerStageIoTypeName[6] = {"Code", "Tokens", "CST", "AST", "IR", "Obj"};
856 // Build output string
857 std::stringstream outputStr;
858 outputStr << "[" << stage << "] for " << fileName << ": ";
859 outputStr << compilerStageIoTypeName[in] << " --> " << compilerStageIoTypeName[out];
860 outputStr << " (" << std::to_string(stageRuntime) << " ms";
861 if (stageRuns > 0)
862 outputStr << "; " << std::to_string(stageRuns) << " run(s)";
863 outputStr << ")\n";
864 // Print
865 std::cout << outputStr.str();
866 }
867 14451 }
868
869 } // namespace spice::compiler
870