Commit | Line | Data |
---|---|---|
d3ce09f5 SS |
1 | /* Parse a printf-style format string. |
2 | ||
3666a048 | 3 | Copyright (C) 1986-2021 Free Software Foundation, Inc. |
d3ce09f5 SS |
4 | |
5 | This file is part of GDB. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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/>. */ | |
19 | ||
6a997029 TT |
20 | #ifndef COMMON_FORMAT_H |
21 | #define COMMON_FORMAT_H | |
22 | ||
268a13a5 | 23 | #include "gdbsupport/gdb_string_view.h" |
b17992c1 | 24 | |
d3ce09f5 SS |
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 | |
31 | ||
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. */ | |
37 | ||
38 | enum argclass | |
39 | { | |
40 | literal_piece, | |
e06f3d6e | 41 | int_arg, long_arg, long_long_arg, size_t_arg, ptr_arg, |
d3ce09f5 | 42 | string_arg, wide_string_arg, wide_char_arg, |
16e812b2 UW |
43 | double_arg, long_double_arg, |
44 | dec32float_arg, dec64float_arg, dec128float_arg | |
d3ce09f5 SS |
45 | }; |
46 | ||
47 | /* A format piece is a section of the format string that may include a | |
48 | single print directive somewhere in it, and the associated class | |
49 | for the argument. */ | |
50 | ||
51 | struct format_piece | |
52 | { | |
2a3c1174 | 53 | format_piece (const char *str, enum argclass argc, int n) |
8e481c3b | 54 | : string (str), |
2a3c1174 PA |
55 | argclass (argc), |
56 | n_int_args (n) | |
8e481c3b TT |
57 | { |
58 | } | |
59 | ||
b17992c1 SM |
60 | bool operator== (const format_piece &other) const |
61 | { | |
62 | return (this->argclass == other.argclass | |
63 | && gdb::string_view (this->string) == other.string); | |
64 | } | |
65 | ||
8e481c3b | 66 | const char *string; |
d3ce09f5 | 67 | enum argclass argclass; |
2a3c1174 PA |
68 | /* Count the number of preceding 'int' arguments that must be passed |
69 | along. This is used for a width or precision of '*'. Note that | |
70 | this feature is only available in "gdb_extensions" mode. */ | |
71 | int n_int_args; | |
d3ce09f5 SS |
72 | }; |
73 | ||
8e481c3b TT |
74 | class format_pieces |
75 | { | |
76 | public: | |
77 | ||
2a3c1174 | 78 | format_pieces (const char **arg, bool gdb_extensions = false); |
8e481c3b TT |
79 | ~format_pieces () = default; |
80 | ||
81 | DISABLE_COPY_AND_ASSIGN (format_pieces); | |
d3ce09f5 | 82 | |
8e481c3b | 83 | typedef std::vector<format_piece>::iterator iterator; |
d3ce09f5 | 84 | |
8e481c3b TT |
85 | iterator begin () |
86 | { | |
87 | return m_pieces.begin (); | |
88 | } | |
d3ce09f5 | 89 | |
8e481c3b TT |
90 | iterator end () |
91 | { | |
92 | return m_pieces.end (); | |
93 | } | |
d3ce09f5 | 94 | |
8e481c3b TT |
95 | private: |
96 | ||
97 | std::vector<format_piece> m_pieces; | |
98 | gdb::unique_xmalloc_ptr<char> m_storage; | |
99 | }; | |
6a997029 TT |
100 | |
101 | #endif /* COMMON_FORMAT_H */ |