GCC Code Coverage Report


Directory: ../
File: src/global/GlobalResourceManager.cpp
Date: 2025-10-23 00:48:11
Coverage Exec Excl Total
Lines: 75.0% 27 4 40
Functions: 66.7% 4 0 6
Branches: 32.1% 27 8 92

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