Debian Codegraph @

gcc/doc/libgdiagnostics/tutorial/06-fix-it-hints.rst

raw
1 .. Copyright (C) 2024-2025 Free Software Foundation, Inc.
2 Originally contributed by David Malcolm <[email protected]>
3
4 This is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see
16 <https://www.gnu.org/licenses/>.
17
18 .. default-domain:: c
19
20 Tutorial part 6: fix-it hints
21 =============================
22
23 libgdiagnostics supports adding "fix-it hints" to a :type:`diagnostic`:
24 suggestions for the user on how to edit their code to fix a problem. These
25 can be expressed as insertions, replacements, and removals of text.
26
27 For example, here we use :func:`diagnostic_add_fix_it_hint_replace` to add
28 a replacement fix-it hint to a diagnostic:
29
30 .. literalinclude:: ../../../testsuite/libgdiagnostics.dg/test-fix-it-hint.c
31 :language: c
32 :start-after: /* begin quoted source */
33 :end-before: /* end quoted source */
34
35 On compiling and running the program, we should get output similar to::
36
37 test-fix-it-hint.c:19:13: error: unknown field 'colour'; did you mean 'color'
38 19 | return p->colour;
39 | ^~~~~~
40 | color
41
42 We can also add a call to :func:`diagnostic_manager_write_patch` to the
43 program cleanup code::
44
45 diagnostic_manager_write_patch (diag_mgr, stderr);
46
47 This will write a patch to the stream (here ``stderr``) giving the effect
48 of all fix-it hints on all diagnostics emitted by the
49 :type:`diagnostic_manager`, giving something like::
50
51 @@ -16,7 +16,7 @@
52 struct rgb
53 get_color (struct object *p)
54 {
55 - return p->colour;
56 + return p->color;
57 }
58
59
60 See the :doc:`guide to fix-it hints <../topics/fix-it-hints>`
61 for more information, or go on to
62 :doc:`the next section of the tutorial <07-execution-paths>`.