GCC Middle and Back End API Reference
gcc-rich-location.h
Go to the documentation of this file.
1/* Declarations relating to class gcc_rich_location
2 Copyright (C) 2014-2024 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#ifndef GCC_RICH_LOCATION_H
21#define GCC_RICH_LOCATION_H
22
23#include "rich-location.h"
24
25/* A gcc_rich_location is libcpp's rich_location with additional
26 helper methods for working with gcc's types. The class is not
27 copyable or assignable because rich_location isn't. */
28
29class gcc_rich_location : public rich_location
30{
31 public:
32 /* Constructors. */
33
34 /* Constructing from a location. */
35 explicit gcc_rich_location (location_t loc)
36 : rich_location (line_table, loc, nullptr, nullptr)
37 {
38 }
39
40 /* Constructing from a location with a label and a highlight color. */
41 explicit gcc_rich_location (location_t loc,
42 const range_label *label,
43 const char *highlight_color)
44 : rich_location (line_table, loc, label, highlight_color)
45 {
46 }
47
48 /* Methods for adding ranges via gcc entities. */
49 void
51 range_label *label,
52 const char *highlight_color);
53
54 void
56 range_label *label,
57 const char *highlight_color);
58
59 void add_fixit_misspelled_id (location_t misspelled_token_loc,
60 tree hint_id);
61
62 /* If LOC is within the spans of lines that will already be printed for
63 this gcc_rich_location, then add it as a secondary location
64 and return true.
65
66 Otherwise return false.
67
68 This allows for a diagnostic to compactly print secondary locations
69 in one diagnostic when these are near enough the primary locations for
70 diagnostics-show-locus.c to cope with them, and to fall back to
71 printing them via a note otherwise e.g.:
72
73 gcc_rich_location richloc (primary_loc);
74 bool added secondary = richloc.add_location_if_nearby (*global_dc,
75 secondary_loc);
76 error_at (&richloc, "main message");
77 if (!added secondary)
78 inform (secondary_loc, "message for secondary");
79
80 Implemented in diagnostic-show-locus.cc. */
81
83 location_t loc,
84 bool restrict_to_current_line_spans = true,
85 const range_label *label = NULL);
86
87 /* Add a fix-it hint suggesting the insertion of CONTENT before
88 INSERTION_POINT.
89
90 Attempt to handle formatting: if INSERTION_POINT is the first thing on
91 its line, and INDENT is sufficiently sane, then add CONTENT on its own
92 line, using the indentation of INDENT.
93 Otherwise, add CONTENT directly before INSERTION_POINT.
94
95 For example, adding "CONTENT;" with the closing brace as the insertion
96 point and using "INDENT;" for indentation:
97
98 if ()
99 {
100 INDENT;
101 }
102
103 would lead to:
104
105 if ()
106 {
107 INDENT;
108 CONTENT;
109 }
110
111 but adding it to:
112
113 if () {INDENT;}
114
115 would lead to:
116
117 if () {INDENT;CONTENT;}
118 */
119 void add_fixit_insert_formatted (const char *content,
120 location_t insertion_point,
121 location_t indent);
122};
123
124/* Concrete subclass of libcpp's range_label.
125 Simple implementation using a string literal. */
126
127class text_range_label : public range_label
128{
129 public:
130 text_range_label (const char *text) : m_text (text) {}
131
132 label_text get_text (unsigned /*range_idx*/) const final override
133 {
134 return label_text::borrow (m_text);
135 }
136
137 private:
138 const char *m_text;
139};
140
141#endif /* GCC_RICH_LOCATION_H */
Definition diagnostic.h:374
Definition genmatch.cc:845
Definition gcc-rich-location.h:30
gcc_rich_location(location_t loc, const range_label *label, const char *highlight_color)
Definition gcc-rich-location.h:41
void add_expr(tree expr, range_label *label, const char *highlight_color)
Definition gcc-rich-location.cc:45
void maybe_add_expr(tree t, range_label *label, const char *highlight_color)
Definition gcc-rich-location.cc:60
void add_fixit_insert_formatted(const char *content, location_t insertion_point, location_t indent)
Definition gcc-rich-location.cc:166
void add_fixit_misspelled_id(location_t misspelled_token_loc, tree hint_id)
Definition gcc-rich-location.cc:71
bool add_location_if_nearby(const diagnostic_context &ctxt, location_t loc, bool restrict_to_current_line_spans=true, const range_label *label=NULL)
Definition diagnostic-show-locus.cc:3191
gcc_rich_location(location_t loc)
Definition gcc-rich-location.h:35
Definition gcc-rich-location.h:128
label_text get_text(unsigned) const final override
Definition gcc-rich-location.h:132
const char * m_text
Definition gcc-rich-location.h:138
text_range_label(const char *text)
Definition gcc-rich-location.h:130
union tree_node * tree
Definition coretypes.h:97
static class line_maps * line_table
Definition genmatch.cc:55
#define NULL
Definition system.h:50