GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 91.7% 33 / 4 / 40
Functions: 66.7% 4 / 0 / 6
Branches: 56.4% 44 / 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/TypeNameDisambiguator.h>
8 #include <global/TypeRegistry.h>
9 #include <symboltablebuilder/Scope.h> // IWYU pragma: keep - Scope
10 #include <typechecker/FunctionManager.h>
11 #include <typechecker/InterfaceManager.h>
12 #include <typechecker/StructManager.h>
13 #include <util/FileUtil.h>
14
15 #include <llvm/IR/Module.h>
16 #include <llvm/Support/ManagedStatic.h>
17 #include <llvm/Support/TargetSelect.h>
18 #include <llvm/TargetParser/Host.h>
19 #include <llvm/TargetParser/SubtargetFeature.h>
20
21 namespace spice::compiler {
22
23 567 GlobalResourceManager::GlobalResourceManager(const CliOptions &cliOptions)
24
6/12
✓ Branch 4 → 5 taken 567 times.
✗ Branch 4 → 87 not taken.
✓ Branch 8 → 9 taken 567 times.
✗ Branch 8 → 81 not taken.
✓ Branch 9 → 10 taken 567 times.
✗ Branch 9 → 81 not taken.
✓ Branch 12 → 13 taken 567 times.
✗ Branch 12 → 75 not taken.
✓ Branch 13 → 14 taken 567 times.
✗ Branch 13 → 73 not taken.
✓ Branch 15 → 16 taken 567 times.
✗ Branch 15 → 71 not taken.
567 : cliOptions(cliOptions), linker(cliOptions), cacheManager(cliOptions), runtimeModuleManager(*this) {
25 // Initialize the required LLVM targets
26
2/2
✓ Branch 18 → 19 taken 562 times.
✓ Branch 18 → 22 taken 5 times.
567 if (cliOptions.isNativeTarget) {
27
1/2
✓ Branch 19 → 20 taken 562 times.
✗ Branch 19 → 69 not taken.
562 llvm::InitializeNativeTarget();
28
1/2
✓ Branch 20 → 21 taken 562 times.
✗ Branch 20 → 69 not taken.
562 llvm::InitializeNativeTargetAsmPrinter();
29
1/2
✓ Branch 21 → 26 taken 562 times.
✗ Branch 21 → 69 not taken.
562 llvm::InitializeNativeTargetAsmParser();
30 } else { // GCOV_EXCL_START
31 llvm::InitializeAllTargets();
32 llvm::InitializeAllTargetMCs();
33 llvm::InitializeAllAsmPrinters();
34 llvm::InitializeAllAsmParsers();
35 } // GCOV_EXCL_STOP
36
37 // Create cpu name and features strings
38
1/2
✓ Branch 26 → 27 taken 567 times.
✗ Branch 26 → 69 not taken.
567 cpuName = "generic";
39
4/4
✓ Branch 27 → 28 taken 562 times.
✓ Branch 27 → 51 taken 5 times.
✓ Branch 28 → 29 taken 3 times.
✓ Branch 28 → 51 taken 559 times.
567 if (cliOptions.isNativeTarget && cliOptions.useCPUFeatures) {
40 // Retrieve native CPU name and the supported CPU features
41
2/4
✓ Branch 29 → 30 taken 3 times.
✗ Branch 29 → 58 not taken.
✓ Branch 30 → 31 taken 3 times.
✗ Branch 30 → 58 not taken.
3 cpuName = llvm::sys::getHostCPUName();
42
2/4
✓ Branch 31 → 32 taken 3 times.
✗ Branch 31 → 59 not taken.
✓ Branch 32 → 33 taken 3 times.
✗ Branch 32 → 59 not taken.
3 llvm::SubtargetFeatures features;
43
9/16
✓ Branch 33 → 34 taken 3 times.
✗ Branch 33 → 63 not taken.
✓ Branch 34 → 35 taken 3 times.
✗ Branch 34 → 61 not taken.
✓ Branch 35 → 36 taken 3 times.
✗ Branch 35 → 61 not taken.
✓ Branch 38 → 39 taken 342 times.
✗ Branch 38 → 60 not taken.
✓ Branch 39 → 40 taken 342 times.
✗ Branch 39 → 60 not taken.
✓ Branch 41 → 42 taken 342 times.
✗ Branch 41 → 61 not taken.
✓ Branch 43 → 44 taken 345 times.
✗ Branch 43 → 61 not taken.
✓ Branch 44 → 37 taken 342 times.
✓ Branch 44 → 45 taken 3 times.
687 for (const auto &[feature, isEnabled] : llvm::sys::getHostCPUFeatures())
44
1/2
✓ Branch 40 → 41 taken 342 times.
✗ Branch 40 → 60 not taken.
342 features.AddFeature(feature, isEnabled);
45
1/2
✓ Branch 46 → 47 taken 3 times.
✗ Branch 46 → 64 not taken.
3 cpuFeatures = features.getString();
46 3 }
47
48
2/2
✓ Branch 51 → 52 taken 1 time.
✓ Branch 51 → 57 taken 566 times.
567 if (cliOptions.useLTO) {
49 // Discard value names if not required
50
1/2
✓ Branch 52 → 53 taken 1 time.
✗ Branch 52 → 69 not taken.
1 ltoContext.setDiscardValueNames(!cliOptions.namesForIRValues);
51 // Create lto module
52
1/2
✓ Branch 53 → 54 taken 1 time.
✗ Branch 53 → 68 not taken.
1 ltoModule = std::make_unique<llvm::Module>(LTO_FILE_NAME, ltoContext);
53 }
54 567 }
55
56 567 GlobalResourceManager::~GlobalResourceManager() {
57 // Notify all global components to prepare to destroy
58 567 TypeRegistry::clear();
59 567 TypeNameDisambiguator::clear();
60 567 FunctionManager::cleanup();
61 567 StructManager::cleanup();
62 567 InterfaceManager::cleanup();
63 // Cleanup all LLVM statics
64 567 llvm::llvm_shutdown();
65 567 }
66
67 3723 SourceFile *GlobalResourceManager::createSourceFile(SourceFile *parent, const std::string &depName,
68 const std::filesystem::path &path, bool isStdFile) {
69 // Check if the source file was already added (e.g. by another source file that imports it)
70
3/6
✓ Branch 2 → 3 taken 3723 times.
✗ Branch 2 → 22 not taken.
✓ Branch 3 → 4 taken 3723 times.
✗ Branch 3 → 20 not taken.
✓ Branch 4 → 5 taken 3723 times.
✗ Branch 4 → 18 not taken.
3723 const std::string filePathStr = weakly_canonical(absolute(path)).string();
71
72 // Create the new source file if it does not exist yet
73
3/4
✓ Branch 7 → 8 taken 3723 times.
✗ Branch 7 → 27 not taken.
✓ Branch 8 → 9 taken 2446 times.
✓ Branch 8 → 13 taken 1277 times.
3723 if (!sourceFiles.contains(filePathStr))
74
2/4
✓ Branch 9 → 10 taken 2446 times.
✗ Branch 9 → 26 not taken.
✓ Branch 10 → 11 taken 2446 times.
✗ Branch 10 → 24 not taken.
2446 sourceFiles.emplace(filePathStr, std::make_unique<SourceFile>(*this, parent, depName, path, isStdFile));
75
76
1/2
✓ Branch 13 → 14 taken 3723 times.
✗ Branch 13 → 27 not taken.
7446 return sourceFiles.at(filePathStr).get();
77 3723 }
78
79 4080 uint64_t GlobalResourceManager::getNextCustomTypeId() { return nextCustomTypeId++; }
80
81 size_t GlobalResourceManager::getTotalLineCount() const {
82 const auto acc = [](size_t sum, const auto &sourceFile) { return sum + FileUtil::getLineCount(sourceFile.second->filePath); };
83 return std::accumulate(sourceFiles.begin(), sourceFiles.end(), 0, acc);
84 }
85
86 } // namespace spice::compiler
87