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