GCC Code Coverage Report


Directory: ../
File: src/util/CodeLoc.h
Date: 2025-02-05 01:09:36
Exec Total Coverage
Lines: 9 9 100.0%
Functions: 3 3 100.0%
Branches: 5 6 83.3%

Line Branch Exec Source
1 // Copyright (c) 2021-2025 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 public:
17 // Constructors
18 16793 explicit CodeLoc(const antlr4::Token *token, SourceFile *sourceFile = nullptr)
19 16793 : sourceFile(sourceFile), sourceInterval(token->getStartIndex(), token->getStopIndex()), line(token->getLine()),
20 16793 col(token->getCharPositionInLine() + 1){};
21 1268394 CodeLoc(const antlr4::Token *token, size_t startIdx, size_t stopIdx, SourceFile *sourceFile = nullptr)
22 1268394 : sourceFile(sourceFile), sourceInterval(startIdx, stopIdx), line(token->getLine()),
23 1268394 col(token->getCharPositionInLine() + 1){};
24 117804 CodeLoc(uint32_t line, uint32_t col, SourceFile *sourceFile = nullptr) : sourceFile(sourceFile), line(line), col(col) {}
25
26 // Public members
27 SourceFile *sourceFile = nullptr;
28 antlr4::misc::Interval sourceInterval;
29 uint32_t line;
30 uint32_t col;
31
32 // Public methods
33 [[nodiscard]] std::string toString() 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
3/4
✓ Branch 0 (4→5) taken 1204 times.
✓ Branch 1 (4→6) taken 283 times.
✗ Branch 2 (4→5) not taken.
✓ Branch 3 (4→6) taken 1492 times.
2979 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
2/2
✓ Branch 0 (115→116) taken 210 times.
✓ Branch 1 (115→117) taken 12149 times.
12359 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