GCC Code Coverage Report


Directory: ../
File: src/objectemitter/ObjectEmitter.cpp
Date: 2025-10-09 06:28:01
Coverage Exec Excl Total
Lines: 100.0% 21 3 24
Functions: 100.0% 3 0 3
Branches: 53.3% 16 14 44

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