aboutsummaryrefslogtreecommitdiff
path: root/gcc/substring-locations.h
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2016-09-07 16:56:23 +0000
committerDavid Malcolm <dmalcolm@gcc.gnu.org>2016-09-07 16:56:23 +0000
commite5106e27feded7797a6df36493aa37cb673bcad1 (patch)
treebcfb76c5cbb7225eaaa830c5addd7d9df3bc60ab /gcc/substring-locations.h
parent7100c1f253908708185aa9878bb147cef986279e (diff)
downloadgcc-e5106e27feded7797a6df36493aa37cb673bcad1.zip
gcc-e5106e27feded7797a6df36493aa37cb673bcad1.tar.gz
gcc-e5106e27feded7797a6df36493aa37cb673bcad1.tar.bz2
Move class substring_loc from c-family into gcc
gcc/ChangeLog: * Makefile.in (OBJS): Add substring-locations.o. * langhooks-def.h (class substring_loc): New forward decl. (lhd_get_substring_location): New decl. (LANG_HOOKS_GET_SUBSTRING_LOCATION): New macro. (LANG_HOOKS_INITIALIZER): Add LANG_HOOKS_GET_SUBSTRING_LOCATION. * langhooks.c (lhd_get_substring_location): New function. * langhooks.h (class substring_loc): New forward decl. (struct lang_hooks): Add field get_substring_location. * substring-locations.c: New file, taking definition of format_warning_va and format_warning_at_substring from c-family/c-format.c, making them non-static. * substring-locations.h (class substring_loc): Move class here from c-family/c-common.h. Add and rewrite comments. (format_warning_va): New decl. (format_warning_at_substring): New decl. (get_source_location_for_substring): Add comment. gcc/c-family/ChangeLog: * c-common.c (get_cpp_ttype_from_string_type): Handle being passed a POINTER_TYPE. (substring_loc::get_location): Move to substring-locations.c, keeping implementation as... (c_get_substring_location): New function, from the above, reworked to use accessors rather than member lookup. * c-common.h (class substring_loc): Move to substring-locations.h, replacing with a forward decl. (c_get_substring_location): New decl. * c-format.c: Include "substring-locations.h". (format_warning_va): Move to substring-locations.c. (format_warning_at_substring): Likewise. gcc/c/ChangeLog: * c-lang.c (LANG_HOOKS_GET_SUBSTRING_LOCATION): Use c_get_substring_location for this new langhook. gcc/testsuite/ChangeLog: * gcc.dg/plugin/diagnostic_plugin_test_string_literals.c: Include "substring-locations.h". From-SVN: r240028
Diffstat (limited to 'gcc/substring-locations.h')
-rw-r--r--gcc/substring-locations.h71
1 files changed, 71 insertions, 0 deletions
diff --git a/gcc/substring-locations.h b/gcc/substring-locations.h
index f839c74..f8788c9 100644
--- a/gcc/substring-locations.h
+++ b/gcc/substring-locations.h
@@ -20,6 +20,77 @@ along with GCC; see the file COPYING3. If not see
#ifndef GCC_SUBSTRING_LOCATIONS_H
#define GCC_SUBSTRING_LOCATIONS_H
+/* The substring_loc class encapsulates information on the source location
+ of a range of characters within a STRING_CST.
+
+ If needed by a diagnostic, the actual location_t of the substring_loc
+ can be calculated by calling its get_location method. This calls a
+ langhook, since this is inherently frontend-specific. For the C family
+ of frontends, it calls back into libcpp to reparse the strings. This
+ gets the location information "on demand", rather than storing the
+ location information in the initial lex for every string. Thus the
+ substring_loc can also be thought of as a deferred call into libcpp,
+ to allow the non-trivial work of reparsing the string to be delayed
+ until we actually need it (to emit a diagnostic for a particular range
+ of characters).
+
+ substring_loc::get_location returns NULL if it succeeds, or an
+ error message if it fails. Error messages are intended for GCC
+ developers (to help debugging) rather than for end-users.
+
+ The easiest way to use a substring_loc is via the format_warning_* APIs,
+ which gracefully handle failure of substring_loc::get_location by using
+ the location of the string as a whole if substring-information is
+ unavailable. */
+
+class substring_loc
+{
+ public:
+ /* Constructor. FMT_STRING_LOC is the location of the string as
+ a whole. STRING_TYPE is the type of the string. It should be an
+ ARRAY_TYPE of INTEGER_TYPE, or a POINTER_TYPE to such an ARRAY_TYPE.
+ CARET_IDX, START_IDX, and END_IDX are offsets from the start
+ of the string data. */
+ substring_loc (location_t fmt_string_loc, tree string_type,
+ int caret_idx, int start_idx, int end_idx)
+ : m_fmt_string_loc (fmt_string_loc), m_string_type (string_type),
+ m_caret_idx (caret_idx), m_start_idx (start_idx), m_end_idx (end_idx) {}
+
+ void set_caret_index (int caret_idx) { m_caret_idx = caret_idx; }
+
+ const char *get_location (location_t *out_loc) const;
+
+ location_t get_fmt_string_loc () const { return m_fmt_string_loc; }
+ tree get_string_type () const { return m_string_type; }
+ int get_caret_idx () const { return m_caret_idx; }
+ int get_start_idx () const { return m_start_idx; }
+ int get_end_idx () const { return m_end_idx; }
+
+ private:
+ location_t m_fmt_string_loc;
+ tree m_string_type;
+ int m_caret_idx;
+ int m_start_idx;
+ int m_end_idx;
+};
+
+/* Functions for emitting a warning about a format string. */
+
+extern bool format_warning_va (const substring_loc &fmt_loc,
+ const source_range *param_range,
+ const char *corrected_substring,
+ int opt, const char *gmsgid, va_list *ap)
+ ATTRIBUTE_GCC_DIAG (5,0);
+
+extern bool format_warning_at_substring (const substring_loc &fmt_loc,
+ const source_range *param_range,
+ const char *corrected_substring,
+ int opt, const char *gmsgid, ...)
+ ATTRIBUTE_GCC_DIAG (5,0);
+
+/* Implementation detail, for use when implementing
+ LANG_HOOKS_GET_SUBSTRING_LOCATION. */
+
extern const char *get_source_location_for_substring (cpp_reader *pfile,
string_concat_db *concats,
location_t strloc,