A11 (C++ runtime)
Native C++ implementation of the A11 streaming action runtime
Loading...
Searching...
No Matches
diagnostic.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 The A11 Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef A11_FLOW_DIAGNOSTIC_H_
18#define A11_FLOW_DIAGNOSTIC_H_
19
20#include <cstddef>
21#include <string>
22#include <string_view>
23#include <vector>
24
25#include <absl/types/span.h>
26
27namespace a11::flow {
28
30enum class Severity {
32 kError,
39};
40
42enum class Family {
44 kSyntax,
46 kForm,
48 kName,
54 kUnused,
55};
56
61struct Position {
62 size_t offset = 0;
63 int line = 1;
64 int column = 1;
65};
66
72
74struct Edit {
75 size_t start = 0;
76 size_t end = 0;
77 std::string text;
78};
79
83struct Fix {
84 std::string label;
85 std::vector<Edit> edits;
86 bool automatic = true;
87};
88
91 std::string source;
93 std::string message;
94};
95
99struct Diagnostic {
100 std::string source;
104 std::string code;
109 std::string message;
111 std::string flow;
112 std::vector<Fix> fixes;
113 std::vector<RelatedDiagnostic> related;
114};
115
117std::string_view SeverityName(Severity severity);
118
120std::string_view FamilyName(Family family);
121
123Severity SeverityFromName(std::string_view name);
124
126Family FamilyFromName(std::string_view name);
127
132struct CodeInfo {
133 std::string_view code;
137 std::string_view summary;
138};
139
144const Diagnostic* absl_nullable FirstError(
145 absl::Span<const Diagnostic> diagnostics);
146
148absl::Span<const CodeInfo> KnownCodes();
149
151const CodeInfo* absl_nullable FindCode(std::string_view code);
152
159 public:
160 explicit LineIndex(std::string_view source);
161
163 std::string_view source() const { return source_; }
164
166 [[nodiscard]] Position At(size_t offset) const;
167
169 [[nodiscard]] Range Between(size_t start, size_t end) const;
170
172 [[nodiscard]] size_t LineCount() const { return line_starts_.size(); }
173
175 [[nodiscard]] size_t LineStart(int line) const;
176
177 private:
178 size_t length_ = 0;
180 std::string_view source_;
182 std::vector<size_t> line_starts_;
183};
184
187void SortDiagnostics(std::vector<Diagnostic>& diagnostics);
188
189} // namespace a11::flow
190
191#endif // A11_FLOW_DIAGNOSTIC_H_
Line and column lookup over one source text.
Definition diagnostic.h:158
Range Between(size_t start, size_t end) const
A range from two byte offsets.
Definition diagnostic.cc:454
size_t LineCount() const
How many lines the source has, counting a trailing partial line.
Definition diagnostic.h:172
std::string_view source() const
The borrowed document used to compute positions.
Definition diagnostic.h:163
Position At(size_t offset) const
The position at a byte offset, clamped to the end of the source.
Definition diagnostic.cc:432
size_t LineStart(int line) const
The byte offset the 1-based line starts at, clamped to the source.
Definition diagnostic.cc:461
size_t end
Definition discover.cc:101
std::string name
The declaration name and its colon.
Definition format.cc:50
size_t start
Definition module.cc:56
Definition catalogue.cc:29
Family
Diagnostic category used by editors and build gates.
Definition diagnostic.h:42
@ kForm
Invalid language form or form in an invalid position.
@ kSyntax
Missing or misplaced Flow syntax.
@ kUnused
Unread status, unused binding, or unused declaration.
@ kSequence
Stage or access sequence with invalid or ineffective behavior.
@ kName
Unresolved name or invalid use of a resolved name.
void SortDiagnostics(std::vector< Diagnostic > &diagnostics)
Sorts diagnostics into the order every frontend presents them in: by position, then by code,...
Definition diagnostic.cc:472
Severity SeverityFromName(std::string_view name)
Severity for a name from the output formats, or kError if unknown.
Definition diagnostic.cc:364
@ kBarrier
A bound wait/close.
Severity
Severity of a Flow diagnostic.
Definition diagnostic.h:30
@ kInformation
Non-blocking information.
@ kWeakWarning
Compilation succeeds, but a source element has no effect.
@ kError
Compilation fails. a11 flow check exits nonzero.
@ kWarning
Compilation succeeds, but behavior may differ from the source's intent.
absl::Span< const CodeInfo > KnownCodes()
Every diagnostic code the language can produce, sorted by code.
Definition diagnostic.cc:406
const CodeInfo *absl_nullable FindCode(std::string_view code)
The entry for a code, or nullptr if nothing publishes it.
Definition diagnostic.cc:410
std::string_view SeverityName(Severity severity)
The spelling of a severity in the output formats.
Definition diagnostic.cc:332
std::string_view FamilyName(Family family)
The spelling of a family in the output formats.
Definition diagnostic.cc:346
Family FamilyFromName(std::string_view name)
Family for a name from the output formats, or kSyntax if unknown.
Definition diagnostic.cc:377
const Diagnostic *absl_nullable FirstError(absl::Span< const Diagnostic > diagnostics)
The first error in diagnostics, or nullptr if none is an error.
Definition diagnostic.cc:396
std::uint32_t offset
Definition runtime_streams.cc:36
Published diagnostic code metadata for documentation and a11 flow codes.
Definition diagnostic.h:132
Family family
Definition diagnostic.h:134
std::string_view summary
One-line summary shown beside the code in catalogue listings.
Definition diagnostic.h:137
Severity severity
Definition diagnostic.h:135
std::string_view code
Definition diagnostic.h:133
Flow problem rendered by the CLI, JSON and SARIF writers, and editors.
Definition diagnostic.h:99
std::vector< RelatedDiagnostic > related
Definition diagnostic.h:113
std::string source
Definition diagnostic.h:100
std::string code
A stable dotted identifier, e.g.
Definition diagnostic.h:104
std::string flow
Enclosing flow name when parsing or resolution identifies one.
Definition diagnostic.h:111
std::vector< Fix > fixes
Definition diagnostic.h:112
Severity severity
Definition diagnostic.h:105
std::string message
One sentence, in the language's own terms, ending in a full stop.
Definition diagnostic.h:109
Family family
Definition diagnostic.h:106
Range range
Definition diagnostic.h:107
One replacement of a span of source. An empty text is a deletion.
Definition diagnostic.h:74
size_t end
Definition diagnostic.h:76
std::string text
Definition diagnostic.h:77
size_t start
Definition diagnostic.h:75
Source edits supplied with a diagnostic.
Definition diagnostic.h:83
std::string label
Definition diagnostic.h:84
std::vector< Edit > edits
Definition diagnostic.h:85
bool automatic
Definition diagnostic.h:86
Source position with a byte offset and 1-based line and column.
Definition diagnostic.h:61
int line
Definition diagnostic.h:63
size_t offset
Definition diagnostic.h:62
int column
Definition diagnostic.h:64
Half-open span of source, [start, end).
Definition diagnostic.h:68
Position start
Definition diagnostic.h:69
Position end
Definition diagnostic.h:70
Another source range explaining a diagnostic.
Definition diagnostic.h:90
std::string message
Definition diagnostic.h:93
std::string source
Definition diagnostic.h:91
Range range
Definition diagnostic.h:92
int line
Definition main.cc:71