GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 21 / 3 / 24
Functions: 100.0% 3 / 0 / 3
Branches: 53.1% 17 / 14 / 46

src/objectemitter/LLVMObjectEmitter.cpp
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #include "LLVMObjectEmitter.h"
4
5 #include <SourceFile.h>
6 #include <driver/Driver.h>
7 #include <exception/CompilerError.h>
8 #include <global/GlobalResourceManager.h>
9 #include <util/RawStringOStream.h>
10
11 #include <llvm/IR/LegacyPassManager.h>
12 #include <llvm/Support/CodeGen.h>
13 #include <llvm/Support/FileSystem.h>
14
15 namespace spice::compiler {
16
17 2210 LLVMObjectEmitter::LLVMObjectEmitter(GlobalResourceManager &resourceManager, SourceFile *sourceFile)
18 : CompilerPass(resourceManager, sourceFile),
19
3/4
✓ Branch 3 → 4 taken 2210 times.
✗ Branch 3 → 8 not taken.
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 2209 times.
2210 module(cliOptions.useLTO ? *resourceManager.ltoModule : *sourceFile->llvmModule) {}
20
21 2210 void LLVMObjectEmitter::emit(const std::filesystem::path &objectPath) const {
22
1/2
✓ Branch 2 → 3 taken 2210 times.
✗ Branch 2 → 58 not taken.
2210 const std::string objectPathString = objectPath.string();
23
24 // Open file output stream
25 2210 std::error_code errorCode;
26
1/2
✓ Branch 5 → 6 taken 2210 times.
✗ Branch 5 → 33 not taken.
2210 llvm::raw_fd_ostream stream(objectPathString, errorCode, llvm::sys::fs::OF_None);
27
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 15 taken 2210 times.
2210 if (errorCode)
28 throw CompilerError(CANT_OPEN_OUTPUT_FILE, "File '" + objectPathString + "' could not be opened"); // GCOV_EXCL_LINE
29
30
1/2
✓ Branch 15 → 16 taken 2210 times.
✗ Branch 15 → 54 not taken.
2210 llvm::legacy::PassManager passManager;
31 2210 constexpr auto fileType = llvm::CodeGenFileType::ObjectFile;
32
2/4
✓ Branch 17 → 18 taken 2210 times.
✗ Branch 17 → 52 not taken.
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 27 taken 2210 times.
2210 if (sourceFile->targetMachine->addPassesToEmitFile(passManager, stream, nullptr, fileType, cliOptions.disableVerifier))
33 throw CompilerError(WRONG_OUTPUT_TYPE, "Target machine can't emit a file of this type"); // GCOV_EXCL_LINE
34
35 // Emit object file
36
1/2
✓ Branch 27 → 28 taken 2210 times.
✗ Branch 27 → 52 not taken.
2210 passManager.run(module);
37
1/2
✓ Branch 28 → 29 taken 2210 times.
✗ Branch 28 → 52 not taken.
2210 stream.flush();
38 2210 }
39
40 2163 void LLVMObjectEmitter::getASMString(std::string &output) const {
41
1/2
✓ Branch 2 → 3 taken 2163 times.
✗ Branch 2 → 33 not taken.
2163 RawStringOStream ostream(output);
42
1/2
✓ Branch 3 → 4 taken 2163 times.
✗ Branch 3 → 31 not taken.
2163 llvm::legacy::PassManager passManager;
43 2163 constexpr auto fileType = llvm::CodeGenFileType::AssemblyFile;
44
2/4
✓ Branch 5 → 6 taken 2163 times.
✗ Branch 5 → 29 not taken.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 15 taken 2163 times.
2163 if (sourceFile->targetMachine->addPassesToEmitFile(passManager, ostream, nullptr, fileType, cliOptions.disableVerifier))
45 throw CompilerError(WRONG_OUTPUT_TYPE, "Target machine can't emit a file of this type"); // GCOV_EXCL_LINE
46
47 // Emit object file
48
1/2
✓ Branch 15 → 16 taken 2163 times.
✗ Branch 15 → 29 not taken.
2163 passManager.run(module);
49
1/2
✓ Branch 16 → 17 taken 2163 times.
✗ Branch 16 → 29 not taken.
2163 ostream.flush();
50 2163 }
51
52 } // namespace spice::compiler
53