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
34enum class Severity {
36 kError,
43};
44
50enum class Family {
52 kSyntax,
54 kForm,
56 kName,
62 kUnused,
63};
64
72struct Position {
73 size_t offset = 0;
74 int line = 1;
75 int column = 1;
76};
77
83
85struct Edit {
86 size_t start = 0;
87 size_t end = 0;
88 std::string text;
89};
90
97struct Fix {
98 std::string label;
99 std::vector<Edit> edits;
100};
101
112 std::string code;
117 std::string message;
119 std::string flow;
120 std::vector<Fix> fixes;
121};
122
124std::string_view SeverityName(Severity severity);
125
127std::string_view FamilyName(Family family);
128
130Severity SeverityFromName(std::string_view name);
131
133Family FamilyFromName(std::string_view name);
134
140struct CodeInfo {
141 std::string_view code;
146 std::string_view summary;
147};
148
153const Diagnostic* absl_nullable FirstError(
154 absl::Span<const Diagnostic> diagnostics);
155
157absl::Span<const CodeInfo> KnownCodes();
158
160const CodeInfo* absl_nullable FindCode(std::string_view code);
161
168 public:
169 explicit LineIndex(std::string_view source);
170
172 [[nodiscard]] Position At(size_t offset) const;
173
175 [[nodiscard]] Range Between(size_t start, size_t end) const;
176
178 [[nodiscard]] size_t LineCount() const { return line_starts_.size(); }
179
181 [[nodiscard]] size_t LineStart(int line) const;
182
183 private:
184 size_t length_ = 0;
186 std::string_view source_;
188 std::vector<size_t> line_starts_;
189};
190
193void SortDiagnostics(std::vector<Diagnostic>& diagnostics);
194
195} // namespace a11::flow
196
197#endif // A11_FLOW_DIAGNOSTIC_H_
Line and column lookup over one source text.
Definition diagnostic.h:167
Range Between(size_t start, size_t end) const
A range from two byte offsets.
Definition diagnostic.cc:398
size_t LineCount() const
How many lines the source has, counting a trailing partial line.
Definition diagnostic.h:178
Position At(size_t offset) const
The position at a byte offset, clamped to the end of the source.
Definition diagnostic.cc:376
size_t LineStart(int line) const
The byte offset the 1-based line starts at, clamped to the source.
Definition diagnostic.cc:405
size_t end
Definition discover.cc:115
std::string name
The name and its colon, which travel together because they always do.
Definition format.cc:49
Definition boundary.cc:32
Family
The kind of problem, which is the grouping a reader thinks in.
Definition diagnostic.h:50
@ kForm
A form the language does not have, or does not have there.
@ kSyntax
The text is not a flow: something is missing or in the wrong place.
@ kUnused
A status, wait or declaration nothing uses.
@ kSequence
A sequence of operations that cannot do what it appears to.
@ kName
A name that cannot be resolved, or is used as the wrong thing.
void SortDiagnostics(std::vector< Diagnostic > &diagnostics)
Sorts diagnostics into the order every frontend presents them in: by position, then by code,...
Definition diagnostic.cc:416
Severity SeverityFromName(std::string_view name)
Severity for a name from the output formats, or kError if unknown.
Definition diagnostic.cc:308
@ kBarrier
A bound wait/drain.
Severity
How much a diagnostic matters.
Definition diagnostic.h:34
@ kInformation
Non-blocking information.
@ kWeakWarning
The flow works, and part of it is doing nothing.
@ kError
The compiler refuses the flow. a11 flow check exits non-zero.
@ kWarning
The flow compiles and does something other than what it says.
absl::Span< const CodeInfo > KnownCodes()
Every diagnostic code the language can produce, sorted by code.
Definition diagnostic.cc:350
const CodeInfo *absl_nullable FindCode(std::string_view code)
The entry for a code, or nullptr if nothing publishes it.
Definition diagnostic.cc:354
std::string_view SeverityName(Severity severity)
The spelling of a severity in the output formats.
Definition diagnostic.cc:276
std::string_view FamilyName(Family family)
The spelling of a family in the output formats.
Definition diagnostic.cc:290
Family FamilyFromName(std::string_view name)
Family for a name from the output formats, or kSyntax if unknown.
Definition diagnostic.cc:321
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:340
What a diagnostic code means, for documentation and for a11 flow codes.
Definition diagnostic.h:140
Family family
Definition diagnostic.h:142
std::string_view summary
One line, in the imperative-free "what is wrong" voice: this is what a --help-style listing shows nex...
Definition diagnostic.h:146
Severity severity
Definition diagnostic.h:143
std::string_view code
Definition diagnostic.h:141
One problem found in a flow.
Definition diagnostic.h:108
std::string code
A stable dotted identifier, e.g.
Definition diagnostic.h:112
std::string flow
The flow the problem is in, where the text got far enough to say.
Definition diagnostic.h:119
std::vector< Fix > fixes
Definition diagnostic.h:120
Severity severity
Definition diagnostic.h:113
std::string message
One sentence, in the language's own terms, ending in a full stop.
Definition diagnostic.h:117
Family family
Definition diagnostic.h:114
Range range
Definition diagnostic.h:115
One replacement of a span of source. An empty text is a deletion.
Definition diagnostic.h:85
size_t end
Definition diagnostic.h:87
std::string text
Definition diagnostic.h:88
size_t start
Definition diagnostic.h:86
An edit, or set of edits, that would fix a diagnostic.
Definition diagnostic.h:97
std::string label
Definition diagnostic.h:98
std::vector< Edit > edits
Definition diagnostic.h:99
One place in the source: the byte offset, and the line and column at it.
Definition diagnostic.h:72
int line
Definition diagnostic.h:74
size_t offset
Definition diagnostic.h:73
int column
Definition diagnostic.h:75
Half-open span of source, [start, end).
Definition diagnostic.h:79
Position start
Definition diagnostic.h:80
Position end
Definition diagnostic.h:81
int line
Definition main.cc:70
long long offset
Definition main.cc:69