GCC Code Coverage Report


Directory: ../
File: /home/runner/work/spice/spice/src/objectemitter/ObjectEmitter.cpp
Date: 2024-05-26 23:47:57
Exec Total Coverage
Lines: 21 21 100.0%
Functions: 3 3 100.0%
Branches: 16 30 53.3%

Line Branch Exec Source
1 // Copyright (c) 2021-2024 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 556 ObjectEmitter::ObjectEmitter(spice::compiler::GlobalResourceManager &resourceManager, spice::compiler::SourceFile *sourceFile)
15 : CompilerPass(resourceManager, sourceFile),
16
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 555 times.
556 module(cliOptions.useLTO ? *resourceManager.ltoModule : *sourceFile->llvmModule) {}
17
18 556 void ObjectEmitter::emit(const std::filesystem::path &objectPath) const {
19
1/2
✓ Branch 1 taken 556 times.
✗ Branch 2 not taken.
556 const std::string objectPathString = objectPath.string();
20
21 // Open file output stream
22 556 std::error_code errorCode;
23
1/2
✓ Branch 2 taken 556 times.
✗ Branch 3 not taken.
556 llvm::raw_fd_ostream stream(objectPathString, errorCode, llvm::sys::fs::OF_None);
24
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 556 times.
556 if (errorCode)
25 throw CompilerError(CANT_OPEN_OUTPUT_FILE, "File '" + objectPathString + "' could not be opened"); // GCOV_EXCL_LINE
26
27
1/2
✓ Branch 1 taken 556 times.
✗ Branch 2 not taken.
556 llvm::legacy::PassManager passManager;
28 556 const llvm::CodeGenFileType fileType = llvm::CodeGenFileType::ObjectFile;
29
2/4
✓ Branch 2 taken 556 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 556 times.
556 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 1 taken 556 times.
✗ Branch 2 not taken.
556 passManager.run(module);
34
1/2
✓ Branch 1 taken 556 times.
✗ Branch 2 not taken.
556 stream.flush();
35 556 }
36
37 556 void ObjectEmitter::getASMString(std::string &output) const {
38
1/2
✓ Branch 1 taken 556 times.
✗ Branch 2 not taken.
556 RawStringOStream ostream(output);
39
1/2
✓ Branch 1 taken 556 times.
✗ Branch 2 not taken.
556 llvm::legacy::PassManager passManager;
40 556 const llvm::CodeGenFileType fileType = llvm::CodeGenFileType::AssemblyFile;
41
2/4
✓ Branch 2 taken 556 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 556 times.
556 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 1 taken 556 times.
✗ Branch 2 not taken.
556 passManager.run(module);
46
1/2
✓ Branch 1 taken 556 times.
✗ Branch 2 not taken.
556 ostream.flush();
47 556 }
48
49 } // namespace spice::compiler
50