GCC Code Coverage Report


Directory: ../
File: src/driver/Driver.cpp
Date: 2025-11-14 16:08:18
Coverage Exec Excl Total
Lines: 86.1% 198 3 233
Functions: 80.8% 21 0 26
Branches: 46.4% 270 8 590

Line Branch Exec Source
1 // Copyright (c) 2021-2025 ChilliBits. All rights reserved.
2
3 #include "Driver.h"
4
5 #include <exception/CliError.h>
6 #include <util/CommonUtil.h>
7 #include <util/CompilerWarning.h>
8 #include <util/FileUtil.h>
9
10 #include <llvm/Support/CommandLine.h>
11 #include <llvm/TargetParser/Host.h>
12 #include <llvm/TargetParser/Triple.h>
13
14 namespace spice::compiler {
15
16
3/6
✓ Branch 4 → 5 taken 440 times.
✗ Branch 4 → 51 not taken.
✓ Branch 7 → 8 taken 440 times.
✗ Branch 7 → 45 not taken.
✓ Branch 8 → 9 taken 440 times.
✗ Branch 8 → 43 not taken.
1760 Driver::Driver(CliOptions &foreignCliOptions, bool dryRun) : cliOptions(foreignCliOptions), performDryRun(dryRun) {
17 // Allow positional args
18 440 app.positionals_at_end();
19 440 app.allow_extras(false);
20
1/2
✓ Branch 17 → 18 taken 440 times.
✗ Branch 17 → 55 not taken.
880 app.footer("(c) Marc Auberer 2021-2025");
21
22 // Add version flag
23
4/8
✓ Branch 23 → 24 taken 440 times.
✗ Branch 23 → 70 not taken.
✓ Branch 24 → 25 taken 440 times.
✗ Branch 24 → 67 not taken.
✓ Branch 27 → 28 taken 440 times.
✗ Branch 27 → 61 not taken.
✓ Branch 28 → 29 taken 440 times.
✗ Branch 28 → 59 not taken.
1760 app.set_version_flag("--version,-v", CommonUtil::buildVersionInfo());
24
25 // Create sub-commands
26
1/2
✓ Branch 34 → 35 taken 440 times.
✗ Branch 34 → 74 not taken.
440 addBuildSubcommand();
27
1/2
✓ Branch 35 → 36 taken 440 times.
✗ Branch 35 → 74 not taken.
440 addRunSubcommand();
28
1/2
✓ Branch 36 → 37 taken 440 times.
✗ Branch 36 → 74 not taken.
440 addTestSubcommand();
29
1/2
✓ Branch 37 → 38 taken 440 times.
✗ Branch 37 → 74 not taken.
440 addInstallSubcommand();
30
1/2
✓ Branch 38 → 39 taken 440 times.
✗ Branch 38 → 74 not taken.
440 addUninstallSubcommand();
31
32 440 app.final_callback([&] {
33 // Print help text for the root command if no sub-command was given
34
2/4
✓ Branch 2 → 3 taken 437 times.
✗ Branch 2 → 122 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 15 taken 437 times.
437 if (app.get_subcommands().empty()) {
35 std::cout << app.help();
36 return;
37 }
38
39
4/4
✓ Branch 15 → 16 taken 436 times.
✓ Branch 15 → 17 taken 1 time.
✓ Branch 16 → 17 taken 1 time.
✓ Branch 16 → 46 taken 435 times.
437 if (shouldInstall || shouldUninstall) {
40 // Prepare the installation path
41
1/2
✓ Branch 17 → 18 taken 2 times.
✗ Branch 17 → 146 not taken.
2 std::filesystem::path installPath = FileUtil::getSpiceBinDir();
42
2/4
✓ Branch 18 → 19 taken 2 times.
✗ Branch 18 → 134 not taken.
✓ Branch 19 → 20 taken 2 times.
✗ Branch 19 → 132 not taken.
2 installPath /= cliOptions.mainSourceFile.stem();
43
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 2 times.
2 if (!performDryRun)
44 create_directories(installPath);
45 #if OS_WINDOWS
46 installPath.replace_extension("exe");
47 #endif
48
49 // If the binary should be installed, set the output path to the Spice bin directory
50
2/2
✓ Branch 23 → 24 taken 1 time.
✓ Branch 23 → 25 taken 1 time.
2 if (shouldInstall)
51
1/2
✓ Branch 24 → 25 taken 1 time.
✗ Branch 24 → 144 not taken.
1 cliOptions.outputPath = installPath;
52
53 // If the binary should be uninstalled, check if the executable exists and uninstall it
54
3/4
✓ Branch 25 → 26 taken 1 time.
✓ Branch 25 → 44 taken 1 time.
✗ Branch 26 → 27 not taken.
✓ Branch 26 → 44 taken 1 time.
2 if (shouldUninstall && !performDryRun) {
55 if (exists(installPath) && std::filesystem::remove(installPath))
56 std::cout << "Successfully uninstalled.\n";
57 else
58 CompilerWarning(UNINSTALL_FAILED, "The executable was not found at the expected location").print();
59 }
60 2 }
61
62 // Abort here if we do not need to compile
63
2/2
✓ Branch 46 → 47 taken 1 time.
✓ Branch 46 → 48 taken 436 times.
437 if (!shouldCompile)
64 1 return;
65
66 // Set output path and dir
67
2/2
✓ Branch 48 → 49 taken 4 times.
✓ Branch 48 → 76 taken 432 times.
436 if (shouldExecute) {
68 4 cliOptions.execute = true;
69
1/2
✓ Branch 51 → 52 taken 4 times.
✗ Branch 51 → 147 not taken.
4 const long millis = duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
70
8/16
✓ Branch 53 → 54 taken 4 times.
✗ Branch 53 → 169 not taken.
✓ Branch 54 → 55 taken 4 times.
✗ Branch 54 → 167 not taken.
✓ Branch 55 → 56 taken 4 times.
✗ Branch 55 → 163 not taken.
✓ Branch 56 → 57 taken 4 times.
✗ Branch 56 → 159 not taken.
✓ Branch 57 → 58 taken 4 times.
✗ Branch 57 → 156 not taken.
✓ Branch 58 → 59 taken 4 times.
✗ Branch 58 → 154 not taken.
✓ Branch 59 → 60 taken 4 times.
✗ Branch 59 → 152 not taken.
✓ Branch 60 → 61 taken 4 times.
✗ Branch 60 → 150 not taken.
4 cliOptions.outputDir = std::filesystem::temp_directory_path() / "spice" / "output" / std::to_string(millis);
71
2/4
✓ Branch 70 → 71 taken 4 times.
✗ Branch 70 → 174 not taken.
✓ Branch 71 → 72 taken 4 times.
✗ Branch 71 → 172 not taken.
4 cliOptions.outputPath = cliOptions.outputDir / cliOptions.mainSourceFile.filename();
72
2/2
✓ Branch 77 → 78 taken 1 time.
✓ Branch 77 → 91 taken 431 times.
432 } else if (!cliOptions.outputPath.empty()) {
73
1/2
✗ Branch 79 → 80 not taken.
✓ Branch 79 → 87 taken 1 time.
1 if (is_directory(cliOptions.outputPath)) {
74 cliOptions.outputDir = cliOptions.outputPath;
75 cliOptions.outputPath = cliOptions.outputDir / cliOptions.mainSourceFile.filename();
76 } else {
77
1/2
✓ Branch 87 → 88 taken 1 time.
✗ Branch 87 → 180 not taken.
1 cliOptions.outputDir = cliOptions.outputPath.parent_path();
78 }
79 } else {
80 431 cliOptions.outputDir = "./";
81
2/4
✓ Branch 92 → 93 taken 431 times.
✗ Branch 92 → 183 not taken.
✓ Branch 93 → 94 taken 431 times.
✗ Branch 93 → 181 not taken.
431 cliOptions.outputPath = cliOptions.outputDir / cliOptions.mainSourceFile.filename();
82 }
83
84 // Set output file extension
85
1/2
✗ Branch 99 → 100 not taken.
✓ Branch 99 → 104 taken 436 times.
436 if (cliOptions.targetTriple.isWasm()) {
86 cliOptions.outputPath.replace_extension("wasm");
87 } else {
88 #if OS_UNIX
89
2/4
✓ Branch 104 → 105 taken 436 times.
✗ Branch 104 → 190 not taken.
✓ Branch 105 → 106 taken 436 times.
✗ Branch 105 → 188 not taken.
436 cliOptions.outputPath.replace_extension("");
90 #elif OS_WINDOWS
91 cliOptions.outputPath.replace_extension("exe");
92 #else
93 #error "Unsupported platform"
94 #endif
95 }
96
97 // Set cache dir
98
5/10
✓ Branch 108 → 109 taken 436 times.
✗ Branch 108 → 202 not taken.
✓ Branch 109 → 110 taken 436 times.
✗ Branch 109 → 198 not taken.
✓ Branch 110 → 111 taken 436 times.
✗ Branch 110 → 195 not taken.
✓ Branch 111 → 112 taken 436 times.
✗ Branch 111 → 193 not taken.
✓ Branch 112 → 113 taken 436 times.
✗ Branch 112 → 191 not taken.
436 cliOptions.cacheDir = std::filesystem::temp_directory_path() / "spice" / "cache";
99
100 // Create directories in case they not exist yet
101 436 create_directories(cliOptions.cacheDir);
102 436 create_directories(cliOptions.outputDir);
103 });
104 440 }
105
106 /**
107 * Start the parsing process
108 *
109 * @param argc Argument count
110 * @param argv Argument vector
111 * @return Return code
112 */
113 440 int Driver::parse(int argc, const char *argv[]) {
114 try {
115
2/2
✓ Branch 2 → 3 taken 437 times.
✓ Branch 2 → 5 taken 1 time.
440 app.parse(argc, argv);
116 437 return EXIT_SUCCESS;
117
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 1 time.
1 } catch (const CLI::ParseError &parseError) {
118
1/2
✓ Branch 8 → 9 taken 1 time.
✗ Branch 8 → 11 not taken.
1 return app.exit(parseError);
119 1 }
120 }
121
122 /**
123 * Initialize the cli options based on the input of the user
124 */
125 438 void Driver::enrich() const {
126 // Make path of given main source file canonical and relative
127
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 9 taken 438 times.
438 if (!performDryRun)
128 cliOptions.mainSourceFile = relative(cliOptions.mainSourceFile);
129
130 // Propagate llvm args to llvm
131
1/2
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 31 taken 438 times.
438 if (!cliOptions.llvmArgs.empty()) {
132 const std::vector<std::string> result = CommonUtil::split("llvm " + cliOptions.llvmArgs);
133 std::vector<const char *> resultCStr;
134 resultCStr.reserve(result.size());
135 for (const std::string &str : result)
136 resultCStr.push_back(str.c_str());
137 llvm::cl::ParseCommandLineOptions(static_cast<int>(result.size()), resultCStr.data());
138 }
139
140 // Propagate target information
141
3/6
✓ Branch 31 → 32 taken 438 times.
✗ Branch 31 → 111 not taken.
✓ Branch 33 → 34 taken 438 times.
✗ Branch 33 → 109 not taken.
✓ Branch 34 → 35 taken 438 times.
✗ Branch 34 → 107 not taken.
438 const llvm::Triple defaultTriple(llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple()));
142
2/2
✓ Branch 38 → 39 taken 434 times.
✓ Branch 38 → 57 taken 4 times.
438 if (cliOptions.targetTriple.empty()) {
143
2/4
✓ Branch 39 → 40 taken 434 times.
✗ Branch 39 → 139 not taken.
✓ Branch 40 → 41 taken 434 times.
✗ Branch 40 → 49 not taken.
434 if (cliOptions.targetArch == TARGET_UNKNOWN) { // We have nothing -> obtain native triplet
144
1/2
✓ Branch 41 → 42 taken 434 times.
✗ Branch 41 → 139 not taken.
434 cliOptions.targetTriple = defaultTriple;
145
2/4
✓ Branch 42 → 43 taken 434 times.
✗ Branch 42 → 114 not taken.
✓ Branch 43 → 44 taken 434 times.
✗ Branch 43 → 114 not taken.
434 cliOptions.targetArch = defaultTriple.getArchName();
146
2/4
✓ Branch 44 → 45 taken 434 times.
✗ Branch 44 → 115 not taken.
✓ Branch 45 → 46 taken 434 times.
✗ Branch 45 → 115 not taken.
434 cliOptions.targetVendor = defaultTriple.getVendorName();
147
2/4
✓ Branch 46 → 47 taken 434 times.
✗ Branch 46 → 116 not taken.
✓ Branch 47 → 48 taken 434 times.
✗ Branch 47 → 116 not taken.
434 cliOptions.targetOs = defaultTriple.getOSName();
148 434 cliOptions.isNativeTarget = true;
149 } else { // We have arch, vendor and os -> obtain triplet
150 cliOptions.targetTriple = llvm::Triple(cliOptions.targetArch, cliOptions.targetVendor, cliOptions.targetOs);
151 cliOptions.isNativeTarget = cliOptions.targetTriple == defaultTriple;
152 }
153 } else { // Obtain arch, vendor and os by the triplet
154
2/4
✓ Branch 57 → 58 taken 4 times.
✗ Branch 57 → 123 not taken.
✓ Branch 58 → 59 taken 4 times.
✗ Branch 58 → 121 not taken.
4 const llvm::Triple triple(cliOptions.targetTriple.normalize());
155
2/4
✓ Branch 60 → 61 taken 4 times.
✗ Branch 60 → 124 not taken.
✓ Branch 61 → 62 taken 4 times.
✗ Branch 61 → 124 not taken.
4 cliOptions.targetArch = triple.getArchName();
156
2/4
✓ Branch 62 → 63 taken 4 times.
✗ Branch 62 → 125 not taken.
✓ Branch 63 → 64 taken 4 times.
✗ Branch 63 → 125 not taken.
4 cliOptions.targetVendor = triple.getVendorName();
157
2/4
✓ Branch 64 → 65 taken 4 times.
✗ Branch 64 → 126 not taken.
✓ Branch 65 → 66 taken 4 times.
✗ Branch 65 → 126 not taken.
4 cliOptions.targetOs = triple.getOSName();
158 4 cliOptions.isNativeTarget = triple == defaultTriple;
159 4 }
160
161 // Always preserve IR value names when dumping IR
162
2/2
✓ Branch 69 → 70 taken 1 time.
✓ Branch 69 → 71 taken 437 times.
438 if (cliOptions.dump.dumpIR)
163 1 cliOptions.namesForIRValues = true;
164
165 // Enable test mode when test mode was selected
166
2/2
✓ Branch 71 → 72 taken 2 times.
✓ Branch 71 → 73 taken 436 times.
438 if (cliOptions.buildMode == BuildMode::TEST) {
167 2 cliOptions.noEntryFct = true;
168 2 cliOptions.generateTestMain = true;
169 }
170
171 438 const Sanitizer sanitizer = cliOptions.instrumentation.sanitizer;
172 // Memory sanitizer is only supported on Linux
173
4/6
✓ Branch 74 → 75 taken 2 times.
✓ Branch 74 → 77 taken 436 times.
✗ Branch 75 → 76 not taken.
✓ Branch 75 → 77 taken 2 times.
✗ Branch 78 → 79 not taken.
✓ Branch 78 → 87 taken 438 times.
438 if (!cliOptions.targetTriple.isOSLinux() && sanitizer == Sanitizer::MEMORY)
174 throw CliError(FEATURE_NOT_SUPPORTED_FOR_TARGET, "Memory sanitizer is only supported for Linux targets");
175 // Some sanitizers need lifetime markers to work properly
176
4/4
✓ Branch 87 → 88 taken 435 times.
✓ Branch 87 → 89 taken 3 times.
✓ Branch 88 → 89 taken 3 times.
✓ Branch 88 → 90 taken 432 times.
438 if (sanitizer == Sanitizer::ADDRESS || sanitizer == Sanitizer::MEMORY)
177 6 cliOptions.useLifetimeMarkers = true;
178 438 }
179
180 /**
181 * Executes the built executable
182 */
183 void Driver::runBinary() const {
184 // Print status message
185 if (cliOptions.printDebugOutput)
186 std::cout << "Running executable ...\n\n";
187
188 // Run executable
189 std::filesystem::path executablePath = cliOptions.outputPath;
190 executablePath.make_preferred();
191 const int exitCode = std::system(executablePath.string().c_str()) / 256;
192 if (exitCode != 0)
193 throw CliError(NON_ZERO_EXIT_CODE, "Your Spice executable exited with non-zero exit code " + std::to_string(exitCode));
194 }
195
196 /**
197 * Add build subcommand to cli interface
198 */
199 440 void Driver::addBuildSubcommand() {
200 // Create sub-command itself
201
3/6
✓ Branch 4 → 5 taken 440 times.
✗ Branch 4 → 145 not taken.
✓ Branch 7 → 8 taken 440 times.
✗ Branch 7 → 139 not taken.
✓ Branch 8 → 9 taken 440 times.
✗ Branch 8 → 137 not taken.
1760 CLI::App *subCmd = app.add_subcommand("build", "Builds your Spice program and emits an executable");
202
2/4
✓ Branch 15 → 16 taken 440 times.
✗ Branch 15 → 151 not taken.
✓ Branch 16 → 17 taken 440 times.
✗ Branch 16 → 149 not taken.
880 subCmd->alias("b");
203 440 subCmd->allow_non_standard_option_names();
204 440 subCmd->configurable();
205 440 subCmd->callback([&] {
206 431 shouldCompile = true; // Requires the source file to be compiled
207 431 });
208
209 440 addCompileSubcommandOptions(subCmd);
210 440 addInstrumentationOptions(subCmd);
211
212 // --target-triple
213
3/6
✓ Branch 28 → 29 taken 440 times.
✗ Branch 28 → 163 not taken.
✓ Branch 31 → 32 taken 440 times.
✗ Branch 31 → 157 not taken.
✓ Branch 32 → 33 taken 440 times.
✗ Branch 32 → 155 not taken.
1760 subCmd->add_option<llvm::Triple>("--target,--target-triple,-t", cliOptions.targetTriple,
214 "Target triple for the emitted executable (for cross-compiling)");
215 // --target-arch
216
3/6
✓ Branch 39 → 40 taken 440 times.
✗ Branch 39 → 175 not taken.
✓ Branch 42 → 43 taken 440 times.
✗ Branch 42 → 169 not taken.
✓ Branch 43 → 44 taken 440 times.
✗ Branch 43 → 167 not taken.
1760 subCmd->add_option<std::string>("--target-arch", cliOptions.targetArch,
217 "Target arch for emitted executable (for cross-compiling)");
218 // --target-vendor
219
3/6
✓ Branch 50 → 51 taken 440 times.
✗ Branch 50 → 187 not taken.
✓ Branch 53 → 54 taken 440 times.
✗ Branch 53 → 181 not taken.
✓ Branch 54 → 55 taken 440 times.
✗ Branch 54 → 179 not taken.
1760 subCmd->add_option<std::string>("--target-vendor", cliOptions.targetVendor,
220 "Target vendor for emitted executable (for cross-compiling)");
221 // --target-os
222
3/6
✓ Branch 61 → 62 taken 440 times.
✗ Branch 61 → 199 not taken.
✓ Branch 64 → 65 taken 440 times.
✗ Branch 64 → 193 not taken.
✓ Branch 65 → 66 taken 440 times.
✗ Branch 65 → 191 not taken.
1760 subCmd->add_option<std::string>("--target-os", cliOptions.targetOs, "Target os for emitted executable (for cross-compiling)");
223 // --output
224
3/6
✓ Branch 72 → 73 taken 440 times.
✗ Branch 72 → 211 not taken.
✓ Branch 75 → 76 taken 440 times.
✗ Branch 75 → 205 not taken.
✓ Branch 76 → 77 taken 440 times.
✗ Branch 76 → 203 not taken.
1760 subCmd->add_option<std::filesystem::path>("--output,-o", cliOptions.outputPath, "Set the output file path");
225 // --disable-verifier
226
3/6
✓ Branch 83 → 84 taken 440 times.
✗ Branch 83 → 223 not taken.
✓ Branch 86 → 87 taken 440 times.
✗ Branch 86 → 217 not taken.
✓ Branch 87 → 88 taken 440 times.
✗ Branch 87 → 215 not taken.
1760 subCmd->add_flag<bool>("--disable-verifier", cliOptions.disableVerifier, "Disable LLVM module and function verification");
227 // --no-entry
228
3/6
✓ Branch 94 → 95 taken 440 times.
✗ Branch 94 → 235 not taken.
✓ Branch 97 → 98 taken 440 times.
✗ Branch 97 → 229 not taken.
✓ Branch 98 → 99 taken 440 times.
✗ Branch 98 → 227 not taken.
1760 subCmd->add_flag<bool>("--no-entry", cliOptions.noEntryFct, "Do not generate main function");
229 // --static
230
3/6
✓ Branch 105 → 106 taken 440 times.
✗ Branch 105 → 247 not taken.
✓ Branch 108 → 109 taken 440 times.
✗ Branch 108 → 241 not taken.
✓ Branch 109 → 110 taken 440 times.
✗ Branch 109 → 239 not taken.
1760 subCmd->add_flag<bool>("--static", cliOptions.staticLinking, "Link statically");
231 // --dump-to-files
232
3/6
✓ Branch 116 → 117 taken 440 times.
✗ Branch 116 → 259 not taken.
✓ Branch 119 → 120 taken 440 times.
✗ Branch 119 → 253 not taken.
✓ Branch 120 → 121 taken 440 times.
✗ Branch 120 → 251 not taken.
1760 subCmd->add_flag<bool>("--dump-to-files", cliOptions.dump.dumpToFiles, "Redirect dumps to files instead of printing");
233 // --abort-after-dump
234
3/6
✓ Branch 127 → 128 taken 440 times.
✗ Branch 127 → 271 not taken.
✓ Branch 130 → 131 taken 440 times.
✗ Branch 130 → 265 not taken.
✓ Branch 131 → 132 taken 440 times.
✗ Branch 131 → 263 not taken.
1760 subCmd->add_flag<bool>("--abort-after-dump", cliOptions.dump.abortAfterDump,
235 "Abort the compilation process after dumping the first requested resource");
236 440 }
237
238 /**
239 * Add run subcommand to cli interface
240 */
241 440 void Driver::addRunSubcommand() {
242 // Create sub-command itself
243
3/6
✓ Branch 4 → 5 taken 440 times.
✗ Branch 4 → 45 not taken.
✓ Branch 7 → 8 taken 440 times.
✗ Branch 7 → 39 not taken.
✓ Branch 8 → 9 taken 440 times.
✗ Branch 8 → 37 not taken.
1760 CLI::App *subCmd = app.add_subcommand("run", "Builds your Spice program and runs it immediately");
244
2/4
✓ Branch 15 → 16 taken 440 times.
✗ Branch 15 → 51 not taken.
✓ Branch 16 → 17 taken 440 times.
✗ Branch 16 → 49 not taken.
880 subCmd->alias("r");
245 440 subCmd->allow_non_standard_option_names();
246 440 subCmd->callback([&] {
247 2 shouldCompile = shouldExecute = true; // Requires the source file to be compiled
248 2 });
249
250 440 addCompileSubcommandOptions(subCmd);
251 440 addInstrumentationOptions(subCmd);
252
253 // --disable-verifier
254
3/6
✓ Branch 27 → 28 taken 440 times.
✗ Branch 27 → 63 not taken.
✓ Branch 30 → 31 taken 440 times.
✗ Branch 30 → 57 not taken.
✓ Branch 31 → 32 taken 440 times.
✗ Branch 31 → 55 not taken.
1760 subCmd->add_flag<bool>("--disable-verifier", cliOptions.disableVerifier, "Disable LLVM module and function verification");
255 440 }
256
257 /**
258 * Add test subcommand to cli interface
259 */
260 440 void Driver::addTestSubcommand() {
261 // Create sub-command itself
262
3/6
✓ Branch 4 → 5 taken 440 times.
✗ Branch 4 → 45 not taken.
✓ Branch 7 → 8 taken 440 times.
✗ Branch 7 → 39 not taken.
✓ Branch 8 → 9 taken 440 times.
✗ Branch 8 → 37 not taken.
1760 CLI::App *subCmd = app.add_subcommand("test", "Builds your Spice program and runs all enclosed tests");
263
2/4
✓ Branch 15 → 16 taken 440 times.
✗ Branch 15 → 51 not taken.
✓ Branch 16 → 17 taken 440 times.
✗ Branch 16 → 49 not taken.
880 subCmd->alias("t");
264 440 subCmd->allow_non_standard_option_names();
265 440 subCmd->callback([&] {
266 2 shouldCompile = shouldExecute = true; // Requires the source file to be compiled
267 2 cliOptions.buildMode = BuildMode::TEST; // Set build mode to test
268 2 cliOptions.generateTestMain = true; // An alternative entry function is generated
269 2 cliOptions.noEntryFct = true; // To not have two main functions, disable normal main
270 2 });
271
272 440 addCompileSubcommandOptions(subCmd);
273 440 addInstrumentationOptions(subCmd);
274
275 // --disable-verifier
276
3/6
✓ Branch 27 → 28 taken 440 times.
✗ Branch 27 → 63 not taken.
✓ Branch 30 → 31 taken 440 times.
✗ Branch 30 → 57 not taken.
✓ Branch 31 → 32 taken 440 times.
✗ Branch 31 → 55 not taken.
1760 subCmd->add_flag<bool>("--disable-verifier", cliOptions.disableVerifier, "Disable LLVM module and function verification");
277 440 }
278
279 /**
280 * Add install subcommand to cli interface
281 */
282 440 void Driver::addInstallSubcommand() {
283 // Create sub-command itself
284
3/6
✓ Branch 4 → 5 taken 440 times.
✗ Branch 4 → 33 not taken.
✓ Branch 7 → 8 taken 440 times.
✗ Branch 7 → 27 not taken.
✓ Branch 8 → 9 taken 440 times.
✗ Branch 8 → 25 not taken.
1760 CLI::App *subCmd = app.add_subcommand("install", "Builds your Spice program and installs it to a directory in the PATH");
285
2/4
✓ Branch 15 → 16 taken 440 times.
✗ Branch 15 → 39 not taken.
✓ Branch 16 → 17 taken 440 times.
✗ Branch 16 → 37 not taken.
880 subCmd->alias("i");
286 440 subCmd->allow_non_standard_option_names();
287 440 subCmd->callback([&] {
288 1 shouldCompile = true;
289 1 shouldInstall = true;
290 1 ensureNotDockerized();
291 1 });
292
293 440 addCompileSubcommandOptions(subCmd);
294 440 }
295
296 /**
297 * Add uninstall subcommand to cli interface
298 */
299 440 void Driver::addUninstallSubcommand() {
300 // Create sub-command itself
301
3/6
✓ Branch 4 → 5 taken 440 times.
✗ Branch 4 → 52 not taken.
✓ Branch 7 → 8 taken 440 times.
✗ Branch 7 → 46 not taken.
✓ Branch 8 → 9 taken 440 times.
✗ Branch 8 → 44 not taken.
1760 CLI::App *subCmd = app.add_subcommand("uninstall", "Uninstalls a Spice program from the system");
302
2/4
✓ Branch 15 → 16 taken 440 times.
✗ Branch 15 → 58 not taken.
✓ Branch 16 → 17 taken 440 times.
✗ Branch 16 → 56 not taken.
880 subCmd->alias("u");
303 440 subCmd->allow_non_standard_option_names();
304 440 subCmd->callback([&] {
305 1 shouldUninstall = true;
306 1 ensureNotDockerized();
307 1 });
308
309 // Source file
310
2/4
✓ Branch 25 → 26 taken 440 times.
✗ Branch 25 → 79 not taken.
✓ Branch 28 → 29 taken 440 times.
✗ Branch 28 → 73 not taken.
1320 subCmd->add_option<std::filesystem::path>("<main-source-file>", cliOptions.mainSourceFile, "Main source file")
311
4/8
✓ Branch 29 → 30 taken 440 times.
✗ Branch 29 → 71 not taken.
✓ Branch 32 → 33 taken 440 times.
✗ Branch 32 → 67 not taken.
✓ Branch 33 → 34 taken 440 times.
✗ Branch 33 → 64 not taken.
✓ Branch 34 → 35 taken 440 times.
✗ Branch 34 → 62 not taken.
2200 ->check(CLI::ExistingFile)
312 440 ->required();
313 440 }
314
315 1760 void Driver::addCompileSubcommandOptions(CLI::App *subCmd) const {
316 4 const auto buildModeCallback = [&](const CLI::results_t &results) {
317
1/2
✓ Branch 3 → 4 taken 4 times.
✗ Branch 3 → 26 not taken.
4 std::string inputString = results.front();
318
1/2
✓ Branch 5 → 6 taken 4 times.
✗ Branch 5 → 24 not taken.
4 std::ranges::transform(inputString, inputString.begin(), tolower);
319
320
2/4
✓ Branch 6 → 7 taken 4 times.
✗ Branch 6 → 24 not taken.
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 4 times.
4 if (inputString == BUILD_MODE_DEBUG)
321 cliOptions.buildMode = BuildMode::DEBUG;
322
3/4
✓ Branch 9 → 10 taken 4 times.
✗ Branch 9 → 24 not taken.
✓ Branch 10 → 11 taken 3 times.
✓ Branch 10 → 12 taken 1 time.
4 else if (inputString == BUILD_MODE_RELEASE)
323 3 cliOptions.buildMode = BuildMode::RELEASE;
324
2/4
✓ Branch 12 → 13 taken 1 time.
✗ Branch 12 → 24 not taken.
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 1 time.
1 else if (inputString == BUILD_MODE_TEST)
325 cliOptions.buildMode = BuildMode::TEST;
326 else
327
1/2
✓ Branch 16 → 17 taken 1 time.
✗ Branch 16 → 21 not taken.
1 throw CliError(INVALID_BUILD_MODE, inputString);
328
329 3 return true;
330 4 };
331
332 // --build-mode
333
3/6
✓ Branch 5 → 6 taken 1760 times.
✗ Branch 5 → 304 not taken.
✓ Branch 9 → 10 taken 1760 times.
✗ Branch 9 → 295 not taken.
✓ Branch 10 → 11 taken 1760 times.
✗ Branch 10 → 293 not taken.
8800 subCmd->add_option("--build-mode,-m", buildModeCallback, "Build mode (debug, release, test)");
334 // --llvm-args
335
3/6
✓ Branch 19 → 20 taken 1760 times.
✗ Branch 19 → 319 not taken.
✓ Branch 22 → 23 taken 1760 times.
✗ Branch 22 → 313 not taken.
✓ Branch 23 → 24 taken 1760 times.
✗ Branch 23 → 311 not taken.
7040 subCmd->add_option<std::string>("--llvm-args,-llvm", cliOptions.llvmArgs, "Additional arguments for LLVM")->join(' ');
336 // --jobs
337
3/6
✓ Branch 31 → 32 taken 1760 times.
✗ Branch 31 → 331 not taken.
✓ Branch 34 → 35 taken 1760 times.
✗ Branch 34 → 325 not taken.
✓ Branch 35 → 36 taken 1760 times.
✗ Branch 35 → 323 not taken.
7040 subCmd->add_option<unsigned short>("--jobs,-j", cliOptions.compileJobCount, "Compile jobs (threads), used for compilation");
338 // --ignore-cache
339
3/6
✓ Branch 42 → 43 taken 1760 times.
✗ Branch 42 → 343 not taken.
✓ Branch 45 → 46 taken 1760 times.
✗ Branch 45 → 337 not taken.
✓ Branch 46 → 47 taken 1760 times.
✗ Branch 46 → 335 not taken.
7040 subCmd->add_flag<bool>("--ignore-cache", cliOptions.ignoreCache, "Force re-compilation of all source files");
340 // --use-lifetime-markers
341
3/6
✓ Branch 53 → 54 taken 1760 times.
✗ Branch 53 → 355 not taken.
✓ Branch 56 → 57 taken 1760 times.
✗ Branch 56 → 349 not taken.
✓ Branch 57 → 58 taken 1760 times.
✗ Branch 57 → 347 not taken.
7040 subCmd->add_flag<bool>("--use-lifetime-markers", cliOptions.useLifetimeMarkers,
342 "Generate lifetime markers to enhance optimizations");
343 // --use-tbaa-metadata
344
3/6
✓ Branch 64 → 65 taken 1760 times.
✗ Branch 64 → 367 not taken.
✓ Branch 67 → 68 taken 1760 times.
✗ Branch 67 → 361 not taken.
✓ Branch 68 → 69 taken 1760 times.
✗ Branch 68 → 359 not taken.
7040 subCmd->add_flag<bool>("--use-tbaa-metadata", cliOptions.useTBAAMetadata,
345 "Generate metadata for type-based alias analysis to enhance optimizations");
346
347 // Opt levels
348
3/6
✓ Branch 75 → 76 taken 1760 times.
✗ Branch 75 → 383 not taken.
✓ Branch 79 → 80 taken 1760 times.
✗ Branch 79 → 373 not taken.
✓ Branch 80 → 81 taken 1760 times.
✗ Branch 80 → 371 not taken.
7040 subCmd->add_flag_callback("-O0", [&] { cliOptions.optLevel = OptLevel::O0; }, "Disable optimization.");
349
3/6
✓ Branch 88 → 89 taken 1760 times.
✗ Branch 88 → 399 not taken.
✓ Branch 92 → 93 taken 1760 times.
✗ Branch 92 → 389 not taken.
✓ Branch 93 → 94 taken 1760 times.
✗ Branch 93 → 387 not taken.
7040 subCmd->add_flag_callback("-O1", [&] { cliOptions.optLevel = OptLevel::O1; }, "Only basic optimization is applied.");
350
3/6
✓ Branch 101 → 102 taken 1760 times.
✗ Branch 101 → 415 not taken.
✓ Branch 105 → 106 taken 1760 times.
✗ Branch 105 → 405 not taken.
✓ Branch 106 → 107 taken 1760 times.
✗ Branch 106 → 403 not taken.
7040 subCmd->add_flag_callback("-O2", [&] { cliOptions.optLevel = OptLevel::O2; }, "More advanced optimization is applied.");
351
3/6
✓ Branch 114 → 115 taken 1760 times.
✗ Branch 114 → 431 not taken.
✓ Branch 118 → 119 taken 1760 times.
✗ Branch 118 → 421 not taken.
✓ Branch 119 → 120 taken 1760 times.
✗ Branch 119 → 419 not taken.
7040 subCmd->add_flag_callback("-O3", [&] { cliOptions.optLevel = OptLevel::O3; }, "Aggressive optimization for best performance.");
352
3/6
✓ Branch 127 → 128 taken 1760 times.
✗ Branch 127 → 447 not taken.
✓ Branch 131 → 132 taken 1760 times.
✗ Branch 131 → 437 not taken.
✓ Branch 132 → 133 taken 1760 times.
✗ Branch 132 → 435 not taken.
7040 subCmd->add_flag_callback("-Os", [&] { cliOptions.optLevel = OptLevel::Os; }, "Size optimization for output executable.");
353
3/6
✓ Branch 140 → 141 taken 1760 times.
✗ Branch 140 → 463 not taken.
✓ Branch 144 → 145 taken 1760 times.
✗ Branch 144 → 453 not taken.
✓ Branch 145 → 146 taken 1760 times.
✗ Branch 145 → 451 not taken.
7040 subCmd->add_flag_callback("-Oz", [&] { cliOptions.optLevel = OptLevel::Oz; }, "Aggressive optimization for best size.");
354
3/6
✓ Branch 153 → 154 taken 1760 times.
✗ Branch 153 → 475 not taken.
✓ Branch 156 → 157 taken 1760 times.
✗ Branch 156 → 469 not taken.
✓ Branch 157 → 158 taken 1760 times.
✗ Branch 157 → 467 not taken.
7040 subCmd->add_flag<bool>("-lto", cliOptions.useLTO, "Enable link time optimization (LTO)");
355
356 // --debug-output
357
3/6
✓ Branch 164 → 165 taken 1760 times.
✗ Branch 164 → 487 not taken.
✓ Branch 167 → 168 taken 1760 times.
✗ Branch 167 → 481 not taken.
✓ Branch 168 → 169 taken 1760 times.
✗ Branch 168 → 479 not taken.
7040 subCmd->add_flag<bool>("--debug-output,-d", cliOptions.printDebugOutput, "Enable debug output");
358 // --dump-cst
359
3/6
✓ Branch 175 → 176 taken 1760 times.
✗ Branch 175 → 499 not taken.
✓ Branch 178 → 179 taken 1760 times.
✗ Branch 178 → 493 not taken.
✓ Branch 179 → 180 taken 1760 times.
✗ Branch 179 → 491 not taken.
7040 subCmd->add_flag<bool>("--dump-cst,-cst", cliOptions.dump.dumpCST, "Dump CST as serialized string and SVG image");
360 // --dump-ast
361
3/6
✓ Branch 186 → 187 taken 1760 times.
✗ Branch 186 → 511 not taken.
✓ Branch 189 → 190 taken 1760 times.
✗ Branch 189 → 505 not taken.
✓ Branch 190 → 191 taken 1760 times.
✗ Branch 190 → 503 not taken.
7040 subCmd->add_flag<bool>("--dump-ast,-ast", cliOptions.dump.dumpAST, "Dump AST as serialized string and SVG image");
362 // --dump-symtab
363
3/6
✓ Branch 197 → 198 taken 1760 times.
✗ Branch 197 → 523 not taken.
✓ Branch 200 → 201 taken 1760 times.
✗ Branch 200 → 517 not taken.
✓ Branch 201 → 202 taken 1760 times.
✗ Branch 201 → 515 not taken.
7040 subCmd->add_flag<bool>("--dump-symtab", cliOptions.dump.dumpSymbolTable, "Dump serialized symbol tables");
364 // --dump-types
365
3/6
✓ Branch 208 → 209 taken 1760 times.
✗ Branch 208 → 535 not taken.
✓ Branch 211 → 212 taken 1760 times.
✗ Branch 211 → 529 not taken.
✓ Branch 212 → 213 taken 1760 times.
✗ Branch 212 → 527 not taken.
7040 subCmd->add_flag<bool>("--dump-types", cliOptions.dump.dumpTypes, "Dump all used types");
366 // --dump-cache-stats
367
3/6
✓ Branch 219 → 220 taken 1760 times.
✗ Branch 219 → 547 not taken.
✓ Branch 222 → 223 taken 1760 times.
✗ Branch 222 → 541 not taken.
✓ Branch 223 → 224 taken 1760 times.
✗ Branch 223 → 539 not taken.
7040 subCmd->add_flag<bool>("--dump-cache-stats", cliOptions.dump.dumpCacheStats, "Dump stats for compiler-internal lookup caches");
368 // --dump-ir
369
3/6
✓ Branch 230 → 231 taken 1760 times.
✗ Branch 230 → 559 not taken.
✓ Branch 233 → 234 taken 1760 times.
✗ Branch 233 → 553 not taken.
✓ Branch 234 → 235 taken 1760 times.
✗ Branch 234 → 551 not taken.
7040 subCmd->add_flag<bool>("--dump-ir,-ir", cliOptions.dump.dumpIR, "Dump LLVM-IR");
370 // --dump-assembly
371
3/6
✓ Branch 241 → 242 taken 1760 times.
✗ Branch 241 → 571 not taken.
✓ Branch 244 → 245 taken 1760 times.
✗ Branch 244 → 565 not taken.
✓ Branch 245 → 246 taken 1760 times.
✗ Branch 245 → 563 not taken.
7040 subCmd->add_flag<bool>("--dump-assembly,-asm,-s", cliOptions.dump.dumpAssembly, "Dump Assembly code");
372 // --dump-object-file
373
3/6
✓ Branch 252 → 253 taken 1760 times.
✗ Branch 252 → 583 not taken.
✓ Branch 255 → 256 taken 1760 times.
✗ Branch 255 → 577 not taken.
✓ Branch 256 → 257 taken 1760 times.
✗ Branch 256 → 575 not taken.
7040 subCmd->add_flag<bool>("--dump-object-file", cliOptions.dump.dumpObjectFiles, "Dump object files");
374 // --dump-dependency-graph
375
3/6
✓ Branch 263 → 264 taken 1760 times.
✗ Branch 263 → 595 not taken.
✓ Branch 266 → 267 taken 1760 times.
✗ Branch 266 → 589 not taken.
✓ Branch 267 → 268 taken 1760 times.
✗ Branch 267 → 587 not taken.
7040 subCmd->add_flag<bool>("--dump-dependency-graph", cliOptions.dump.dumpDependencyGraph, "Dump compile unit dependency graph");
376
377 // Source file
378
2/4
✓ Branch 274 → 275 taken 1760 times.
✗ Branch 274 → 616 not taken.
✓ Branch 277 → 278 taken 1760 times.
✗ Branch 277 → 610 not taken.
5280 subCmd->add_option<std::filesystem::path>("<main-source-file>", cliOptions.mainSourceFile, "Main source file")
379
4/8
✓ Branch 278 → 279 taken 1760 times.
✗ Branch 278 → 608 not taken.
✓ Branch 281 → 282 taken 1760 times.
✗ Branch 281 → 604 not taken.
✓ Branch 282 → 283 taken 1760 times.
✗ Branch 282 → 601 not taken.
✓ Branch 283 → 284 taken 1760 times.
✗ Branch 283 → 599 not taken.
8800 ->check(CLI::ExistingFile)
380 1760 ->required();
381 1760 }
382
383 1320 void Driver::addInstrumentationOptions(CLI::App *subCmd) const {
384 10 const auto sanitizerCallback = [&](const CLI::results_t &results) {
385
1/2
✓ Branch 3 → 4 taken 10 times.
✗ Branch 3 → 29 not taken.
10 std::string inputString = results.front();
386
1/2
✓ Branch 5 → 6 taken 10 times.
✗ Branch 5 → 27 not taken.
10 std::ranges::transform(inputString, inputString.begin(), tolower);
387
388
2/4
✓ Branch 6 → 7 taken 10 times.
✗ Branch 6 → 27 not taken.
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 10 times.
10 if (inputString == SANITIZER_NONE)
389 cliOptions.instrumentation.sanitizer = Sanitizer::NONE;
390
3/4
✓ Branch 9 → 10 taken 10 times.
✗ Branch 9 → 27 not taken.
✓ Branch 10 → 11 taken 3 times.
✓ Branch 10 → 12 taken 7 times.
10 else if (inputString == SANITIZER_ADDRESS)
391 3 cliOptions.instrumentation.sanitizer = Sanitizer::ADDRESS;
392
3/4
✓ Branch 12 → 13 taken 7 times.
✗ Branch 12 → 27 not taken.
✓ Branch 13 → 14 taken 3 times.
✓ Branch 13 → 15 taken 4 times.
7 else if (inputString == SANITIZER_THREAD)
393 3 cliOptions.instrumentation.sanitizer = Sanitizer::THREAD;
394
3/4
✓ Branch 15 → 16 taken 4 times.
✗ Branch 15 → 27 not taken.
✓ Branch 16 → 17 taken 3 times.
✓ Branch 16 → 18 taken 1 time.
4 else if (inputString == SANITIZER_MEMORY)
395 3 cliOptions.instrumentation.sanitizer = Sanitizer::MEMORY;
396 else
397
1/2
✓ Branch 19 → 20 taken 1 time.
✗ Branch 19 → 24 not taken.
1 throw CliError(INVALID_SANITIZER, inputString);
398
399 9 return true;
400 10 };
401
402 // --debug-info
403
3/6
✓ Branch 4 → 5 taken 1320 times.
✗ Branch 4 → 37 not taken.
✓ Branch 7 → 8 taken 1320 times.
✗ Branch 7 → 31 not taken.
✓ Branch 8 → 9 taken 1320 times.
✗ Branch 8 → 29 not taken.
5280 subCmd->add_flag<bool>("--debug-info,-g", cliOptions.instrumentation.generateDebugInfo, "Generate debug info");
404 // --sanitizer
405
3/6
✓ Branch 16 → 17 taken 1320 times.
✗ Branch 16 → 52 not taken.
✓ Branch 20 → 21 taken 1320 times.
✗ Branch 20 → 43 not taken.
✓ Branch 21 → 22 taken 1320 times.
✗ Branch 21 → 41 not taken.
6600 subCmd->add_option("--sanitizer", sanitizerCallback, "Enable sanitizer. Possible values: none, address, thread, memory");
406 1320 }
407
408 /**
409 * Ensure that the compiler is not running in a Docker container
410 */
411 2 void Driver::ensureNotDockerized() {
412 2 const char *envValue = std::getenv(ENV_VAR_DOCKERIZED);
413 if (envValue != nullptr && std::strcmp(envValue, "true") == 0) { // LCOV_EXCL_START
414 auto errorMsg = "This feature is not supported in a containerized environment. Please use the standalone version of Spice.";
415 throw CliError(FEATURE_NOT_SUPPORTED_WHEN_DOCKERIZED, errorMsg);
416 } // LCOV_EXCL_STOP
417 2 }
418
419 } // namespace spice::compiler
420