src/util/CodeLoc.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #include "CodeLoc.h" | ||
| 4 | |||
| 5 | #include <filesystem> | ||
| 6 | #include <string> | ||
| 7 | |||
| 8 | #include <SourceFile.h> | ||
| 9 | |||
| 10 | namespace spice::compiler { | ||
| 11 | |||
| 12 | /** | ||
| 13 | * Returns the code location as a string for using it as a map key or similar | ||
| 14 | * | ||
| 15 | * @return Code location string | ||
| 16 | */ | ||
| 17 |
3/6✓ Branch 4 → 5 taken 3186529 times.
✗ Branch 4 → 18 not taken.
✓ Branch 5 → 6 taken 3186529 times.
✗ Branch 5 → 16 not taken.
✓ Branch 6 → 7 taken 3186529 times.
✗ Branch 6 → 14 not taken.
|
6373058 | std::string CodeLoc::toString() const { return "L" + std::to_string(line) + "C" + std::to_string(col); } |
| 18 | |||
| 19 | /** | ||
| 20 | * Returns the code location in a pretty form | ||
| 21 | * | ||
| 22 | * @return Pretty code location | ||
| 23 | */ | ||
| 24 | 7858 | std::string CodeLoc::toPrettyString() const { | |
| 25 |
1/2✓ Branch 2 → 3 taken 7858 times.
✗ Branch 2 → 47 not taken.
|
7858 | const std::string filePath = toPrettyFilePath(); |
| 26 |
3/10✗ Branch 4 → 5 not taken.
✓ Branch 4 → 8 taken 7858 times.
✗ Branch 7 → 9 not taken.
✗ Branch 7 → 26 not taken.
✓ Branch 8 → 9 taken 7858 times.
✗ Branch 8 → 26 not taken.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 12 taken 7858 times.
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 29 not taken.
|
7858 | const std::string prefix = filePath.empty() ? "" : filePath + ":"; |
| 27 |
3/6✓ Branch 14 → 15 taken 7858 times.
✗ Branch 14 → 35 not taken.
✓ Branch 15 → 16 taken 7858 times.
✗ Branch 15 → 33 not taken.
✓ Branch 16 → 17 taken 7858 times.
✗ Branch 16 → 31 not taken.
|
23574 | return prefix + std::to_string(line) + ":" + std::to_string(col); |
| 28 | 7858 | } | |
| 29 | |||
| 30 | /** | ||
| 31 | * Returns the path of the source file this code location points into, relative to the root source file | ||
| 32 | * | ||
| 33 | * @return Pretty file path | ||
| 34 | */ | ||
| 35 | 7859 | std::string CodeLoc::toPrettyFilePath() const { | |
| 36 |
1/2✓ Branch 2 → 3 taken 7859 times.
✗ Branch 2 → 17 not taken.
|
7859 | const std::filesystem::path &rootSourceFilePath = sourceFile->getRootSourceFile()->filePath; |
| 37 |
1/2✓ Branch 3 → 4 taken 7859 times.
✗ Branch 3 → 17 not taken.
|
7859 | std::filesystem::path sourceFilePath = relative(sourceFile->filePath, rootSourceFilePath); |
| 38 |
3/4✓ Branch 4 → 5 taken 7859 times.
✗ Branch 4 → 14 not taken.
✓ Branch 7 → 8 taken 525 times.
✓ Branch 7 → 9 taken 7334 times.
|
7859 | if (sourceFilePath == ".") |
| 39 |
1/2✓ Branch 8 → 9 taken 525 times.
✗ Branch 8 → 15 not taken.
|
525 | sourceFilePath /= sourceFile->fileName; |
| 40 |
1/2✓ Branch 9 → 10 taken 7859 times.
✗ Branch 9 → 15 not taken.
|
15718 | return sourceFilePath.generic_string(); |
| 41 | 7859 | } | |
| 42 | |||
| 43 | /** | ||
| 44 | * Returns the line number in a pretty form | ||
| 45 | * | ||
| 46 | * @return Pretty line number | ||
| 47 | */ | ||
| 48 |
1/2✓ Branch 3 → 4 taken 33127 times.
✗ Branch 3 → 8 not taken.
|
66254 | std::string CodeLoc::toPrettyLine() const { return "L" + std::to_string(line); } |
| 49 | |||
| 50 | /** | ||
| 51 | * Returns the line and column numbers in a pretty form | ||
| 52 | * | ||
| 53 | * @return Pretty line and column numbers | ||
| 54 | */ | ||
| 55 | 123226 | std::string CodeLoc::toPrettyLineAndColumn() const { return toString(); } | |
| 56 | |||
| 57 | 31263 | bool operator==(const CodeLoc &a, const CodeLoc &b) { | |
| 58 |
5/6✓ Branch 3 → 4 taken 31263 times.
✗ Branch 3 → 7 not taken.
✓ Branch 4 → 5 taken 1351 times.
✓ Branch 4 → 7 taken 29912 times.
✓ Branch 5 → 6 taken 295 times.
✓ Branch 5 → 7 taken 1056 times.
|
31263 | return a.sourceFile->filePath == b.sourceFile->filePath && a.line == b.line && a.col == b.col; |
| 59 | } | ||
| 60 | |||
| 61 | } // namespace spice::compiler | ||
| 62 |