format.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Parse a printf-style format string.
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef COMMON_FORMAT_H
  15. #define COMMON_FORMAT_H
  16. #include "gdbsupport/gdb_string_view.h"
  17. #if defined(__MINGW32__) && !defined(PRINTF_HAS_LONG_LONG)
  18. # define USE_PRINTF_I64 1
  19. # define PRINTF_HAS_LONG_LONG
  20. #else
  21. # define USE_PRINTF_I64 0
  22. #endif
  23. /* The argclass represents the general type of data that goes with a
  24. format directive; int_arg for %d, long_arg for %l, and so forth.
  25. Note that these primarily distinguish types by size and need for
  26. special handling, so for instance %u and %x are (at present) also
  27. classed as int_arg. */
  28. enum argclass
  29. {
  30. literal_piece,
  31. int_arg, long_arg, long_long_arg, size_t_arg, ptr_arg,
  32. string_arg, wide_string_arg, wide_char_arg,
  33. double_arg, long_double_arg,
  34. dec32float_arg, dec64float_arg, dec128float_arg
  35. };
  36. /* A format piece is a section of the format string that may include a
  37. single print directive somewhere in it, and the associated class
  38. for the argument. */
  39. struct format_piece
  40. {
  41. format_piece (const char *str, enum argclass argc, int n)
  42. : string (str),
  43. argclass (argc),
  44. n_int_args (n)
  45. {
  46. }
  47. bool operator== (const format_piece &other) const
  48. {
  49. return (this->argclass == other.argclass
  50. && gdb::string_view (this->string) == other.string);
  51. }
  52. const char *string;
  53. enum argclass argclass;
  54. /* Count the number of preceding 'int' arguments that must be passed
  55. along. This is used for a width or precision of '*'. Note that
  56. this feature is only available in "gdb_extensions" mode. */
  57. int n_int_args;
  58. };
  59. class format_pieces
  60. {
  61. public:
  62. format_pieces (const char **arg, bool gdb_extensions = false);
  63. ~format_pieces () = default;
  64. DISABLE_COPY_AND_ASSIGN (format_pieces);
  65. typedef std::vector<format_piece>::iterator iterator;
  66. iterator begin ()
  67. {
  68. return m_pieces.begin ();
  69. }
  70. iterator end ()
  71. {
  72. return m_pieces.end ();
  73. }
  74. private:
  75. std::vector<format_piece> m_pieces;
  76. gdb::unique_xmalloc_ptr<char> m_storage;
  77. };
  78. #endif /* COMMON_FORMAT_H */