GCC Code Coverage Report


Directory: ../
File: src/global/GlobalResourceManager.cpp
Date: 2025-11-14 09:25:32
Coverage Exec Excl Total
Lines: 73.5% 25 4 38
Functions: 66.7% 4 0 6
Branches: 36.5% 27 8 82

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