xref: /netbsd-src/external/gpl3/gdb/dist/gdbsupport/format.h (revision 5ba1f45f2a09259cc846f20c7c5501604d633c90)
18dffb485Schristos /* Parse a printf-style format string.
28dffb485Schristos 
3*5ba1f45fSchristos    Copyright (C) 1986-2024 Free Software Foundation, Inc.
48dffb485Schristos 
58dffb485Schristos    This file is part of GDB.
68dffb485Schristos 
78dffb485Schristos    This program is free software; you can redistribute it and/or modify
88dffb485Schristos    it under the terms of the GNU General Public License as published by
98dffb485Schristos    the Free Software Foundation; either version 3 of the License, or
108dffb485Schristos    (at your option) any later version.
118dffb485Schristos 
128dffb485Schristos    This program is distributed in the hope that it will be useful,
138dffb485Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
148dffb485Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
158dffb485Schristos    GNU General Public License for more details.
168dffb485Schristos 
178dffb485Schristos    You should have received a copy of the GNU General Public License
188dffb485Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
198dffb485Schristos 
208dffb485Schristos #ifndef COMMON_FORMAT_H
218dffb485Schristos #define COMMON_FORMAT_H
228dffb485Schristos 
23*5ba1f45fSchristos #include <string_view>
248dffb485Schristos 
258dffb485Schristos #if defined(__MINGW32__) && !defined(PRINTF_HAS_LONG_LONG)
268dffb485Schristos # define USE_PRINTF_I64 1
278dffb485Schristos # define PRINTF_HAS_LONG_LONG
288dffb485Schristos #else
298dffb485Schristos # define USE_PRINTF_I64 0
308dffb485Schristos #endif
318dffb485Schristos 
328dffb485Schristos /* The argclass represents the general type of data that goes with a
338dffb485Schristos    format directive; int_arg for %d, long_arg for %l, and so forth.
348dffb485Schristos    Note that these primarily distinguish types by size and need for
358dffb485Schristos    special handling, so for instance %u and %x are (at present) also
368dffb485Schristos    classed as int_arg.  */
378dffb485Schristos 
388dffb485Schristos enum argclass
398dffb485Schristos   {
408dffb485Schristos     literal_piece,
418dffb485Schristos     int_arg, long_arg, long_long_arg, size_t_arg, ptr_arg,
428dffb485Schristos     string_arg, wide_string_arg, wide_char_arg,
438dffb485Schristos     double_arg, long_double_arg,
44*5ba1f45fSchristos     dec32float_arg, dec64float_arg, dec128float_arg,
45*5ba1f45fSchristos     value_arg
468dffb485Schristos   };
478dffb485Schristos 
488dffb485Schristos /* A format piece is a section of the format string that may include a
498dffb485Schristos    single print directive somewhere in it, and the associated class
508dffb485Schristos    for the argument.  */
518dffb485Schristos 
528dffb485Schristos struct format_piece
538dffb485Schristos {
548dffb485Schristos   format_piece (const char *str, enum argclass argc, int n)
558dffb485Schristos     : string (str),
568dffb485Schristos       argclass (argc),
578dffb485Schristos       n_int_args (n)
588dffb485Schristos   {
59*5ba1f45fSchristos     gdb_assert (str != nullptr);
608dffb485Schristos   }
618dffb485Schristos 
628dffb485Schristos   bool operator== (const format_piece &other) const
638dffb485Schristos   {
648dffb485Schristos     return (this->argclass == other.argclass
65*5ba1f45fSchristos 	    && std::string_view (this->string) == other.string);
668dffb485Schristos   }
678dffb485Schristos 
688dffb485Schristos   const char *string;
698dffb485Schristos   enum argclass argclass;
708dffb485Schristos   /* Count the number of preceding 'int' arguments that must be passed
718dffb485Schristos      along.  This is used for a width or precision of '*'.  Note that
728dffb485Schristos      this feature is only available in "gdb_extensions" mode.  */
738dffb485Schristos   int n_int_args;
748dffb485Schristos };
758dffb485Schristos 
768dffb485Schristos class format_pieces
778dffb485Schristos {
788dffb485Schristos public:
798dffb485Schristos 
80*5ba1f45fSchristos   format_pieces (const char **arg, bool gdb_extensions = false,
81*5ba1f45fSchristos 		 bool value_extension = false);
828dffb485Schristos   ~format_pieces () = default;
838dffb485Schristos 
848dffb485Schristos   DISABLE_COPY_AND_ASSIGN (format_pieces);
858dffb485Schristos 
868dffb485Schristos   typedef std::vector<format_piece>::iterator iterator;
878dffb485Schristos 
888dffb485Schristos   iterator begin ()
898dffb485Schristos   {
908dffb485Schristos     return m_pieces.begin ();
918dffb485Schristos   }
928dffb485Schristos 
938dffb485Schristos   iterator end ()
948dffb485Schristos   {
958dffb485Schristos     return m_pieces.end ();
968dffb485Schristos   }
978dffb485Schristos 
988dffb485Schristos private:
998dffb485Schristos 
1008dffb485Schristos   std::vector<format_piece> m_pieces;
1018dffb485Schristos   gdb::unique_xmalloc_ptr<char> m_storage;
1028dffb485Schristos };
1038dffb485Schristos 
1048dffb485Schristos #endif /* COMMON_FORMAT_H */
105