annotate gcc/substring-locations.h @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Source locations within string literals.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 2016-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of GCC.
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
7 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
8 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
9 version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
14 for more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
17 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
18 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 #ifndef GCC_SUBSTRING_LOCATIONS_H
kono
parents:
diff changeset
21 #define GCC_SUBSTRING_LOCATIONS_H
kono
parents:
diff changeset
22
kono
parents:
diff changeset
23 /* The substring_loc class encapsulates information on the source location
kono
parents:
diff changeset
24 of a range of characters within a STRING_CST.
kono
parents:
diff changeset
25
kono
parents:
diff changeset
26 If needed by a diagnostic, the actual location_t of the substring_loc
kono
parents:
diff changeset
27 can be calculated by calling its get_location method. This calls a
kono
parents:
diff changeset
28 langhook, since this is inherently frontend-specific. For the C family
kono
parents:
diff changeset
29 of frontends, it calls back into libcpp to reparse the strings. This
kono
parents:
diff changeset
30 gets the location information "on demand", rather than storing the
kono
parents:
diff changeset
31 location information in the initial lex for every string. Thus the
kono
parents:
diff changeset
32 substring_loc can also be thought of as a deferred call into libcpp,
kono
parents:
diff changeset
33 to allow the non-trivial work of reparsing the string to be delayed
kono
parents:
diff changeset
34 until we actually need it (to emit a diagnostic for a particular range
kono
parents:
diff changeset
35 of characters).
kono
parents:
diff changeset
36
kono
parents:
diff changeset
37 substring_loc::get_location returns NULL if it succeeds, or an
kono
parents:
diff changeset
38 error message if it fails. Error messages are intended for GCC
kono
parents:
diff changeset
39 developers (to help debugging) rather than for end-users.
kono
parents:
diff changeset
40
kono
parents:
diff changeset
41 The easiest way to use a substring_loc is via the format_warning_* APIs,
kono
parents:
diff changeset
42 which gracefully handle failure of substring_loc::get_location by using
kono
parents:
diff changeset
43 the location of the string as a whole if substring-information is
kono
parents:
diff changeset
44 unavailable. */
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 class substring_loc
kono
parents:
diff changeset
47 {
kono
parents:
diff changeset
48 public:
kono
parents:
diff changeset
49 /* Constructor. FMT_STRING_LOC is the location of the string as
kono
parents:
diff changeset
50 a whole. STRING_TYPE is the type of the string. It should be an
kono
parents:
diff changeset
51 ARRAY_TYPE of INTEGER_TYPE, or a POINTER_TYPE to such an ARRAY_TYPE.
kono
parents:
diff changeset
52 CARET_IDX, START_IDX, and END_IDX are offsets from the start
kono
parents:
diff changeset
53 of the string data. */
kono
parents:
diff changeset
54 substring_loc (location_t fmt_string_loc, tree string_type,
kono
parents:
diff changeset
55 int caret_idx, int start_idx, int end_idx)
kono
parents:
diff changeset
56 : m_fmt_string_loc (fmt_string_loc), m_string_type (string_type),
kono
parents:
diff changeset
57 m_caret_idx (caret_idx), m_start_idx (start_idx), m_end_idx (end_idx) {}
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 void set_caret_index (int caret_idx) { m_caret_idx = caret_idx; }
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 const char *get_location (location_t *out_loc) const;
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 location_t get_fmt_string_loc () const { return m_fmt_string_loc; }
kono
parents:
diff changeset
64 tree get_string_type () const { return m_string_type; }
kono
parents:
diff changeset
65 int get_caret_idx () const { return m_caret_idx; }
kono
parents:
diff changeset
66 int get_start_idx () const { return m_start_idx; }
kono
parents:
diff changeset
67 int get_end_idx () const { return m_end_idx; }
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 private:
kono
parents:
diff changeset
70 location_t m_fmt_string_loc;
kono
parents:
diff changeset
71 tree m_string_type;
kono
parents:
diff changeset
72 int m_caret_idx;
kono
parents:
diff changeset
73 int m_start_idx;
kono
parents:
diff changeset
74 int m_end_idx;
kono
parents:
diff changeset
75 };
kono
parents:
diff changeset
76
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
77 /* A bundle of state for emitting a diagnostic relating to a format string. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
78
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
79 class format_string_diagnostic_t
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
80 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
81 public:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
82 format_string_diagnostic_t (const substring_loc &fmt_loc,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
83 const range_label *fmt_label,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
84 location_t param_loc,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
85 const range_label *param_label,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
86 const char *corrected_substring);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
87
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
88 /* Functions for emitting a warning about a format string. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
89
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
90 bool emit_warning_va (int opt, const char *gmsgid, va_list *ap) const
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
91 ATTRIBUTE_GCC_DIAG (3, 0);
111
kono
parents:
diff changeset
92
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
93 bool emit_warning_n_va (int opt, unsigned HOST_WIDE_INT n,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
94 const char *singular_gmsgid,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
95 const char *plural_gmsgid, va_list *ap) const
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
96 ATTRIBUTE_GCC_DIAG (4, 0) ATTRIBUTE_GCC_DIAG (5, 0);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
97
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
98 bool emit_warning (int opt, const char *gmsgid, ...) const
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
99 ATTRIBUTE_GCC_DIAG (3, 4);
111
kono
parents:
diff changeset
100
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
101 bool emit_warning_n (int opt, unsigned HOST_WIDE_INT n,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
102 const char *singular_gmsgid,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
103 const char *plural_gmsgid, ...) const
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
104 ATTRIBUTE_GCC_DIAG (4, 6) ATTRIBUTE_GCC_DIAG (5, 6);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
105
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
106 private:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
107 const substring_loc &m_fmt_loc;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
108 const range_label *m_fmt_label;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
109 location_t m_param_loc;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
110 const range_label *m_param_label;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
111 const char *m_corrected_substring;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
112 };
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
113
111
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 /* Implementation detail, for use when implementing
kono
parents:
diff changeset
116 LANG_HOOKS_GET_SUBSTRING_LOCATION. */
kono
parents:
diff changeset
117
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
118 extern const char *get_location_within_string (cpp_reader *pfile,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
119 string_concat_db *concats,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
120 location_t strloc,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
121 enum cpp_ttype type,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
122 int caret_idx,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
123 int start_idx, int end_idx,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
124 location_t *out_loc);
111
kono
parents:
diff changeset
125
kono
parents:
diff changeset
126 #endif /* ! GCC_SUBSTRING_LOCATIONS_H */