GCC Code Coverage Report


Directory: ../
File: src/driver/Driver.cpp
Date: 2025-10-09 06:28:01
Coverage Exec Excl Total
Lines: 79.8% 166 0 208
Functions: 79.2% 19 0 24
Branches: 42.1% 228 0 542

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 8 void Driver::init() {
17 // Allow positional args
18 8 app.positionals_at_end();
19 8 app.allow_extras(false);
20
1/2
✓ Branch 6 → 7 taken 8 times.
✗ Branch 6 → 32 not taken.
16 app.footer("(c) Marc Auberer 2021-2025");
21
22 // Add version flag
23
4/8
✓ Branch 12 → 13 taken 8 times.
✗ Branch 12 → 47 not taken.
✓ Branch 13 → 14 taken 8 times.
✗ Branch 13 → 44 not taken.
✓ Branch 16 → 17 taken 8 times.
✗ Branch 16 → 38 not taken.
✓ Branch 17 → 18 taken 8 times.
✗ Branch 17 → 36 not taken.
32 app.set_version_flag("--version,-v", CommonUtil::buildVersionInfo());
24
25 // Create sub-commands
26 8 addBuildSubcommand();
27 8 addRunSubcommand();
28 8 addTestSubcommand();
29 8 addInstallSubcommand();
30 8 addUninstallSubcommand();
31
32 8 app.final_callback([&] {
33 // Print help text for the root command if no sub-command was given
34
2/4
✓ Branch 2 → 3 taken 8 times.
✗ Branch 2 → 127 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 15 taken 8 times.
8 if (app.get_subcommands().empty()) {
35 std::cout << app.help();
36 return;
37 }
38
39
4/4
✓ Branch 15 → 16 taken 7 times.
✓ Branch 15 → 17 taken 1 time.
✓ Branch 16 → 17 taken 1 time.
✓ Branch 16 → 46 taken 6 times.
8 if (shouldInstall || shouldUninstall) {
40 // Prepare the installation path
41
1/2
✓ Branch 17 → 18 taken 2 times.
✗ Branch 17 → 151 not taken.
2 std::filesystem::path installPath = FileUtil::getSpiceBinDir();
42
2/4
✓ Branch 18 → 19 taken 2 times.
✗ Branch 18 → 139 not taken.
✓ Branch 19 → 20 taken 2 times.
✗ Branch 19 → 137 not taken.
2 installPath /= cliOptions.mainSourceFile.stem();
43
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 2 times.
2 if (!dryRun)
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 → 149 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 && !dryRun) {
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 7 times.
8 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 3 times.
7 if (shouldExecute) {
68 4 cliOptions.execute = true;
69
1/2
✓ Branch 51 → 52 taken 4 times.
✗ Branch 51 → 152 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 → 174 not taken.
✓ Branch 54 → 55 taken 4 times.
✗ Branch 54 → 172 not taken.
✓ Branch 55 → 56 taken 4 times.
✗ Branch 55 → 168 not taken.
✓ Branch 56 → 57 taken 4 times.
✗ Branch 56 → 164 not taken.
✓ Branch 57 → 58 taken 4 times.
✗ Branch 57 → 161 not taken.
✓ Branch 58 → 59 taken 4 times.
✗ Branch 58 → 159 not taken.
✓ Branch 59 → 60 taken 4 times.
✗ Branch 59 → 157 not taken.
✓ Branch 60 → 61 taken 4 times.
✗ Branch 60 → 155 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 → 179 not taken.
✓ Branch 71 → 72 taken 4 times.
✗ Branch 71 → 177 not taken.
4 cliOptions.outputPath = cliOptions.outputDir / cliOptions.mainSourceFile.filename();
72
2/2
✓ Branch 77 → 78 taken 1 time.
✓ Branch 77 → 91 taken 2 times.
3 } 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 → 185 not taken.
1 cliOptions.outputDir = cliOptions.outputPath.parent_path();
78 }
79 } else {
80 2 cliOptions.outputDir = "./";
81
2/4
✓ Branch 92 → 93 taken 2 times.
✗ Branch 92 → 188 not taken.
✓ Branch 93 → 94 taken 2 times.
✗ Branch 93 → 186 not taken.
2 cliOptions.outputPath = cliOptions.outputDir / cliOptions.mainSourceFile.filename();
82 }
83
84 // Set output file extension
85
3/6
✓ Branch 99 → 100 taken 7 times.
✗ Branch 99 → 102 not taken.
✗ Branch 101 → 102 not taken.
✓ Branch 101 → 103 taken 7 times.
✗ Branch 104 → 105 not taken.
✓ Branch 104 → 109 taken 7 times.
7 if (cliOptions.targetArch == TARGET_WASM32 || cliOptions.targetArch == TARGET_WASM64) {
86 cliOptions.outputPath.replace_extension("wasm");
87 } else {
88 #if OS_UNIX
89
2/4
✓ Branch 109 → 110 taken 7 times.
✗ Branch 109 → 195 not taken.
✓ Branch 110 → 111 taken 7 times.
✗ Branch 110 → 193 not taken.
7 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 113 → 114 taken 7 times.
✗ Branch 113 → 207 not taken.
✓ Branch 114 → 115 taken 7 times.
✗ Branch 114 → 203 not taken.
✓ Branch 115 → 116 taken 7 times.
✗ Branch 115 → 200 not taken.
✓ Branch 116 → 117 taken 7 times.
✗ Branch 116 → 198 not taken.
✓ Branch 117 → 118 taken 7 times.
✗ Branch 117 → 196 not taken.
7 cliOptions.cacheDir = std::filesystem::temp_directory_path() / "spice" / "cache";
99
100 // Create directories in case they not exist yet
101 7 create_directories(cliOptions.cacheDir);
102 7 create_directories(cliOptions.outputDir);
103 });
104 8 }
105
106 /**
107 * Start the parsing process
108 *
109 * @param argc Argument count
110 * @param argv Argument vector
111 * @return Return code
112 */
113 8 int Driver::parse(int argc, const char *argv[]) {
114 try {
115
1/2
✓ Branch 2 → 3 taken 8 times.
✗ Branch 2 → 5 not taken.
8 app.parse(argc, argv);
116 8 return EXIT_SUCCESS;
117 } catch (const CLI::ParseError &parseError) {
118 return app.exit(parseError);
119 }
120 }
121
122 /**
123 * Initialize the cli options based on the input of the user
124 */
125 8 void Driver::enrich() {
126 // Make path of given main source file canonical and relative
127
2/4
✓ Branch 2 → 3 taken 8 times.
✗ Branch 2 → 75 not taken.
✓ Branch 3 → 4 taken 8 times.
✗ Branch 3 → 73 not taken.
8 cliOptions.mainSourceFile = relative(cliOptions.mainSourceFile);
128
129 // Propagate llvm args to llvm
130
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 29 taken 8 times.
8 if (!cliOptions.llvmArgs.empty()) {
131 const std::vector<std::string> result = CommonUtil::split("llvm " + cliOptions.llvmArgs);
132 std::vector<const char *> resultCStr;
133 resultCStr.reserve(result.size());
134 for (const std::string &str : result)
135 resultCStr.push_back(str.c_str());
136 llvm::cl::ParseCommandLineOptions(static_cast<int>(result.size()), resultCStr.data());
137 }
138
139 // Propagate target information
140
3/6
✓ Branch 29 → 30 taken 8 times.
✗ Branch 29 → 92 not taken.
✓ Branch 31 → 32 taken 8 times.
✗ Branch 31 → 90 not taken.
✓ Branch 32 → 33 taken 8 times.
✗ Branch 32 → 88 not taken.
8 const llvm::Triple defaultTriple(llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple()));
141
1/2
✓ Branch 36 → 37 taken 8 times.
✗ Branch 36 → 55 not taken.
8 if (cliOptions.targetTriple.empty()) {
142
2/4
✓ Branch 37 → 38 taken 8 times.
✗ Branch 37 → 111 not taken.
✓ Branch 38 → 39 taken 8 times.
✗ Branch 38 → 47 not taken.
8 if (cliOptions.targetArch == TARGET_UNKNOWN) { // We have nothing -> obtain native triplet
143
1/2
✓ Branch 39 → 40 taken 8 times.
✗ Branch 39 → 111 not taken.
8 cliOptions.targetTriple = defaultTriple;
144
2/4
✓ Branch 40 → 41 taken 8 times.
✗ Branch 40 → 95 not taken.
✓ Branch 41 → 42 taken 8 times.
✗ Branch 41 → 95 not taken.
8 cliOptions.targetArch = defaultTriple.getArchName();
145
2/4
✓ Branch 42 → 43 taken 8 times.
✗ Branch 42 → 96 not taken.
✓ Branch 43 → 44 taken 8 times.
✗ Branch 43 → 96 not taken.
8 cliOptions.targetVendor = defaultTriple.getVendorName();
146
2/4
✓ Branch 44 → 45 taken 8 times.
✗ Branch 44 → 97 not taken.
✓ Branch 45 → 46 taken 8 times.
✗ Branch 45 → 97 not taken.
8 cliOptions.targetOs = defaultTriple.getOSName();
147 8 cliOptions.isNativeTarget = true;
148 } else { // We have arch, vendor and os -> obtain triplet
149 cliOptions.targetTriple = llvm::Triple(cliOptions.targetArch, cliOptions.targetVendor, cliOptions.targetOs);
150 cliOptions.isNativeTarget = cliOptions.targetTriple == defaultTriple;
151 }
152 } else { // Obtain arch, vendor and os by the triplet
153 const llvm::Triple triple(cliOptions.targetTriple.normalize());
154 cliOptions.targetArch = triple.getArchName();
155 cliOptions.targetVendor = triple.getVendorName();
156 cliOptions.targetOs = triple.getOSName();
157 cliOptions.isNativeTarget = triple == defaultTriple;
158 }
159
160 // Always preserve IR value names when dumping IR
161
2/2
✓ Branch 67 → 68 taken 1 time.
✓ Branch 67 → 69 taken 7 times.
8 if (cliOptions.dumpSettings.dumpIR)
162 1 cliOptions.namesForIRValues = true;
163
164 // Enable test mode when test mode was selected
165
2/2
✓ Branch 69 → 70 taken 2 times.
✓ Branch 69 → 71 taken 6 times.
8 if (cliOptions.buildMode == TEST) {
166 2 cliOptions.noEntryFct = true;
167 2 cliOptions.generateTestMain = true;
168 }
169 8 }
170
171 /**
172 * Executes the built executable
173 */
174 void Driver::runBinary() const {
175 // Print status message
176 if (cliOptions.printDebugOutput)
177 std::cout << "Running executable ...\n\n";
178
179 // Run executable
180 std::filesystem::path executablePath = cliOptions.outputPath;
181 executablePath.make_preferred();
182 const int exitCode = std::system(executablePath.string().c_str()) / 256;
183 if (exitCode != 0)
184 throw CliError(NON_ZERO_EXIT_CODE, "Your Spice executable exited with non-zero exit code " + std::to_string(exitCode));
185 }
186
187 /**
188 * Add build subcommand to cli interface
189 */
190 8 void Driver::addBuildSubcommand() {
191 // Create sub-command itself
192
3/6
✓ Branch 4 → 5 taken 8 times.
✗ Branch 4 → 155 not taken.
✓ Branch 7 → 8 taken 8 times.
✗ Branch 7 → 149 not taken.
✓ Branch 8 → 9 taken 8 times.
✗ Branch 8 → 147 not taken.
32 CLI::App *subCmd = app.add_subcommand("build", "Builds your Spice program and emits an executable");
193
2/4
✓ Branch 15 → 16 taken 8 times.
✗ Branch 15 → 161 not taken.
✓ Branch 16 → 17 taken 8 times.
✗ Branch 16 → 159 not taken.
16 subCmd->alias("b");
194 8 subCmd->allow_non_standard_option_names();
195 8 subCmd->configurable();
196 8 subCmd->callback([&] {
197 2 shouldCompile = true; // Requires the source file to be compiled
198 2 });
199
200 8 addCompileSubcommandOptions(subCmd);
201
202 // --target-triple
203
3/6
✓ Branch 27 → 28 taken 8 times.
✗ Branch 27 → 173 not taken.
✓ Branch 30 → 31 taken 8 times.
✗ Branch 30 → 167 not taken.
✓ Branch 31 → 32 taken 8 times.
✗ Branch 31 → 165 not taken.
32 subCmd->add_option<llvm::Triple>("--target,--target-triple,-t", cliOptions.targetTriple,
204 "Target triple for the emitted executable (for cross-compiling)");
205 // --target-arch
206
3/6
✓ Branch 38 → 39 taken 8 times.
✗ Branch 38 → 185 not taken.
✓ Branch 41 → 42 taken 8 times.
✗ Branch 41 → 179 not taken.
✓ Branch 42 → 43 taken 8 times.
✗ Branch 42 → 177 not taken.
32 subCmd->add_option<std::string>("--target-arch", cliOptions.targetArch,
207 "Target arch for emitted executable (for cross-compiling)");
208 // --target-vendor
209
3/6
✓ Branch 49 → 50 taken 8 times.
✗ Branch 49 → 197 not taken.
✓ Branch 52 → 53 taken 8 times.
✗ Branch 52 → 191 not taken.
✓ Branch 53 → 54 taken 8 times.
✗ Branch 53 → 189 not taken.
32 subCmd->add_option<std::string>("--target-vendor", cliOptions.targetVendor,
210 "Target vendor for emitted executable (for cross-compiling)");
211 // --target-os
212
3/6
✓ Branch 60 → 61 taken 8 times.
✗ Branch 60 → 209 not taken.
✓ Branch 63 → 64 taken 8 times.
✗ Branch 63 → 203 not taken.
✓ Branch 64 → 65 taken 8 times.
✗ Branch 64 → 201 not taken.
32 subCmd->add_option<std::string>("--target-os", cliOptions.targetOs, "Target os for emitted executable (for cross-compiling)");
213
214 // --output
215
3/6
✓ Branch 71 → 72 taken 8 times.
✗ Branch 71 → 221 not taken.
✓ Branch 74 → 75 taken 8 times.
✗ Branch 74 → 215 not taken.
✓ Branch 75 → 76 taken 8 times.
✗ Branch 75 → 213 not taken.
32 subCmd->add_option<std::filesystem::path>("--output,-o", cliOptions.outputPath, "Set the output file path");
216 // --debug-info
217
3/6
✓ Branch 82 → 83 taken 8 times.
✗ Branch 82 → 233 not taken.
✓ Branch 85 → 86 taken 8 times.
✗ Branch 85 → 227 not taken.
✓ Branch 86 → 87 taken 8 times.
✗ Branch 86 → 225 not taken.
32 subCmd->add_flag<bool>("--debug-info,-g", cliOptions.generateDebugInfo, "Generate debug info");
218 // --disable-verifier
219
3/6
✓ Branch 93 → 94 taken 8 times.
✗ Branch 93 → 245 not taken.
✓ Branch 96 → 97 taken 8 times.
✗ Branch 96 → 239 not taken.
✓ Branch 97 → 98 taken 8 times.
✗ Branch 97 → 237 not taken.
32 subCmd->add_flag<bool>("--disable-verifier", cliOptions.disableVerifier, "Disable LLVM module and function verification");
220 // --no-entry
221
3/6
✓ Branch 104 → 105 taken 8 times.
✗ Branch 104 → 257 not taken.
✓ Branch 107 → 108 taken 8 times.
✗ Branch 107 → 251 not taken.
✓ Branch 108 → 109 taken 8 times.
✗ Branch 108 → 249 not taken.
32 subCmd->add_flag<bool>("--no-entry", cliOptions.noEntryFct, "Do not generate main function");
222 // --static
223
3/6
✓ Branch 115 → 116 taken 8 times.
✗ Branch 115 → 269 not taken.
✓ Branch 118 → 119 taken 8 times.
✗ Branch 118 → 263 not taken.
✓ Branch 119 → 120 taken 8 times.
✗ Branch 119 → 261 not taken.
32 subCmd->add_flag<bool>("--static", cliOptions.staticLinking, "Link statically");
224 // --dump-to-files
225
3/6
✓ Branch 126 → 127 taken 8 times.
✗ Branch 126 → 281 not taken.
✓ Branch 129 → 130 taken 8 times.
✗ Branch 129 → 275 not taken.
✓ Branch 130 → 131 taken 8 times.
✗ Branch 130 → 273 not taken.
32 subCmd->add_flag<bool>("--dump-to-files", cliOptions.dumpSettings.dumpToFiles, "Redirect dumps to files instead of printing");
226 // --abort-after-dump
227
3/6
✓ Branch 137 → 138 taken 8 times.
✗ Branch 137 → 293 not taken.
✓ Branch 140 → 141 taken 8 times.
✗ Branch 140 → 287 not taken.
✓ Branch 141 → 142 taken 8 times.
✗ Branch 141 → 285 not taken.
32 subCmd->add_flag<bool>("--abort-after-dump", cliOptions.dumpSettings.abortAfterDump,
228 "Abort the compilation process after dumping the first requested resource");
229 8 }
230
231 /**
232 * Add run subcommand to cli interface
233 */
234 8 void Driver::addRunSubcommand() {
235 // Create sub-command itself
236
3/6
✓ Branch 4 → 5 taken 8 times.
✗ Branch 4 → 55 not taken.
✓ Branch 7 → 8 taken 8 times.
✗ Branch 7 → 49 not taken.
✓ Branch 8 → 9 taken 8 times.
✗ Branch 8 → 47 not taken.
32 CLI::App *subCmd = app.add_subcommand("run", "Builds your Spice program and runs it immediately");
237
2/4
✓ Branch 15 → 16 taken 8 times.
✗ Branch 15 → 61 not taken.
✓ Branch 16 → 17 taken 8 times.
✗ Branch 16 → 59 not taken.
16 subCmd->alias("r");
238 8 subCmd->allow_non_standard_option_names();
239 8 subCmd->callback([&] {
240 2 shouldCompile = shouldExecute = true; // Requires the source file to be compiled
241 2 });
242
243 8 addCompileSubcommandOptions(subCmd);
244
245 // --debug-info
246
3/6
✓ Branch 26 → 27 taken 8 times.
✗ Branch 26 → 73 not taken.
✓ Branch 29 → 30 taken 8 times.
✗ Branch 29 → 67 not taken.
✓ Branch 30 → 31 taken 8 times.
✗ Branch 30 → 65 not taken.
32 subCmd->add_flag<bool>("--debug-info,-g", cliOptions.generateDebugInfo, "Generate debug info");
247 // --disable-verifier
248
3/6
✓ Branch 37 → 38 taken 8 times.
✗ Branch 37 → 85 not taken.
✓ Branch 40 → 41 taken 8 times.
✗ Branch 40 → 79 not taken.
✓ Branch 41 → 42 taken 8 times.
✗ Branch 41 → 77 not taken.
32 subCmd->add_flag<bool>("--disable-verifier", cliOptions.disableVerifier, "Disable LLVM module and function verification");
249 8 }
250
251 /**
252 * Add test subcommand to cli interface
253 */
254 8 void Driver::addTestSubcommand() {
255 // Create sub-command itself
256
3/6
✓ Branch 4 → 5 taken 8 times.
✗ Branch 4 → 55 not taken.
✓ Branch 7 → 8 taken 8 times.
✗ Branch 7 → 49 not taken.
✓ Branch 8 → 9 taken 8 times.
✗ Branch 8 → 47 not taken.
32 CLI::App *subCmd = app.add_subcommand("test", "Builds your Spice program and runs all enclosed tests");
257
2/4
✓ Branch 15 → 16 taken 8 times.
✗ Branch 15 → 61 not taken.
✓ Branch 16 → 17 taken 8 times.
✗ Branch 16 → 59 not taken.
16 subCmd->alias("t");
258 8 subCmd->allow_non_standard_option_names();
259 8 subCmd->callback([&] {
260 2 shouldCompile = shouldExecute = true; // Requires the source file to be compiled
261 2 cliOptions.buildMode = TEST; // Set build mode to test
262 2 cliOptions.generateTestMain = true; // An alternative entry function is generated
263 2 cliOptions.noEntryFct = true; // To not have two main functions, disable normal main
264 2 });
265
266 8 addCompileSubcommandOptions(subCmd);
267
268 // --debug-info
269
3/6
✓ Branch 26 → 27 taken 8 times.
✗ Branch 26 → 73 not taken.
✓ Branch 29 → 30 taken 8 times.
✗ Branch 29 → 67 not taken.
✓ Branch 30 → 31 taken 8 times.
✗ Branch 30 → 65 not taken.
32 subCmd->add_flag<bool>("--debug-info,-g", cliOptions.generateDebugInfo, "Generate debug info");
270 // --disable-verifier
271
3/6
✓ Branch 37 → 38 taken 8 times.
✗ Branch 37 → 85 not taken.
✓ Branch 40 → 41 taken 8 times.
✗ Branch 40 → 79 not taken.
✓ Branch 41 → 42 taken 8 times.
✗ Branch 41 → 77 not taken.
32 subCmd->add_flag<bool>("--disable-verifier", cliOptions.disableVerifier, "Disable LLVM module and function verification");
272 8 }
273
274 /**
275 * Add install subcommand to cli interface
276 */
277 8 void Driver::addInstallSubcommand() {
278 // Create sub-command itself
279
3/6
✓ Branch 4 → 5 taken 8 times.
✗ Branch 4 → 33 not taken.
✓ Branch 7 → 8 taken 8 times.
✗ Branch 7 → 27 not taken.
✓ Branch 8 → 9 taken 8 times.
✗ Branch 8 → 25 not taken.
32 CLI::App *subCmd = app.add_subcommand("install", "Builds your Spice program and installs it to a directory in the PATH");
280
2/4
✓ Branch 15 → 16 taken 8 times.
✗ Branch 15 → 39 not taken.
✓ Branch 16 → 17 taken 8 times.
✗ Branch 16 → 37 not taken.
16 subCmd->alias("i");
281 8 subCmd->allow_non_standard_option_names();
282 8 subCmd->callback([&] {
283 1 shouldCompile = true;
284 1 shouldInstall = true;
285 1 ensureNotDockerized();
286 1 });
287
288 8 addCompileSubcommandOptions(subCmd);
289 8 }
290
291 /**
292 * Add uninstall subcommand to cli interface
293 */
294 8 void Driver::addUninstallSubcommand() {
295 // Create sub-command itself
296
3/6
✓ Branch 4 → 5 taken 8 times.
✗ Branch 4 → 52 not taken.
✓ Branch 7 → 8 taken 8 times.
✗ Branch 7 → 46 not taken.
✓ Branch 8 → 9 taken 8 times.
✗ Branch 8 → 44 not taken.
32 CLI::App *subCmd = app.add_subcommand("uninstall", "Uninstalls a Spice program from the system");
297
2/4
✓ Branch 15 → 16 taken 8 times.
✗ Branch 15 → 58 not taken.
✓ Branch 16 → 17 taken 8 times.
✗ Branch 16 → 56 not taken.
16 subCmd->alias("u");
298 8 subCmd->allow_non_standard_option_names();
299 8 subCmd->callback([&] {
300 1 shouldUninstall = true;
301 1 ensureNotDockerized();
302 1 });
303
304 // Source file
305
2/4
✓ Branch 25 → 26 taken 8 times.
✗ Branch 25 → 79 not taken.
✓ Branch 28 → 29 taken 8 times.
✗ Branch 28 → 73 not taken.
24 subCmd->add_option<std::filesystem::path>("<main-source-file>", cliOptions.mainSourceFile, "Main source file")
306
4/8
✓ Branch 29 → 30 taken 8 times.
✗ Branch 29 → 71 not taken.
✓ Branch 32 → 33 taken 8 times.
✗ Branch 32 → 67 not taken.
✓ Branch 33 → 34 taken 8 times.
✗ Branch 33 → 64 not taken.
✓ Branch 34 → 35 taken 8 times.
✗ Branch 34 → 62 not taken.
40 ->check(CLI::ExistingFile)
307 8 ->required();
308 8 }
309
310 32 void Driver::addCompileSubcommandOptions(CLI::App *subCmd) {
311 1 const auto buildModeCallback = [&](const CLI::results_t &results) {
312
1/2
✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 31 not taken.
1 std::string inputString = results.front();
313
1/2
✓ Branch 5 → 6 taken 1 time.
✗ Branch 5 → 29 not taken.
1 std::ranges::transform(inputString, inputString.begin(), tolower);
314
315
2/4
✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 29 not taken.
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 1 time.
1 if (inputString == BUILD_MODE_DEBUG)
316 cliOptions.buildMode = DEBUG;
317
2/4
✓ Branch 9 → 10 taken 1 time.
✗ Branch 9 → 29 not taken.
✓ Branch 10 → 11 taken 1 time.
✗ Branch 10 → 12 not taken.
1 else if (inputString == BUILD_MODE_RELEASE)
318 1 cliOptions.buildMode = RELEASE;
319 else if (inputString == BUILD_MODE_TEST)
320 cliOptions.buildMode = TEST;
321 else
322 throw CliError(INVALID_BUILD_MODE, "Invalid build mode: " + inputString);
323
324 1 return true;
325 1 };
326
327 // --build-mode
328
3/6
✓ Branch 5 → 6 taken 32 times.
✗ Branch 5 → 293 not taken.
✓ Branch 9 → 10 taken 32 times.
✗ Branch 9 → 284 not taken.
✓ Branch 10 → 11 taken 32 times.
✗ Branch 10 → 282 not taken.
160 subCmd->add_option("--build-mode,-m", buildModeCallback, "Build mode (debug, release, test)");
329 // --llvm-args
330
3/6
✓ Branch 19 → 20 taken 32 times.
✗ Branch 19 → 308 not taken.
✓ Branch 22 → 23 taken 32 times.
✗ Branch 22 → 302 not taken.
✓ Branch 23 → 24 taken 32 times.
✗ Branch 23 → 300 not taken.
128 subCmd->add_option<std::string>("--llvm-args,-llvm", cliOptions.llvmArgs, "Additional arguments for LLVM")->join(' ');
331 // --jobs
332
3/6
✓ Branch 31 → 32 taken 32 times.
✗ Branch 31 → 320 not taken.
✓ Branch 34 → 35 taken 32 times.
✗ Branch 34 → 314 not taken.
✓ Branch 35 → 36 taken 32 times.
✗ Branch 35 → 312 not taken.
128 subCmd->add_option<unsigned short>("--jobs,-j", cliOptions.compileJobCount, "Compile jobs (threads), used for compilation");
333 // --ignore-cache
334
3/6
✓ Branch 42 → 43 taken 32 times.
✗ Branch 42 → 332 not taken.
✓ Branch 45 → 46 taken 32 times.
✗ Branch 45 → 326 not taken.
✓ Branch 46 → 47 taken 32 times.
✗ Branch 46 → 324 not taken.
128 subCmd->add_flag<bool>("--ignore-cache", cliOptions.ignoreCache, "Force re-compilation of all source files");
335 // --use-lifetime-markers
336
3/6
✓ Branch 53 → 54 taken 32 times.
✗ Branch 53 → 344 not taken.
✓ Branch 56 → 57 taken 32 times.
✗ Branch 56 → 338 not taken.
✓ Branch 57 → 58 taken 32 times.
✗ Branch 57 → 336 not taken.
128 subCmd->add_flag<bool>("--use-lifetime-markers", cliOptions.useLifetimeMarkers,
337 "Generate lifetime markers to enhance optimizations");
338
339 // Opt levels
340
3/6
✓ Branch 64 → 65 taken 32 times.
✗ Branch 64 → 360 not taken.
✓ Branch 68 → 69 taken 32 times.
✗ Branch 68 → 350 not taken.
✓ Branch 69 → 70 taken 32 times.
✗ Branch 69 → 348 not taken.
128 subCmd->add_flag_callback("-O0", [&] { cliOptions.optLevel = O0; }, "Disable optimization for the output executable.");
341
3/6
✓ Branch 77 → 78 taken 32 times.
✗ Branch 77 → 376 not taken.
✓ Branch 81 → 82 taken 32 times.
✗ Branch 81 → 366 not taken.
✓ Branch 82 → 83 taken 32 times.
✗ Branch 82 → 364 not taken.
128 subCmd->add_flag_callback("-O1", [&] { cliOptions.optLevel = O1; }, "Only basic optimization is applied.");
342
3/6
✓ Branch 90 → 91 taken 32 times.
✗ Branch 90 → 392 not taken.
✓ Branch 94 → 95 taken 32 times.
✗ Branch 94 → 382 not taken.
✓ Branch 95 → 96 taken 32 times.
✗ Branch 95 → 380 not taken.
128 subCmd->add_flag_callback("-O2", [&] { cliOptions.optLevel = O2; }, "More advanced optimization is applied.");
343
3/6
✓ Branch 103 → 104 taken 32 times.
✗ Branch 103 → 408 not taken.
✓ Branch 107 → 108 taken 32 times.
✗ Branch 107 → 398 not taken.
✓ Branch 108 → 109 taken 32 times.
✗ Branch 108 → 396 not taken.
128 subCmd->add_flag_callback("-O3", [&] { cliOptions.optLevel = O3; }, "Aggressive optimization for best performance.");
344
3/6
✓ Branch 116 → 117 taken 32 times.
✗ Branch 116 → 424 not taken.
✓ Branch 120 → 121 taken 32 times.
✗ Branch 120 → 414 not taken.
✓ Branch 121 → 122 taken 32 times.
✗ Branch 121 → 412 not taken.
128 subCmd->add_flag_callback("-Os", [&] { cliOptions.optLevel = Os; }, "Size optimization for output executable.");
345
3/6
✓ Branch 129 → 130 taken 32 times.
✗ Branch 129 → 440 not taken.
✓ Branch 133 → 134 taken 32 times.
✗ Branch 133 → 430 not taken.
✓ Branch 134 → 135 taken 32 times.
✗ Branch 134 → 428 not taken.
128 subCmd->add_flag_callback("-Oz", [&] { cliOptions.optLevel = Oz; }, "Aggressive optimization for best size.");
346
3/6
✓ Branch 142 → 143 taken 32 times.
✗ Branch 142 → 452 not taken.
✓ Branch 145 → 146 taken 32 times.
✗ Branch 145 → 446 not taken.
✓ Branch 146 → 147 taken 32 times.
✗ Branch 146 → 444 not taken.
128 subCmd->add_flag<bool>("-lto", cliOptions.useLTO, "Enable link time optimization (LTO)");
347
348 // --debug-output
349
3/6
✓ Branch 153 → 154 taken 32 times.
✗ Branch 153 → 464 not taken.
✓ Branch 156 → 157 taken 32 times.
✗ Branch 156 → 458 not taken.
✓ Branch 157 → 158 taken 32 times.
✗ Branch 157 → 456 not taken.
128 subCmd->add_flag<bool>("--debug-output,-d", cliOptions.printDebugOutput, "Enable debug output");
350 // --dump-cst
351
3/6
✓ Branch 164 → 165 taken 32 times.
✗ Branch 164 → 476 not taken.
✓ Branch 167 → 168 taken 32 times.
✗ Branch 167 → 470 not taken.
✓ Branch 168 → 169 taken 32 times.
✗ Branch 168 → 468 not taken.
128 subCmd->add_flag<bool>("--dump-cst,-cst", cliOptions.dumpSettings.dumpCST, "Dump CST as serialized string and SVG image");
352 // --dump-ast
353
3/6
✓ Branch 175 → 176 taken 32 times.
✗ Branch 175 → 488 not taken.
✓ Branch 178 → 179 taken 32 times.
✗ Branch 178 → 482 not taken.
✓ Branch 179 → 180 taken 32 times.
✗ Branch 179 → 480 not taken.
128 subCmd->add_flag<bool>("--dump-ast,-ast", cliOptions.dumpSettings.dumpAST, "Dump AST as serialized string and SVG image");
354 // --dump-symtab
355
3/6
✓ Branch 186 → 187 taken 32 times.
✗ Branch 186 → 500 not taken.
✓ Branch 189 → 190 taken 32 times.
✗ Branch 189 → 494 not taken.
✓ Branch 190 → 191 taken 32 times.
✗ Branch 190 → 492 not taken.
128 subCmd->add_flag<bool>("--dump-symtab", cliOptions.dumpSettings.dumpSymbolTable, "Dump serialized symbol tables");
356 // --dump-types
357
3/6
✓ Branch 197 → 198 taken 32 times.
✗ Branch 197 → 512 not taken.
✓ Branch 200 → 201 taken 32 times.
✗ Branch 200 → 506 not taken.
✓ Branch 201 → 202 taken 32 times.
✗ Branch 201 → 504 not taken.
128 subCmd->add_flag<bool>("--dump-types", cliOptions.dumpSettings.dumpTypes, "Dump all used types");
358 // --dump-cache-stats
359
3/6
✓ Branch 208 → 209 taken 32 times.
✗ Branch 208 → 524 not taken.
✓ Branch 211 → 212 taken 32 times.
✗ Branch 211 → 518 not taken.
✓ Branch 212 → 213 taken 32 times.
✗ Branch 212 → 516 not taken.
128 subCmd->add_flag<bool>("--dump-cache-stats", cliOptions.dumpSettings.dumpCacheStats,
360 "Dump stats for compiler-internal lookup caches");
361 // --dump-ir
362
3/6
✓ Branch 219 → 220 taken 32 times.
✗ Branch 219 → 536 not taken.
✓ Branch 222 → 223 taken 32 times.
✗ Branch 222 → 530 not taken.
✓ Branch 223 → 224 taken 32 times.
✗ Branch 223 → 528 not taken.
128 subCmd->add_flag<bool>("--dump-ir,-ir", cliOptions.dumpSettings.dumpIR, "Dump LLVM-IR");
363 // --dump-assembly
364
3/6
✓ Branch 230 → 231 taken 32 times.
✗ Branch 230 → 548 not taken.
✓ Branch 233 → 234 taken 32 times.
✗ Branch 233 → 542 not taken.
✓ Branch 234 → 235 taken 32 times.
✗ Branch 234 → 540 not taken.
128 subCmd->add_flag<bool>("--dump-assembly,-asm,-s", cliOptions.dumpSettings.dumpAssembly, "Dump Assembly code");
365 // --dump-object-file
366
3/6
✓ Branch 241 → 242 taken 32 times.
✗ Branch 241 → 560 not taken.
✓ Branch 244 → 245 taken 32 times.
✗ Branch 244 → 554 not taken.
✓ Branch 245 → 246 taken 32 times.
✗ Branch 245 → 552 not taken.
128 subCmd->add_flag<bool>("--dump-object-file", cliOptions.dumpSettings.dumpObjectFile, "Dump object file");
367 // --dump-dependency-graph
368
3/6
✓ Branch 252 → 253 taken 32 times.
✗ Branch 252 → 572 not taken.
✓ Branch 255 → 256 taken 32 times.
✗ Branch 255 → 566 not taken.
✓ Branch 256 → 257 taken 32 times.
✗ Branch 256 → 564 not taken.
128 subCmd->add_flag<bool>("--dump-dependency-graph", cliOptions.dumpSettings.dumpDependencyGraph,
369 "Dump compile unit dependency graph");
370
371 // Source file
372
2/4
✓ Branch 263 → 264 taken 32 times.
✗ Branch 263 → 593 not taken.
✓ Branch 266 → 267 taken 32 times.
✗ Branch 266 → 587 not taken.
96 subCmd->add_option<std::filesystem::path>("<main-source-file>", cliOptions.mainSourceFile, "Main source file")
373
4/8
✓ Branch 267 → 268 taken 32 times.
✗ Branch 267 → 585 not taken.
✓ Branch 270 → 271 taken 32 times.
✗ Branch 270 → 581 not taken.
✓ Branch 271 → 272 taken 32 times.
✗ Branch 271 → 578 not taken.
✓ Branch 272 → 273 taken 32 times.
✗ Branch 272 → 576 not taken.
160 ->check(CLI::ExistingFile)
374 32 ->required();
375 32 }
376
377 /**
378 * Ensure that the compiler is not running in a Docker container
379 */
380 2 void Driver::ensureNotDockerized() {
381 2 const char *envValue = std::getenv(ENV_VAR_DOCKERIZED);
382
1/4
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 13 taken 2 times.
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 13 not taken.
2 if (envValue != nullptr && std::strcmp(envValue, "true") == 0)
383 throw CliError(FEATURE_NOT_SUPPORTED_WHEN_DOCKERIZED,
384 "This feature is not supported in a containerized environment. Please use the standalone version of Spice.");
385 2 }
386
387 } // namespace spice::compiler
388