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