GCC Code Coverage Report


Directory: ../
File: src/driver/Driver.cpp
Date: 2025-12-07 00:53:49
Coverage Exec Excl Total
Lines: 86.8% 203 3 237
Functions: 80.8% 21 0 26
Branches: 47.3% 278 8 596

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