GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 73.5% 25 / 4 / 38
Functions: 66.7% 4 / 0 / 6
Branches: 35.9% 28 / 8 / 86

src/global/GlobalResourceManager.cpp
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #include "GlobalResourceManager.h"
4
5 #include <SourceFile.h>
6 #include <driver/Driver.h>
7 #include <global/TypeRegistry.h>
8 #include <symboltablebuilder/Scope.h> // IWYU pragma: keep - Scope
9 #include <typechecker/FunctionManager.h>
10 #include <typechecker/InterfaceManager.h>
11 #include <typechecker/StructManager.h>
12 #include <util/FileUtil.h>
13
14 #include <llvm/IR/Module.h>
15 #include <llvm/Support/ManagedStatic.h>
16 #include <llvm/Support/TargetSelect.h>
17 #include <llvm/TargetParser/Host.h>
18 #include <llvm/TargetParser/SubtargetFeature.h>
19
20 namespace spice::compiler {
21
22 443 GlobalResourceManager::GlobalResourceManager(const CliOptions &cliOptions)
23
6/12
✓ Branch 4 → 5 taken 443 times.
✗ Branch 4 → 83 not taken.
✓ Branch 8 → 9 taken 443 times.
✗ Branch 8 → 77 not taken.
✓ Branch 9 → 10 taken 443 times.
✗ Branch 9 → 77 not taken.
✓ Branch 11 → 12 taken 443 times.
✗ Branch 11 → 73 not taken.
✓ Branch 12 → 13 taken 443 times.
✗ Branch 12 → 71 not taken.
✓ Branch 14 → 15 taken 443 times.
✗ Branch 14 → 69 not taken.
443 : cliOptions(cliOptions), linker(cliOptions), cacheManager(cliOptions), runtimeModuleManager(*this) {
24 // Initialize the required LLVM targets
25
2/2
✓ Branch 17 → 18 taken 438 times.
✓ Branch 17 → 21 taken 5 times.
443 if (cliOptions.isNativeTarget) {
26
1/2
✓ Branch 18 → 19 taken 438 times.
✗ Branch 18 → 67 not taken.
438 llvm::InitializeNativeTarget();
27
1/2
✓ Branch 19 → 20 taken 438 times.
✗ Branch 19 → 67 not taken.
438 llvm::InitializeNativeTargetAsmPrinter();
28
1/2
✓ Branch 20 → 25 taken 438 times.
✗ Branch 20 → 67 not taken.
438 llvm::InitializeNativeTargetAsmParser();
29 } else { // GCOV_EXCL_START
30 llvm::InitializeAllTargets();
31 llvm::InitializeAllTargetMCs();
32 llvm::InitializeAllAsmPrinters();
33 llvm::InitializeAllAsmParsers();
34 } // GCOV_EXCL_STOP
35
36 // Create cpu name and features strings
37
1/2
✓ Branch 25 → 26 taken 443 times.
✗ Branch 25 → 67 not taken.
443 cpuName = "generic";
38
3/4
✓ Branch 26 → 27 taken 438 times.
✓ Branch 26 → 50 taken 5 times.
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 50 taken 438 times.
443 if (cliOptions.isNativeTarget && cliOptions.useCPUFeatures) {
39 // Retrieve native CPU name and the supported CPU features
40 cpuName = llvm::sys::getHostCPUName();
41 llvm::SubtargetFeatures features;
42 for (const auto &[feature, isEnabled] : llvm::sys::getHostCPUFeatures())
43 features.AddFeature(feature, isEnabled);
44 cpuFeatures = features.getString();
45 }
46
47 // Create lto module
48
2/2
✓ Branch 50 → 51 taken 1 time.
✓ Branch 50 → 55 taken 442 times.
443 if (cliOptions.useLTO)
49
1/2
✓ Branch 51 → 52 taken 1 time.
✗ Branch 51 → 66 not taken.
1 ltoModule = std::make_unique<llvm::Module>(LTO_FILE_NAME, ltoContext);
50 443 }
51
52 443 GlobalResourceManager::~GlobalResourceManager() {
53 // Notify all global components to prepare to destroy
54 443 TypeRegistry::clear();
55 443 FunctionManager::cleanup();
56 443 StructManager::cleanup();
57 443 InterfaceManager::cleanup();
58 // Cleanup all LLVM statics
59 443 llvm::llvm_shutdown();
60 443 }
61
62 1451 SourceFile *GlobalResourceManager::createSourceFile(SourceFile *parent, const std::string &depName,
63 const std::filesystem::path &path, bool isStdFile) {
64 // Check if the source file was already added (e.g. by another source file that imports it)
65
3/6
✓ Branch 2 → 3 taken 1451 times.
✗ Branch 2 → 24 not taken.
✓ Branch 3 → 4 taken 1451 times.
✗ Branch 3 → 22 not taken.
✓ Branch 4 → 5 taken 1451 times.
✗ Branch 4 → 20 not taken.
1451 const std::string filePathStr = weakly_canonical(absolute(path)).string();
66
67 // Create the new source file if it does not exist yet
68
3/4
✓ Branch 7 → 8 taken 1451 times.
✗ Branch 7 → 32 not taken.
✓ Branch 8 → 9 taken 1237 times.
✓ Branch 8 → 15 taken 214 times.
1451 if (!sourceFiles.contains(filePathStr))
69
3/6
✓ Branch 9 → 10 taken 1237 times.
✗ Branch 9 → 30 not taken.
✓ Branch 10 → 11 taken 1237 times.
✗ Branch 10 → 28 not taken.
✓ Branch 11 → 12 taken 1237 times.
✗ Branch 11 → 26 not taken.
1237 sourceFiles.insert({filePathStr, std::make_unique<SourceFile>(*this, parent, depName, path, isStdFile)});
70
71
1/2
✓ Branch 15 → 16 taken 1451 times.
✗ Branch 15 → 32 not taken.
2902 return sourceFiles.at(filePathStr).get();
72 1451 }
73
74 984 uint64_t GlobalResourceManager::getNextCustomTypeId() { return nextCustomTypeId++; }
75
76 size_t GlobalResourceManager::getTotalLineCount() const {
77 const auto acc = [](size_t sum, const auto &sourceFile) { return sum + FileUtil::getLineCount(sourceFile.second->filePath); };
78 return std::accumulate(sourceFiles.begin(), sourceFiles.end(), 0, acc);
79 }
80
81 } // namespace spice::compiler
82