GCC Code Coverage Report


Directory: ../
File: src/global/GlobalResourceManager.cpp
Date: 2025-02-05 01:09:36
Exec Total Coverage
Lines: 27 36 75.0%
Functions: 4 6 66.7%
Branches: 27 84 32.1%

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 401 GlobalResourceManager::GlobalResourceManager(const CliOptions &cliOptions)
19
5/10
✓ Branch 0 (4→5) taken 401 times.
✗ Branch 1 (4→94) not taken.
✓ Branch 2 (8→9) taken 401 times.
✗ Branch 3 (8→88) not taken.
✓ Branch 4 (9→10) taken 401 times.
✗ Branch 5 (9→88) not taken.
✓ Branch 6 (11→12) taken 401 times.
✗ Branch 7 (11→84) not taken.
✓ Branch 8 (14→15) taken 401 times.
✗ Branch 9 (14→80) not taken.
401 : cliOptions(cliOptions), linker(cliOptions), cacheManager(cliOptions.cacheDir), runtimeModuleManager(*this) {
20 // Initialize the required LLVM targets
21
1/2
✓ Branch 0 (17→18) taken 401 times.
✗ Branch 1 (17→21) not taken.
401 if (cliOptions.isNativeTarget) {
22
1/2
✓ Branch 0 (18→19) taken 401 times.
✗ Branch 1 (18→77) not taken.
401 llvm::InitializeNativeTarget();
23
1/2
✓ Branch 0 (19→20) taken 401 times.
✗ Branch 1 (19→77) not taken.
401 llvm::InitializeNativeTargetAsmPrinter();
24
1/2
✓ Branch 0 (20→25) taken 401 times.
✗ Branch 1 (20→77) not taken.
401 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 0 (25→26) taken 401 times.
✗ Branch 1 (25→77) not taken.
401 cpuName = "generic";
34
1/2
✓ Branch 0 (26→27) taken 401 times.
✗ Branch 1 (26→77) not taken.
401 std::stringstream featureString;
35
2/4
✓ Branch 0 (27→28) taken 401 times.
✗ Branch 1 (27→55) not taken.
✗ Branch 2 (28→29) not taken.
✓ Branch 3 (28→55) taken 401 times.
401 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 0 (55→56) taken 401 times.
✗ Branch 1 (55→73) not taken.
401 cpuFeatures = featureString.str();
45
46 // Create lto module
47
2/2
✓ Branch 0 (58→59) taken 1 times.
✓ Branch 1 (58→63) taken 400 times.
401 if (cliOptions.useLTO)
48
1/2
✓ Branch 0 (59→60) taken 1 times.
✗ Branch 1 (59→74) not taken.
1 ltoModule = std::make_unique<llvm::Module>(LTO_FILE_NAME, ltoContext);
49 401 }
50
51 401 GlobalResourceManager::~GlobalResourceManager() {
52 // Cleanup all statics
53 401 TypeRegistry::clear();
54 401 FunctionManager::clear();
55 401 StructManager::clear();
56 401 InterfaceManager::clear();
57 // Cleanup all LLVM statics
58 401 llvm::llvm_shutdown();
59 401 }
60
61 1159 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 0 (2→3) taken 1159 times.
✗ Branch 1 (2→24) not taken.
✓ Branch 2 (3→4) taken 1159 times.
✗ Branch 3 (3→22) not taken.
✓ Branch 4 (4→5) taken 1159 times.
✗ Branch 5 (4→20) not taken.
1159 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 0 (7→8) taken 1159 times.
✗ Branch 1 (7→32) not taken.
✓ Branch 2 (8→9) taken 1025 times.
✓ Branch 3 (8→15) taken 134 times.
1159 if (!sourceFiles.contains(filePathStr))
68
3/6
✓ Branch 0 (9→10) taken 1025 times.
✗ Branch 1 (9→30) not taken.
✓ Branch 2 (10→11) taken 1025 times.
✗ Branch 3 (10→28) not taken.
✓ Branch 4 (11→12) taken 1025 times.
✗ Branch 5 (11→26) not taken.
1025 sourceFiles.insert({filePathStr, std::make_unique<SourceFile>(*this, parent, depName, path, isStdFile)});
69
70
1/2
✓ Branch 0 (15→16) taken 1159 times.
✗ Branch 1 (15→32) not taken.
2318 return sourceFiles.at(filePathStr).get();
71 1159 }
72
73 784 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