GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 0.0% 0 / 0 / 44
Functions: 0.0% 0 / 0 / 2
Branches: 0.0% 0 / 0 / 80

src/main.cpp
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #include <SourceFile.h>
4 #include <driver/Driver.h>
5 #include <exception/CliError.h>
6 #include <exception/LexerError.h>
7 #include <exception/LinkerError.h>
8 #include <exception/ParserError.h>
9 #include <exception/SemanticError.h>
10 #include <global/GlobalResourceManager.h>
11 #include <typechecker/MacroDefs.h>
12
13 using namespace spice::compiler;
14
15 /**
16 * Compile main source file. All files, that are included by the main source file will be resolved recursively.
17 *
18 * @param cliOptions Command line options
19 * @return Successful or not
20 */
21 bool compileProject(const CliOptions &cliOptions) {
22 try {
23 // Instantiate GlobalResourceManager
24 GlobalResourceManager resourceManager(cliOptions);
25
26 // Create source file instance for main source file
27 SourceFile *mainSourceFile = resourceManager.createSourceFile(nullptr, MAIN_FILE_NAME, cliOptions.mainSourceFile, false);
28
29 // Run compile pipeline for main source file. All dependent source files are triggered by their parents
30 mainSourceFile->runFrontEnd();
31 CHECK_ABORT_FLAG_B()
32 mainSourceFile->runMiddleEnd();
33 CHECK_ABORT_FLAG_B()
34 mainSourceFile->runBackEnd();
35 CHECK_ABORT_FLAG_B()
36
37 // Link the target executable (link object files to executable/library)
38 if (cliOptions.outputContainer != OutputContainer::OBJECT_FILE) {
39 resourceManager.linker.prepare();
40 resourceManager.linker.run();
41 resourceManager.linker.cleanup();
42 }
43
44 // Print compiler warnings
45 mainSourceFile->collectAndPrintWarnings();
46
47 return true;
48 } catch (LexerError &e) {
49 std::cout << e.what() << "\n";
50 } catch (ParserError &e) {
51 std::cout << e.what() << "\n";
52 } catch (SemanticError &e) {
53 std::cout << e.what() << "\n";
54 } catch (CompilerError &e) {
55 std::cout << e.what() << "\n";
56 } catch (LinkerError &e) {
57 std::cout << e.what() << "\n";
58 }
59 return false;
60 }
61
62 /**
63 * Entry point to the Spice compiler
64 *
65 * @param argc Argument count
66 * @param argv Argument vector
67 * @return Return code
68 */
69 int main(int argc, const char *argv[]) {
70 // Initialize command line parser
71 try {
72 CliOptions cliOptions;
73 Driver driver(cliOptions);
74 if (const int exitCode = driver.parse(argc, argv); exitCode != EXIT_SUCCESS)
75 return exitCode;
76
77 // Cancel here if we do not have to compile
78 if (!driver.shouldCompile)
79 return EXIT_SUCCESS;
80
81 driver.enrich(); // Prepare the cli options
82
83 // Kick off the compilation process
84 if (!compileProject(driver.cliOptions))
85 return EXIT_FAILURE;
86
87 // Execute
88 if (driver.cliOptions.execute)
89 driver.runBinary();
90
91 return EXIT_SUCCESS;
92 } catch (CliError &e) {
93 std::cout << e.what() << "\n";
94 return EXIT_FAILURE;
95 }
96 }
97