aboutsummaryrefslogtreecommitdiff
path: root/gcc/doc/libgdiagnostics/topics/message-buffers.rst
blob: c6f5851e16e94eb2d96f30b80edc6c75552e98d8 (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
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
.. Copyright (C) 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

Message buffers
===============

.. type:: diagnostic_message_buffer

A :type:`diagnostic_message_buffer` is a buffer into which text can be
accumulated, before being used:

* as the message of a diagnostic, using :func:`diagnostic_finish_via_msg_buf`

* as the text of a label for a :type:`diagnostic_physical_location` using
  :func:`diagnostic_add_location_with_label_via_msg_buf`

* as the text of an event within a :type:`diagnostic_execution_path` using
  :func:`diagnostic_execution_path_add_event_via_msg_buf`

This is to allow more flexible creation of messages than a "format string
plus variadic arguments" API.

.. function:: diagnostic_message_buffer * diagnostic_message_buffer_new (void)

   This function creates a new :type:`diagnostic_message_buffer`.

   The caller is responsible for cleaning it up, either by handing it off
   to one of the API entrypoints that takes ownership of it (such as
   :func:`diagnostic_finish_via_msg_buf`), or by calling
   :func:`diagnostic_message_buffer_release` on it.

   This function was added in :ref:`LIBGDIAGNOSTICS_ABI_4`; you can
   test for its presence using

   .. code-block:: c

      #ifdef LIBDIAGNOSTICS_HAVE_diagnostic_message_buffer

.. function:: void diagnostic_message_buffer_release (diagnostic_message_buffer *msg_buf)

   This function releases ``msg_buf``.

   Typically you don't need to call this, but instead will pass the
   buffer to one of the API entrypoints that takes over ownership of
   it (such as :func:`diagnostic_finish_via_msg_buf`); calling it
   after this would lead to a double-free bug, as you no longer "own"
   the buffer.

   ``msg_buf`` must be non-NULL.

   This function was added in :ref:`LIBGDIAGNOSTICS_ABI_4`; you can
   test for its presence using

   .. code-block:: c

      #ifdef LIBDIAGNOSTICS_HAVE_diagnostic_message_buffer

.. function:: void diagnostic_message_buffer_append_str (diagnostic_message_buffer *msg_buf, \
				      const char *p)

   This function appends the null-terminated string ``p`` to the buffer.
   The string is assumed to be UTF-8 encoded.

   ``msg_buf`` and ``p`` must both be non-NULL.

   This function was added in :ref:`LIBGDIAGNOSTICS_ABI_4`; you can
   test for its presence using

   .. code-block:: c

      #ifdef LIBDIAGNOSTICS_HAVE_diagnostic_message_buffer

.. function:: void diagnostic_message_buffer_append_text (diagnostic_message_buffer *msg_buf, \
	       const char *p, \
	       size_t len)

   This function appends ``len`` bytes from ``p`` to the buffer.
   The bytes are assumed to be UTF-8 encoded.

   ``msg_buf`` and ``p`` must both be non-NULL.

   This function was added in :ref:`LIBGDIAGNOSTICS_ABI_4`; you can
   test for its presence using

   .. code-block:: c

      #ifdef LIBDIAGNOSTICS_HAVE_diagnostic_message_buffer

.. function:: void diagnostic_message_buffer_append_byte (diagnostic_message_buffer *msg_buf,\
				       char ch)

   This function appends ``ch`` to the buffer.  This should be either
   ASCII, or part of UTF-8 encoded text.

   ``msg_buf`` must be non-NULL.

   This function was added in :ref:`LIBGDIAGNOSTICS_ABI_4`; you can
   test for its presence using

   .. code-block:: c

      #ifdef LIBDIAGNOSTICS_HAVE_diagnostic_message_buffer

.. function:: void diagnostic_message_buffer_append_printf (diagnostic_message_buffer *msg_buf, \
					 const char *fmt, ...)

   This function appends a formatted string to the buffer, using the
   formatting rules for ``printf``.

   The string is assumed to be UTF-8 encoded.

   ``msg_buf`` and ``fmt`` must both be non-NULL.

   This function was added in :ref:`LIBGDIAGNOSTICS_ABI_4`; you can
   test for its presence using

   .. code-block:: c

      #ifdef LIBDIAGNOSTICS_HAVE_diagnostic_message_buffer

.. function:: void diagnostic_message_buffer_append_event_id (diagnostic_message_buffer *msg_buf, \
					   diagnostic_event_id event_id)

   This function appends a :type:`diagnostic_event_id` to the buffer.

   ``msg_buf`` must be non-NULL.

   For text output, the event will be printed in the form ``(1)``.

   This is analogous to the
   :doc:`%@ message formatting code <message-formatting>`.

   This function was added in :ref:`LIBGDIAGNOSTICS_ABI_4`; you can
   test for its presence using

   .. code-block:: c

      #ifdef LIBDIAGNOSTICS_HAVE_diagnostic_message_buffer

Hyperlink support
*****************

.. function:: void diagnostic_message_buffer_begin_url (diagnostic_message_buffer *msg_buf, \
				     const char *url)

   This function indicates the beginning of a run of text that should be
   associated with the given URL.  The run of text should be closed with
   a matching call to :func:`diagnostic_message_buffer_end_url`.

   ``msg_buf`` and ``url`` must both be non-NULL.

   For text output in a suitably modern terminal, the run of text will
   be emitted as a clickable hyperlink to the URL.

   For SARIF sinks, the run of text will be emitted using SARIF's
   embedded link syntax.

   This is analogous to the
   :doc:`%{ message formatting code <message-formatting>`.

   This function was added in :ref:`LIBGDIAGNOSTICS_ABI_4`; you can
   test for its presence using

   .. code-block:: c

      #ifdef LIBDIAGNOSTICS_HAVE_diagnostic_message_buffer

.. function:: void diagnostic_message_buffer_end_url (diagnostic_message_buffer *msg_buf)

   This function ends a run of text within the buffer started with
   :func:`diagnostic_message_buffer_begin_url`.

   ``msg_buf`` must be non-NULL.

   This is analogous to the
   :doc:`%} message formatting code <message-formatting>`.

   This function was added in :ref:`LIBGDIAGNOSTICS_ABI_4`; you can
   test for its presence using

   .. code-block:: c

      #ifdef LIBDIAGNOSTICS_HAVE_diagnostic_message_buffer

Quoted text
***********

.. function:: void diagnostic_message_buffer_begin_quote (diagnostic_message_buffer *msg_buf)

   This function indicates the beginning of a run of text that should be
   printed in quotes.  The run of text should be closed with
   a matching call to :func:`diagnostic_message_buffer_end_quote`.

   ``msg_buf`` must be non-NULL.

   For text output in a suitably modern terminal, the run of text will
   appear in bold.
   be emitted as a clickable hyperlink to the URL.

   For SARIF sinks, the run of text will be emitted using SARIF's
   embedded link syntax.

   This is analogous to the
   ``%<``:doc:`message formatting code <message-formatting>`.

   This function was added in :ref:`LIBGDIAGNOSTICS_ABI_4`; you can
   test for its presence using

   .. code-block:: c

      #ifdef LIBDIAGNOSTICS_HAVE_diagnostic_message_buffer

.. function:: void diagnostic_message_buffer_end_url (diagnostic_message_buffer *msg_buf)

   This function ends a run of text within the buffer started with
   :func:`diagnostic_message_buffer_begin_url`.

   ``msg_buf`` must be non-NULL.

   This is analogous to the
   :doc:`%> message formatting code <message-formatting>`.

   This function was added in :ref:`LIBGDIAGNOSTICS_ABI_4`; you can
   test for its presence using

   .. code-block:: c

      #ifdef LIBDIAGNOSTICS_HAVE_diagnostic_message_buffer

Color
*****

.. function:: void diagnostic_message_buffer_begin_color (diagnostic_message_buffer *msg_buf, \
				     const char *color)

   This function indicates the beginning of a run of text that should be
   colorized as the given color.  The run of text should be closed with
   a matching call to :func:`diagnostic_message_buffer_end_color`.

   The precise set of available color names is currently undocumented.

   ``msg_buf`` and ``color`` must both be non-NULL.

   For text output in a suitable terminal, the run of text will
   be colorized.

   For SARIF sinks, the run of text will be emitted using SARIF's
   embedded link syntax.

   This is analogous to the
   :doc:`%r message formatting code <message-formatting>`.

   This function was added in :ref:`LIBGDIAGNOSTICS_ABI_4`; you can
   test for its presence using

   .. code-block:: c

      #ifdef LIBDIAGNOSTICS_HAVE_diagnostic_message_buffer

.. function:: void diagnostic_message_buffer_end_color (diagnostic_message_buffer *msg_buf)

   This function ends a run of text within the buffer started with
   :func:`diagnostic_message_buffer_begin_color`.

   ``msg_buf`` must be non-NULL.

   This is analogous to the
   :doc:`%R message formatting code <message-formatting>`.

   This function was added in :ref:`LIBGDIAGNOSTICS_ABI_4`; you can
   test for its presence using

   .. code-block:: c

      #ifdef LIBDIAGNOSTICS_HAVE_diagnostic_message_buffer

Debugging a message buffer
**************************

.. function:: void diagnostic_message_buffer_dump (const diagnostic_message_buffer *msg_buf, \
				FILE *outf)

   This function writes a representation of the contents of ``msg_buf``
   to ``outf``, for debugging.

   ``msg_buf`` can be NULL or non-NULL.
   ``outf`` must be non-NULL.

   This function was added in :ref:`LIBGDIAGNOSTICS_ABI_4`; you can
   test for its presence using

   .. code-block:: c

      #ifdef LIBDIAGNOSTICS_HAVE_diagnostic_message_buffer