GCC Code Coverage Report


Directory: ../
File: src/driver/Driver.cpp
Date: 2025-11-11 00:12:13
Coverage Exec Excl Total
Lines: 82.5% 189 3 232
Functions: 80.8% 21 0 26
Branches: 44.1% 249 8 572

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