GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 9 / 0 / 9
Functions: 100.0% 3 / 0 / 3
Branches: 87.5% 7 / 0 / 8

src/util/CodeLoc.h
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #pragma once
4
5 #include <Token.h>
6 #include <misc/Interval.h>
7
8 #include <util/GlobalDefinitions.h>
9
10 namespace spice::compiler {
11
12 // Forward declarations
13 class SourceFile;
14
15 struct CodeLoc {
16 // Constructors
17 187541 explicit CodeLoc(const antlr4::Token *token, SourceFile *sourceFile = nullptr)
18 187541 : sourceFile(sourceFile), sourceInterval(token->getStartIndex(), token->getStopIndex()), line(token->getLine()),
19 187541 col(token->getCharPositionInLine() + 1){};
20 4787678 CodeLoc(const antlr4::Token *token, size_t startIdx, size_t stopIdx, SourceFile *sourceFile = nullptr)
21 4787678 : sourceFile(sourceFile), sourceInterval(startIdx, stopIdx), line(token->getLine()),
22 4787678 col(token->getCharPositionInLine() + 1){};
23 389567 CodeLoc(uint32_t line, uint32_t col, SourceFile *sourceFile = nullptr) : sourceFile(sourceFile), line(line), col(col) {}
24
25 // Public members
26 SourceFile *sourceFile = nullptr;
27 antlr4::misc::Interval sourceInterval;
28 uint32_t line;
29 uint32_t col;
30
31 // Public methods
32 [[nodiscard]] std::string toString() const;
33 [[nodiscard]] std::string toPrettyFilePath() const;
34 [[nodiscard]] std::string toPrettyString() const;
35 [[nodiscard]] std::string toPrettyLine() const;
36 [[nodiscard]] std::string toPrettyLineAndColumn() const;
37
38 // Operators
39 friend bool operator==(const CodeLoc &a, const CodeLoc &b);
40 ALWAYS_INLINE friend bool operator<(const CodeLoc &a, const CodeLoc &b) {
41
4/4
✓ Branch 2 → 3 taken 28292 times.
✓ Branch 2 → 4 taken 320 times.
✓ Branch 4 → 5 taken 28 times.
✓ Branch 4 → 6 taken 29386 times.
58026 return a.line == b.line ? a.col < b.col : a.line < b.line;
42 }
43 ALWAYS_INLINE friend bool operator>(const CodeLoc &a, const CodeLoc &b) {
44
3/4
✓ Branch 4 → 5 taken 4070 times.
✓ Branch 4 → 6 taken 86025 times.
✗ Branch 146 → 147 not taken.
✓ Branch 146 → 148 taken 86776 times.
176871 return a.line == b.line ? a.col > b.col : a.line > b.line;
45 }
46 };
47
48 // Make sure we have no unexpected increases in memory consumption
49 static_assert(sizeof(CodeLoc) == 32);
50
51 } // namespace spice::compiler
52