GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 85.0% 534 / 3 / 631
Functions: 89.8% 44 / 0 / 49
Branches: 48.2% 564 / 12 / 1182

src/SourceFile.cpp
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #include "SourceFile.h"
4
5 #include <algorithm>
6 #include <queue>
7 #include <unordered_set>
8
9 #include <ast/ASTBuilder.h>
10 #include <driver/Driver.h>
11 #include <exception/AntlrThrowingErrorListener.h>
12 #include <exception/CompilerError.h>
13 #include <global/GlobalResourceManager.h>
14 #include <global/TypeRegistry.h>
15 #include <importcollector/ImportCollector.h>
16 #include <irgenerator/IRGenerator.h>
17 #include <iroptimizer/IROptimizer.h>
18 #include <linker/BitcodeLinker.h>
19 #include <linter/LintPass.h>
20 #include <objectemitter/LLVMObjectEmitter.h>
21 #ifdef SPICE_ENABLE_TPDE
22 #include <objectemitter/TPDEObjectEmitter.h>
23 #endif
24 #include <symboltablebuilder/SymbolTable.h>
25 #include <symboltablebuilder/SymbolTableBuilder.h>
26 #include <typechecker/FunctionManager.h>
27 #include <typechecker/InterfaceManager.h>
28 #include <typechecker/MacroDefs.h>
29 #include <typechecker/PostTypeCheckingVerifier.h>
30 #include <typechecker/StructManager.h>
31 #include <typechecker/TypeChecker.h>
32 #include <util/CompilerWarning.h>
33 #include <util/Concurrency.h>
34 #include <util/FileUtil.h>
35 #include <util/SystemUtil.h>
36 #include <util/ThreadPool.h>
37 #include <util/Timer.h>
38 #include <visualizer/ASTVisualizer.h>
39 #include <visualizer/CSTVisualizer.h>
40 #include <visualizer/DependencyGraphVisualizer.h>
41
42 #include <llvm/IR/Module.h>
43 #include <llvm/MC/TargetRegistry.h>
44
45 namespace spice::compiler {
46
47 2802 SourceFile::SourceFile(GlobalResourceManager &resourceManager, SourceFile *parent, std::string name,
48 const std::filesystem::path &filePath, bool stdFile)
49
1/2
✓ Branch 6 → 7 taken 2802 times.
✗ Branch 6 → 132 not taken.
8406 : name(std::move(name)), filePath(filePath), isStdFile(stdFile), parent(parent),
50
3/4
✓ Branch 18 → 19 taken 2 times.
✓ Branch 18 → 20 taken 2800 times.
✓ Branch 21 → 22 taken 2802 times.
✗ Branch 21 → 64 not taken.
2802 builder(resourceManager.cliOptions.useLTO ? resourceManager.ltoContext : context), resourceManager(resourceManager),
51
1/2
✓ Branch 16 → 17 taken 2802 times.
✗ Branch 16 → 112 not taken.
8406 cliOptions(resourceManager.cliOptions) {
52 // Deduce fileName and fileDir
53
3/6
✓ Branch 29 → 30 taken 2802 times.
✗ Branch 29 → 69 not taken.
✓ Branch 30 → 31 taken 2802 times.
✗ Branch 30 → 67 not taken.
✓ Branch 31 → 32 taken 2802 times.
✗ Branch 31 → 65 not taken.
2802 fileName = std::filesystem::path(filePath).filename().string();
54
3/6
✓ Branch 36 → 37 taken 2802 times.
✗ Branch 36 → 76 not taken.
✓ Branch 37 → 38 taken 2802 times.
✗ Branch 37 → 74 not taken.
✓ Branch 38 → 39 taken 2802 times.
✗ Branch 38 → 72 not taken.
2802 fileDir = std::filesystem::path(filePath).parent_path().string();
55
56 // Discard value names if not required
57
1/2
✓ Branch 43 → 44 taken 2802 times.
✗ Branch 43 → 93 not taken.
2802 context.setDiscardValueNames(!cliOptions.namesForIRValues);
58
59 // Search after the selected target
60 2802 std::string error;
61
1/2
✓ Branch 45 → 46 taken 2802 times.
✗ Branch 45 → 91 not taken.
2802 const llvm::Target *target = llvm::TargetRegistry::lookupTarget(cliOptions.targetTriple, error);
62
1/2
✗ Branch 46 → 47 not taken.
✓ Branch 46 → 52 taken 2802 times.
2802 if (!target)
63 throw CompilerError(TARGET_NOT_AVAILABLE, "Selected target was not found: " + error); // GCOV_EXCL_LINE
64
65 // Create the target machine
66
1/2
✓ Branch 52 → 53 taken 2802 times.
✗ Branch 52 → 91 not taken.
2802 llvm::TargetOptions opt;
67 2802 opt.MCOptions.AsmVerbose = true;
68 2802 opt.MCOptions.PreserveAsmComments = true;
69 2802 const std::string &cpuName = resourceManager.cpuName;
70 2802 const std::string &features = resourceManager.cpuFeatures;
71 2802 const llvm::Triple &targetTriple = cliOptions.targetTriple;
72 2802 constexpr llvm::Reloc::Model relocModel = llvm::Reloc::PIC_;
73
1/2
✓ Branch 57 → 58 taken 2802 times.
✗ Branch 57 → 85 not taken.
2802 llvm::TargetMachine *targetMachineRaw = target->createTargetMachine(targetTriple, cpuName, features, opt, relocModel);
74 2802 targetMachine = std::unique_ptr<llvm::TargetMachine>(targetMachineRaw);
75 2802 }
76
77 4146 void SourceFile::runLexer() {
78
2/2
✓ Branch 2 → 3 taken 622 times.
✓ Branch 2 → 4 taken 3524 times.
4146 if (isMainFile)
79
1/2
✓ Branch 3 → 4 taken 622 times.
✗ Branch 3 → 83 not taken.
622 resourceManager.totalTimer.start();
80
81 // Check if this stage has already been done
82
2/2
✓ Branch 4 → 5 taken 1349 times.
✓ Branch 4 → 6 taken 2797 times.
4146 if (previousStage >= LEXER)
83 1349 return;
84
85
1/2
✓ Branch 6 → 7 taken 2797 times.
✗ Branch 6 → 83 not taken.
2797 Timer timer(&compilerOutput.times.lexer);
86
1/2
✓ Branch 7 → 8 taken 2797 times.
✗ Branch 7 → 83 not taken.
2797 timer.start();
87
88 // Read from the input source file
89
1/2
✓ Branch 8 → 9 taken 2797 times.
✗ Branch 8 → 83 not taken.
2797 std::ifstream fileInputStream(filePath);
90
3/4
✓ Branch 9 → 10 taken 2797 times.
✗ Branch 9 → 81 not taken.
✓ Branch 10 → 11 taken 1 time.
✓ Branch 10 → 20 taken 2796 times.
2797 if (!fileInputStream)
91
4/8
✓ Branch 12 → 13 taken 1 time.
✗ Branch 12 → 59 not taken.
✓ Branch 13 → 14 taken 1 time.
✗ Branch 13 → 57 not taken.
✓ Branch 14 → 15 taken 1 time.
✗ Branch 14 → 55 not taken.
✓ Branch 15 → 16 taken 1 time.
✗ Branch 15 → 52 not taken.
1 throw CompilerError(SOURCE_FILE_NOT_FOUND, "Source file at path '" + filePath.string() + "' does not exist.");
92
93 // Tokenize input
94
1/2
✓ Branch 20 → 21 taken 2796 times.
✗ Branch 20 → 64 not taken.
2796 antlrCtx.inputStream = std::make_unique<antlr4::ANTLRInputStream>(fileInputStream);
95
1/2
✓ Branch 24 → 25 taken 2796 times.
✗ Branch 24 → 65 not taken.
2796 antlrCtx.lexer = std::make_unique<SpiceLexer>(antlrCtx.inputStream.get());
96
1/2
✓ Branch 28 → 29 taken 2796 times.
✗ Branch 28 → 81 not taken.
2796 antlrCtx.lexer->removeErrorListeners();
97
1/2
✓ Branch 29 → 30 taken 2796 times.
✗ Branch 29 → 67 not taken.
2796 antlrCtx.lexerErrorHandler = std::make_unique<AntlrThrowingErrorListener>(ThrowingErrorListenerMode::LEXER, this);
98
1/2
✓ Branch 34 → 35 taken 2796 times.
✗ Branch 34 → 81 not taken.
2796 antlrCtx.lexer->addErrorListener(antlrCtx.lexerErrorHandler.get());
99
1/2
✓ Branch 36 → 37 taken 2796 times.
✗ Branch 36 → 70 not taken.
2796 antlrCtx.tokenStream = std::make_unique<antlr4::CommonTokenStream>(antlrCtx.lexer.get());
100
101 // Pre-compute a local cache key so the field is populated for cycle-aware fallbacks.
102 // The final key (which folds in transitive dependency cache keys) is computed at the end
103 // of runImportCollector, once every dependency's cache key has been finalized.
104
3/4
✓ Branch 41 → 42 taken 2795 times.
✓ Branch 41 → 74 taken 1 time.
✓ Branch 42 → 43 taken 2795 times.
✗ Branch 42 → 72 not taken.
2797 cacheKey = resourceManager.cacheManager.computeCacheKey(antlrCtx.tokenStream->getText());
105
106 2795 previousStage = LEXER;
107
1/2
✓ Branch 47 → 48 taken 2795 times.
✗ Branch 47 → 81 not taken.
2795 timer.stop();
108
1/2
✓ Branch 48 → 49 taken 2795 times.
✗ Branch 48 → 79 not taken.
2795 printStatusMessage("Lexer", IO_CODE, IO_TOKENS, compilerOutput.times.lexer);
109 2797 }
110
111 4144 void SourceFile::runParser() {
112 // Skip if restored from the cache or this stage has already been done
113
3/4
✓ Branch 2 → 3 taken 4144 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 1349 times.
✓ Branch 3 → 5 taken 2795 times.
4144 if (restoredFromCache || previousStage >= PARSER)
114 1349 return;
115
116
1/2
✓ Branch 5 → 6 taken 2795 times.
✗ Branch 5 → 32 not taken.
2795 Timer timer(&compilerOutput.times.parser);
117
1/2
✓ Branch 6 → 7 taken 2795 times.
✗ Branch 6 → 32 not taken.
2795 timer.start();
118
119 // Parse input
120
1/2
✓ Branch 8 → 9 taken 2795 times.
✗ Branch 8 → 25 not taken.
2795 antlrCtx.parser = std::make_unique<SpiceParser>(antlrCtx.tokenStream.get()); // Check for syntax errors
121
1/2
✓ Branch 12 → 13 taken 2795 times.
✗ Branch 12 → 32 not taken.
2795 antlrCtx.parser->removeErrorListeners();
122
1/2
✓ Branch 13 → 14 taken 2795 times.
✗ Branch 13 → 27 not taken.
2795 antlrCtx.parserErrorHandler = std::make_unique<AntlrThrowingErrorListener>(ThrowingErrorListenerMode::PARSER, this);
123
1/2
✓ Branch 18 → 19 taken 2795 times.
✗ Branch 18 → 32 not taken.
2795 antlrCtx.parser->addErrorListener(antlrCtx.parserErrorHandler.get());
124
1/2
✓ Branch 20 → 21 taken 2795 times.
✗ Branch 20 → 32 not taken.
2795 antlrCtx.parser->removeParseListeners();
125
126 2795 previousStage = PARSER;
127
1/2
✓ Branch 21 → 22 taken 2795 times.
✗ Branch 21 → 32 not taken.
2795 timer.stop();
128
1/2
✓ Branch 22 → 23 taken 2795 times.
✗ Branch 22 → 30 not taken.
2795 printStatusMessage("Parser", IO_TOKENS, IO_CST, compilerOutput.times.parser);
129 }
130
131 3537 void SourceFile::runCSTVisualizer() {
132 // Only execute if enabled
133
4/6
✓ Branch 2 → 3 taken 3537 times.
✗ Branch 2 → 5 not taken.
✓ Branch 3 → 4 taken 3537 times.
✗ Branch 3 → 6 not taken.
✓ Branch 4 → 5 taken 7 times.
✓ Branch 4 → 6 taken 3530 times.
3537 if (restoredFromCache || (!cliOptions.dump.dumpCST && !cliOptions.testMode))
134 1356 return;
135 // Check if this stage has already been done
136
2/2
✓ Branch 6 → 7 taken 1349 times.
✓ Branch 6 → 8 taken 2181 times.
3530 if (previousStage >= CST_VISUALIZER)
137 1349 return;
138
139
1/2
✓ Branch 8 → 9 taken 2181 times.
✗ Branch 8 → 66 not taken.
2181 Timer timer(&compilerOutput.times.cstVisualizer);
140
1/2
✓ Branch 9 → 10 taken 2181 times.
✗ Branch 9 → 66 not taken.
2181 timer.start();
141
142 // Generate dot code for this source file
143
1/2
✓ Branch 10 → 11 taken 2181 times.
✗ Branch 10 → 66 not taken.
2181 std::stringstream dotCode;
144
1/2
✓ Branch 11 → 12 taken 2181 times.
✗ Branch 11 → 64 not taken.
2181 visualizerPreamble(dotCode);
145
1/2
✓ Branch 14 → 15 taken 2181 times.
✗ Branch 14 → 64 not taken.
2181 CSTVisualizer cstVisualizer(resourceManager, this, antlrCtx.lexer.get(), antlrCtx.parser.get());
146
6/12
✓ Branch 15 → 16 taken 2181 times.
✗ Branch 15 → 62 not taken.
✓ Branch 17 → 18 taken 2181 times.
✗ Branch 17 → 51 not taken.
✓ Branch 18 → 19 taken 2181 times.
✗ Branch 18 → 51 not taken.
✓ Branch 19 → 20 taken 2181 times.
✗ Branch 19 → 49 not taken.
✓ Branch 20 → 21 taken 2181 times.
✗ Branch 20 → 47 not taken.
✓ Branch 21 → 22 taken 2181 times.
✗ Branch 21 → 47 not taken.
2181 dotCode << " " << std::any_cast<std::string>(cstVisualizer.visit(antlrCtx.parser->entry())) << "}";
147
1/2
✓ Branch 25 → 26 taken 2181 times.
✗ Branch 25 → 62 not taken.
2181 antlrCtx.parser->reset();
148
149 // Dump the serialized CST string and the SVG file
150
2/4
✓ Branch 26 → 27 taken 2181 times.
✗ Branch 26 → 28 not taken.
✓ Branch 27 → 28 taken 2181 times.
✗ Branch 27 → 32 not taken.
2181 if (cliOptions.dump.dumpCST || cliOptions.testMode)
151
1/2
✓ Branch 28 → 29 taken 2181 times.
✗ Branch 28 → 53 not taken.
2181 compilerOutput.cstString = dotCode.str();
152
153
1/2
✗ Branch 32 → 33 not taken.
✓ Branch 32 → 40 taken 2181 times.
2181 if (cliOptions.dump.dumpCST)
154 visualizerOutput("CST", compilerOutput.cstString);
155
156 2181 previousStage = CST_VISUALIZER;
157
1/2
✓ Branch 40 → 41 taken 2181 times.
✗ Branch 40 → 62 not taken.
2181 timer.stop();
158
1/2
✓ Branch 41 → 42 taken 2181 times.
✗ Branch 41 → 60 not taken.
2181 printStatusMessage("CST Visualizer", IO_CST, IO_CST, compilerOutput.times.cstVisualizer);
159 2181 }
160
161 4144 void SourceFile::runASTBuilder() {
162 // Skip if restored from the cache or this stage has already been done
163
3/4
✓ Branch 2 → 3 taken 4144 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 1349 times.
✓ Branch 3 → 5 taken 2795 times.
4144 if (restoredFromCache || previousStage >= AST_BUILDER)
164 1349 return;
165
166
1/2
✓ Branch 5 → 6 taken 2795 times.
✗ Branch 5 → 36 not taken.
2795 Timer timer(&compilerOutput.times.astBuilder);
167
1/2
✓ Branch 6 → 7 taken 2795 times.
✗ Branch 6 → 36 not taken.
2795 timer.start();
168
169 // Build AST for this source file
170
1/2
✓ Branch 8 → 9 taken 2795 times.
✗ Branch 8 → 36 not taken.
2795 ASTBuilder astBuilder(resourceManager, this, antlrCtx.inputStream.get());
171
5/6
✓ Branch 10 → 11 taken 2793 times.
✓ Branch 10 → 26 taken 2 times.
✓ Branch 11 → 12 taken 2787 times.
✓ Branch 11 → 26 taken 6 times.
✓ Branch 12 → 13 taken 2787 times.
✗ Branch 12 → 24 not taken.
2795 ast = std::any_cast<EntryNode *>(astBuilder.visit(antlrCtx.parser->entry()));
172
1/2
✓ Branch 15 → 16 taken 2787 times.
✗ Branch 15 → 34 not taken.
2787 antlrCtx.parser->reset();
173
174 // Create global scope
175
1/2
✓ Branch 16 → 17 taken 2787 times.
✗ Branch 16 → 27 not taken.
2787 globalScope = std::make_unique<Scope>(nullptr, this, ScopeType::GLOBAL, &ast->codeLoc);
176
177 2787 previousStage = AST_BUILDER;
178
1/2
✓ Branch 19 → 20 taken 2787 times.
✗ Branch 19 → 34 not taken.
2787 timer.stop();
179
1/2
✓ Branch 20 → 21 taken 2787 times.
✗ Branch 20 → 32 not taken.
2787 printStatusMessage("AST Builder", IO_CST, IO_AST, compilerOutput.times.astBuilder);
180 2795 }
181
182 3537 void SourceFile::runASTVisualizer() {
183 // Only execute if enabled
184
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 3537 times.
3537 if (restoredFromCache)
185 1356 return;
186
3/4
✓ Branch 4 → 5 taken 3537 times.
✗ Branch 4 → 7 not taken.
✓ Branch 5 → 6 taken 7 times.
✓ Branch 5 → 7 taken 3530 times.
3537 if (!cliOptions.dump.dumpAST && !cliOptions.testMode)
187 7 return;
188 // Check if this stage has already been done
189
2/2
✓ Branch 7 → 8 taken 1349 times.
✓ Branch 7 → 9 taken 2181 times.
3530 if (previousStage >= AST_VISUALIZER)
190 1349 return;
191
192
1/2
✓ Branch 9 → 10 taken 2181 times.
✗ Branch 9 → 58 not taken.
2181 Timer timer(&compilerOutput.times.astVisualizer);
193
1/2
✓ Branch 10 → 11 taken 2181 times.
✗ Branch 10 → 58 not taken.
2181 timer.start();
194
195 // Generate dot code for this source file
196
1/2
✓ Branch 11 → 12 taken 2181 times.
✗ Branch 11 → 58 not taken.
2181 std::stringstream dotCode;
197
1/2
✓ Branch 12 → 13 taken 2181 times.
✗ Branch 12 → 56 not taken.
2181 visualizerPreamble(dotCode);
198
1/2
✓ Branch 13 → 14 taken 2181 times.
✗ Branch 13 → 56 not taken.
2181 ASTVisualizer astVisualizer(resourceManager, this);
199
5/10
✓ Branch 14 → 15 taken 2181 times.
✗ Branch 14 → 54 not taken.
✓ Branch 15 → 16 taken 2181 times.
✗ Branch 15 → 43 not taken.
✓ Branch 16 → 17 taken 2181 times.
✗ Branch 16 → 41 not taken.
✓ Branch 17 → 18 taken 2181 times.
✗ Branch 17 → 39 not taken.
✓ Branch 18 → 19 taken 2181 times.
✗ Branch 18 → 39 not taken.
2181 dotCode << " " << std::any_cast<std::string>(astVisualizer.visit(ast)) << "}";
200
201 // Dump the serialized AST string and the SVG file
202
1/2
✓ Branch 21 → 22 taken 2181 times.
✗ Branch 21 → 45 not taken.
2181 compilerOutput.astString = dotCode.str();
203
204
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 32 taken 2181 times.
2181 if (cliOptions.dump.dumpAST)
205 visualizerOutput("AST", compilerOutput.astString);
206
207 2181 previousStage = AST_VISUALIZER;
208
1/2
✓ Branch 32 → 33 taken 2181 times.
✗ Branch 32 → 54 not taken.
2181 timer.stop();
209
1/2
✓ Branch 33 → 34 taken 2181 times.
✗ Branch 33 → 52 not taken.
2181 printStatusMessage("AST Visualizer", IO_AST, IO_AST, compilerOutput.times.astVisualizer);
210 2181 }
211
212 4136 void SourceFile::runImportCollector() { // NOLINT(misc-no-recursion)
213 // Skip if restored from the cache or this stage has already been done
214
3/4
✓ Branch 2 → 3 taken 4136 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 1349 times.
✓ Branch 3 → 5 taken 2787 times.
4136 if (restoredFromCache || previousStage >= IMPORT_COLLECTOR)
215 1349 return;
216
217
1/2
✓ Branch 5 → 6 taken 2787 times.
✗ Branch 5 → 85 not taken.
2787 Timer timer(&compilerOutput.times.importCollector);
218
1/2
✓ Branch 6 → 7 taken 2787 times.
✗ Branch 6 → 85 not taken.
2787 timer.start();
219
220 // Collect the imports for this source file
221
1/2
✓ Branch 7 → 8 taken 2787 times.
✗ Branch 7 → 85 not taken.
2787 ImportCollector importCollector(resourceManager, this);
222
2/2
✓ Branch 8 → 9 taken 2783 times.
✓ Branch 8 → 67 taken 4 times.
2787 importCollector.visit(ast);
223
224 2783 previousStage = IMPORT_COLLECTOR;
225
226 // Run first part of pipeline for the imported source file
227
5/8
✓ Branch 10 → 11 taken 2783 times.
✗ Branch 10 → 68 not taken.
✓ Branch 11 → 12 taken 2783 times.
✗ Branch 11 → 68 not taken.
✓ Branch 12 → 13 taken 2783 times.
✗ Branch 12 → 68 not taken.
✓ Branch 18 → 14 taken 2731 times.
✓ Branch 18 → 19 taken 2783 times.
5514 for (SourceFile *sourceFile : dependencies | std::views::values)
228
1/2
✓ Branch 15 → 16 taken 2731 times.
✗ Branch 15 → 68 not taken.
2731 sourceFile->runFrontEnd();
229
230 // Now that every transitive dependency has its final cache key, fold them into our own
231 // cache key. This way any change to a dependency invalidates the cache entry of every
232 // dependent (and transitively of the dependents' dependents), avoiding stale object files.
233 2783 std::vector<std::string> transitiveDepCacheKeys;
234 2783 std::unordered_set<std::string> visited;
235
1/2
✓ Branch 20 → 21 taken 2783 times.
✗ Branch 20 → 79 not taken.
2783 std::queue<const SourceFile *> worklist;
236
5/8
✓ Branch 21 → 22 taken 2783 times.
✗ Branch 21 → 69 not taken.
✓ Branch 22 → 23 taken 2783 times.
✗ Branch 22 → 69 not taken.
✓ Branch 23 → 24 taken 2783 times.
✗ Branch 23 → 69 not taken.
✓ Branch 29 → 25 taken 2731 times.
✓ Branch 29 → 30 taken 2783 times.
5514 for (const SourceFile *dep : dependencies | std::views::values)
237
1/2
✓ Branch 26 → 27 taken 2731 times.
✗ Branch 26 → 69 not taken.
2731 worklist.push(dep);
238
2/2
✓ Branch 49 → 31 taken 12449 times.
✓ Branch 49 → 50 taken 2783 times.
15232 while (!worklist.empty()) {
239 12449 const SourceFile *dep = worklist.front();
240 12449 worklist.pop();
241
3/4
✓ Branch 33 → 34 taken 12449 times.
✗ Branch 33 → 77 not taken.
✓ Branch 34 → 35 taken 6361 times.
✓ Branch 34 → 36 taken 6088 times.
12449 if (!visited.insert(dep->cacheKey).second)
242 6361 continue;
243
1/2
✓ Branch 36 → 37 taken 6088 times.
✗ Branch 36 → 77 not taken.
6088 transitiveDepCacheKeys.push_back(dep->cacheKey);
244
5/8
✓ Branch 37 → 38 taken 6088 times.
✗ Branch 37 → 70 not taken.
✓ Branch 38 → 39 taken 6088 times.
✗ Branch 38 → 70 not taken.
✓ Branch 39 → 40 taken 6088 times.
✗ Branch 39 → 70 not taken.
✓ Branch 45 → 41 taken 9718 times.
✓ Branch 45 → 46 taken 6088 times.
15806 for (const SourceFile *transitive : dep->dependencies | std::views::values)
245
1/2
✓ Branch 42 → 43 taken 9718 times.
✗ Branch 42 → 70 not taken.
9718 worklist.push(transitive);
246 }
247
2/4
✓ Branch 51 → 52 taken 2783 times.
✗ Branch 51 → 73 not taken.
✓ Branch 52 → 53 taken 2783 times.
✗ Branch 52 → 71 not taken.
2783 cacheKey = resourceManager.cacheManager.computeCacheKey(antlrCtx.tokenStream->getText(), transitiveDepCacheKeys);
248
249 // Try to load from the cache. Deferred from runLexer so that dep cache keys can participate.
250
2/2
✓ Branch 56 → 57 taken 4 times.
✓ Branch 56 → 59 taken 2779 times.
2783 if (!cliOptions.ignoreCache)
251
1/2
✓ Branch 57 → 58 taken 4 times.
✗ Branch 57 → 77 not taken.
4 restoredFromCache = resourceManager.cacheManager.lookupSourceFile(this);
252
253
1/2
✓ Branch 59 → 60 taken 2783 times.
✗ Branch 59 → 77 not taken.
2783 timer.stop();
254
1/2
✓ Branch 60 → 61 taken 2783 times.
✗ Branch 60 → 75 not taken.
2783 printStatusMessage("Import Collector", IO_AST, IO_AST, compilerOutput.times.importCollector);
255 2787 }
256
257 4132 void SourceFile::runSymbolTableBuilder() {
258 // Skip if this stage has already been done. Unlike the later stages, this one must still run even if the file was
259 // restored from the cache: it's the only pass that populates exportedNameRegistry, and a dependant that isn't itself
260 // a cache hit needs that registry to resolve the symbols it imports from this file.
261
2/2
✓ Branch 2 → 3 taken 1349 times.
✓ Branch 2 → 4 taken 2783 times.
4132 if (previousStage >= SYMBOL_TABLE_BUILDER)
262 1349 return;
263
264
1/2
✓ Branch 4 → 5 taken 2783 times.
✗ Branch 4 → 19 not taken.
2783 Timer timer(&compilerOutput.times.symbolTableBuilder);
265
1/2
✓ Branch 5 → 6 taken 2783 times.
✗ Branch 5 → 19 not taken.
2783 timer.start();
266
267 // Build symbol table of the current file. The dependencies' exported name registries are merged in afterwards, in a
268 // separate pass (mergeNameRegistriesRecursive), once every reachable file has built its own registry. This deferral
269 // is what makes circular imports work: with a cycle, a dependency's registry is not fully populated yet at this point.
270
1/2
✓ Branch 6 → 7 taken 2783 times.
✗ Branch 6 → 19 not taken.
2783 SymbolTableBuilder symbolTableBuilder(resourceManager, this);
271
2/2
✓ Branch 7 → 8 taken 2764 times.
✓ Branch 7 → 14 taken 19 times.
2783 symbolTableBuilder.visit(ast);
272
273 2764 previousStage = SYMBOL_TABLE_BUILDER;
274
1/2
✓ Branch 9 → 10 taken 2764 times.
✗ Branch 9 → 17 not taken.
2764 timer.stop();
275
1/2
✓ Branch 10 → 11 taken 2764 times.
✗ Branch 10 → 15 not taken.
2764 printStatusMessage("Symbol Table Builder", IO_AST, IO_AST, compilerOutput.times.symbolTableBuilder);
276 2783 }
277
278 4112 void SourceFile::runTypeCheckerPre() { // NOLINT(misc-no-recursion)
279 // Skip if this stage has already been done. Unlike the later (codegen) stages, this one must still run even if the
280 // file was restored from the cache: it's what populates the FunctionManager/StructManager manifestations that a
281 // dependant which isn't itself a cache hit needs for overload resolution and generic substantiation.
282 // The typeCheckerPreRunning guard breaks the recursion on a circular import: a cyclic back-edge returns immediately
283 // instead of recursing forever. The file is still pre-checked once the in-progress invocation reaches it, and any
284 // cross-file references left unresolved (because a cycle peer was not pre-checked yet) are fixed up by the post run.
285
4/4
✓ Branch 2 → 3 taken 2788 times.
✓ Branch 2 → 4 taken 1324 times.
✓ Branch 3 → 4 taken 25 times.
✓ Branch 3 → 5 taken 2763 times.
4112 if (previousStage >= TYPE_CHECKER_PRE || typeCheckerPreRunning)
286 1349 return;
287 2763 typeCheckerPreRunning = true;
288
289 // Type-check all dependencies first
290
5/8
✓ Branch 5 → 6 taken 2763 times.
✗ Branch 5 → 24 not taken.
✓ Branch 6 → 7 taken 2763 times.
✗ Branch 6 → 24 not taken.
✓ Branch 7 → 8 taken 2763 times.
✗ Branch 7 → 24 not taken.
✓ Branch 13 → 9 taken 2730 times.
✓ Branch 13 → 14 taken 2762 times.
5492 for (SourceFile *sourceFile : dependencies | std::views::values)
291
2/2
✓ Branch 10 → 11 taken 2729 times.
✓ Branch 10 → 24 taken 1 time.
2730 sourceFile->runTypeCheckerPre();
292
293
1/2
✓ Branch 14 → 15 taken 2762 times.
✗ Branch 14 → 30 not taken.
2762 Timer timer(&compilerOutput.times.typeCheckerPre);
294
1/2
✓ Branch 15 → 16 taken 2762 times.
✗ Branch 15 → 30 not taken.
2762 timer.start();
295
296 // Then type-check the current file
297
1/2
✓ Branch 16 → 17 taken 2762 times.
✗ Branch 16 → 30 not taken.
2762 TypeChecker typeChecker(resourceManager, this, TC_MODE_PRE);
298
2/2
✓ Branch 17 → 18 taken 2747 times.
✓ Branch 17 → 25 taken 15 times.
2762 typeChecker.visit(ast);
299
300 2747 previousStage = TYPE_CHECKER_PRE;
301 2747 typeCheckerPreRunning = false;
302
1/2
✓ Branch 19 → 20 taken 2747 times.
✗ Branch 19 → 28 not taken.
2747 timer.stop();
303
1/2
✓ Branch 20 → 21 taken 2747 times.
✗ Branch 20 → 26 not taken.
2747 printStatusMessage("Type Checker Pre", IO_AST, IO_AST, compilerOutput.times.typeCheckerPre);
304 2762 }
305
306 11035 void SourceFile::runTypeCheckerPost() { // NOLINT(misc-no-recursion)
307 // Re-entrancy guard: within an import cycle, a dependency's post-run recurses back into this file's post-run. The
308 // in-flight fixpoint loop below already revisits this file, so the nested call must be a no-op to avoid unbounded
309 // mutual recursion. Convergence is driven by the reVisitRequested flags propagating across the cycle.
310
2/2
✓ Branch 2 → 3 taken 266 times.
✓ Branch 2 → 4 taken 10769 times.
11035 if (typeCheckerPostRunning)
311 3584 return;
312
313 // Skip if not all dependants finished type checking yet. This still has to run for files restored from the cache,
314 // for the same reason as runTypeCheckerPre (see comment there).
315
3/4
✓ Branch 4 → 5 taken 10769 times.
✗ Branch 4 → 80 not taken.
✓ Branch 5 → 6 taken 3318 times.
✓ Branch 5 → 7 taken 7451 times.
10769 if (!haveAllDependantsBeenTypeChecked())
316 3318 return;
317
318 7451 typeCheckerPostRunning = true;
319
320
1/2
✓ Branch 7 → 8 taken 7451 times.
✗ Branch 7 → 80 not taken.
7451 Timer timer(&compilerOutput.times.typeCheckerPost);
321
1/2
✓ Branch 8 → 9 taken 7451 times.
✗ Branch 8 → 80 not taken.
7451 timer.start();
322
323 // Start type-checking loop. The type-checker can request a re-execution. The max number of type-checker runs is limited
324
1/2
✓ Branch 9 → 10 taken 7451 times.
✗ Branch 9 → 80 not taken.
7451 TypeChecker typeChecker(resourceManager, this, TC_MODE_POST);
325 7451 unsigned short typeCheckerRuns = 0;
326
2/2
✓ Branch 25 → 11 taken 4212 times.
✓ Branch 25 → 26 taken 7375 times.
11587 while (reVisitRequested) {
327 4212 typeCheckerRuns++;
328 4212 totalTypeCheckerRuns++;
329 4212 reVisitRequested = false;
330
331 // Type-check the current file first. Multiple times, if requested
332 4212 timer.resume();
333
2/2
✓ Branch 12 → 13 taken 4180 times.
✓ Branch 12 → 58 taken 32 times.
4212 typeChecker.visit(ast);
334
1/2
✓ Branch 14 → 15 taken 4180 times.
✗ Branch 14 → 78 not taken.
4180 timer.pause();
335
336 // Then type-check all dependencies
337
5/8
✓ Branch 15 → 16 taken 4180 times.
✗ Branch 15 → 59 not taken.
✓ Branch 16 → 17 taken 4180 times.
✗ Branch 16 → 59 not taken.
✓ Branch 17 → 18 taken 4180 times.
✗ Branch 17 → 59 not taken.
✓ Branch 23 → 19 taken 10453 times.
✓ Branch 23 → 24 taken 4136 times.
14589 for (SourceFile *sourceFile : dependencies | std::views::values)
338
2/2
✓ Branch 20 → 21 taken 10409 times.
✓ Branch 20 → 59 taken 44 times.
10453 sourceFile->runTypeCheckerPost();
339 }
340
341 7375 typeCheckerPostRunning = false;
342
343
2/2
✓ Branch 26 → 27 taken 7256 times.
✓ Branch 26 → 78 taken 119 times.
7375 checkForSoftErrors();
344
345 // Check if all dyn variables were type-inferred successfully
346
2/2
✓ Branch 28 → 29 taken 7255 times.
✓ Branch 28 → 78 taken 1 time.
7256 globalScope->ensureSuccessfulTypeInference();
347
348 #ifndef NDEBUG
349 // In debug builds, verify that the TypeChecker fully annotated the AST
350
1/2
✓ Branch 29 → 30 taken 7255 times.
✗ Branch 29 → 78 not taken.
7255 runPostTypeCheckingVerifier();
351 #endif
352
353 7255 previousStage = TYPE_CHECKER_POST;
354
1/2
✓ Branch 30 → 31 taken 7255 times.
✗ Branch 30 → 78 not taken.
7255 timer.stop();
355
1/2
✓ Branch 31 → 32 taken 7255 times.
✗ Branch 31 → 60 not taken.
7255 printStatusMessage("Type Checker Post", IO_AST, IO_AST, compilerOutput.times.typeCheckerPost, typeCheckerRuns);
356
357 // Save the JSON version in the compiler output
358
3/4
✓ Branch 32 → 33 taken 7255 times.
✗ Branch 32 → 34 not taken.
✓ Branch 33 → 34 taken 7248 times.
✓ Branch 33 → 41 taken 7 times.
7255 if (cliOptions.dump.dumpSymbolTable || cliOptions.testMode)
359
2/4
✓ Branch 35 → 36 taken 7248 times.
✗ Branch 35 → 64 not taken.
✓ Branch 36 → 37 taken 7248 times.
✗ Branch 36 → 62 not taken.
7248 compilerOutput.symbolTableString = globalScope->getSymbolTableJSON().dump(/*indent=*/2);
360
361 // Dump symbol table
362
1/2
✗ Branch 41 → 42 not taken.
✓ Branch 41 → 54 taken 7255 times.
7255 if (cliOptions.dump.dumpSymbolTable)
363 dumpOutput(compilerOutput.symbolTableString, "Symbol Table", "symbol-table.json");
364 7451 }
365
366 7255 void SourceFile::runPostTypeCheckingVerifier() {
367
1/2
✓ Branch 2 → 3 taken 7255 times.
✗ Branch 2 → 8 not taken.
7255 PostTypeCheckingVerifier verifier(resourceManager, this);
368
1/2
✓ Branch 3 → 4 taken 7255 times.
✗ Branch 3 → 6 not taken.
7255 verifier.verify(ast);
369 7255 }
370
371 423 void SourceFile::runDependencyGraphVisualizer() {
372 // Only execute if enabled
373
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 423 times.
423 if (restoredFromCache)
374 7 return;
375
3/4
✓ Branch 4 → 5 taken 423 times.
✗ Branch 4 → 7 not taken.
✓ Branch 5 → 6 taken 5 times.
✓ Branch 5 → 7 taken 418 times.
423 if (!cliOptions.dump.dumpDependencyGraph && !cliOptions.testMode)
376 5 return;
377 // Check if this stage has already been done
378
2/2
✓ Branch 7 → 8 taken 2 times.
✓ Branch 7 → 9 taken 416 times.
418 if (previousStage >= DEP_GRAPH_VISUALIZER)
379 2 return;
380
381
1/2
✓ Branch 9 → 10 taken 416 times.
✗ Branch 9 → 47 not taken.
416 Timer timer(&compilerOutput.times.depGraphVisualizer);
382
1/2
✓ Branch 10 → 11 taken 416 times.
✗ Branch 10 → 47 not taken.
416 timer.start();
383
384 // Generate dot code for this source file
385
1/2
✓ Branch 11 → 12 taken 416 times.
✗ Branch 11 → 47 not taken.
416 std::stringstream dotCode;
386
1/2
✓ Branch 12 → 13 taken 416 times.
✗ Branch 12 → 45 not taken.
416 visualizerPreamble(dotCode);
387
1/2
✓ Branch 13 → 14 taken 416 times.
✗ Branch 13 → 45 not taken.
416 DependencyGraphVisualizer depGraphVisualizer(resourceManager, this);
388
1/2
✓ Branch 14 → 15 taken 416 times.
✗ Branch 14 → 43 not taken.
416 depGraphVisualizer.getDependencyGraph(dotCode);
389
1/2
✓ Branch 15 → 16 taken 416 times.
✗ Branch 15 → 43 not taken.
416 dotCode << "}";
390
391 // Dump the serialized AST string and the SVG file
392
1/2
✓ Branch 16 → 17 taken 416 times.
✗ Branch 16 → 34 not taken.
416 compilerOutput.depGraphString = dotCode.str();
393
394
1/2
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 27 taken 416 times.
416 if (cliOptions.dump.dumpDependencyGraph)
395 visualizerOutput("Dependency Graph", compilerOutput.depGraphString);
396
397 416 previousStage = DEP_GRAPH_VISUALIZER;
398
1/2
✓ Branch 27 → 28 taken 416 times.
✗ Branch 27 → 43 not taken.
416 timer.stop();
399
1/2
✓ Branch 28 → 29 taken 416 times.
✗ Branch 28 → 41 not taken.
416 printStatusMessage("AST Visualizer", IO_AST, IO_AST, compilerOutput.times.depGraphVisualizer);
400 416 }
401
402 2564 void SourceFile::runIRGenerator() {
403 // Skip if restored from the cache or this stage has already been done
404
4/4
✓ Branch 2 → 3 taken 2563 times.
✓ Branch 2 → 4 taken 1 time.
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 2562 times.
2564 if (restoredFromCache || previousStage >= IR_GENERATOR)
405 2 return;
406
407
1/2
✓ Branch 5 → 6 taken 2562 times.
✗ Branch 5 → 60 not taken.
2562 Timer timer(&compilerOutput.times.irGenerator);
408
1/2
✓ Branch 6 → 7 taken 2562 times.
✗ Branch 6 → 60 not taken.
2562 timer.start();
409
410 // Create the LLVM module for this source file
411
2/2
✓ Branch 7 → 8 taken 4 times.
✓ Branch 7 → 9 taken 2558 times.
2562 llvm::LLVMContext &llvmContext = cliOptions.useLTO ? resourceManager.ltoContext : context;
412
1/2
✓ Branch 10 → 11 taken 2562 times.
✗ Branch 10 → 41 not taken.
2562 llvmModule = std::make_unique<llvm::Module>(fileName, llvmContext);
413
414 // Generate this source file
415
1/2
✓ Branch 13 → 14 taken 2562 times.
✗ Branch 13 → 60 not taken.
2562 IRGenerator irGenerator(resourceManager, this);
416
1/2
✓ Branch 14 → 15 taken 2562 times.
✗ Branch 14 → 42 not taken.
2562 irGenerator.visit(ast);
417
418 // Save the ir string in the compiler output
419
3/4
✓ Branch 16 → 17 taken 2562 times.
✗ Branch 16 → 18 not taken.
✓ Branch 17 → 18 taken 2559 times.
✓ Branch 17 → 23 taken 3 times.
2562 if (cliOptions.dump.dumpIR || cliOptions.testMode)
420
1/2
✓ Branch 19 → 20 taken 2559 times.
✗ Branch 19 → 43 not taken.
2559 compilerOutput.irString = IRGenerator::getIRString(llvmModule.get(), cliOptions);
421
422 // Dump unoptimized IR code
423
1/2
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 36 taken 2562 times.
2562 if (cliOptions.dump.dumpIR)
424 dumpOutput(compilerOutput.irString, "Unoptimized IR Code", "ir-code.ll");
425
426 2562 previousStage = IR_GENERATOR;
427
1/2
✓ Branch 36 → 37 taken 2562 times.
✗ Branch 36 → 58 not taken.
2562 timer.stop();
428
1/2
✓ Branch 37 → 38 taken 2562 times.
✗ Branch 37 → 56 not taken.
2562 printStatusMessage("IR Generator", IO_AST, IO_IR, compilerOutput.times.irGenerator);
429 2562 }
430
431 2320 void SourceFile::runDefaultIROptimizer() {
432
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 2320 times.
2320 assert(!cliOptions.useLTO);
433
434 // Skip if restored from the cache or this stage has already been done
435
4/8
✓ Branch 4 → 5 taken 2319 times.
✓ Branch 4 → 8 taken 1 time.
✓ Branch 5 → 6 taken 2319 times.
✗ Branch 5 → 8 not taken.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 9 taken 2319 times.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 9 not taken.
2320 if (restoredFromCache || previousStage > IR_OPTIMIZER || (previousStage == IR_OPTIMIZER && !cliOptions.testMode))
436 1 return;
437
438
1/2
✓ Branch 9 → 10 taken 2319 times.
✗ Branch 9 → 60 not taken.
2319 Timer timer(&compilerOutput.times.irOptimizer);
439
1/2
✓ Branch 10 → 11 taken 2319 times.
✗ Branch 10 → 60 not taken.
2319 timer.start();
440
441 // Optimize this source file
442
1/2
✓ Branch 11 → 12 taken 2319 times.
✗ Branch 11 → 60 not taken.
2319 IROptimizer irOptimizer(resourceManager, this);
443
1/2
✓ Branch 12 → 13 taken 2319 times.
✗ Branch 12 → 58 not taken.
2319 irOptimizer.prepare();
444
1/2
✓ Branch 13 → 14 taken 2319 times.
✗ Branch 13 → 58 not taken.
2319 irOptimizer.optimizeDefault();
445
446 // Save the optimized ir string in the compiler output
447
3/4
✓ Branch 14 → 15 taken 2319 times.
✗ Branch 14 → 16 not taken.
✓ Branch 15 → 16 taken 2316 times.
✓ Branch 15 → 21 taken 3 times.
2319 if (cliOptions.dump.dumpIR || cliOptions.testMode)
448
1/2
✓ Branch 17 → 18 taken 2316 times.
✗ Branch 17 → 40 not taken.
2316 compilerOutput.irOptString = IRGenerator::getIRString(llvmModule.get(), cliOptions);
449
450 // Dump optimized IR code
451
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 35 taken 2319 times.
2319 if (cliOptions.dump.dumpIR)
452 dumpOutput(compilerOutput.irOptString, "Optimized IR Code",
453 "ir-code-O" + std::to_string(static_cast<uint8_t>(cliOptions.optLevel)) + ".ll");
454
455 2319 previousStage = IR_OPTIMIZER;
456
1/2
✓ Branch 35 → 36 taken 2319 times.
✗ Branch 35 → 58 not taken.
2319 timer.stop();
457
1/2
✓ Branch 36 → 37 taken 2319 times.
✗ Branch 36 → 56 not taken.
2319 printStatusMessage("IR Optimizer", IO_IR, IO_IR, compilerOutput.times.irOptimizer);
458 2319 }
459
460 3 void SourceFile::runPreLinkIROptimizer() {
461
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 3 times.
3 assert(cliOptions.useLTO);
462
463 // Skip if restored from the cache or this stage has already been done
464
2/4
✓ Branch 4 → 5 taken 3 times.
✗ Branch 4 → 6 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 3 times.
3 if (restoredFromCache || previousStage >= IR_OPTIMIZER)
465 return;
466
467
1/2
✓ Branch 7 → 8 taken 3 times.
✗ Branch 7 → 51 not taken.
3 Timer timer(&compilerOutput.times.irOptimizer);
468
1/2
✓ Branch 8 → 9 taken 3 times.
✗ Branch 8 → 51 not taken.
3 timer.start();
469
470 // Optimize this source file
471
1/2
✓ Branch 9 → 10 taken 3 times.
✗ Branch 9 → 51 not taken.
3 IROptimizer irOptimizer(resourceManager, this);
472
1/2
✓ Branch 10 → 11 taken 3 times.
✗ Branch 10 → 49 not taken.
3 irOptimizer.prepare();
473
1/2
✓ Branch 11 → 12 taken 3 times.
✗ Branch 11 → 49 not taken.
3 irOptimizer.optimizePreLink();
474
475 // Save the optimized ir string in the compiler output
476
2/4
✓ Branch 12 → 13 taken 3 times.
✗ Branch 12 → 14 not taken.
✓ Branch 13 → 14 taken 3 times.
✗ Branch 13 → 19 not taken.
3 if (cliOptions.dump.dumpIR || cliOptions.testMode)
477
1/2
✓ Branch 15 → 16 taken 3 times.
✗ Branch 15 → 36 not taken.
3 compilerOutput.irOptString = IRGenerator::getIRString(llvmModule.get(), cliOptions);
478
479 // Dump optimized IR code
480
1/2
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 32 taken 3 times.
3 if (cliOptions.dump.dumpIR)
481 dumpOutput(compilerOutput.irOptString, "Optimized IR Code (pre-link)", "ir-code-lto-pre-link.ll");
482
483
1/2
✓ Branch 32 → 33 taken 3 times.
✗ Branch 32 → 49 not taken.
3 timer.pause();
484 3 }
485
486 2 void SourceFile::runBitcodeLinker() {
487
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 2 times.
2 assert(cliOptions.useLTO);
488
489 // Skip if this is not the main source file
490
2/2
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 1 time.
2 if (!isMainFile)
491 1 return;
492
493 // Skip if restored from the cache or this stage has already been done
494
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)
495 return;
496
497
1/2
✓ Branch 9 → 10 taken 1 time.
✗ Branch 9 → 20 not taken.
1 Timer timer(&compilerOutput.times.irOptimizer);
498 1 timer.resume();
499
500 // Link all source files together
501
1/2
✓ Branch 11 → 12 taken 1 time.
✗ Branch 11 → 20 not taken.
1 BitcodeLinker linker(resourceManager);
502
1/2
✓ Branch 12 → 13 taken 1 time.
✗ Branch 12 → 18 not taken.
1 linker.link();
503
504
1/2
✓ Branch 13 → 14 taken 1 time.
✗ Branch 13 → 18 not taken.
1 timer.pause();
505 1 }
506
507 2 void SourceFile::runPostLinkIROptimizer() {
508
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 2 times.
2 assert(cliOptions.useLTO);
509
510 // Skip if this is not the main source file
511
2/2
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 1 time.
2 if (!isMainFile)
512 1 return;
513
514 // Skip if restored from the cache or this stage has already been done
515
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)
516 return;
517
518
1/2
✓ Branch 9 → 10 taken 1 time.
✗ Branch 9 → 57 not taken.
1 Timer timer(&compilerOutput.times.irOptimizer);
519 1 timer.resume();
520
521 // Optimize LTO module
522
1/2
✓ Branch 11 → 12 taken 1 time.
✗ Branch 11 → 57 not taken.
1 IROptimizer irOptimizer(resourceManager, this);
523
1/2
✓ Branch 12 → 13 taken 1 time.
✗ Branch 12 → 55 not taken.
1 irOptimizer.prepare();
524
1/2
✓ Branch 13 → 14 taken 1 time.
✗ Branch 13 → 55 not taken.
1 irOptimizer.optimizePostLink();
525
526 // Save the optimized ir string in the compiler output
527
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) {
528 1 llvm::Module *module = resourceManager.ltoModule.get();
529
1/2
✓ Branch 17 → 18 taken 1 time.
✗ Branch 17 → 40 not taken.
1 compilerOutput.irOptString = IRGenerator::getIRString(module, cliOptions);
530 }
531
532 // Dump optimized IR code
533
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 34 taken 1 time.
1 if (cliOptions.dump.dumpIR)
534 dumpOutput(compilerOutput.irOptString, "Optimized IR Code (post-Link)", "ir-code-lto-post-link.ll");
535
536 1 previousStage = IR_OPTIMIZER;
537
1/2
✓ Branch 34 → 35 taken 1 time.
✗ Branch 34 → 55 not taken.
1 timer.stop();
538
1/2
✓ Branch 35 → 36 taken 1 time.
✗ Branch 35 → 53 not taken.
1 printStatusMessage("IR Optimizer", IO_IR, IO_IR, compilerOutput.times.irOptimizer);
539 1 }
540
541 2473 void SourceFile::runObjectEmitter() {
542 // Skip if restored from the cache or this stage has already been done
543
4/4
✓ Branch 2 → 3 taken 2472 times.
✓ Branch 2 → 4 taken 1 time.
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 2471 times.
2473 if (restoredFromCache || previousStage >= OBJECT_EMITTER)
544 3 return;
545
546 // Skip if LTO is enabled and this is not the main source file
547
4/4
✓ Branch 5 → 6 taken 2 times.
✓ Branch 5 → 8 taken 2469 times.
✓ Branch 6 → 7 taken 1 time.
✓ Branch 6 → 8 taken 1 time.
2471 if (cliOptions.useLTO && !isMainFile)
548 1 return;
549
550
1/2
✓ Branch 8 → 9 taken 2470 times.
✗ Branch 8 → 82 not taken.
2470 Timer timer(&compilerOutput.times.objectEmitter);
551
1/2
✓ Branch 9 → 10 taken 2470 times.
✗ Branch 9 → 82 not taken.
2470 timer.start();
552
553 // Deduce an object file path
554
2/4
✓ Branch 10 → 11 taken 2470 times.
✗ Branch 10 → 58 not taken.
✓ Branch 11 → 12 taken 2470 times.
✗ Branch 11 → 56 not taken.
2470 objectFilePath = cliOptions.outputDir / filePath.filename();
555
2/4
✓ Branch 15 → 16 taken 2470 times.
✗ Branch 15 → 62 not taken.
✓ Branch 16 → 17 taken 2470 times.
✗ Branch 16 → 60 not taken.
2470 objectFilePath.replace_extension("o");
556
557 // Pick a concrete emitter based on the selected backend. The TPDE emitter is compiled into a
558 // sibling library (spice_tpde) that keeps its -fno-rtti requirement out of spicecore; the
559 // AbstractObjectEmitter base gives us a single interface both branches produce.
560 2470 std::unique_ptr<AbstractObjectEmitter> objectEmitter;
561 #ifdef SPICE_ENABLE_TPDE
562
2/2
✓ Branch 18 → 19 taken 1 time.
✓ Branch 18 → 26 taken 2469 times.
2470 if (cliOptions.backend == Backend::TPDE) {
563
1/2
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 21 taken 1 time.
1 llvm::Module &module = cliOptions.useLTO ? *resourceManager.ltoModule : *llvmModule;
564
1/2
✓ Branch 22 → 23 taken 1 time.
✗ Branch 22 → 63 not taken.
1 objectEmitter = std::make_unique<TPDEObjectEmitter>(module);
565 } else {
566
1/2
✓ Branch 26 → 27 taken 2469 times.
✗ Branch 26 → 64 not taken.
2469 objectEmitter = std::make_unique<LLVMObjectEmitter>(resourceManager, this);
567 }
568 #else
569 objectEmitter = std::make_unique<LLVMObjectEmitter>(resourceManager, this);
570 #endif
571
572 // Emit object for this source file
573
1/2
✓ Branch 31 → 32 taken 2470 times.
✗ Branch 31 → 80 not taken.
2470 objectEmitter->emit(objectFilePath);
574
575 // Save assembly string in the compiler output (TPDE emits a placeholder note)
576
5/6
✓ Branch 32 → 33 taken 2426 times.
✓ Branch 32 → 37 taken 44 times.
✓ Branch 33 → 34 taken 2426 times.
✗ Branch 33 → 35 not taken.
✓ Branch 34 → 35 taken 2423 times.
✓ Branch 34 → 37 taken 3 times.
2470 if (cliOptions.isNativeTarget && (cliOptions.dump.dumpAssembly || cliOptions.testMode))
577
1/2
✓ Branch 36 → 37 taken 2423 times.
✗ Branch 36 → 80 not taken.
2423 objectEmitter->getASMString(compilerOutput.asmString);
578
579 // Dump assembly code
580
1/2
✗ Branch 37 → 38 not taken.
✓ Branch 37 → 50 taken 2470 times.
2470 if (cliOptions.dump.dumpAssembly)
581 dumpOutput(compilerOutput.asmString, "Assembly code", "assembly-code.s");
582
583 // The object file is registered with the linker in concludeCompilation and not here, because this stage may run on a
584 // worker thread of the parallel back end and the linker input order has to stay deterministic.
585
586 2470 previousStage = OBJECT_EMITTER;
587
1/2
✓ Branch 50 → 51 taken 2470 times.
✗ Branch 50 → 80 not taken.
2470 timer.stop();
588
1/2
✓ Branch 51 → 52 taken 2470 times.
✗ Branch 51 → 78 not taken.
2470 printStatusMessage("Object Emitter", IO_IR, IO_OBJECT_FILE, compilerOutput.times.objectEmitter);
589 2470 }
590
591 2473 void SourceFile::concludeCompilation() {
592 // Handle cache-restored files: register all cached objects with linker
593
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 51 taken 2472 times.
2473 if (restoredFromCache) {
594
2/2
✓ Branch 17 → 5 taken 1 time.
✓ Branch 17 → 18 taken 1 time.
3 for (const auto &cachedObjectFilePath : cachedObjectFilePaths)
595
1/2
✓ Branch 7 → 8 taken 1 time.
✗ Branch 7 → 103 not taken.
1 resourceManager.linker.addFileToLinkage(cachedObjectFilePath);
596
1/2
✗ Branch 32 → 20 not taken.
✓ Branch 32 → 33 taken 1 time.
2 for (const auto &flag : sourceLinkerFlags)
597 resourceManager.linker.addLinkerFlag(flag);
598
1/2
✗ Branch 49 → 35 not taken.
✓ Branch 49 → 50 taken 1 time.
2 for (const auto &path : sourceAdditionalSourcePaths)
599 resourceManager.linker.addAdditionalSourcePath(path);
600 1 return;
601 }
602
603
2/2
✓ Branch 51 → 52 taken 1 time.
✓ Branch 51 → 53 taken 2471 times.
2472 if (previousStage >= FINISHED)
604 1 return;
605
606 // Add the emitted object file to the linker objects. This happens here and not in runObjectEmitter, because
607 // concludeCompilation is always driven serially and in dependency order, while the object emitter may run on a worker
608 // thread of the parallel back end.
609
2/2
✓ Branch 54 → 55 taken 2470 times.
✓ Branch 54 → 56 taken 1 time.
2471 if (!objectFilePath.empty())
610 2470 resourceManager.linker.addFileToLinkage(objectFilePath);
611
612 // Cache the source file
613
2/2
✓ Branch 56 → 57 taken 3 times.
✓ Branch 56 → 58 taken 2468 times.
2471 if (!cliOptions.ignoreCache)
614 3 resourceManager.cacheManager.cacheSourceFile(this);
615
616 // Save type registry as string in the compiler output
617
5/6
✓ Branch 58 → 59 taken 367 times.
✓ Branch 58 → 65 taken 2104 times.
✓ Branch 59 → 60 taken 367 times.
✗ Branch 59 → 61 not taken.
✓ Branch 60 → 61 taken 365 times.
✓ Branch 60 → 65 taken 2 times.
2471 if (isMainFile && (cliOptions.dump.dumpTypes || cliOptions.testMode))
618
1/2
✓ Branch 61 → 62 taken 365 times.
✗ Branch 61 → 109 not taken.
365 compilerOutput.typesString = TypeRegistry::dump();
619
620 // Dump type registry
621
3/4
✓ Branch 65 → 66 taken 367 times.
✓ Branch 65 → 79 taken 2104 times.
✗ Branch 66 → 67 not taken.
✓ Branch 66 → 79 taken 367 times.
2471 if (isMainFile && cliOptions.dump.dumpTypes)
622 dumpOutput(compilerOutput.typesString, "Type Registry", "type-registry.out");
623
624 // Save cache statistics as string in the compiler output
625
5/6
✓ Branch 79 → 80 taken 367 times.
✓ Branch 79 → 83 taken 2104 times.
✓ Branch 80 → 81 taken 367 times.
✗ Branch 80 → 82 not taken.
✓ Branch 81 → 82 taken 365 times.
✓ Branch 81 → 83 taken 2 times.
2471 if (isMainFile && (cliOptions.dump.dumpCacheStats || cliOptions.testMode))
626 365 dumpCacheStats();
627
628 // Dump lookup cache statistics
629
3/4
✓ Branch 83 → 84 taken 367 times.
✓ Branch 83 → 97 taken 2104 times.
✗ Branch 84 → 85 not taken.
✓ Branch 84 → 97 taken 367 times.
2471 if (isMainFile && cliOptions.dump.dumpCacheStats)
630 dumpOutput(compilerOutput.cacheStats, "Cache Statistics", "cache-stats.out");
631
632
1/2
✗ Branch 97 → 98 not taken.
✓ Branch 97 → 101 taken 2471 times.
2471 if (cliOptions.printDebugOutput)
633 std::cout << "Finished compiling " << fileName << std::endl;
634
635 2471 previousStage = FINISHED;
636 }
637
638 3530 void SourceFile::runFrontEnd() { // NOLINT(misc-no-recursion)
639 3530 runLexer();
640
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 3530 times.
3530 CHECK_ABORT_FLAG_V()
641 3530 runParser();
642
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 3530 times.
3530 CHECK_ABORT_FLAG_V()
643 3530 runCSTVisualizer();
644
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 3530 times.
3530 CHECK_ABORT_FLAG_V()
645 3530 runASTBuilder();
646
1/2
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 3530 times.
3530 CHECK_ABORT_FLAG_V()
647 3530 runASTVisualizer();
648
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 3530 times.
3530 CHECK_ABORT_FLAG_V()
649 3530 runImportCollector();
650
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 3530 times.
3530 CHECK_ABORT_FLAG_V()
651 3530 runSymbolTableBuilder();
652
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 3530 times.
3530 CHECK_ABORT_FLAG_V()
653 }
654
655 588 void SourceFile::runMiddleEnd() {
656 // Merge the exported name registries of all (transitive) dependencies into the respective importing files. This is
657 // the deferred tail of the front-end: it must run after every reachable file has built its own registry, which is
658 // why it cannot live inside the per-file front-end recursion (a circular import would otherwise merge a dependency
659 // whose registry is not populated yet). runMiddleEnd is the first stage that is only ever invoked at the top level.
660
1/2
✓ Branch 2 → 3 taken 588 times.
✗ Branch 2 → 72 not taken.
588 mergeNameRegistriesRecursive();
661
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 588 times.
588 CHECK_ABORT_FLAG_V()
662 // From here on, struct manifestations may be substantiated, and each of them gets its compiler-generated default
663 // members decided right at that point (see TypeChecker::createImplicitDefaultMembers). Once the middle end is done,
664 // every manifestation exists and was decided on, so the back end must not create any more of them.
665 588 const DefaultMemberCreationSection defaultMemberCreationSection;
666 // We need two runs here due to generics.
667 // The first run to determine all concrete function/struct/interface substantiations
668
2/2
✓ Branch 7 → 8 taken 573 times.
✓ Branch 7 → 70 taken 15 times.
588 runTypeCheckerPre(); // Visit the dependency tree from bottom to top
669
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 573 times.
573 CHECK_ABORT_FLAG_V()
670 // The second run to ensure, also generic scopes are type-checked properly
671
2/2
✓ Branch 11 → 12 taken 421 times.
✓ Branch 11 → 70 taken 152 times.
573 runTypeCheckerPost(); // Visit the dependency tree from top to bottom in topological order
672
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 421 times.
421 CHECK_ABORT_FLAG_V()
673 // The per-file convergence loop inside runTypeCheckerPost is scoped to its own call stack: a cross-file revisit
674 // request that lands on a file whose loop already unwound (e.g. a recursive generic dtor chain that closes back
675 // through a runtime module which was itself gated behind another, not-yet-checked importer) is otherwise dropped,
676 // leaving a fully-substantiated manifestation whose body was never type-checked. Sweep every source file in the
677 // program for a straggling revisit request and drive it to convergence directly, repeating until none are left.
678 bool anySourceFileRevisitPending;
679
2/2
✓ Branch 49 → 15 taken 7 times.
✓ Branch 49 → 50 taken 421 times.
428 do {
680 428 anySourceFileRevisitPending = false;
681 // Snapshot the current files before driving any of them: runTypeCheckerPost() below can itself trigger a
682 // freshly-discovered runtime import (SourceFile::requestRuntimeModule -> GlobalResourceManager::createSourceFile),
683 // which inserts into resourceManager.sourceFiles - iterating that map while it is being mutated is undefined
684 // behavior, so a stable list of raw pointers is collected first.
685 428 std::vector<SourceFile *> sourceFilesSnapshot;
686
1/2
✓ Branch 16 → 17 taken 428 times.
✗ Branch 16 → 67 not taken.
428 sourceFilesSnapshot.reserve(resourceManager.sourceFiles.size());
687
5/8
✓ Branch 17 → 18 taken 428 times.
✗ Branch 17 → 65 not taken.
✓ Branch 18 → 19 taken 428 times.
✗ Branch 18 → 65 not taken.
✓ Branch 19 → 20 taken 428 times.
✗ Branch 19 → 65 not taken.
✓ Branch 26 → 21 taken 2841 times.
✓ Branch 26 → 27 taken 428 times.
3269 for (const std::unique_ptr<SourceFile> &sourceFile : resourceManager.sourceFiles | std::views::values)
688
1/2
✓ Branch 23 → 24 taken 2841 times.
✗ Branch 23 → 64 not taken.
2841 sourceFilesSnapshot.push_back(sourceFile.get());
689
2/2
✓ Branch 43 → 29 taken 2841 times.
✓ Branch 43 → 44 taken 428 times.
3697 for (SourceFile *sourceFile : sourceFilesSnapshot) {
690
2/2
✓ Branch 31 → 32 taken 9 times.
✓ Branch 31 → 34 taken 2832 times.
2841 if (sourceFile->reVisitRequested) {
691
1/2
✓ Branch 32 → 33 taken 9 times.
✗ Branch 32 → 66 not taken.
9 sourceFile->runTypeCheckerPost();
692 9 anySourceFileRevisitPending = true;
693 }
694 }
695 // A source file created mid-sweep (see above) still needs its own pass; it starts out with reVisitRequested
696 // true, so re-looping picks it up via the snapshot taken on the next iteration.
697
1/2
✗ Branch 46 → 47 not taken.
✓ Branch 46 → 48 taken 428 times.
428 if (resourceManager.sourceFiles.size() > sourceFilesSnapshot.size())
698 anySourceFileRevisitPending = true;
699 428 } while (anySourceFileRevisitPending);
700
1/2
✗ Branch 51 → 52 not taken.
✓ Branch 51 → 53 taken 421 times.
421 CHECK_ABORT_FLAG_V()
701 // Visualize dependency graph
702
1/2
✓ Branch 53 → 54 taken 421 times.
✗ Branch 53 → 70 not taken.
421 runDependencyGraphVisualizer();
703
1/2
✗ Branch 55 → 56 not taken.
✓ Branch 55 → 57 taken 421 times.
421 CHECK_ABORT_FLAG_V()
704
1/2
✓ Branch 59 → 60 taken 421 times.
✗ Branch 59 → 62 not taken.
588 }
705
706 5395 void SourceFile::collectBackEndSourceFiles(std::vector<SourceFile *> &backEndSourceFiles) { // NOLINT(misc-no-recursion)
707 // Guard against collecting a file that already went through the back end. Circular imports form a cycle in the
708 // dependency graph, so the deps-first recursion below would otherwise loop forever.
709
2/2
✓ Branch 2 → 3 taken 3287 times.
✓ Branch 2 → 4 taken 2108 times.
5395 if (backEndStarted)
710 3287 return;
711 2108 backEndStarted = true;
712
713 // Collect all dependencies first, so that they end up in front of this file in the resulting list
714
5/8
✓ Branch 4 → 5 taken 2108 times.
✗ Branch 4 → 16 not taken.
✓ Branch 5 → 6 taken 2108 times.
✗ Branch 5 → 16 not taken.
✓ Branch 6 → 7 taken 2108 times.
✗ Branch 6 → 16 not taken.
✓ Branch 12 → 8 taken 4902 times.
✓ Branch 12 → 13 taken 2108 times.
7010 for (SourceFile *sourceFile : dependencies | std::views::values)
715
1/2
✓ Branch 9 → 10 taken 4902 times.
✗ Branch 9 → 16 not taken.
4902 sourceFile->collectBackEndSourceFiles(backEndSourceFiles);
716
717
1/2
✓ Branch 13 → 14 taken 2108 times.
✗ Branch 13 → 17 not taken.
2108 backEndSourceFiles.push_back(this);
718 }
719
720 2108 void SourceFile::runBackEndForThisFile() {
721 2108 runIRGenerator();
722
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 2108 times.
2108 CHECK_ABORT_FLAG_V()
723
2/2
✓ Branch 6 → 7 taken 1 time.
✓ Branch 6 → 19 taken 2107 times.
2108 if (cliOptions.useLTO) {
724 1 runPreLinkIROptimizer();
725
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 1 time.
1 CHECK_ABORT_FLAG_V()
726 1 runBitcodeLinker();
727
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 1 time.
1 CHECK_ABORT_FLAG_V()
728 1 runPostLinkIROptimizer();
729
1/2
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 23 taken 1 time.
1 CHECK_ABORT_FLAG_V()
730 } else {
731 2107 runDefaultIROptimizer();
732
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 2107 times.
2107 CHECK_ABORT_FLAG_V()
733 }
734 2108 runObjectEmitter();
735 }
736
737 493 void SourceFile::runBackEnd() {
738 // Flatten the dependency graph into the order the back end used to recurse in: every file comes after all of its
739 // dependencies, and files that already ran their back end are skipped.
740 493 std::vector<SourceFile *> backEndSourceFiles;
741
1/2
✓ Branch 2 → 3 taken 493 times.
✗ Branch 2 → 102 not taken.
493 collectBackEndSourceFiles(backEndSourceFiles);
742
743 // Nothing to do if this file and all of its dependencies already went through the back end
744
2/2
✓ Branch 4 → 5 taken 84 times.
✓ Branch 4 → 6 taken 409 times.
493 if (backEndSourceFiles.empty())
745 84 return;
746
747 // Unlike the front end and the middle end, the back end has no cross-file data dependencies: every source file owns
748 // its own LLVMContext, IRBuilder, TargetMachine and llvm::Module, and references to symbols of other files are emitted
749 // as declarations into the local module. So the per-file pipelines can simply be spread over a worker pool.
750 // Exceptions, in which the back end stays serial:
751 // - LTO, because all source files share the LTO context and module of the GlobalResourceManager
752 // - dump modes, because they write to the console/output dir and their ordering is part of the user-visible output
753
3/6
✓ Branch 6 → 7 taken 409 times.
✗ Branch 6 → 9 not taken.
✓ Branch 7 → 8 taken 409 times.
✗ Branch 7 → 9 not taken.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 409 times.
409 const bool dumpRequested = cliOptions.dump.dumpIR || cliOptions.dump.dumpAssembly || cliOptions.dump.dumpObjectFiles;
754
1/2
✓ Branch 12 → 13 taken 409 times.
✗ Branch 12 → 90 not taken.
409 const size_t jobCount = std::min(resourceManager.getCompileJobCount(), backEndSourceFiles.size());
755
1/6
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 18 taken 409 times.
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 18 not taken.
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 18 not taken.
409 const bool runParallel = jobCount > 1 && !cliOptions.useLTO && !dumpRequested;
756
757
1/2
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 42 taken 409 times.
409 if (runParallel) {
758 ThreadPool &threadPool = resourceManager.getThreadPool(jobCount);
759 const ParallelSection parallelSection;
760 for (SourceFile *sourceFile : backEndSourceFiles)
761 threadPool.submit([sourceFile] { sourceFile->runBackEndForThisFile(); });
762 threadPool.waitForAll(); // Re-throws the exception of the first failing source file, if there was one
763 } else {
764
2/2
✓ Branch 59 → 44 taken 2108 times.
✓ Branch 59 → 60 taken 409 times.
2926 for (SourceFile *sourceFile : backEndSourceFiles) {
765
1/2
✓ Branch 46 → 47 taken 2108 times.
✗ Branch 46 → 100 not taken.
2108 sourceFile->runBackEndForThisFile();
766
1/2
✗ Branch 48 → 49 not taken.
✓ Branch 48 → 50 taken 2108 times.
2108 CHECK_ABORT_FLAG_V()
767 }
768 }
769
1/2
✗ Branch 62 → 63 not taken.
✓ Branch 62 → 64 taken 409 times.
409 CHECK_ABORT_FLAG_V()
770
771 // Conclude the compilation of all source files. This registers the emitted object files with the linker and writes the
772 // compilation cache, both of which have to happen serially and in a fixed order to stay deterministic.
773
2/2
✓ Branch 78 → 66 taken 2108 times.
✓ Branch 78 → 79 taken 409 times.
2926 for (SourceFile *sourceFile : backEndSourceFiles)
774
1/2
✓ Branch 68 → 69 taken 2108 times.
✗ Branch 68 → 101 not taken.
2108 sourceFile->concludeCompilation();
775
776
2/2
✓ Branch 79 → 80 taken 2 times.
✓ Branch 79 → 83 taken 407 times.
409 if (isMainFile) {
777
1/2
✓ Branch 80 → 81 taken 2 times.
✗ Branch 80 → 102 not taken.
2 resourceManager.totalTimer.stop();
778
1/2
✗ Branch 81 → 82 not taken.
✓ Branch 81 → 83 taken 2 times.
2 if (cliOptions.printDebugOutput)
779 dumpCompilationStats();
780 }
781
2/2
✓ Branch 85 → 86 taken 409 times.
✓ Branch 85 → 88 taken 84 times.
493 }
782
783 5477 void SourceFile::addDependency(SourceFile *sourceFile, const std::string &dependencyName) {
784 // Circular imports are explicitly supported, so cycles are not rejected here. Source files are deduplicated by path
785 // in GlobalResourceManager::createSourceFile, so a cyclic import resolves to the same SourceFile instance and the
786 // pipeline drivers guard against re-entering a file that is already in progress.
787
788 // Add the dependency. Do not demote the compilation root (parent == nullptr) to a non-main file: with a circular
789 // import the root can be imported by one of its own transitive dependencies, yet it must remain the main file (the
790 // isMainFile flag drives getRootSourceFile, object emission, timing, etc.).
791
2/2
✓ Branch 2 → 3 taken 5476 times.
✓ Branch 2 → 4 taken 1 time.
5477 if (sourceFile->parent != nullptr)
792 5476 sourceFile->isMainFile = false;
793 5477 dependencies.emplace(dependencyName, sourceFile);
794
795 // Add the dependant
796
1/2
✓ Branch 5 → 6 taken 5477 times.
✗ Branch 5 → 7 not taken.
5477 sourceFile->dependants.push_back(this);
797 5477 }
798
799 365086 bool SourceFile::imports(const SourceFile *sourceFile) const {
800 1724846 return std::ranges::any_of(dependencies, [=](const auto &dependency) { return dependency.second == sourceFile; });
801 }
802
803 18806 SourceFile *SourceFile::requestRuntimeModule(RuntimeModule runtimeModule) {
804 // Check if the module was already imported
805
2/2
✓ Branch 3 → 4 taken 16062 times.
✓ Branch 3 → 6 taken 2744 times.
18806 if (isRuntimeModuleAvailable(runtimeModule))
806 16062 return resourceManager.runtimeModuleManager.getModule(runtimeModule);
807 2744 return resourceManager.runtimeModuleManager.requestModule(this, runtimeModule);
808 }
809
810 20973 bool SourceFile::isRuntimeModuleAvailable(RuntimeModule runtimeModule) const { return importedRuntimeModules & runtimeModule; }
811
812 191927 void SourceFile::addNameRegistryEntry(const std::string &symbolName, uint64_t typeId, SymbolTableEntry *entry, Scope *scope,
813 bool keepNewOnCollision, SymbolTableEntry *importEntry) {
814
6/6
✓ Branch 2 → 3 taken 88171 times.
✓ Branch 2 → 5 taken 103756 times.
✓ Branch 4 → 5 taken 87752 times.
✓ Branch 4 → 6 taken 419 times.
✓ Branch 7 → 8 taken 191508 times.
✓ Branch 7 → 15 taken 419 times.
191927 if (keepNewOnCollision || !exportedNameRegistry.contains(symbolName)) // Overwrite potential existing entry
815 383016 exportedNameRegistry[symbolName] = {symbolName, typeId, entry, scope, importEntry};
816 else // Name collision => we must remove the existing entry
817 419 exportedNameRegistry.erase(symbolName);
818
3/8
✓ Branch 8 → 9 taken 191508 times.
✗ Branch 8 → 22 not taken.
✓ Branch 9 → 10 taken 191508 times.
✗ Branch 9 → 17 not taken.
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 191508 times.
✗ Branch 19 → 20 not taken.
✗ Branch 19 → 21 not taken.
574943 }
819
820 1215821 const NameRegistryEntry *SourceFile::getNameRegistryEntry(const std::string &symbolName) const {
821
1/2
✓ Branch 2 → 3 taken 1215821 times.
✗ Branch 2 → 13 not taken.
1215821 const auto it = exportedNameRegistry.find(symbolName);
822
2/2
✓ Branch 5 → 6 taken 559385 times.
✓ Branch 5 → 7 taken 656436 times.
1215821 if (it == exportedNameRegistry.end())
823 559385 return nullptr;
824
825 // Resolve registry entry for the given name
826 656436 const NameRegistryEntry *entry = &it->second;
827
828 // Mark the import entry as used
829
2/2
✓ Branch 8 → 9 taken 79260 times.
✓ Branch 8 → 10 taken 577176 times.
656436 if (entry->importEntry != nullptr)
830 79260 entry->importEntry->used = true;
831
832 656436 return entry;
833 }
834
835 1334544 llvm::Type *SourceFile::getLLVMType(const Type *type) {
836 // Check if the type is already in the mapping
837
1/2
✓ Branch 2 → 3 taken 1334544 times.
✗ Branch 2 → 13 not taken.
1334544 const auto it = typeToLLVMTypeMapping.find(type);
838
2/2
✓ Branch 5 → 6 taken 1298154 times.
✓ Branch 5 → 8 taken 36390 times.
1334544 if (it != typeToLLVMTypeMapping.end())
839 1298154 return it->second;
840
841 // If not, generate the LLVM type
842
1/2
✓ Branch 8 → 9 taken 36390 times.
✗ Branch 8 → 13 not taken.
36390 llvm::Type *llvmType = type->toLLVMType(this);
843
1/2
✓ Branch 9 → 10 taken 36390 times.
✗ Branch 9 → 13 not taken.
36390 typeToLLVMTypeMapping[type] = llvmType;
844 36390 return llvmType;
845 }
846
847 7377 void SourceFile::checkForSoftErrors() const {
848 // Check if there are any soft errors and if so, print them
849
2/2
✓ Branch 3 → 4 taken 121 times.
✓ Branch 3 → 27 taken 7256 times.
7377 if (!resourceManager.errorManager.softErrors.empty()) {
850
1/2
✓ Branch 4 → 5 taken 121 times.
✗ Branch 4 → 37 not taken.
121 std::stringstream errorStream;
851
1/2
✓ Branch 5 → 6 taken 121 times.
✗ Branch 5 → 35 not taken.
121 errorStream << "There are unresolved errors. Please fix them and recompile.";
852
2/2
✓ Branch 21 → 8 taken 192 times.
✓ Branch 21 → 22 taken 121 times.
434 for (const auto &[codeLoc, message] : resourceManager.errorManager.softErrors)
853
2/4
✓ Branch 10 → 11 taken 192 times.
✗ Branch 10 → 28 not taken.
✓ Branch 11 → 12 taken 192 times.
✗ Branch 11 → 28 not taken.
192 errorStream << "\n\n" << message;
854
2/4
✓ Branch 23 → 24 taken 121 times.
✗ Branch 23 → 32 not taken.
✓ Branch 24 → 25 taken 121 times.
✗ Branch 24 → 29 not taken.
121 throw CompilerError(UNRESOLVED_SOFT_ERRORS, errorStream.str());
855 121 }
856 7256 }
857
858 436 void SourceFile::collectAndPrintWarnings() { // NOLINT(misc-no-recursion)
859 // Skip if restored from cache (no scope tree available), or if already visited. The latter guard keeps circular
860 // imports from recursing infinitely, since the dependency graph may contain cycles.
861
3/4
✓ Branch 2 → 3 taken 436 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 15 times.
✓ Branch 3 → 5 taken 421 times.
436 if (restoredFromCache || warningsCollected)
862 15 return;
863 421 warningsCollected = true;
864 // Print warnings for all dependencies
865
5/8
✓ Branch 5 → 6 taken 421 times.
✗ Branch 5 → 35 not taken.
✓ Branch 6 → 7 taken 421 times.
✗ Branch 6 → 35 not taken.
✓ Branch 7 → 8 taken 421 times.
✗ Branch 7 → 35 not taken.
✓ Branch 14 → 9 taken 435 times.
✓ Branch 14 → 15 taken 421 times.
856 for (SourceFile *sourceFile : dependencies | std::views::values)
866
2/2
✓ Branch 10 → 11 taken 51 times.
✓ Branch 10 → 12 taken 384 times.
435 if (!sourceFile->isStdFile)
867
1/2
✓ Branch 11 → 12 taken 51 times.
✗ Branch 11 → 35 not taken.
51 sourceFile->collectAndPrintWarnings();
868 // Collect warnings for this file
869
1/2
✓ Branch 15 → 16 taken 421 times.
✗ Branch 15 → 18 not taken.
421 if (!ignoreWarnings)
870 421 globalScope->collectWarnings(compilerOutput.warnings);
871 // Print warnings for this file
872
2/2
✓ Branch 32 → 20 taken 222 times.
✓ Branch 32 → 33 taken 421 times.
1064 for (const CompilerWarning &warning : compilerOutput.warnings)
873
1/2
✓ Branch 22 → 23 taken 222 times.
✗ Branch 22 → 36 not taken.
222 warning.print();
874 }
875
876 void SourceFile::collectAndPrintLintFindings() { // NOLINT(misc-no-recursion)
877 // Skip if restored from cache (no AST available), or if already visited. The latter guard keeps circular
878 // imports from recursing infinitely, since the dependency graph may contain cycles.
879 if (restoredFromCache || lintFindingsCollected)
880 return;
881 lintFindingsCollected = true;
882 // Collect lint findings for all dependencies
883 for (SourceFile *sourceFile : dependencies | std::views::values)
884 if (!sourceFile->isStdFile)
885 sourceFile->collectAndPrintLintFindings();
886 // Collect lint findings for this file
887 if (!ignoreWarnings) {
888 LintPass lintPass(resourceManager, this);
889 compilerOutput.lintFindings = lintPass.lint(ast);
890 }
891 // Print lint findings for this file
892 for (const LintFinding &finding : compilerOutput.lintFindings)
893 finding.print();
894 }
895
896 25162 const SourceFile *SourceFile::getRootSourceFile() const { // NOLINT(misc-no-recursion)
897
2/2
✓ Branch 2 → 3 taken 7904 times.
✓ Branch 2 → 4 taken 17258 times.
25162 return isMainFile ? this : parent->getRootSourceFile();
898 }
899
900 36057 bool SourceFile::isRT(RuntimeModule runtimeModule) const {
901
2/4
✓ Branch 2 → 3 taken 36057 times.
✗ Branch 2 → 38 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 36057 times.
36057 assert(IDENTIFYING_TOP_LEVEL_NAMES.contains(runtimeModule));
902
1/2
✓ Branch 5 → 6 taken 36057 times.
✗ Branch 5 → 38 not taken.
36057 const char *topLevelName = IDENTIFYING_TOP_LEVEL_NAMES.at(runtimeModule);
903
2/4
✓ Branch 8 → 9 taken 36057 times.
✗ Branch 8 → 28 not taken.
✓ Branch 9 → 10 taken 36057 times.
✗ Branch 9 → 26 not taken.
72114 const auto it = exportedNameRegistry.find(topLevelName);
904
2/2
✓ Branch 14 → 15 taken 4898 times.
✓ Branch 14 → 16 taken 31159 times.
36057 if (it == exportedNameRegistry.end())
905 4898 return false;
906
2/4
✓ Branch 18 → 19 taken 31159 times.
✗ Branch 18 → 34 not taken.
✓ Branch 19 → 20 taken 31159 times.
✗ Branch 19 → 32 not taken.
93477 return exportedNameRegistry.at(topLevelName).targetEntry->scope == globalScope.get();
907 }
908
909 10769 bool SourceFile::haveAllDependantsBeenTypeChecked() const {
910 10769 return std::ranges::all_of(dependants, [this](const SourceFile *dependant) {
911 // Ignore dependants that are part of the same import cycle (i.e. this file transitively depends on them). They
912 // cannot be type-checked before us either, so waiting on them would deadlock the whole cycle. Such a strongly
913 // connected component is type-checked as a unit and converges through the reVisitRequested fixpoint loop instead.
914
2/2
✓ Branch 3 → 4 taken 843 times.
✓ Branch 3 → 5 taken 31052 times.
31895 if (dependsOn(dependant))
915 843 return true;
916 31052 return dependant->totalTypeCheckerRuns >= 1;
917 10769 });
918 }
919
920 /**
921 * Check whether this source file transitively depends on (imports) the given other source file.
922 * Used to detect strongly connected components (import cycles) in the dependency graph.
923 *
924 * @param other Potential (transitive) dependency
925 * @return true if this file reaches the other file by following dependency edges
926 */
927 31895 bool SourceFile::dependsOn(const SourceFile *other) const {
928 31895 std::unordered_set<const SourceFile *> visited;
929
1/2
✓ Branch 3 → 4 taken 31895 times.
✗ Branch 3 → 35 not taken.
31895 std::queue<const SourceFile *> worklist;
930
1/2
✓ Branch 4 → 5 taken 31895 times.
✗ Branch 4 → 30 not taken.
31895 worklist.push(this);
931
1/2
✓ Branch 5 → 6 taken 31895 times.
✗ Branch 5 → 31 not taken.
31895 visited.insert(this);
932
2/2
✓ Branch 24 → 7 taken 159443 times.
✓ Branch 24 → 25 taken 31052 times.
190495 while (!worklist.empty()) {
933 159443 const SourceFile *current = worklist.front();
934 159443 worklist.pop();
935
5/8
✓ Branch 9 → 10 taken 159443 times.
✗ Branch 9 → 32 not taken.
✓ Branch 10 → 11 taken 159443 times.
✗ Branch 10 → 32 not taken.
✓ Branch 11 → 12 taken 159443 times.
✗ Branch 11 → 32 not taken.
✓ Branch 21 → 13 taken 275322 times.
✓ Branch 21 → 22 taken 158600 times.
433922 for (const SourceFile *dependency : current->dependencies | std::views::values) {
936
2/2
✓ Branch 14 → 15 taken 843 times.
✓ Branch 14 → 16 taken 274479 times.
275322 if (dependency == other)
937 843 return true;
938
3/4
✓ Branch 16 → 17 taken 274479 times.
✗ Branch 16 → 32 not taken.
✓ Branch 17 → 18 taken 128465 times.
✓ Branch 17 → 19 taken 146014 times.
274479 if (visited.insert(dependency).second)
939
1/2
✓ Branch 18 → 19 taken 128465 times.
✗ Branch 18 → 32 not taken.
128465 worklist.push(dependency);
940 }
941 }
942 31052 return false;
943 31895 }
944
945 /**
946 * Acquire all publicly visible symbols from the imported source file and put them in the name registry of the current one.
947 * But only do that for the symbols that are actually defined in the imported source file. Do not allow transitive dependencies.
948 * Here, we also register privately visible symbols to know that the symbol exist. The error handling regarding the visibility
949 * is issued later in the pipeline.
950 *
951 * @param importedSourceFile Imported source file
952 * @param importName First fragment of all fully qualified symbol names from that import
953 */
954 5474 void SourceFile::mergeNameRegistries(const SourceFile &importedSourceFile, const std::string &importName) {
955 // Retrieve import entry
956 5474 SymbolTableEntry *importEntry = globalScope->lookupStrict(importName);
957
3/4
✓ Branch 6 → 7 taken 2744 times.
✓ Branch 6 → 10 taken 2730 times.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 2744 times.
5474 assert(importEntry != nullptr || importName.starts_with("__")); // Runtime imports start with two underscores
958
959
2/2
✓ Branch 42 → 12 taken 314214 times.
✓ Branch 42 → 43 taken 5474 times.
319688 for (const auto &[originalName, entry] : importedSourceFile.exportedNameRegistry) {
960 // Skip if we introduce a transitive dependency
961
2/2
✓ Branch 16 → 17 taken 183313 times.
✓ Branch 16 → 18 taken 130901 times.
314214 if (entry.targetScope->sourceFile->globalScope != importedSourceFile.globalScope)
962 183313 continue;
963 // Add the fully qualified name
964
1/2
✓ Branch 18 → 19 taken 130901 times.
✗ Branch 18 → 52 not taken.
130901 std::string newName = importName;
965
1/2
✓ Branch 19 → 20 taken 130901 times.
✗ Branch 19 → 50 not taken.
130901 newName += SCOPE_ACCESS_TOKEN;
966
1/2
✓ Branch 20 → 21 taken 130901 times.
✗ Branch 20 → 50 not taken.
130901 newName += originalName;
967 261802 exportedNameRegistry.emplace(newName,
968
3/8
✓ Branch 21 → 22 taken 130901 times.
✗ Branch 21 → 49 not taken.
✓ Branch 22 → 23 taken 130901 times.
✗ Branch 22 → 44 not taken.
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 130901 times.
✗ Branch 46 → 47 not taken.
✗ Branch 46 → 48 not taken.
130901 NameRegistryEntry{newName, entry.typeId, entry.targetEntry, entry.targetScope, importEntry});
969 // Add the shortened name, considering the name collision. A symbol defined in the importing file itself always
970 // shadows imported symbols of the same name. Since this merge runs after the file built its own registry (so that
971 // circular imports work), we must explicitly avoid letting an import overwrite or erase such an own symbol - the old
972 // ordering achieved this implicitly by registering own symbols last with keep-on-collision.
973
1/2
✓ Branch 26 → 27 taken 130901 times.
✗ Branch 26 → 50 not taken.
130901 const auto existing = exportedNameRegistry.find(originalName);
974 const bool existingIsOwn =
975
4/4
✓ Branch 29 → 30 taken 3824 times.
✓ Branch 29 → 34 taken 127077 times.
✓ Branch 32 → 33 taken 1230 times.
✓ Branch 32 → 34 taken 2594 times.
130901 existing != exportedNameRegistry.end() && existing->second.targetScope->sourceFile->globalScope == globalScope;
976
2/2
✓ Branch 35 → 36 taken 129671 times.
✓ Branch 35 → 37 taken 1230 times.
130901 if (!existingIsOwn) {
977 129671 const bool keepOnCollision = importedSourceFile.alwaysKeepSymbolsOnNameCollision;
978
1/2
✓ Branch 36 → 37 taken 129671 times.
✗ Branch 36 → 50 not taken.
129671 addNameRegistryEntry(originalName, entry.typeId, entry.targetEntry, entry.targetScope, keepOnCollision, importEntry);
979 }
980 130901 }
981 5474 }
982
983 /**
984 * Recursively merge the exported name registries of all (transitive) dependencies into the respective importing source
985 * files. Each file merges its direct dependencies' registries exactly once (guarded by registriesMerged), so this is
986 * safe to call on overlapping subgraphs and on graphs that contain cycles (circular imports).
987 *
988 * Must only be called once every reachable file has built its own exported name registry (i.e. after the front-end).
989 */
990 4112 void SourceFile::mergeNameRegistriesRecursive() { // NOLINT(misc-no-recursion)
991
2/2
✓ Branch 2 → 3 taken 1349 times.
✓ Branch 2 → 4 taken 2763 times.
4112 if (registriesMerged)
992 1349 return;
993 2763 registriesMerged = true;
994
995 // Merge the direct dependencies' registries into this file. Their own registries are fully built by now, so the
996 // order in which the reachable files are visited does not matter (even across import cycles).
997
2/2
✓ Branch 12 → 6 taken 2730 times.
✓ Branch 12 → 13 taken 2763 times.
5493 for (const auto &[importName, dependency] : dependencies)
998
1/2
✓ Branch 9 → 10 taken 2730 times.
✗ Branch 9 → 24 not taken.
2730 mergeNameRegistries(*dependency, importName);
999
1000 // Recurse into the dependencies to cover the rest of the reachable graph
1001
5/8
✓ Branch 13 → 14 taken 2763 times.
✗ Branch 13 → 25 not taken.
✓ Branch 14 → 15 taken 2763 times.
✗ Branch 14 → 25 not taken.
✓ Branch 15 → 16 taken 2763 times.
✗ Branch 15 → 25 not taken.
✓ Branch 21 → 17 taken 2730 times.
✓ Branch 21 → 22 taken 2763 times.
5493 for (SourceFile *dependency : dependencies | std::views::values)
1002
1/2
✓ Branch 18 → 19 taken 2730 times.
✗ Branch 18 → 25 not taken.
2730 dependency->mergeNameRegistriesRecursive();
1003 }
1004
1005 365 void SourceFile::dumpCacheStats() {
1006
1/2
✓ Branch 2 → 3 taken 365 times.
✗ Branch 2 → 32 not taken.
365 std::stringstream cacheStats;
1007
3/6
✓ Branch 3 → 4 taken 365 times.
✗ Branch 3 → 22 not taken.
✓ Branch 4 → 5 taken 365 times.
✗ Branch 4 → 20 not taken.
✓ Branch 5 → 6 taken 365 times.
✗ Branch 5 → 20 not taken.
365 cacheStats << FunctionManager::dumpLookupCacheStatistics() << std::endl;
1008
3/6
✓ Branch 7 → 8 taken 365 times.
✗ Branch 7 → 25 not taken.
✓ Branch 8 → 9 taken 365 times.
✗ Branch 8 → 23 not taken.
✓ Branch 9 → 10 taken 365 times.
✗ Branch 9 → 23 not taken.
365 cacheStats << StructManager::dumpLookupCacheStatistics() << std::endl;
1009
3/6
✓ Branch 11 → 12 taken 365 times.
✗ Branch 11 → 28 not taken.
✓ Branch 12 → 13 taken 365 times.
✗ Branch 12 → 26 not taken.
✓ Branch 13 → 14 taken 365 times.
✗ Branch 13 → 26 not taken.
365 cacheStats << InterfaceManager::dumpLookupCacheStatistics() << std::endl;
1010
1/2
✓ Branch 15 → 16 taken 365 times.
✗ Branch 15 → 29 not taken.
365 compilerOutput.cacheStats = cacheStats.str();
1011 365 }
1012
1013 void SourceFile::dumpCompilationStats() const {
1014 const size_t sourceFileCount = resourceManager.sourceFiles.size();
1015 const size_t totalLineCount = resourceManager.getTotalLineCount();
1016 const size_t totalTypeCount = TypeRegistry::getTypeCount();
1017 const size_t allocatedBytes = resourceManager.astNodeAlloc.getTotalAllocatedSize();
1018 const size_t allocationCount = resourceManager.astNodeAlloc.getAllocationCount();
1019 const size_t totalDuration = resourceManager.totalTimer.getDurationMilliseconds();
1020 std::cout << "\nSuccessfully compiled " << std::to_string(sourceFileCount) << " source file(s)";
1021 std::cout << " or " << std::to_string(totalLineCount) << " lines in total.\n";
1022 std::cout << "Total number of blocks allocated via BlockAllocator: " << CommonUtil::formatBytes(allocatedBytes);
1023 std::cout << " in " << std::to_string(allocationCount) << " allocations.\n";
1024 #ifndef NDEBUG
1025 resourceManager.astNodeAlloc.printAllocatedClassStatistic();
1026 #endif
1027 std::cout << "Total number of types: " << std::to_string(totalTypeCount) << "\n";
1028 std::cout << "Total compile time: " << std::to_string(totalDuration) << " ms\n";
1029 }
1030
1031 void SourceFile::dumpOutput(const std::string &content, const std::string &caption, const std::string &fileSuffix) const {
1032 if (cliOptions.dump.dumpToFiles) {
1033 // Dump to file
1034 const std::string dumpFileName = filePath.stem().string() + "-" + fileSuffix;
1035 std::filesystem::path dumpFilePath = cliOptions.outputDir / dumpFileName;
1036 dumpFilePath.make_preferred();
1037 FileUtil::writeToFile(dumpFilePath, content);
1038 } else {
1039 // Dump to console
1040 std::cout << "\n" << caption << ":\n" << content;
1041 }
1042
1043 // If the abort after dump is requested, set the abort compilation flag
1044 if (cliOptions.dump.abortAfterDump) {
1045 // If this is an IR dump whilst having optimization enabled, we may not abort when dumping unoptimized IR,
1046 // because we also have to dump the optimized IR
1047 if (cliOptions.dump.dumpIR && fileSuffix == "ir-code.ll") {
1048 resourceManager.abortCompilation = cliOptions.optLevel == OptLevel::O0;
1049 } else {
1050 resourceManager.abortCompilation = true;
1051 }
1052 }
1053 }
1054
1055 4778 void SourceFile::visualizerPreamble(std::stringstream &output) const {
1056
2/2
✓ Branch 2 → 3 taken 430 times.
✓ Branch 2 → 4 taken 4348 times.
4778 if (isMainFile)
1057 430 output << "digraph {\n rankdir=\"TB\";\n";
1058 else
1059 4348 output << "subgraph {\n";
1060
3/6
✓ Branch 6 → 7 taken 4778 times.
✗ Branch 6 → 13 not taken.
✓ Branch 7 → 8 taken 4778 times.
✗ Branch 7 → 11 not taken.
✓ Branch 8 → 9 taken 4778 times.
✗ Branch 8 → 11 not taken.
4778 output << " label=\"" << filePath.generic_string() << "\";\n";
1061 4778 }
1062
1063 void SourceFile::visualizerOutput(std::string outputName, const std::string &output) const {
1064 if (cliOptions.dump.dumpToFiles) {
1065 // Check if graphviz is installed
1066 // GCOV_EXCL_START
1067 if (!SystemUtil::isGraphvizInstalled())
1068 throw CompilerError(IO_ERROR, "Please check if you have installed Graphviz and added it to the PATH variable");
1069 // GCOV_EXCL_STOP
1070
1071 // Write to a dot file
1072 std::ranges::transform(outputName, outputName.begin(), ::tolower);
1073 dumpOutput(output, outputName, outputName + ".dot");
1074
1075 // Generate SVG. This only works if the dot code was dumped into a file
1076 std::cout << "\nGenerating SVG file ... ";
1077 const std::string dotFileName = filePath.stem().string() + "-" + outputName + ".dot";
1078 std::filesystem::path dotFilePath = cliOptions.outputDir / dotFileName;
1079 std::filesystem::path svgFilePath = dotFilePath;
1080 svgFilePath.replace_extension("svg");
1081 dotFilePath.make_preferred();
1082 svgFilePath.make_preferred();
1083 SystemUtil::exec("dot -T svg -o" + svgFilePath.string() + " " + dotFilePath.string());
1084 std::cout << "done.\nSVG file can be found at: " << svgFilePath << "\n";
1085 } else {
1086 // Dump to console
1087 std::cout << "\nSerialized " << outputName << ":\n\n" << output << "\n";
1088 }
1089
1090 // If the abort after dump is requested, set the abort compilation flag
1091 if (cliOptions.dump.abortAfterDump)
1092 resourceManager.abortCompilation = true;
1093 }
1094
1095 36056 void SourceFile::printStatusMessage(const char *stage, const CompileStageIOType &in, const CompileStageIOType &out,
1096 uint64_t stageRuntime, unsigned short stageRuns) const {
1097
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 30 taken 36056 times.
36056 if (cliOptions.printDebugOutput) {
1098 static constexpr const char *const compilerStageIoTypeName[6] = {"Code", "Tokens", "CST", "AST", "IR", "Obj"};
1099 // Build output string
1100 std::stringstream outputStr;
1101 outputStr << "[" << stage << "] for " << fileName << ": ";
1102 outputStr << compilerStageIoTypeName[in] << " --> " << compilerStageIoTypeName[out];
1103 outputStr << " (" << std::to_string(stageRuntime) << " ms";
1104 if (stageRuns > 0)
1105 outputStr << "; " << std::to_string(stageRuns) << " run(s)";
1106 outputStr << ")\n";
1107 // Print
1108 std::cout << outputStr.str();
1109 }
1110 36056 }
1111
1112 } // namespace spice::compiler
1113