GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 85.1% 537 / 3 / 634
Functions: 90.0% 45 / 0 / 50
Branches: 47.8% 567 / 12 / 1198

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 6177 SourceFile::SourceFile(GlobalResourceManager &resourceManager, SourceFile *parent, std::string name,
48 const std::filesystem::path &filePath, bool stdFile)
49
1/2
✓ Branch 6 → 7 taken 6177 times.
✗ Branch 6 → 132 not taken.
18531 : name(std::move(name)), filePath(filePath), isStdFile(stdFile), parent(parent),
50
3/4
✓ Branch 18 → 19 taken 2 times.
✓ Branch 18 → 20 taken 6175 times.
✓ Branch 21 → 22 taken 6177 times.
✗ Branch 21 → 64 not taken.
6177 builder(resourceManager.cliOptions.useLTO ? resourceManager.ltoContext : context), resourceManager(resourceManager),
51
1/2
✓ Branch 16 → 17 taken 6177 times.
✗ Branch 16 → 112 not taken.
18531 cliOptions(resourceManager.cliOptions) {
52 // Deduce fileName and fileDir
53
3/6
✓ Branch 29 → 30 taken 6177 times.
✗ Branch 29 → 69 not taken.
✓ Branch 30 → 31 taken 6177 times.
✗ Branch 30 → 67 not taken.
✓ Branch 31 → 32 taken 6177 times.
✗ Branch 31 → 65 not taken.
6177 fileName = std::filesystem::path(filePath).filename().string();
54
3/6
✓ Branch 36 → 37 taken 6177 times.
✗ Branch 36 → 76 not taken.
✓ Branch 37 → 38 taken 6177 times.
✗ Branch 37 → 74 not taken.
✓ Branch 38 → 39 taken 6177 times.
✗ Branch 38 → 72 not taken.
6177 fileDir = std::filesystem::path(filePath).parent_path().string();
55
56 // Discard value names if not required
57
1/2
✓ Branch 43 → 44 taken 6177 times.
✗ Branch 43 → 93 not taken.
6177 context.setDiscardValueNames(!cliOptions.namesForIRValues);
58
59 // Search after the selected target
60 6177 std::string error;
61
1/2
✓ Branch 45 → 46 taken 6177 times.
✗ Branch 45 → 91 not taken.
6177 const llvm::Target *target = llvm::TargetRegistry::lookupTarget(cliOptions.targetTriple, error);
62
1/2
✗ Branch 46 → 47 not taken.
✓ Branch 46 → 52 taken 6177 times.
6177 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 6177 times.
✗ Branch 52 → 91 not taken.
6177 llvm::TargetOptions opt;
67 6177 opt.MCOptions.AsmVerbose = true;
68 6177 opt.MCOptions.PreserveAsmComments = true;
69 6177 const std::string &cpuName = resourceManager.cpuName;
70 6177 const std::string &features = resourceManager.cpuFeatures;
71 6177 const llvm::Triple &targetTriple = cliOptions.targetTriple;
72 6177 constexpr llvm::Reloc::Model relocModel = llvm::Reloc::PIC_;
73
1/2
✓ Branch 57 → 58 taken 6177 times.
✗ Branch 57 → 85 not taken.
6177 llvm::TargetMachine *targetMachineRaw = target->createTargetMachine(targetTriple, cpuName, features, opt, relocModel);
74 6177 targetMachine = std::unique_ptr<llvm::TargetMachine>(targetMachineRaw);
75 6177 }
76
77 9316 void SourceFile::runLexer() {
78
2/2
✓ Branch 2 → 3 taken 1312 times.
✓ Branch 2 → 4 taken 8004 times.
9316 if (isMainFile)
79
1/2
✓ Branch 3 → 4 taken 1312 times.
✗ Branch 3 → 83 not taken.
1312 resourceManager.totalTimer.start();
80
81 // Check if this stage has already been done
82
2/2
✓ Branch 4 → 5 taken 3149 times.
✓ Branch 4 → 6 taken 6167 times.
9316 if (previousStage >= LEXER)
83 3149 return;
84
85
1/2
✓ Branch 6 → 7 taken 6167 times.
✗ Branch 6 → 83 not taken.
6167 Timer timer(&compilerOutput.times.lexer);
86
1/2
✓ Branch 7 → 8 taken 6167 times.
✗ Branch 7 → 83 not taken.
6167 timer.start();
87
88 // Read from the input source file
89
1/2
✓ Branch 8 → 9 taken 6167 times.
✗ Branch 8 → 83 not taken.
6167 std::ifstream fileInputStream(filePath);
90
3/4
✓ Branch 9 → 10 taken 6167 times.
✗ Branch 9 → 81 not taken.
✓ Branch 10 → 11 taken 2 times.
✓ Branch 10 → 20 taken 6165 times.
6167 if (!fileInputStream)
91
4/8
✓ Branch 12 → 13 taken 2 times.
✗ Branch 12 → 59 not taken.
✓ Branch 13 → 14 taken 2 times.
✗ Branch 13 → 57 not taken.
✓ Branch 14 → 15 taken 2 times.
✗ Branch 14 → 55 not taken.
✓ Branch 15 → 16 taken 2 times.
✗ Branch 15 → 52 not taken.
2 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 6165 times.
✗ Branch 20 → 64 not taken.
6165 antlrCtx.inputStream = std::make_unique<antlr4::ANTLRInputStream>(fileInputStream);
95
1/2
✓ Branch 24 → 25 taken 6165 times.
✗ Branch 24 → 65 not taken.
6165 antlrCtx.lexer = std::make_unique<SpiceLexer>(antlrCtx.inputStream.get());
96
1/2
✓ Branch 28 → 29 taken 6165 times.
✗ Branch 28 → 81 not taken.
6165 antlrCtx.lexer->removeErrorListeners();
97
1/2
✓ Branch 29 → 30 taken 6165 times.
✗ Branch 29 → 67 not taken.
6165 antlrCtx.lexerErrorHandler = std::make_unique<AntlrThrowingErrorListener>(ThrowingErrorListenerMode::LEXER, this);
98
1/2
✓ Branch 34 → 35 taken 6165 times.
✗ Branch 34 → 81 not taken.
6165 antlrCtx.lexer->addErrorListener(antlrCtx.lexerErrorHandler.get());
99
1/2
✓ Branch 36 → 37 taken 6165 times.
✗ Branch 36 → 70 not taken.
6165 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 6163 times.
✓ Branch 41 → 74 taken 2 times.
✓ Branch 42 → 43 taken 6163 times.
✗ Branch 42 → 72 not taken.
6167 cacheKey = resourceManager.cacheManager.computeCacheKey(antlrCtx.tokenStream->getText());
105
106 6163 previousStage = LEXER;
107
1/2
✓ Branch 47 → 48 taken 6163 times.
✗ Branch 47 → 81 not taken.
6163 timer.stop();
108
1/2
✓ Branch 48 → 49 taken 6163 times.
✗ Branch 48 → 79 not taken.
6163 printStatusMessage("Lexer", IO_CODE, IO_TOKENS, compilerOutput.times.lexer);
109 6167 }
110
111 9312 void SourceFile::runParser() {
112 // Skip if restored from the cache or this stage has already been done
113
3/4
✓ Branch 2 → 3 taken 9312 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 3149 times.
✓ Branch 3 → 5 taken 6163 times.
9312 if (restoredFromCache || previousStage >= PARSER)
114 3149 return;
115
116
1/2
✓ Branch 5 → 6 taken 6163 times.
✗ Branch 5 → 32 not taken.
6163 Timer timer(&compilerOutput.times.parser);
117
1/2
✓ Branch 6 → 7 taken 6163 times.
✗ Branch 6 → 32 not taken.
6163 timer.start();
118
119 // Parse input
120
1/2
✓ Branch 8 → 9 taken 6163 times.
✗ Branch 8 → 25 not taken.
6163 antlrCtx.parser = std::make_unique<SpiceParser>(antlrCtx.tokenStream.get()); // Check for syntax errors
121
1/2
✓ Branch 12 → 13 taken 6163 times.
✗ Branch 12 → 32 not taken.
6163 antlrCtx.parser->removeErrorListeners();
122
1/2
✓ Branch 13 → 14 taken 6163 times.
✗ Branch 13 → 27 not taken.
6163 antlrCtx.parserErrorHandler = std::make_unique<AntlrThrowingErrorListener>(ThrowingErrorListenerMode::PARSER, this);
123
1/2
✓ Branch 18 → 19 taken 6163 times.
✗ Branch 18 → 32 not taken.
6163 antlrCtx.parser->addErrorListener(antlrCtx.parserErrorHandler.get());
124
1/2
✓ Branch 20 → 21 taken 6163 times.
✗ Branch 20 → 32 not taken.
6163 antlrCtx.parser->removeParseListeners();
125
126 6163 previousStage = PARSER;
127
1/2
✓ Branch 21 → 22 taken 6163 times.
✗ Branch 21 → 32 not taken.
6163 timer.stop();
128
1/2
✓ Branch 22 → 23 taken 6163 times.
✗ Branch 22 → 30 not taken.
6163 printStatusMessage("Parser", IO_TOKENS, IO_CST, compilerOutput.times.parser);
129 }
130
131 8030 void SourceFile::runCSTVisualizer() {
132 // Only execute if enabled
133
4/6
✓ Branch 2 → 3 taken 8030 times.
✗ Branch 2 → 5 not taken.
✓ Branch 3 → 4 taken 8030 times.
✗ Branch 3 → 6 not taken.
✓ Branch 4 → 5 taken 14 times.
✓ Branch 4 → 6 taken 8016 times.
8030 if (restoredFromCache || (!cliOptions.dump.dumpCST && !cliOptions.testMode))
134 3163 return;
135 // Check if this stage has already been done
136
2/2
✓ Branch 6 → 7 taken 3149 times.
✓ Branch 6 → 8 taken 4867 times.
8016 if (previousStage >= CST_VISUALIZER)
137 3149 return;
138
139
1/2
✓ Branch 8 → 9 taken 4867 times.
✗ Branch 8 → 66 not taken.
4867 Timer timer(&compilerOutput.times.cstVisualizer);
140
1/2
✓ Branch 9 → 10 taken 4867 times.
✗ Branch 9 → 66 not taken.
4867 timer.start();
141
142 // Generate dot code for this source file
143
1/2
✓ Branch 10 → 11 taken 4867 times.
✗ Branch 10 → 66 not taken.
4867 std::stringstream dotCode;
144
1/2
✓ Branch 11 → 12 taken 4867 times.
✗ Branch 11 → 64 not taken.
4867 visualizerPreamble(dotCode);
145
1/2
✓ Branch 14 → 15 taken 4867 times.
✗ Branch 14 → 64 not taken.
4867 CSTVisualizer cstVisualizer(resourceManager, this, antlrCtx.lexer.get(), antlrCtx.parser.get());
146
6/12
✓ Branch 15 → 16 taken 4867 times.
✗ Branch 15 → 62 not taken.
✓ Branch 17 → 18 taken 4867 times.
✗ Branch 17 → 51 not taken.
✓ Branch 18 → 19 taken 4867 times.
✗ Branch 18 → 51 not taken.
✓ Branch 19 → 20 taken 4867 times.
✗ Branch 19 → 49 not taken.
✓ Branch 20 → 21 taken 4867 times.
✗ Branch 20 → 47 not taken.
✓ Branch 21 → 22 taken 4867 times.
✗ Branch 21 → 47 not taken.
4867 dotCode << " " << std::any_cast<std::string>(cstVisualizer.visit(antlrCtx.parser->entry())) << "}";
147
1/2
✓ Branch 25 → 26 taken 4867 times.
✗ Branch 25 → 62 not taken.
4867 antlrCtx.parser->reset();
148
149 // Dump the serialized CST string and the SVG file
150
2/4
✓ Branch 26 → 27 taken 4867 times.
✗ Branch 26 → 28 not taken.
✓ Branch 27 → 28 taken 4867 times.
✗ Branch 27 → 32 not taken.
4867 if (cliOptions.dump.dumpCST || cliOptions.testMode)
151
1/2
✓ Branch 28 → 29 taken 4867 times.
✗ Branch 28 → 53 not taken.
4867 compilerOutput.cstString = dotCode.str();
152
153
1/2
✗ Branch 32 → 33 not taken.
✓ Branch 32 → 40 taken 4867 times.
4867 if (cliOptions.dump.dumpCST)
154 visualizerOutput("CST", compilerOutput.cstString);
155
156 4867 previousStage = CST_VISUALIZER;
157
1/2
✓ Branch 40 → 41 taken 4867 times.
✗ Branch 40 → 62 not taken.
4867 timer.stop();
158
1/2
✓ Branch 41 → 42 taken 4867 times.
✗ Branch 41 → 60 not taken.
4867 printStatusMessage("CST Visualizer", IO_CST, IO_CST, compilerOutput.times.cstVisualizer);
159 4867 }
160
161 9312 void SourceFile::runASTBuilder() {
162 // Skip if restored from the cache or this stage has already been done
163
3/4
✓ Branch 2 → 3 taken 9312 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 3149 times.
✓ Branch 3 → 5 taken 6163 times.
9312 if (restoredFromCache || previousStage >= AST_BUILDER)
164 3149 return;
165
166
1/2
✓ Branch 5 → 6 taken 6163 times.
✗ Branch 5 → 36 not taken.
6163 Timer timer(&compilerOutput.times.astBuilder);
167
1/2
✓ Branch 6 → 7 taken 6163 times.
✗ Branch 6 → 36 not taken.
6163 timer.start();
168
169 // Build AST for this source file
170
1/2
✓ Branch 8 → 9 taken 6163 times.
✗ Branch 8 → 36 not taken.
6163 ASTBuilder astBuilder(resourceManager, this, antlrCtx.inputStream.get());
171
5/6
✓ Branch 10 → 11 taken 6159 times.
✓ Branch 10 → 26 taken 4 times.
✓ Branch 11 → 12 taken 6147 times.
✓ Branch 11 → 26 taken 12 times.
✓ Branch 12 → 13 taken 6147 times.
✗ Branch 12 → 24 not taken.
6163 ast = std::any_cast<EntryNode *>(astBuilder.visit(antlrCtx.parser->entry()));
172
1/2
✓ Branch 15 → 16 taken 6147 times.
✗ Branch 15 → 34 not taken.
6147 antlrCtx.parser->reset();
173
174 // Create global scope
175
1/2
✓ Branch 16 → 17 taken 6147 times.
✗ Branch 16 → 27 not taken.
6147 globalScope = std::make_unique<Scope>(nullptr, this, ScopeType::GLOBAL, &ast->codeLoc);
176
177 6147 previousStage = AST_BUILDER;
178
1/2
✓ Branch 19 → 20 taken 6147 times.
✗ Branch 19 → 34 not taken.
6147 timer.stop();
179
1/2
✓ Branch 20 → 21 taken 6147 times.
✗ Branch 20 → 32 not taken.
6147 printStatusMessage("AST Builder", IO_CST, IO_AST, compilerOutput.times.astBuilder);
180 6163 }
181
182 8030 void SourceFile::runASTVisualizer() {
183 // Only execute if enabled
184
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 8030 times.
8030 if (restoredFromCache)
185 3163 return;
186
3/4
✓ Branch 4 → 5 taken 8030 times.
✗ Branch 4 → 7 not taken.
✓ Branch 5 → 6 taken 14 times.
✓ Branch 5 → 7 taken 8016 times.
8030 if (!cliOptions.dump.dumpAST && !cliOptions.testMode)
187 14 return;
188 // Check if this stage has already been done
189
2/2
✓ Branch 7 → 8 taken 3149 times.
✓ Branch 7 → 9 taken 4867 times.
8016 if (previousStage >= AST_VISUALIZER)
190 3149 return;
191
192
1/2
✓ Branch 9 → 10 taken 4867 times.
✗ Branch 9 → 58 not taken.
4867 Timer timer(&compilerOutput.times.astVisualizer);
193
1/2
✓ Branch 10 → 11 taken 4867 times.
✗ Branch 10 → 58 not taken.
4867 timer.start();
194
195 // Generate dot code for this source file
196
1/2
✓ Branch 11 → 12 taken 4867 times.
✗ Branch 11 → 58 not taken.
4867 std::stringstream dotCode;
197
1/2
✓ Branch 12 → 13 taken 4867 times.
✗ Branch 12 → 56 not taken.
4867 visualizerPreamble(dotCode);
198
1/2
✓ Branch 13 → 14 taken 4867 times.
✗ Branch 13 → 56 not taken.
4867 ASTVisualizer astVisualizer(resourceManager, this);
199
5/10
✓ Branch 14 → 15 taken 4867 times.
✗ Branch 14 → 54 not taken.
✓ Branch 15 → 16 taken 4867 times.
✗ Branch 15 → 43 not taken.
✓ Branch 16 → 17 taken 4867 times.
✗ Branch 16 → 41 not taken.
✓ Branch 17 → 18 taken 4867 times.
✗ Branch 17 → 39 not taken.
✓ Branch 18 → 19 taken 4867 times.
✗ Branch 18 → 39 not taken.
4867 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 4867 times.
✗ Branch 21 → 45 not taken.
4867 compilerOutput.astString = dotCode.str();
203
204
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 32 taken 4867 times.
4867 if (cliOptions.dump.dumpAST)
205 visualizerOutput("AST", compilerOutput.astString);
206
207 4867 previousStage = AST_VISUALIZER;
208
1/2
✓ Branch 32 → 33 taken 4867 times.
✗ Branch 32 → 54 not taken.
4867 timer.stop();
209
1/2
✓ Branch 33 → 34 taken 4867 times.
✗ Branch 33 → 52 not taken.
4867 printStatusMessage("AST Visualizer", IO_AST, IO_AST, compilerOutput.times.astVisualizer);
210 4867 }
211
212 9296 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 9296 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 3149 times.
✓ Branch 3 → 5 taken 6147 times.
9296 if (restoredFromCache || previousStage >= IMPORT_COLLECTOR)
215 3149 return;
216
217
1/2
✓ Branch 5 → 6 taken 6147 times.
✗ Branch 5 → 85 not taken.
6147 Timer timer(&compilerOutput.times.importCollector);
218
1/2
✓ Branch 6 → 7 taken 6147 times.
✗ Branch 6 → 85 not taken.
6147 timer.start();
219
220 // Collect the imports for this source file
221
1/2
✓ Branch 7 → 8 taken 6147 times.
✗ Branch 7 → 85 not taken.
6147 ImportCollector importCollector(resourceManager, this);
222
2/2
✓ Branch 8 → 9 taken 6135 times.
✓ Branch 8 → 67 taken 12 times.
6147 importCollector.visit(ast);
223
224 6135 previousStage = IMPORT_COLLECTOR;
225
226 // Run first part of pipeline for the imported source file
227
5/8
✓ Branch 10 → 11 taken 6135 times.
✗ Branch 10 → 68 not taken.
✓ Branch 11 → 12 taken 6135 times.
✗ Branch 11 → 68 not taken.
✓ Branch 12 → 13 taken 6135 times.
✗ Branch 12 → 68 not taken.
✓ Branch 18 → 14 taken 5924 times.
✓ Branch 18 → 19 taken 6135 times.
12059 for (SourceFile *sourceFile : dependencies | std::views::values)
228
1/2
✓ Branch 15 → 16 taken 5924 times.
✗ Branch 15 → 68 not taken.
5924 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 6135 std::vector<std::string> transitiveDepCacheKeys;
234 6135 std::unordered_set<std::string> visited;
235
1/2
✓ Branch 20 → 21 taken 6135 times.
✗ Branch 20 → 79 not taken.
6135 std::queue<const SourceFile *> worklist;
236
5/8
✓ Branch 21 → 22 taken 6135 times.
✗ Branch 21 → 69 not taken.
✓ Branch 22 → 23 taken 6135 times.
✗ Branch 22 → 69 not taken.
✓ Branch 23 → 24 taken 6135 times.
✗ Branch 23 → 69 not taken.
✓ Branch 29 → 25 taken 5924 times.
✓ Branch 29 → 30 taken 6135 times.
12059 for (const SourceFile *dep : dependencies | std::views::values)
237
1/2
✓ Branch 26 → 27 taken 5924 times.
✗ Branch 26 → 69 not taken.
5924 worklist.push(dep);
238
2/2
✓ Branch 49 → 31 taken 25332 times.
✓ Branch 49 → 50 taken 6135 times.
31467 while (!worklist.empty()) {
239 25332 const SourceFile *dep = worklist.front();
240 25332 worklist.pop();
241
3/4
✓ Branch 33 → 34 taken 25332 times.
✗ Branch 33 → 77 not taken.
✓ Branch 34 → 35 taken 12677 times.
✓ Branch 34 → 36 taken 12655 times.
25332 if (!visited.insert(dep->cacheKey).second)
242 12677 continue;
243
1/2
✓ Branch 36 → 37 taken 12655 times.
✗ Branch 36 → 77 not taken.
12655 transitiveDepCacheKeys.push_back(dep->cacheKey);
244
5/8
✓ Branch 37 → 38 taken 12655 times.
✗ Branch 37 → 70 not taken.
✓ Branch 38 → 39 taken 12655 times.
✗ Branch 38 → 70 not taken.
✓ Branch 39 → 40 taken 12655 times.
✗ Branch 39 → 70 not taken.
✓ Branch 45 → 41 taken 19408 times.
✓ Branch 45 → 46 taken 12655 times.
32063 for (const SourceFile *transitive : dep->dependencies | std::views::values)
245
1/2
✓ Branch 42 → 43 taken 19408 times.
✗ Branch 42 → 70 not taken.
19408 worklist.push(transitive);
246 }
247
2/4
✓ Branch 51 → 52 taken 6135 times.
✗ Branch 51 → 73 not taken.
✓ Branch 52 → 53 taken 6135 times.
✗ Branch 52 → 71 not taken.
6135 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 8 times.
✓ Branch 56 → 59 taken 6127 times.
6135 if (!cliOptions.ignoreCache)
251
1/2
✓ Branch 57 → 58 taken 8 times.
✗ Branch 57 → 77 not taken.
8 restoredFromCache = resourceManager.cacheManager.lookupSourceFile(this);
252
253
1/2
✓ Branch 59 → 60 taken 6135 times.
✗ Branch 59 → 77 not taken.
6135 timer.stop();
254
1/2
✓ Branch 60 → 61 taken 6135 times.
✗ Branch 60 → 75 not taken.
6135 printStatusMessage("Import Collector", IO_AST, IO_AST, compilerOutput.times.importCollector);
255 6147 }
256
257 9284 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 3149 times.
✓ Branch 2 → 4 taken 6135 times.
9284 if (previousStage >= SYMBOL_TABLE_BUILDER)
262 3149 return;
263
264
1/2
✓ Branch 4 → 5 taken 6135 times.
✗ Branch 4 → 19 not taken.
6135 Timer timer(&compilerOutput.times.symbolTableBuilder);
265
1/2
✓ Branch 5 → 6 taken 6135 times.
✗ Branch 5 → 19 not taken.
6135 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 6135 times.
✗ Branch 6 → 19 not taken.
6135 SymbolTableBuilder symbolTableBuilder(resourceManager, this);
271
2/2
✓ Branch 7 → 8 taken 6093 times.
✓ Branch 7 → 14 taken 42 times.
6135 symbolTableBuilder.visit(ast);
272
273 6093 previousStage = SYMBOL_TABLE_BUILDER;
274
1/2
✓ Branch 9 → 10 taken 6093 times.
✗ Branch 9 → 17 not taken.
6093 timer.stop();
275
1/2
✓ Branch 10 → 11 taken 6093 times.
✗ Branch 10 → 15 not taken.
6093 printStatusMessage("Symbol Table Builder", IO_AST, IO_AST, compilerOutput.times.symbolTableBuilder);
276 6135 }
277
278 9240 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 6141 times.
✓ Branch 2 → 4 taken 3099 times.
✓ Branch 3 → 4 taken 50 times.
✓ Branch 3 → 5 taken 6091 times.
9240 if (previousStage >= TYPE_CHECKER_PRE || typeCheckerPreRunning)
286 3149 return;
287 6091 typeCheckerPreRunning = true;
288
289 // Type-check all dependencies first
290
5/8
✓ Branch 5 → 6 taken 6091 times.
✗ Branch 5 → 24 not taken.
✓ Branch 6 → 7 taken 6091 times.
✗ Branch 6 → 24 not taken.
✓ Branch 7 → 8 taken 6091 times.
✗ Branch 7 → 24 not taken.
✓ Branch 13 → 9 taken 5922 times.
✓ Branch 13 → 14 taken 6089 times.
12011 for (SourceFile *sourceFile : dependencies | std::views::values)
291
2/2
✓ Branch 10 → 11 taken 5920 times.
✓ Branch 10 → 24 taken 2 times.
5922 sourceFile->runTypeCheckerPre();
292
293
1/2
✓ Branch 14 → 15 taken 6089 times.
✗ Branch 14 → 30 not taken.
6089 Timer timer(&compilerOutput.times.typeCheckerPre);
294
1/2
✓ Branch 15 → 16 taken 6089 times.
✗ Branch 15 → 30 not taken.
6089 timer.start();
295
296 // Then type-check the current file
297
1/2
✓ Branch 16 → 17 taken 6089 times.
✗ Branch 16 → 30 not taken.
6089 TypeChecker typeChecker(resourceManager, this, TC_MODE_PRE);
298
2/2
✓ Branch 17 → 18 taken 6053 times.
✓ Branch 17 → 25 taken 36 times.
6089 typeChecker.visit(ast);
299
300 6053 previousStage = TYPE_CHECKER_PRE;
301 6053 typeCheckerPreRunning = false;
302
1/2
✓ Branch 19 → 20 taken 6053 times.
✗ Branch 19 → 28 not taken.
6053 timer.stop();
303
1/2
✓ Branch 20 → 21 taken 6053 times.
✗ Branch 20 → 26 not taken.
6053 printStatusMessage("Type Checker Pre", IO_AST, IO_AST, compilerOutput.times.typeCheckerPre);
304 6089 }
305
306 24273 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 576 times.
✓ Branch 2 → 4 taken 23697 times.
24273 if (typeCheckerPostRunning)
311 7177 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 23697 times.
✗ Branch 4 → 80 not taken.
✓ Branch 5 → 6 taken 6601 times.
✓ Branch 5 → 7 taken 17096 times.
23697 if (!haveAllDependantsBeenTypeChecked())
316 6601 return;
317
318 17096 typeCheckerPostRunning = true;
319
320
1/2
✓ Branch 7 → 8 taken 17096 times.
✗ Branch 7 → 80 not taken.
17096 Timer timer(&compilerOutput.times.typeCheckerPost);
321
1/2
✓ Branch 8 → 9 taken 17096 times.
✗ Branch 8 → 80 not taken.
17096 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 17096 times.
✗ Branch 9 → 80 not taken.
17096 TypeChecker typeChecker(resourceManager, this, TC_MODE_POST);
325 17096 unsigned short typeCheckerRuns = 0;
326
2/2
✓ Branch 25 → 11 taken 10073 times.
✓ Branch 25 → 26 taken 16914 times.
26987 while (reVisitRequested) {
327 10073 typeCheckerRuns++;
328 10073 totalTypeCheckerRuns++;
329 10073 reVisitRequested = false;
330
331 // Type-check the current file first. Multiple times, if requested
332 10073 timer.resume();
333
2/2
✓ Branch 12 → 13 taken 10007 times.
✓ Branch 12 → 58 taken 66 times.
10073 typeChecker.visit(ast);
334
1/2
✓ Branch 14 → 15 taken 10007 times.
✗ Branch 14 → 78 not taken.
10007 timer.pause();
335
336 // Then type-check all dependencies
337
5/8
✓ Branch 15 → 16 taken 10007 times.
✗ Branch 15 → 59 not taken.
✓ Branch 16 → 17 taken 10007 times.
✗ Branch 16 → 59 not taken.
✓ Branch 17 → 18 taken 10007 times.
✗ Branch 17 → 59 not taken.
✓ Branch 23 → 19 taken 23055 times.
✓ Branch 23 → 24 taken 9891 times.
32946 for (SourceFile *sourceFile : dependencies | std::views::values)
338
2/2
✓ Branch 20 → 21 taken 22939 times.
✓ Branch 20 → 59 taken 116 times.
23055 sourceFile->runTypeCheckerPost();
339 }
340
341 16914 typeCheckerPostRunning = false;
342
343
2/2
✓ Branch 26 → 27 taken 16658 times.
✓ Branch 26 → 78 taken 256 times.
16914 checkForSoftErrors();
344
345 // Check if all dyn variables were type-inferred successfully
346
2/2
✓ Branch 28 → 29 taken 16656 times.
✓ Branch 28 → 78 taken 2 times.
16658 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 16656 times.
✗ Branch 29 → 78 not taken.
16656 runPostTypeCheckingVerifier();
351 #endif
352
353 16656 previousStage = TYPE_CHECKER_POST;
354
1/2
✓ Branch 30 → 31 taken 16656 times.
✗ Branch 30 → 78 not taken.
16656 timer.stop();
355
1/2
✓ Branch 31 → 32 taken 16656 times.
✗ Branch 31 → 60 not taken.
16656 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 16656 times.
✗ Branch 32 → 34 not taken.
✓ Branch 33 → 34 taken 16642 times.
✓ Branch 33 → 41 taken 14 times.
16656 if (cliOptions.dump.dumpSymbolTable || cliOptions.testMode)
359
2/4
✓ Branch 35 → 36 taken 16642 times.
✗ Branch 35 → 64 not taken.
✓ Branch 36 → 37 taken 16642 times.
✗ Branch 36 → 62 not taken.
16642 compilerOutput.symbolTableString = globalScope->getSymbolTableJSON().dump(/*indent=*/2);
360
361 // Dump symbol table
362
1/2
✗ Branch 41 → 42 not taken.
✓ Branch 41 → 54 taken 16656 times.
16656 if (cliOptions.dump.dumpSymbolTable)
363 dumpOutput(compilerOutput.symbolTableString, "Symbol Table", "symbol-table.json");
364 17096 }
365
366 16656 void SourceFile::runPostTypeCheckingVerifier() {
367
1/2
✓ Branch 2 → 3 taken 16656 times.
✗ Branch 2 → 8 not taken.
16656 PostTypeCheckingVerifier verifier(resourceManager, this);
368
1/2
✓ Branch 3 → 4 taken 16656 times.
✗ Branch 3 → 6 not taken.
16656 verifier.verify(ast);
369 16656 }
370
371 880 void SourceFile::runDependencyGraphVisualizer() {
372 // Only execute if enabled
373
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 880 times.
880 if (restoredFromCache)
374 14 return;
375
3/4
✓ Branch 4 → 5 taken 880 times.
✗ Branch 4 → 7 not taken.
✓ Branch 5 → 6 taken 10 times.
✓ Branch 5 → 7 taken 870 times.
880 if (!cliOptions.dump.dumpDependencyGraph && !cliOptions.testMode)
376 10 return;
377 // Check if this stage has already been done
378
2/2
✓ Branch 7 → 8 taken 4 times.
✓ Branch 7 → 9 taken 866 times.
870 if (previousStage >= DEP_GRAPH_VISUALIZER)
379 4 return;
380
381
1/2
✓ Branch 9 → 10 taken 866 times.
✗ Branch 9 → 47 not taken.
866 Timer timer(&compilerOutput.times.depGraphVisualizer);
382
1/2
✓ Branch 10 → 11 taken 866 times.
✗ Branch 10 → 47 not taken.
866 timer.start();
383
384 // Generate dot code for this source file
385
1/2
✓ Branch 11 → 12 taken 866 times.
✗ Branch 11 → 47 not taken.
866 std::stringstream dotCode;
386
1/2
✓ Branch 12 → 13 taken 866 times.
✗ Branch 12 → 45 not taken.
866 visualizerPreamble(dotCode);
387
1/2
✓ Branch 13 → 14 taken 866 times.
✗ Branch 13 → 45 not taken.
866 DependencyGraphVisualizer depGraphVisualizer(resourceManager, this);
388
1/2
✓ Branch 14 → 15 taken 866 times.
✗ Branch 14 → 43 not taken.
866 depGraphVisualizer.getDependencyGraph(dotCode);
389
1/2
✓ Branch 15 → 16 taken 866 times.
✗ Branch 15 → 43 not taken.
866 dotCode << "}";
390
391 // Dump the serialized AST string and the SVG file
392
1/2
✓ Branch 16 → 17 taken 866 times.
✗ Branch 16 → 34 not taken.
866 compilerOutput.depGraphString = dotCode.str();
393
394
1/2
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 27 taken 866 times.
866 if (cliOptions.dump.dumpDependencyGraph)
395 visualizerOutput("Dependency Graph", compilerOutput.depGraphString);
396
397 866 previousStage = DEP_GRAPH_VISUALIZER;
398
1/2
✓ Branch 27 → 28 taken 866 times.
✗ Branch 27 → 43 not taken.
866 timer.stop();
399
1/2
✓ Branch 28 → 29 taken 866 times.
✗ Branch 28 → 41 not taken.
866 printStatusMessage("AST Visualizer", IO_AST, IO_AST, compilerOutput.times.depGraphVisualizer);
400 866 }
401
402 5637 void SourceFile::runIRGenerator() {
403 // Skip if restored from the cache or this stage has already been done
404
4/4
✓ Branch 2 → 3 taken 5635 times.
✓ Branch 2 → 4 taken 2 times.
✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 5 taken 5633 times.
5637 if (restoredFromCache || previousStage >= IR_GENERATOR)
405 4 return;
406
407
1/2
✓ Branch 5 → 6 taken 5633 times.
✗ Branch 5 → 60 not taken.
5633 Timer timer(&compilerOutput.times.irGenerator);
408
1/2
✓ Branch 6 → 7 taken 5633 times.
✗ Branch 6 → 60 not taken.
5633 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 5629 times.
5633 llvm::LLVMContext &llvmContext = cliOptions.useLTO ? resourceManager.ltoContext : context;
412
1/2
✓ Branch 10 → 11 taken 5633 times.
✗ Branch 10 → 41 not taken.
5633 llvmModule = std::make_unique<llvm::Module>(fileName, llvmContext);
413
414 // Generate this source file
415
1/2
✓ Branch 13 → 14 taken 5633 times.
✗ Branch 13 → 60 not taken.
5633 IRGenerator irGenerator(resourceManager, this);
416
1/2
✓ Branch 14 → 15 taken 5633 times.
✗ Branch 14 → 42 not taken.
5633 irGenerator.visit(ast);
417
418 // Save the ir string in the compiler output
419
3/4
✓ Branch 16 → 17 taken 5633 times.
✗ Branch 16 → 18 not taken.
✓ Branch 17 → 18 taken 5627 times.
✓ Branch 17 → 23 taken 6 times.
5633 if (cliOptions.dump.dumpIR || cliOptions.testMode)
420
1/2
✓ Branch 19 → 20 taken 5627 times.
✗ Branch 19 → 43 not taken.
5627 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 5633 times.
5633 if (cliOptions.dump.dumpIR)
424 dumpOutput(compilerOutput.irString, "Unoptimized IR Code", "ir-code.ll");
425
426 5633 previousStage = IR_GENERATOR;
427
1/2
✓ Branch 36 → 37 taken 5633 times.
✗ Branch 36 → 58 not taken.
5633 timer.stop();
428
1/2
✓ Branch 37 → 38 taken 5633 times.
✗ Branch 37 → 56 not taken.
5633 printStatusMessage("IR Generator", IO_AST, IO_IR, compilerOutput.times.irGenerator);
429 5633 }
430
431 5367 void SourceFile::runDefaultIROptimizer() {
432
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 5367 times.
5367 assert(!cliOptions.useLTO);
433
434 // Skip if restored from the cache or this stage has already been done
435
5/8
✓ Branch 4 → 5 taken 5365 times.
✓ Branch 4 → 8 taken 2 times.
✓ Branch 5 → 6 taken 5364 times.
✓ Branch 5 → 8 taken 1 time.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 9 taken 5364 times.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 9 not taken.
5367 if (restoredFromCache || previousStage > IR_OPTIMIZER || (previousStage == IR_OPTIMIZER && !cliOptions.testMode))
436 3 return;
437
438
1/2
✓ Branch 9 → 10 taken 5364 times.
✗ Branch 9 → 60 not taken.
5364 Timer timer(&compilerOutput.times.irOptimizer);
439
1/2
✓ Branch 10 → 11 taken 5364 times.
✗ Branch 10 → 60 not taken.
5364 timer.start();
440
441 // Optimize this source file
442
1/2
✓ Branch 11 → 12 taken 5364 times.
✗ Branch 11 → 60 not taken.
5364 IROptimizer irOptimizer(resourceManager, this);
443
1/2
✓ Branch 12 → 13 taken 5364 times.
✗ Branch 12 → 58 not taken.
5364 irOptimizer.prepare();
444
1/2
✓ Branch 13 → 14 taken 5364 times.
✗ Branch 13 → 58 not taken.
5364 irOptimizer.optimizeDefault();
445
446 // Save the optimized ir string in the compiler output
447
3/4
✓ Branch 14 → 15 taken 5364 times.
✗ Branch 14 → 16 not taken.
✓ Branch 15 → 16 taken 5358 times.
✓ Branch 15 → 21 taken 6 times.
5364 if (cliOptions.dump.dumpIR || cliOptions.testMode)
448
1/2
✓ Branch 17 → 18 taken 5358 times.
✗ Branch 17 → 40 not taken.
5358 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 5364 times.
5364 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 5364 previousStage = IR_OPTIMIZER;
456
1/2
✓ Branch 35 → 36 taken 5364 times.
✗ Branch 35 → 58 not taken.
5364 timer.stop();
457
1/2
✓ Branch 36 → 37 taken 5364 times.
✗ Branch 36 → 56 not taken.
5364 printStatusMessage("IR Optimizer", IO_IR, IO_IR, compilerOutput.times.irOptimizer);
458 5364 }
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 5451 void SourceFile::runObjectEmitter() {
542 // Skip if restored from the cache or this stage has already been done
543
4/4
✓ Branch 2 → 3 taken 5449 times.
✓ Branch 2 → 4 taken 2 times.
✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 5 taken 5447 times.
5451 if (restoredFromCache || previousStage >= OBJECT_EMITTER)
544 5 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 5445 times.
✓ Branch 6 → 7 taken 1 time.
✓ Branch 6 → 8 taken 1 time.
5447 if (cliOptions.useLTO && !isMainFile)
548 1 return;
549
550
1/2
✓ Branch 8 → 9 taken 5446 times.
✗ Branch 8 → 82 not taken.
5446 Timer timer(&compilerOutput.times.objectEmitter);
551
1/2
✓ Branch 9 → 10 taken 5446 times.
✗ Branch 9 → 82 not taken.
5446 timer.start();
552
553 // Deduce an object file path
554
2/4
✓ Branch 10 → 11 taken 5446 times.
✗ Branch 10 → 58 not taken.
✓ Branch 11 → 12 taken 5446 times.
✗ Branch 11 → 56 not taken.
5446 objectFilePath = cliOptions.outputDir / filePath.filename();
555
2/4
✓ Branch 15 → 16 taken 5446 times.
✗ Branch 15 → 62 not taken.
✓ Branch 16 → 17 taken 5446 times.
✗ Branch 16 → 60 not taken.
5446 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 5446 std::unique_ptr<AbstractObjectEmitter> objectEmitter;
561 #ifdef SPICE_ENABLE_TPDE
562
2/2
✓ Branch 18 → 19 taken 1 time.
✓ Branch 18 → 26 taken 5445 times.
5446 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 5445 times.
✗ Branch 26 → 64 not taken.
5445 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 5446 times.
✗ Branch 31 → 80 not taken.
5446 objectEmitter->emit(objectFilePath);
574
575 // Save assembly string in the compiler output (TPDE emits a placeholder note)
576
5/6
✓ Branch 32 → 33 taken 5350 times.
✓ Branch 32 → 37 taken 96 times.
✓ Branch 33 → 34 taken 5350 times.
✗ Branch 33 → 35 not taken.
✓ Branch 34 → 35 taken 5344 times.
✓ Branch 34 → 37 taken 6 times.
5446 if (cliOptions.isNativeTarget && (cliOptions.dump.dumpAssembly || cliOptions.testMode))
577
1/2
✓ Branch 36 → 37 taken 5344 times.
✗ Branch 36 → 80 not taken.
5344 objectEmitter->getASMString(compilerOutput.asmString);
578
579 // Dump assembly code
580
1/2
✗ Branch 37 → 38 not taken.
✓ Branch 37 → 50 taken 5446 times.
5446 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 5446 previousStage = OBJECT_EMITTER;
587
1/2
✓ Branch 50 → 51 taken 5446 times.
✗ Branch 50 → 80 not taken.
5446 timer.stop();
588
1/2
✓ Branch 51 → 52 taken 5446 times.
✗ Branch 51 → 78 not taken.
5446 printStatusMessage("Object Emitter", IO_IR, IO_OBJECT_FILE, compilerOutput.times.objectEmitter);
589 5446 }
590
591 5451 void SourceFile::concludeCompilation() {
592 // Handle cache-restored files: register all cached objects with linker
593
2/2
✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 51 taken 5449 times.
5451 if (restoredFromCache) {
594
2/2
✓ Branch 17 → 5 taken 2 times.
✓ Branch 17 → 18 taken 2 times.
6 for (const auto &cachedObjectFilePath : cachedObjectFilePaths)
595
1/2
✓ Branch 7 → 8 taken 2 times.
✗ Branch 7 → 103 not taken.
2 resourceManager.linker.addFileToLinkage(cachedObjectFilePath);
596
1/2
✗ Branch 32 → 20 not taken.
✓ Branch 32 → 33 taken 2 times.
4 for (const auto &flag : sourceLinkerFlags)
597 resourceManager.linker.addLinkerFlag(flag);
598
1/2
✗ Branch 49 → 35 not taken.
✓ Branch 49 → 50 taken 2 times.
4 for (const auto &path : sourceAdditionalSourcePaths)
599 resourceManager.linker.addAdditionalSourcePath(path);
600 2 return;
601 }
602
603
2/2
✓ Branch 51 → 52 taken 2 times.
✓ Branch 51 → 53 taken 5447 times.
5449 if (previousStage >= FINISHED)
604 2 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 5446 times.
✓ Branch 54 → 56 taken 1 time.
5447 if (!objectFilePath.empty())
610 5446 resourceManager.linker.addFileToLinkage(objectFilePath);
611
612 // Cache the source file
613
2/2
✓ Branch 56 → 57 taken 6 times.
✓ Branch 56 → 58 taken 5441 times.
5447 if (!cliOptions.ignoreCache)
614 6 resourceManager.cacheManager.cacheSourceFile(this);
615
616 // Save type registry as string in the compiler output
617
5/6
✓ Branch 58 → 59 taken 762 times.
✓ Branch 58 → 65 taken 4685 times.
✓ Branch 59 → 60 taken 762 times.
✗ Branch 59 → 61 not taken.
✓ Branch 60 → 61 taken 758 times.
✓ Branch 60 → 65 taken 4 times.
5447 if (isMainFile && (cliOptions.dump.dumpTypes || cliOptions.testMode))
618
1/2
✓ Branch 61 → 62 taken 758 times.
✗ Branch 61 → 109 not taken.
758 compilerOutput.typesString = TypeRegistry::dump();
619
620 // Dump type registry
621
3/4
✓ Branch 65 → 66 taken 762 times.
✓ Branch 65 → 79 taken 4685 times.
✗ Branch 66 → 67 not taken.
✓ Branch 66 → 79 taken 762 times.
5447 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 762 times.
✓ Branch 79 → 83 taken 4685 times.
✓ Branch 80 → 81 taken 762 times.
✗ Branch 80 → 82 not taken.
✓ Branch 81 → 82 taken 758 times.
✓ Branch 81 → 83 taken 4 times.
5447 if (isMainFile && (cliOptions.dump.dumpCacheStats || cliOptions.testMode))
626 758 dumpCacheStats();
627
628 // Dump lookup cache statistics
629
3/4
✓ Branch 83 → 84 taken 762 times.
✓ Branch 83 → 97 taken 4685 times.
✗ Branch 84 → 85 not taken.
✓ Branch 84 → 97 taken 762 times.
5447 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 5447 times.
5447 if (cliOptions.printDebugOutput)
633 std::cout << "Finished compiling " << fileName << std::endl;
634
635 5447 previousStage = FINISHED;
636 }
637
638 8016 void SourceFile::runFrontEnd() { // NOLINT(misc-no-recursion)
639 8016 runLexer();
640
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 8016 times.
8016 CHECK_ABORT_FLAG_V()
641 8016 runParser();
642
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 8016 times.
8016 CHECK_ABORT_FLAG_V()
643 8016 runCSTVisualizer();
644
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 8016 times.
8016 CHECK_ABORT_FLAG_V()
645 8016 runASTBuilder();
646
1/2
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 8016 times.
8016 CHECK_ABORT_FLAG_V()
647 8016 runASTVisualizer();
648
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 8016 times.
8016 CHECK_ABORT_FLAG_V()
649 8016 runImportCollector();
650
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 8016 times.
8016 CHECK_ABORT_FLAG_V()
651 8016 runSymbolTableBuilder();
652
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 8016 times.
8016 CHECK_ABORT_FLAG_V()
653 }
654
655 1236 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 1236 times.
✗ Branch 2 → 72 not taken.
1236 mergeNameRegistriesRecursive();
661
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 1236 times.
1236 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 1236 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 1200 times.
✓ Branch 7 → 70 taken 36 times.
1236 runTypeCheckerPre(); // Visit the dependency tree from bottom to top
669
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 1200 times.
1200 CHECK_ABORT_FLAG_V()
670 // The second run to ensure, also generic scopes are type-checked properly
671
2/2
✓ Branch 11 → 12 taken 876 times.
✓ Branch 11 → 70 taken 324 times.
1200 runTypeCheckerPost(); // Visit the dependency tree from top to bottom in topological order
672
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 876 times.
876 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 14 times.
✓ Branch 49 → 50 taken 876 times.
890 do {
680 890 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 890 std::vector<SourceFile *> sourceFilesSnapshot;
686
1/2
✓ Branch 16 → 17 taken 890 times.
✗ Branch 16 → 67 not taken.
890 sourceFilesSnapshot.reserve(resourceManager.sourceFiles.size());
687
5/8
✓ Branch 17 → 18 taken 890 times.
✗ Branch 17 → 65 not taken.
✓ Branch 18 → 19 taken 890 times.
✗ Branch 18 → 65 not taken.
✓ Branch 19 → 20 taken 890 times.
✗ Branch 19 → 65 not taken.
✓ Branch 26 → 21 taken 6207 times.
✓ Branch 26 → 27 taken 890 times.
7097 for (const std::unique_ptr<SourceFile> &sourceFile : resourceManager.sourceFiles | std::views::values)
688
1/2
✓ Branch 23 → 24 taken 6207 times.
✗ Branch 23 → 64 not taken.
6207 sourceFilesSnapshot.push_back(sourceFile.get());
689
2/2
✓ Branch 43 → 29 taken 6207 times.
✓ Branch 43 → 44 taken 890 times.
7987 for (SourceFile *sourceFile : sourceFilesSnapshot) {
690
2/2
✓ Branch 31 → 32 taken 18 times.
✓ Branch 31 → 34 taken 6189 times.
6207 if (sourceFile->reVisitRequested) {
691
1/2
✓ Branch 32 → 33 taken 18 times.
✗ Branch 32 → 66 not taken.
18 sourceFile->runTypeCheckerPost();
692 18 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 890 times.
890 if (resourceManager.sourceFiles.size() > sourceFilesSnapshot.size())
698 anySourceFileRevisitPending = true;
699 890 } while (anySourceFileRevisitPending);
700
1/2
✗ Branch 51 → 52 not taken.
✓ Branch 51 → 53 taken 876 times.
876 CHECK_ABORT_FLAG_V()
701 // Visualize dependency graph
702
1/2
✓ Branch 53 → 54 taken 876 times.
✗ Branch 53 → 70 not taken.
876 runDependencyGraphVisualizer();
703
1/2
✗ Branch 55 → 56 not taken.
✓ Branch 55 → 57 taken 876 times.
876 CHECK_ABORT_FLAG_V()
704
1/2
✓ Branch 59 → 60 taken 876 times.
✗ Branch 59 → 62 not taken.
1236 }
705
706 11666 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 6973 times.
✓ Branch 2 → 4 taken 4693 times.
11666 if (backEndStarted)
710 6973 return;
711 4693 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 4693 times.
✗ Branch 4 → 16 not taken.
✓ Branch 5 → 6 taken 4693 times.
✗ Branch 5 → 16 not taken.
✓ Branch 6 → 7 taken 4693 times.
✗ Branch 6 → 16 not taken.
✓ Branch 12 → 8 taken 10667 times.
✓ Branch 12 → 13 taken 4693 times.
15360 for (SourceFile *sourceFile : dependencies | std::views::values)
715
1/2
✓ Branch 9 → 10 taken 10667 times.
✗ Branch 9 → 16 not taken.
10667 sourceFile->collectBackEndSourceFiles(backEndSourceFiles);
716
717
1/2
✓ Branch 13 → 14 taken 4693 times.
✗ Branch 13 → 17 not taken.
4693 backEndSourceFiles.push_back(this);
718 }
719
720 4693 void SourceFile::runBackEndForThisFile() {
721 4693 runIRGenerator();
722
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 4693 times.
4693 CHECK_ABORT_FLAG_V()
723
2/2
✓ Branch 6 → 7 taken 1 time.
✓ Branch 6 → 19 taken 4692 times.
4693 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 4692 runDefaultIROptimizer();
732
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 4692 times.
4692 CHECK_ABORT_FLAG_V()
733 }
734 4693 runObjectEmitter();
735 }
736
737 999 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 999 std::vector<SourceFile *> backEndSourceFiles;
741
1/2
✓ Branch 2 → 3 taken 999 times.
✗ Branch 2 → 102 not taken.
999 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 169 times.
✓ Branch 4 → 6 taken 830 times.
999 if (backEndSourceFiles.empty())
745 169 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 830 times.
✗ Branch 6 → 9 not taken.
✓ Branch 7 → 8 taken 830 times.
✗ Branch 7 → 9 not taken.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 830 times.
830 const bool dumpRequested = cliOptions.dump.dumpIR || cliOptions.dump.dumpAssembly || cliOptions.dump.dumpObjectFiles;
754
1/2
✓ Branch 12 → 13 taken 830 times.
✗ Branch 12 → 90 not taken.
830 const size_t jobCount = std::min(resourceManager.getCompileJobCount(), backEndSourceFiles.size());
755
1/6
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 18 taken 830 times.
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 18 not taken.
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 18 not taken.
830 const bool runParallel = jobCount > 1 && !cliOptions.useLTO && !dumpRequested;
756
757
1/2
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 42 taken 830 times.
830 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 4693 times.
✓ Branch 59 → 60 taken 830 times.
6353 for (SourceFile *sourceFile : backEndSourceFiles) {
765
1/2
✓ Branch 46 → 47 taken 4693 times.
✗ Branch 46 → 100 not taken.
4693 sourceFile->runBackEndForThisFile();
766
1/2
✗ Branch 48 → 49 not taken.
✓ Branch 48 → 50 taken 4693 times.
4693 CHECK_ABORT_FLAG_V()
767 }
768 }
769
1/2
✗ Branch 62 → 63 not taken.
✓ Branch 62 → 64 taken 830 times.
830 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 4693 times.
✓ Branch 78 → 79 taken 830 times.
6353 for (SourceFile *sourceFile : backEndSourceFiles)
774
1/2
✓ Branch 68 → 69 taken 4693 times.
✗ Branch 68 → 101 not taken.
4693 sourceFile->concludeCompilation();
775
776
2/2
✓ Branch 79 → 80 taken 4 times.
✓ Branch 79 → 83 taken 826 times.
830 if (isMainFile) {
777
1/2
✓ Branch 80 → 81 taken 4 times.
✗ Branch 80 → 102 not taken.
4 resourceManager.totalTimer.stop();
778
1/2
✗ Branch 81 → 82 not taken.
✓ Branch 81 → 83 taken 4 times.
4 if (cliOptions.printDebugOutput)
779 dumpCompilationStats();
780 }
781
2/2
✓ Branch 85 → 86 taken 830 times.
✓ Branch 85 → 88 taken 169 times.
999 }
782
783 11886 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 11884 times.
✓ Branch 2 → 4 taken 2 times.
11886 if (sourceFile->parent != nullptr)
792 11884 sourceFile->isMainFile = false;
793 11886 dependencies.emplace(dependencyName, sourceFile);
794
795 // Add the dependant
796
1/2
✓ Branch 5 → 6 taken 11886 times.
✗ Branch 5 → 7 not taken.
11886 sourceFile->dependants.push_back(this);
797 11886 }
798
799 766836 bool SourceFile::imports(const SourceFile *sourceFile) const {
800 3496671 return std::ranges::any_of(dependencies, [=](const auto &dependency) { return dependency.second == sourceFile; });
801 }
802
803 36117 SourceFile *SourceFile::requestRuntimeModule(RuntimeModule runtimeModule) {
804 // Check if the module was already imported
805
2/2
✓ Branch 3 → 4 taken 30159 times.
✓ Branch 3 → 6 taken 5958 times.
36117 if (isRuntimeModuleAvailable(runtimeModule))
806 30159 return resourceManager.runtimeModuleManager.getModule(runtimeModule);
807 5958 return resourceManager.runtimeModuleManager.requestModule(this, runtimeModule);
808 }
809
810 40421 bool SourceFile::isRuntimeModuleAvailable(RuntimeModule runtimeModule) const { return importedRuntimeModules & runtimeModule; }
811
812 418348 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 179921 times.
✓ Branch 2 → 5 taken 238427 times.
✓ Branch 4 → 5 taken 179094 times.
✓ Branch 4 → 6 taken 827 times.
✓ Branch 7 → 8 taken 417521 times.
✓ Branch 7 → 15 taken 827 times.
418348 if (keepNewOnCollision || !exportedNameRegistry.contains(symbolName)) // Overwrite potential existing entry
815 835042 exportedNameRegistry[symbolName] = {symbolName, typeId, entry, scope, importEntry};
816 else // Name collision => we must remove the existing entry
817 827 exportedNameRegistry.erase(symbolName);
818
3/8
✓ Branch 8 → 9 taken 417521 times.
✗ Branch 8 → 22 not taken.
✓ Branch 9 → 10 taken 417521 times.
✗ Branch 9 → 17 not taken.
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 417521 times.
✗ Branch 19 → 20 not taken.
✗ Branch 19 → 21 not taken.
1253390 }
819
820 2631652 const NameRegistryEntry *SourceFile::getNameRegistryEntry(const std::string &symbolName) const {
821
1/2
✓ Branch 2 → 3 taken 2631652 times.
✗ Branch 2 → 13 not taken.
2631652 const auto it = exportedNameRegistry.find(symbolName);
822
2/2
✓ Branch 5 → 6 taken 1270014 times.
✓ Branch 5 → 7 taken 1361638 times.
2631652 if (it == exportedNameRegistry.end())
823 1270014 return nullptr;
824
825 // Resolve registry entry for the given name
826 1361638 const NameRegistryEntry *entry = &it->second;
827
828 // Mark the import entry as used
829
2/2
✓ Branch 8 → 9 taken 161058 times.
✓ Branch 8 → 10 taken 1200580 times.
1361638 if (entry->importEntry != nullptr)
830 161058 entry->importEntry->used = true;
831
832 1361638 return entry;
833 }
834
835 2804888 llvm::Type *SourceFile::getLLVMType(const Type *type) {
836 // Check if the type is already in the mapping
837
1/2
✓ Branch 2 → 3 taken 2804888 times.
✗ Branch 2 → 13 not taken.
2804888 const auto it = typeToLLVMTypeMapping.find(type);
838
2/2
✓ Branch 5 → 6 taken 2722829 times.
✓ Branch 5 → 8 taken 82059 times.
2804888 if (it != typeToLLVMTypeMapping.end())
839 2722829 return it->second;
840
841 // If not, generate the LLVM type
842
1/2
✓ Branch 8 → 9 taken 82059 times.
✗ Branch 8 → 13 not taken.
82059 llvm::Type *llvmType = type->toLLVMType(this);
843
1/2
✓ Branch 9 → 10 taken 82059 times.
✗ Branch 9 → 13 not taken.
82059 typeToLLVMTypeMapping[type] = llvmType;
844 82059 return llvmType;
845 }
846
847 16918 void SourceFile::checkForSoftErrors() const {
848 // Check if there are any soft errors and if so, print them
849
2/2
✓ Branch 3 → 4 taken 260 times.
✓ Branch 3 → 27 taken 16658 times.
16918 if (!resourceManager.errorManager.softErrors.empty()) {
850
1/2
✓ Branch 4 → 5 taken 260 times.
✗ Branch 4 → 37 not taken.
260 std::stringstream errorStream;
851
1/2
✓ Branch 5 → 6 taken 260 times.
✗ Branch 5 → 35 not taken.
260 errorStream << "There are unresolved errors. Please fix them and recompile.";
852
2/2
✓ Branch 21 → 8 taken 402 times.
✓ Branch 21 → 22 taken 260 times.
922 for (const auto &[codeLoc, message] : resourceManager.errorManager.softErrors)
853
2/4
✓ Branch 10 → 11 taken 402 times.
✗ Branch 10 → 28 not taken.
✓ Branch 11 → 12 taken 402 times.
✗ Branch 11 → 28 not taken.
402 errorStream << "\n\n" << message;
854
2/4
✓ Branch 23 → 24 taken 260 times.
✗ Branch 23 → 32 not taken.
✓ Branch 24 → 25 taken 260 times.
✗ Branch 24 → 29 not taken.
260 throw CompilerError(UNRESOLVED_SOFT_ERRORS, errorStream.str());
855 260 }
856 16658 }
857
858 119 bool SourceFile::isLibraryOutput() const {
859
1/2
✓ Branch 2 → 3 taken 119 times.
✗ Branch 2 → 4 not taken.
238 return cliOptions.outputContainer == OutputContainer::STATIC_LIBRARY ||
860
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 119 times.
238 cliOptions.outputContainer == OutputContainer::SHARED_LIBRARY;
861 }
862
863 905 void SourceFile::collectAndPrintWarnings() { // NOLINT(misc-no-recursion)
864 // Skip if restored from cache (no scope tree available), or if already visited. The latter guard keeps circular
865 // imports from recursing infinitely, since the dependency graph may contain cycles.
866
3/4
✓ Branch 2 → 3 taken 905 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 30 times.
✓ Branch 3 → 5 taken 875 times.
905 if (restoredFromCache || warningsCollected)
867 30 return;
868 875 warningsCollected = true;
869 // Print warnings for all dependencies
870
5/8
✓ Branch 5 → 6 taken 875 times.
✗ Branch 5 → 35 not taken.
✓ Branch 6 → 7 taken 875 times.
✗ Branch 6 → 35 not taken.
✓ Branch 7 → 8 taken 875 times.
✗ Branch 7 → 35 not taken.
✓ Branch 14 → 9 taken 883 times.
✓ Branch 14 → 15 taken 875 times.
1758 for (SourceFile *sourceFile : dependencies | std::views::values)
871
2/2
✓ Branch 10 → 11 taken 101 times.
✓ Branch 10 → 12 taken 782 times.
883 if (!sourceFile->isStdFile)
872
1/2
✓ Branch 11 → 12 taken 101 times.
✗ Branch 11 → 35 not taken.
101 sourceFile->collectAndPrintWarnings();
873 // Collect warnings for this file
874
1/2
✓ Branch 15 → 16 taken 875 times.
✗ Branch 15 → 18 not taken.
875 if (!ignoreWarnings)
875 875 globalScope->collectWarnings(compilerOutput.warnings);
876 // Print warnings for this file
877
2/2
✓ Branch 32 → 20 taken 469 times.
✓ Branch 32 → 33 taken 875 times.
2219 for (const CompilerWarning &warning : compilerOutput.warnings)
878
1/2
✓ Branch 22 → 23 taken 469 times.
✗ Branch 22 → 36 not taken.
469 warning.print();
879 }
880
881 void SourceFile::collectAndPrintLintFindings() { // NOLINT(misc-no-recursion)
882 // Skip if restored from cache (no AST available), or if already visited. The latter guard keeps circular
883 // imports from recursing infinitely, since the dependency graph may contain cycles.
884 if (restoredFromCache || lintFindingsCollected)
885 return;
886 lintFindingsCollected = true;
887 // Collect lint findings for all dependencies
888 for (SourceFile *sourceFile : dependencies | std::views::values)
889 if (!sourceFile->isStdFile)
890 sourceFile->collectAndPrintLintFindings();
891 // Collect lint findings for this file
892 if (!ignoreWarnings) {
893 LintPass lintPass(resourceManager, this);
894 compilerOutput.lintFindings = lintPass.lint(ast);
895 }
896 // Print lint findings for this file
897 for (const LintFinding &finding : compilerOutput.lintFindings)
898 finding.print();
899 }
900
901 57660 const SourceFile *SourceFile::getRootSourceFile() const { // NOLINT(misc-no-recursion)
902
2/2
✓ Branch 2 → 3 taken 17374 times.
✓ Branch 2 → 4 taken 40286 times.
57660 return isMainFile ? this : parent->getRootSourceFile();
903 }
904
905 71061 bool SourceFile::isRT(RuntimeModule runtimeModule) const {
906
2/4
✓ Branch 2 → 3 taken 71061 times.
✗ Branch 2 → 38 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 71061 times.
71061 assert(IDENTIFYING_TOP_LEVEL_NAMES.contains(runtimeModule));
907
1/2
✓ Branch 5 → 6 taken 71061 times.
✗ Branch 5 → 38 not taken.
71061 const char *topLevelName = IDENTIFYING_TOP_LEVEL_NAMES.at(runtimeModule);
908
2/4
✓ Branch 8 → 9 taken 71061 times.
✗ Branch 8 → 28 not taken.
✓ Branch 9 → 10 taken 71061 times.
✗ Branch 9 → 26 not taken.
142122 const auto it = exportedNameRegistry.find(topLevelName);
909
2/2
✓ Branch 14 → 15 taken 9654 times.
✓ Branch 14 → 16 taken 61407 times.
71061 if (it == exportedNameRegistry.end())
910 9654 return false;
911
2/4
✓ Branch 18 → 19 taken 61407 times.
✗ Branch 18 → 34 not taken.
✓ Branch 19 → 20 taken 61407 times.
✗ Branch 19 → 32 not taken.
184221 return exportedNameRegistry.at(topLevelName).targetEntry->scope == globalScope.get();
912 }
913
914 23697 bool SourceFile::haveAllDependantsBeenTypeChecked() const {
915 23697 return std::ranges::all_of(dependants, [this](const SourceFile *dependant) {
916 // Ignore dependants that are part of the same import cycle (i.e. this file transitively depends on them). They
917 // cannot be type-checked before us either, so waiting on them would deadlock the whole cycle. Such a strongly
918 // connected component is type-checked as a unit and converges through the reVisitRequested fixpoint loop instead.
919
2/2
✓ Branch 3 → 4 taken 1814 times.
✓ Branch 3 → 5 taken 66668 times.
68482 if (dependsOn(dependant))
920 1814 return true;
921 66668 return dependant->totalTypeCheckerRuns >= 1;
922 23697 });
923 }
924
925 /**
926 * Check whether this source file transitively depends on (imports) the given other source file.
927 * Used to detect strongly connected components (import cycles) in the dependency graph.
928 *
929 * @param other Potential (transitive) dependency
930 * @return true if this file reaches the other file by following dependency edges
931 */
932 68482 bool SourceFile::dependsOn(const SourceFile *other) const {
933 68482 std::unordered_set<const SourceFile *> visited;
934
1/2
✓ Branch 3 → 4 taken 68482 times.
✗ Branch 3 → 35 not taken.
68482 std::queue<const SourceFile *> worklist;
935
1/2
✓ Branch 4 → 5 taken 68482 times.
✗ Branch 4 → 30 not taken.
68482 worklist.push(this);
936
1/2
✓ Branch 5 → 6 taken 68482 times.
✗ Branch 5 → 31 not taken.
68482 visited.insert(this);
937
2/2
✓ Branch 24 → 7 taken 360186 times.
✓ Branch 24 → 25 taken 66668 times.
426854 while (!worklist.empty()) {
938 360186 const SourceFile *current = worklist.front();
939 360186 worklist.pop();
940
5/8
✓ Branch 9 → 10 taken 360186 times.
✗ Branch 9 → 32 not taken.
✓ Branch 10 → 11 taken 360186 times.
✗ Branch 10 → 32 not taken.
✓ Branch 11 → 12 taken 360186 times.
✗ Branch 11 → 32 not taken.
✓ Branch 21 → 13 taken 615757 times.
✓ Branch 21 → 22 taken 358372 times.
974129 for (const SourceFile *dependency : current->dependencies | std::views::values) {
941
2/2
✓ Branch 14 → 15 taken 1814 times.
✓ Branch 14 → 16 taken 613943 times.
615757 if (dependency == other)
942 1814 return true;
943
3/4
✓ Branch 16 → 17 taken 613943 times.
✗ Branch 16 → 32 not taken.
✓ Branch 17 → 18 taken 293666 times.
✓ Branch 17 → 19 taken 320277 times.
613943 if (visited.insert(dependency).second)
944
1/2
✓ Branch 18 → 19 taken 293666 times.
✗ Branch 18 → 32 not taken.
293666 worklist.push(dependency);
945 }
946 }
947 66668 return false;
948 68482 }
949
950 /**
951 * Acquire all publicly visible symbols from the imported source file and put them in the name registry of the current one.
952 * But only do that for the symbols that are actually defined in the imported source file. Do not allow transitive dependencies.
953 * Here, we also register privately visible symbols to know that the symbol exist. The error handling regarding the visibility
954 * is issued later in the pipeline.
955 *
956 * @param importedSourceFile Imported source file
957 * @param importName First fragment of all fully qualified symbol names from that import
958 */
959 11880 void SourceFile::mergeNameRegistries(const SourceFile &importedSourceFile, const std::string &importName) {
960 // Retrieve import entry
961 11880 SymbolTableEntry *importEntry = globalScope->lookupStrict(importName);
962
3/4
✓ Branch 6 → 7 taken 5958 times.
✓ Branch 6 → 10 taken 5922 times.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 5958 times.
11880 assert(importEntry != nullptr || importName.starts_with("__")); // Runtime imports start with two underscores
963
964
2/2
✓ Branch 42 → 12 taken 706009 times.
✓ Branch 42 → 43 taken 11880 times.
717889 for (const auto &[originalName, entry] : importedSourceFile.exportedNameRegistry) {
965 // Skip if we introduce a transitive dependency
966
2/2
✓ Branch 16 → 17 taken 421784 times.
✓ Branch 16 → 18 taken 284225 times.
706009 if (entry.targetScope->sourceFile->globalScope != importedSourceFile.globalScope)
967 421784 continue;
968 // Add the fully qualified name
969
1/2
✓ Branch 18 → 19 taken 284225 times.
✗ Branch 18 → 52 not taken.
284225 std::string newName = importName;
970
1/2
✓ Branch 19 → 20 taken 284225 times.
✗ Branch 19 → 50 not taken.
284225 newName += SCOPE_ACCESS_TOKEN;
971
1/2
✓ Branch 20 → 21 taken 284225 times.
✗ Branch 20 → 50 not taken.
284225 newName += originalName;
972 568450 exportedNameRegistry.emplace(newName,
973
3/8
✓ Branch 21 → 22 taken 284225 times.
✗ Branch 21 → 49 not taken.
✓ Branch 22 → 23 taken 284225 times.
✗ Branch 22 → 44 not taken.
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 284225 times.
✗ Branch 46 → 47 not taken.
✗ Branch 46 → 48 not taken.
284225 NameRegistryEntry{newName, entry.typeId, entry.targetEntry, entry.targetScope, importEntry});
974 // Add the shortened name, considering the name collision. A symbol defined in the importing file itself always
975 // shadows imported symbols of the same name. Since this merge runs after the file built its own registry (so that
976 // circular imports work), we must explicitly avoid letting an import overwrite or erase such an own symbol - the old
977 // ordering achieved this implicitly by registering own symbols last with keep-on-collision.
978
1/2
✓ Branch 26 → 27 taken 284225 times.
✗ Branch 26 → 50 not taken.
284225 const auto existing = exportedNameRegistry.find(originalName);
979 const bool existingIsOwn =
980
4/4
✓ Branch 29 → 30 taken 12962 times.
✓ Branch 29 → 34 taken 271263 times.
✓ Branch 32 → 33 taken 3367 times.
✓ Branch 32 → 34 taken 9595 times.
284225 existing != exportedNameRegistry.end() && existing->second.targetScope->sourceFile->globalScope == globalScope;
981
2/2
✓ Branch 35 → 36 taken 280858 times.
✓ Branch 35 → 37 taken 3367 times.
284225 if (!existingIsOwn) {
982 280858 const bool keepOnCollision = importedSourceFile.alwaysKeepSymbolsOnNameCollision;
983
1/2
✓ Branch 36 → 37 taken 280858 times.
✗ Branch 36 → 50 not taken.
280858 addNameRegistryEntry(originalName, entry.typeId, entry.targetEntry, entry.targetScope, keepOnCollision, importEntry);
984 }
985 284225 }
986 11880 }
987
988 /**
989 * Recursively merge the exported name registries of all (transitive) dependencies into the respective importing source
990 * files. Each file merges its direct dependencies' registries exactly once (guarded by registriesMerged), so this is
991 * safe to call on overlapping subgraphs and on graphs that contain cycles (circular imports).
992 *
993 * Must only be called once every reachable file has built its own exported name registry (i.e. after the front-end).
994 */
995 9240 void SourceFile::mergeNameRegistriesRecursive() { // NOLINT(misc-no-recursion)
996
2/2
✓ Branch 2 → 3 taken 3149 times.
✓ Branch 2 → 4 taken 6091 times.
9240 if (registriesMerged)
997 3149 return;
998 6091 registriesMerged = true;
999
1000 // Merge the direct dependencies' registries into this file. Their own registries are fully built by now, so the
1001 // order in which the reachable files are visited does not matter (even across import cycles).
1002
2/2
✓ Branch 12 → 6 taken 5922 times.
✓ Branch 12 → 13 taken 6091 times.
12013 for (const auto &[importName, dependency] : dependencies)
1003
1/2
✓ Branch 9 → 10 taken 5922 times.
✗ Branch 9 → 24 not taken.
5922 mergeNameRegistries(*dependency, importName);
1004
1005 // Recurse into the dependencies to cover the rest of the reachable graph
1006
5/8
✓ Branch 13 → 14 taken 6091 times.
✗ Branch 13 → 25 not taken.
✓ Branch 14 → 15 taken 6091 times.
✗ Branch 14 → 25 not taken.
✓ Branch 15 → 16 taken 6091 times.
✗ Branch 15 → 25 not taken.
✓ Branch 21 → 17 taken 5922 times.
✓ Branch 21 → 22 taken 6091 times.
12013 for (SourceFile *dependency : dependencies | std::views::values)
1007
1/2
✓ Branch 18 → 19 taken 5922 times.
✗ Branch 18 → 25 not taken.
5922 dependency->mergeNameRegistriesRecursive();
1008 }
1009
1010 758 void SourceFile::dumpCacheStats() {
1011
1/2
✓ Branch 2 → 3 taken 758 times.
✗ Branch 2 → 32 not taken.
758 std::stringstream cacheStats;
1012
3/6
✓ Branch 3 → 4 taken 758 times.
✗ Branch 3 → 22 not taken.
✓ Branch 4 → 5 taken 758 times.
✗ Branch 4 → 20 not taken.
✓ Branch 5 → 6 taken 758 times.
✗ Branch 5 → 20 not taken.
758 cacheStats << FunctionManager::dumpLookupCacheStatistics() << std::endl;
1013
3/6
✓ Branch 7 → 8 taken 758 times.
✗ Branch 7 → 25 not taken.
✓ Branch 8 → 9 taken 758 times.
✗ Branch 8 → 23 not taken.
✓ Branch 9 → 10 taken 758 times.
✗ Branch 9 → 23 not taken.
758 cacheStats << StructManager::dumpLookupCacheStatistics() << std::endl;
1014
3/6
✓ Branch 11 → 12 taken 758 times.
✗ Branch 11 → 28 not taken.
✓ Branch 12 → 13 taken 758 times.
✗ Branch 12 → 26 not taken.
✓ Branch 13 → 14 taken 758 times.
✗ Branch 13 → 26 not taken.
758 cacheStats << InterfaceManager::dumpLookupCacheStatistics() << std::endl;
1015
1/2
✓ Branch 15 → 16 taken 758 times.
✗ Branch 15 → 29 not taken.
758 compilerOutput.cacheStats = cacheStats.str();
1016 758 }
1017
1018 void SourceFile::dumpCompilationStats() const {
1019 const size_t sourceFileCount = resourceManager.sourceFiles.size();
1020 const size_t totalLineCount = resourceManager.getTotalLineCount();
1021 const size_t totalTypeCount = TypeRegistry::getTypeCount();
1022 const size_t allocatedBytes = resourceManager.astNodeAlloc.getTotalAllocatedSize();
1023 const size_t allocationCount = resourceManager.astNodeAlloc.getAllocationCount();
1024 const size_t totalDuration = resourceManager.totalTimer.getDurationMilliseconds();
1025 std::cout << "\nSuccessfully compiled " << std::to_string(sourceFileCount) << " source file(s)";
1026 std::cout << " or " << std::to_string(totalLineCount) << " lines in total.\n";
1027 std::cout << "Total number of blocks allocated via BlockAllocator: " << CommonUtil::formatBytes(allocatedBytes);
1028 std::cout << " in " << std::to_string(allocationCount) << " allocations.\n";
1029 #ifndef NDEBUG
1030 resourceManager.astNodeAlloc.printAllocatedClassStatistic();
1031 #endif
1032 std::cout << "Total number of types: " << std::to_string(totalTypeCount) << "\n";
1033 std::cout << "Total compile time: " << std::to_string(totalDuration) << " ms\n";
1034 }
1035
1036 void SourceFile::dumpOutput(const std::string &content, const std::string &caption, const std::string &fileSuffix) const {
1037 if (cliOptions.dump.dumpToFiles) {
1038 // Dump to file
1039 const std::string dumpFileName = filePath.stem().string() + "-" + fileSuffix;
1040 std::filesystem::path dumpFilePath = cliOptions.outputDir / dumpFileName;
1041 dumpFilePath.make_preferred();
1042 FileUtil::writeToFile(dumpFilePath, content);
1043 } else {
1044 // Dump to console
1045 std::cout << "\n" << caption << ":\n" << content;
1046 }
1047
1048 // If the abort after dump is requested, set the abort compilation flag
1049 if (cliOptions.dump.abortAfterDump) {
1050 // If this is an IR dump whilst having optimization enabled, we may not abort when dumping unoptimized IR,
1051 // because we also have to dump the optimized IR
1052 if (cliOptions.dump.dumpIR && fileSuffix == "ir-code.ll") {
1053 resourceManager.abortCompilation = cliOptions.optLevel == OptLevel::O0;
1054 } else {
1055 resourceManager.abortCompilation = true;
1056 }
1057 }
1058 }
1059
1060 10600 void SourceFile::visualizerPreamble(std::stringstream &output) const {
1061
2/2
✓ Branch 2 → 3 taken 894 times.
✓ Branch 2 → 4 taken 9706 times.
10600 if (isMainFile)
1062 894 output << "digraph {\n rankdir=\"TB\";\n";
1063 else
1064 9706 output << "subgraph {\n";
1065
3/6
✓ Branch 6 → 7 taken 10600 times.
✗ Branch 6 → 13 not taken.
✓ Branch 7 → 8 taken 10600 times.
✗ Branch 7 → 11 not taken.
✓ Branch 8 → 9 taken 10600 times.
✗ Branch 8 → 11 not taken.
10600 output << " label=\"" << filePath.generic_string() << "\";\n";
1066 10600 }
1067
1068 void SourceFile::visualizerOutput(std::string outputName, const std::string &output) const {
1069 if (cliOptions.dump.dumpToFiles) {
1070 // Check if graphviz is installed
1071 // GCOV_EXCL_START
1072 if (!SystemUtil::isGraphvizInstalled())
1073 throw CompilerError(IO_ERROR, "Please check if you have installed Graphviz and added it to the PATH variable");
1074 // GCOV_EXCL_STOP
1075
1076 // Write to a dot file
1077 std::ranges::transform(outputName, outputName.begin(), ::tolower);
1078 dumpOutput(output, outputName, outputName + ".dot");
1079
1080 // Generate SVG. This only works if the dot code was dumped into a file
1081 std::cout << "\nGenerating SVG file ... ";
1082 const std::string dotFileName = filePath.stem().string() + "-" + outputName + ".dot";
1083 std::filesystem::path dotFilePath = cliOptions.outputDir / dotFileName;
1084 std::filesystem::path svgFilePath = dotFilePath;
1085 svgFilePath.replace_extension("svg");
1086 dotFilePath.make_preferred();
1087 svgFilePath.make_preferred();
1088 SystemUtil::exec("dot", {"-T", "svg", "-o", svgFilePath.string(), dotFilePath.string()});
1089 std::cout << "done.\nSVG file can be found at: " << svgFilePath << "\n";
1090 } else {
1091 // Dump to console
1092 std::cout << "\nSerialized " << outputName << ":\n\n" << output << "\n";
1093 }
1094
1095 // If the abort after dump is requested, set the abort compilation flag
1096 if (cliOptions.dump.abortAfterDump)
1097 resourceManager.abortCompilation = true;
1098 }
1099
1100 80454 void SourceFile::printStatusMessage(const char *stage, const CompileStageIOType &in, const CompileStageIOType &out,
1101 uint64_t stageRuntime, unsigned short stageRuns) const {
1102
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 30 taken 80454 times.
80454 if (cliOptions.printDebugOutput) {
1103 static constexpr const char *const compilerStageIoTypeName[6] = {"Code", "Tokens", "CST", "AST", "IR", "Obj"};
1104 // Build output string
1105 std::stringstream outputStr;
1106 outputStr << "[" << stage << "] for " << fileName << ": ";
1107 outputStr << compilerStageIoTypeName[in] << " --> " << compilerStageIoTypeName[out];
1108 outputStr << " (" << std::to_string(stageRuntime) << " ms";
1109 if (stageRuns > 0)
1110 outputStr << "; " << std::to_string(stageRuns) << " run(s)";
1111 outputStr << ")\n";
1112 // Print
1113 std::cout << outputStr.str();
1114 }
1115 80454 }
1116
1117 } // namespace spice::compiler
1118