GCC Code Coverage Report


Directory: ../
File: src/driver/Driver.cpp
Date: 2025-02-09 04:23:07
Exec Total Coverage
Lines: 163 211 77.3%
Functions: 19 24 79.2%
Branches: 225 542 41.5%

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