blob: 4c214864b3707e6ec0c3be9434fc7406f7bb7934 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
// SPDX-License-Identifier: GPL-3.0-or-later
#ifndef RCC_SUPPORT_TEXTDIAGNOSTIC_H
#define RCC_SUPPORT_TEXTDIAGNOSTIC_H
#include <cstddef>
#include <span>
#include <string>
#include <string_view>
#include <vector>
namespace rcc {
struct TextPosition final {
std::size_t Offset = 0;
std::size_t Line = 1;
std::size_t Column = 1;
friend constexpr bool operator==(TextPosition Left, TextPosition Right) noexcept = default;
};
struct TextRange final {
TextPosition Begin;
TextPosition End;
[[nodiscard]] constexpr bool isOrdered() const noexcept {
return Begin.Offset <= End.Offset;
}
friend constexpr bool operator==(TextRange Left, TextRange Right) noexcept = default;
};
enum class DiagnosticSeverity : unsigned char {
Note,
Warning,
Error,
};
[[nodiscard]] std::string_view getDiagnosticSeverityName(DiagnosticSeverity Severity) noexcept;
struct TextDiagnostic final {
DiagnosticSeverity Severity = DiagnosticSeverity::Error;
std::string Category;
std::string Message;
std::string SourceName;
TextRange Range;
};
class TextSource final {
public:
TextSource(std::string Name, std::string Text);
[[nodiscard]] std::string_view name() const noexcept {
return Name;
}
[[nodiscard]] std::string_view text() const noexcept {
return Text;
}
[[nodiscard]] std::size_t lineCount() const noexcept {
return LineOffsets.size();
}
[[nodiscard]] TextPosition positionAt(std::size_t Offset) const noexcept;
[[nodiscard]] TextRange range(std::size_t Begin, std::size_t End) const noexcept;
[[nodiscard]] std::string_view line(std::size_t OneBasedLine) const noexcept;
[[nodiscard]] std::span<const std::size_t> lineOffsets() const noexcept {
return LineOffsets;
}
private:
std::string Name;
std::string Text;
std::vector<std::size_t> LineOffsets;
};
[[nodiscard]] std::string formatTextDiagnostic(const TextDiagnostic &Diagnostic,
const TextSource &Source);
} // namespace rcc
#endif // RCC_SUPPORT_TEXTDIAGNOSTIC_H
|