substring-locations.h 4.6 KB

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