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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
.. Copyright (C) 2024-2025 Free Software Foundation, Inc.
Originally contributed by David Malcolm <dmalcolm@redhat.com>
This is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see
<https://www.gnu.org/licenses/>.
.. default-domain:: c
Physical locations
==================
A "physical" source location is a location expressed in terms of
a specific file, and line(s) and column(s) (as opposed to a
:doc:`"logical" location <logical-locations>`,
which refers to semantic constructs in a programming language).
Creating location information
*****************************
The :type:`diagnostic_manager` manages various objects relating to
locations.
.. type:: diagnostic_file
A :type:`diagnostic_file` is an opaque type describing a particular input file.
.. function:: diagnostic_file * diagnostic_manager_new_file (diagnostic_manager *diag_mgr, \
const char *name, \
const char *sarif_source_language)
Create a new :type:`diagnostic_file` for file ``name``. Repeated calls
with strings that match ``name`` will return the same object.
Both ``diag_mgr`` and ``name`` must be non-NULL.
If ``sarif_source_language`` is non-NULL, it specifies a
``sourceLanguage`` value for the file for use when writing
:doc:`SARIF <sarif>`
(`SARIF v2.1.0 ยง3.24.10 <https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/sarif-v2.1.0-errata01-os-complete.html#_Toc141790871>`_).
See
`SARIF v2.1.0 Appendix J <https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/sarif-v2.1.0-errata01-os-complete.html#_Toc141791197>`_
for suggested values for various programmming languages.
For example, this creates a :type:`diagnostic_file` for ``foo.c``
and identifies it as C source code::
foo_c = diagnostic_manager_new_file (diag_mgr,
"foo.c",
"c" /* source_language */);
.. function:: void diagnostic_manager_debug_dump_file (diagnostic_manager *diag_mgr, \
const diagnostic_file *file, \
FILE *out)
Write a representation of ``file`` to ``out``, for debugging.
Both ``diag_mgr`` and ``out`` must be non-NULL.
`file`` may be NULL.
For example::
diagnostic_manager_debug_dump_file (diag_mgr, foo_c, stderr);
might lead to this output::
file(name="foo.c", sarif_source_language="c")
.. type:: diagnostic_line_num_t
A :type:`diagnostic_line_num_t` is used for representing line numbers
within text files. libgdiagnostics treats the first line of a text file
as line 1.
.. type:: diagnostic_column_num_t
A :type:`diagnostic_column_num_t` is used for representing column numbers
within text files. libgdiagnostics treats the first column of a text line
as column 1, **not** column 0.
.. note::
Both libgdiagnostics and Emacs number source *lines* starting at 1, but
they have differing conventions for *columns*.
libgdiagnostics uses a 1-based convention for source columns,
whereas Emacs's ``M-x column-number-mode`` uses a 0-based convention.
For example, an error in the initial, left-hand
column of source line 3 is reported by libgdiagnostics as::
some-file.c:3:1: error: ...etc...
On navigating to the location of that error in Emacs
(e.g. via ``next-error``),
the locus is reported in the Mode Line
(assuming ``M-x column-number-mode``) as::
some-file.c 10% (3, 0)
i.e. ``3:1:`` in libgdiagnostics corresponds to ``(3, 0)`` in Emacs.
.. type:: diagnostic_physical_location
A :type:`diagnostic_physical_location` is an opaque type representing a
key into a database of source locations within a :type:`diagnostic_manager`.
:type:`diagnostic_physical_location` instances are created by various API
calls into the :type:`diagnostic_manager` expressing source code points
and ranges.
They persist until the :type:`diagnostic_manager` is released, which
cleans them up.
A ``NULL`` value means "unknown", and can be returned by the
:type:`diagnostic_manager` as a fallback when a problem occurs
(e.g. too many locations).
A :type:`diagnostic_physical_location` can be a single point within the
source code, such as here (at the the '"' at the start of the string literal)::
int i = "foo";
^
or be a range with a start and finish, and a "caret" location::
a = (foo && bar)
~~~~~^~~~~~~
where the caret here is at the first "&", and the start and finish
are at the parentheses.
.. function:: const diagnostic_physical_location *diagnostic_manager_new_location_from_file_and_line (diagnostic_manager *diag_mgr, \
const diagnostic_file *file, \
diagnostic_line_num_t line_num)
Attempt to create a :type:`diagnostic_physical_location` representing
``FILENAME:LINE_NUM``, with no column information (thus representing
the whole of the given line.
Both ``diag_mgr`` and ``file`` must be non-NULL.
.. function:: const diagnostic_physical_location * diagnostic_manager_new_location_from_file_line_column (diagnostic_manager *diag_mgr, \
const diagnostic_file *file, \
diagnostic_line_num_t line_num, \
diagnostic_column_num_t column_num)
Attempt to create a :type:`diagnostic_physical_location` for
``FILENAME:LINE_NUM:COLUMN_NUM`` representing a particular point
in the source file.
Both ``diag_mgr`` and ``file`` must be non-NULL.
.. function:: const diagnostic_physical_location *diagnostic_manager_new_location_from_range (diagnostic_manager *diag_mgr,\
const diagnostic_physical_location *loc_caret,\
const diagnostic_physical_location *loc_start,\
const diagnostic_physical_location *loc_end)
Attempt to create a diagnostic_physical_location representing a
range within a source file, with a highlighted "caret" location.
All must be within the same file, but they can be on different lines.
For example, consider the location of the binary expression below::
...|__________1111111112222222
...|12345678901234567890123456
...|
521|int sum (int foo, int bar)
522|{
523| return foo + bar;
...| ~~~~^~~~~
524|}
The location's caret is at the "+", line 523 column 15, but starts
earlier, at the "f" of "foo" at column 11. The finish is at the "r"
of "bar" at column 19.
``diag_mgr`` must be non-NULL.
.. function:: void diagnostic_manager_debug_dump_location (const diagnostic_manager *diag_mgr,\
const diagnostic_physical_location *loc, \
FILE *out)
Write a representation of ``loc`` to ``out``, for debugging.
Both ``diag_mgr`` and ``out`` must be non-NULL.
`loc`` may be NULL.
TODO: example of output
.. function:: diagnostic_file *diagnostic_physical_location_get_file (const diagnostic_physical_location *physical_loc)
Get the :type:`diagnostic_file` associated with a given physical location.
Associating diagnostics with locations
**************************************
A :type:`diagnostic` has an optional primary physical location
and zero or more secondary physical locations. For example::
a = (foo && bar)
~~~~~^~~~~~~
This diagnostic has a single :type:`diagnostic_physical_location`,
with the caret at the first "&", and the start/finish at the parentheses.
Contrast with::
a = (foo && bar)
~~~ ^~ ~~~
This diagnostic has three locations
* The primary location (at "&&") has its caret and start location at
the first "&" and end at the second "&.
* The secondary location for "foo" has its start and finish at the "f"
and "o" of "foo"; the caret is not displayed, but is perhaps at
the "f" of "foo".
* Similarly, the other secondary location (for "bar") has its start and
finish at the "b" and "r" of "bar"; the caret is not displayed, but
is perhaps at the"b" of "bar".
.. function:: void diagnostic_set_location (diagnostic *diag, \
const diagnostic_physical_location * loc)
Set the primary location of ``diag``.
``diag`` must be non-NULL; ``loc`` can be NULL.
.. function:: void diagnostic_set_location_with_label (diagnostic *diag, \
const diagnostic_physical_location *loc, \
const char *fmt, ...)
Set the primary location of ``diag``, with a label. The label is
formatted as per the rules FIXME
``diag`` and ``fmt`` must be non-NULL; ``loc`` can be NULL.
See :doc:`message-formatting` for details of how to use ``fmt``.
TODO: example of use
.. function:: void diagnostic_add_location (diagnostic *diag, \
const diagnostic_physical_location * loc)
Add a secondary location to ``diag``.
``diag`` must be non-NULL; ``loc`` can be NULL.
.. function:: void diagnostic_add_location_with_label (diagnostic *diag, \
const diagnostic_physical_location *loc, \
const char *text)
Add a secondary location to ``diag``, with a label. The label is
formatted as per the rules FIXME
``diag`` and ``fmt`` must be non-NULL; ``loc`` can be NULL.
For example,
.. literalinclude:: ../../../testsuite/libgdiagnostics.dg/test-labelled-ranges.c
:language: c
:start-after: /* begin quoted source */
:end-before: /* end quoted source */
might give this text output::
test-labelled-ranges.c:9:6: error: mismatching types: 'int' and 'const char *'
19 | 42 + "foo"
| ~~ ^ ~~~~~
| | |
| int const char *
.. function:: void diagnostic_add_location_with_label_via_msg_buf (diagnostic *diag, \
const diagnostic_physical_location *loc, \
diagnostic_message_buffer *msg_buf)
This is equivalent to :func:`diagnostic_add_location_with_label` but
using a message buffer rather than a text string.
``diag`` and ``msg_buf`` must both be non-NULL.
Calling this function transfers ownership of ``msg_buf`` to the
diagnostic - do not call :func:`diagnostic_message_buffer_release` on
it.
This function was added in :ref:`LIBGDIAGNOSTICS_ABI_3`; you can
test for its presence using
.. code-block:: c
#ifdef LIBDIAGNOSTICS_HAVE_diagnostic_message_buffer
.. function:: void diagnostic_manager_set_debug_physical_locations (diagnostic_manager *mgr, \
int value)
Calling ``diagnostic_manager_set_debug_physical_locations (mgr, 1);``
will lead to debugging information being printed to ``stderr`` when
creating :type:`diagnostic_physical_location` instances.
The precise format of these messages is subject to change.
This function was added in :ref:`LIBGDIAGNOSTICS_ABI_5`; you can
test for its presence using
.. code-block:: c
#ifdef LIBDIAGNOSTICS_HAVE_diagnostic_manager_set_debug_physical_locations
|