GCC Middle and Back End API Reference
substring-locations.h
Go to the documentation of this file.
1/* Source locations within string literals.
2 Copyright (C) 2016-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_SUBSTRING_LOCATIONS_H
21#define GCC_SUBSTRING_LOCATIONS_H
22
23/* The substring_loc class encapsulates information on the source location
24 of a range of characters within a STRING_CST.
25
26 If needed by a diagnostic, the actual location_t of the substring_loc
27 can be calculated by calling its get_location method. This calls a
28 langhook, since this is inherently frontend-specific. For the C family
29 of frontends, it calls back into libcpp to reparse the strings. This
30 gets the location information "on demand", rather than storing the
31 location information in the initial lex for every string. Thus the
32 substring_loc can also be thought of as a deferred call into libcpp,
33 to allow the non-trivial work of reparsing the string to be delayed
34 until we actually need it (to emit a diagnostic for a particular range
35 of characters).
36
37 substring_loc::get_location returns NULL if it succeeds, or an
38 error message if it fails. Error messages are intended for GCC
39 developers (to help debugging) rather than for end-users.
40
41 The easiest way to use a substring_loc is via the format_warning_* APIs,
42 which gracefully handle failure of substring_loc::get_location by using
43 the location of the string as a whole if substring-information is
44 unavailable. */
45
47{
48 public:
49 /* Constructor. FMT_STRING_LOC is the location of the string as
50 a whole. STRING_TYPE is the type of the string. It should be an
51 ARRAY_TYPE of INTEGER_TYPE, or a POINTER_TYPE to such an ARRAY_TYPE.
52 CARET_IDX, START_IDX, and END_IDX are offsets from the start
53 of the string data. */
54 substring_loc (location_t fmt_string_loc, tree string_type,
55 int caret_idx, int start_idx, int end_idx)
56 : m_fmt_string_loc (fmt_string_loc), m_string_type (string_type),
57 m_caret_idx (caret_idx), m_start_idx (start_idx), m_end_idx (end_idx) {}
58
59 void set_caret_index (int caret_idx) { m_caret_idx = caret_idx; }
60
61 const char *get_location (location_t *out_loc) const;
62
63 location_t get_fmt_string_loc () const { return m_fmt_string_loc; }
64 tree get_string_type () const { return m_string_type; }
65 int get_caret_idx () const { return m_caret_idx; }
66 int get_start_idx () const { return m_start_idx; }
67 int get_end_idx () const { return m_end_idx; }
68
69 private:
70 location_t m_fmt_string_loc;
75};
76
77/* A bundle of state for emitting a diagnostic relating to a format string. */
78
80{
81 public:
82 static const char * const highlight_color_format_string;
83 static const char * const highlight_color_param;
84
86 const range_label *fmt_label,
87 location_t param_loc,
88 const range_label *param_label,
89 const char *corrected_substring);
90
91 /* Functions for emitting a warning about a format string. */
92
94 const char *gmsgid,
95 va_list *ap) const
96 ATTRIBUTE_GCC_DIAG (3, 0);
97
99 unsigned HOST_WIDE_INT n,
100 const char *singular_gmsgid,
101 const char *plural_gmsgid,
102 va_list *ap) const
104
105 bool emit_warning (diagnostic_option_id option_id,
106 const char *gmsgid, ...) const
107 ATTRIBUTE_GCC_DIAG (3, 4);
108
109 bool emit_warning_n (diagnostic_option_id option_id,
110 unsigned HOST_WIDE_INT n,
111 const char *singular_gmsgid,
112 const char *plural_gmsgid, ...) const
114
115 private:
117 const range_label *m_fmt_label;
118 location_t m_param_loc;
119 const range_label *m_param_label;
121};
122
123
124/* Implementation detail, for use when implementing
125 LANG_HOOKS_GET_SUBSTRING_LOCATION. */
126
127extern const char *get_location_within_string (cpp_reader *pfile,
128 file_cache &fc,
129 string_concat_db *concats,
130 location_t strloc,
131 enum cpp_ttype type,
132 int caret_idx,
133 int start_idx, int end_idx,
134 location_t *out_loc);
135
136#endif /* ! GCC_SUBSTRING_LOCATIONS_H */
Definition input.h:136
Definition substring-locations.h:80
bool emit_warning(diagnostic_option_id option_id, const char *gmsgid,...) const ATTRIBUTE_GCC_DIAG(3
Definition substring-locations.cc:261
const char * m_corrected_substring
Definition substring-locations.h:120
static const char *const highlight_color_param
Definition substring-locations.h:83
location_t m_param_loc
Definition substring-locations.h:118
bool bool emit_warning_n(diagnostic_option_id option_id, unsigned HOST_WIDE_INT n, const char *singular_gmsgid, const char *plural_gmsgid,...) const ATTRIBUTE_GCC_DIAG(4
Definition substring-locations.cc:276
bool bool emit_warning_n_va(diagnostic_option_id option_id, unsigned HOST_WIDE_INT n, const char *singular_gmsgid, const char *plural_gmsgid, va_list *ap) const ATTRIBUTE_GCC_DIAG(4
Definition substring-locations.cc:154
format_string_diagnostic_t(const substring_loc &fmt_loc, const range_label *fmt_label, location_t param_loc, const range_label *param_label, const char *corrected_substring)
Definition substring-locations.cc:133
static const char *const highlight_color_format_string
Definition substring-locations.h:82
bool bool ATTRIBUTE_GCC_DIAG(5, 0)
const range_label * m_fmt_label
Definition substring-locations.h:117
const substring_loc & m_fmt_loc
Definition substring-locations.h:116
bool emit_warning_va(diagnostic_option_id option_id, const char *gmsgid, va_list *ap) const ATTRIBUTE_GCC_DIAG(3
Definition substring-locations.cc:251
const range_label * m_param_label
Definition substring-locations.h:119
Definition input.h:268
Definition substring-locations.h:47
tree m_string_type
Definition substring-locations.h:71
void set_caret_index(int caret_idx)
Definition substring-locations.h:59
int m_start_idx
Definition substring-locations.h:73
int get_caret_idx() const
Definition substring-locations.h:65
location_t get_fmt_string_loc() const
Definition substring-locations.h:63
int get_start_idx() const
Definition substring-locations.h:66
substring_loc(location_t fmt_string_loc, tree string_type, int caret_idx, int start_idx, int end_idx)
Definition substring-locations.h:54
int m_end_idx
Definition substring-locations.h:74
int get_end_idx() const
Definition substring-locations.h:67
int m_caret_idx
Definition substring-locations.h:72
tree get_string_type() const
Definition substring-locations.h:64
location_t m_fmt_string_loc
Definition substring-locations.h:70
const char * get_location(location_t *out_loc) const
Definition substring-locations.cc:297
union tree_node * tree
Definition coretypes.h:97
#define ATTRIBUTE_GCC_DIAG(m, n)
Definition diagnostic-core.h:88
struct type string_type
Definition gengtype.cc:489
static void const char va_list ap
Definition read-md.cc:205
Definition diagnostic-core.h:60
Definition gengtype.h:252
const char * get_location_within_string(cpp_reader *pfile, file_cache &fc, string_concat_db *concats, location_t strloc, enum cpp_ttype type, int caret_idx, int start_idx, int end_idx, location_t *out_loc)
Definition input.cc:1827