annotate gcc/pretty-print.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents f6334be47118
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 /* Various declarations for language-independent pretty-print subroutines.
111
kono
parents: 67
diff changeset
2 Copyright (C) 2003-2017 Free Software Foundation, Inc.
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 This file is part of GCC.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 GCC is free software; you can redistribute it and/or modify it under
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 the terms of the GNU General Public License as published by the Free
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 Software Foundation; either version 3, or (at your option) any later
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 version.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 for more details.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 along with GCC; see the file COPYING3. If not see
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 <http://www.gnu.org/licenses/>. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 #include "config.h"
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 #include "system.h"
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 #include "coretypes.h"
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 #include "intl.h"
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 #include "pretty-print.h"
111
kono
parents: 67
diff changeset
26 #include "diagnostic-color.h"
kono
parents: 67
diff changeset
27 #include "selftest.h"
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
28
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
29 #if HAVE_ICONV
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
30 #include <iconv.h>
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
31 #endif
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
32
111
kono
parents: 67
diff changeset
33 #ifdef __MINGW32__
kono
parents: 67
diff changeset
34
kono
parents: 67
diff changeset
35 /* Replacement for fputs() that handles ANSI escape codes on Windows NT.
kono
parents: 67
diff changeset
36 Contributed by: Liu Hao (lh_mouse at 126 dot com)
kono
parents: 67
diff changeset
37
kono
parents: 67
diff changeset
38 XXX: This file is compiled into libcommon.a that will be self-contained.
kono
parents: 67
diff changeset
39 It looks like that these functions can be put nowhere else. */
kono
parents: 67
diff changeset
40
kono
parents: 67
diff changeset
41 #include <io.h>
kono
parents: 67
diff changeset
42 #define WIN32_LEAN_AND_MEAN 1
kono
parents: 67
diff changeset
43 #include <windows.h>
kono
parents: 67
diff changeset
44
kono
parents: 67
diff changeset
45 /* Write all bytes in [s,s+n) into the specified stream.
kono
parents: 67
diff changeset
46 Errors are ignored. */
kono
parents: 67
diff changeset
47 static void
kono
parents: 67
diff changeset
48 write_all (HANDLE h, const char *s, size_t n)
kono
parents: 67
diff changeset
49 {
kono
parents: 67
diff changeset
50 size_t rem = n;
kono
parents: 67
diff changeset
51 DWORD step;
kono
parents: 67
diff changeset
52
kono
parents: 67
diff changeset
53 while (rem != 0)
kono
parents: 67
diff changeset
54 {
kono
parents: 67
diff changeset
55 if (rem <= UINT_MAX)
kono
parents: 67
diff changeset
56 step = rem;
kono
parents: 67
diff changeset
57 else
kono
parents: 67
diff changeset
58 step = UINT_MAX;
kono
parents: 67
diff changeset
59 if (!WriteFile (h, s + n - rem, step, &step, NULL))
kono
parents: 67
diff changeset
60 break;
kono
parents: 67
diff changeset
61 rem -= step;
kono
parents: 67
diff changeset
62 }
kono
parents: 67
diff changeset
63 }
kono
parents: 67
diff changeset
64
kono
parents: 67
diff changeset
65 /* Find the beginning of an escape sequence.
kono
parents: 67
diff changeset
66 There are two cases:
kono
parents: 67
diff changeset
67 1. If the sequence begins with an ESC character (0x1B) and a second
kono
parents: 67
diff changeset
68 character X in [0x40,0x5F], returns X and stores a pointer to
kono
parents: 67
diff changeset
69 the third character into *head.
kono
parents: 67
diff changeset
70 2. If the sequence begins with a character X in [0x80,0x9F], returns
kono
parents: 67
diff changeset
71 (X-0x40) and stores a pointer to the second character into *head.
kono
parents: 67
diff changeset
72 Stores the number of ESC character(s) in *prefix_len.
kono
parents: 67
diff changeset
73 Returns 0 if no such sequence can be found. */
kono
parents: 67
diff changeset
74 static int
kono
parents: 67
diff changeset
75 find_esc_head (int *prefix_len, const char **head, const char *str)
kono
parents: 67
diff changeset
76 {
kono
parents: 67
diff changeset
77 int c;
kono
parents: 67
diff changeset
78 const char *r = str;
kono
parents: 67
diff changeset
79 int escaped = 0;
kono
parents: 67
diff changeset
80
kono
parents: 67
diff changeset
81 for (;;)
kono
parents: 67
diff changeset
82 {
kono
parents: 67
diff changeset
83 c = (unsigned char) *r;
kono
parents: 67
diff changeset
84 if (c == 0)
kono
parents: 67
diff changeset
85 {
kono
parents: 67
diff changeset
86 /* Not found. */
kono
parents: 67
diff changeset
87 return 0;
kono
parents: 67
diff changeset
88 }
kono
parents: 67
diff changeset
89 if (escaped && 0x40 <= c && c <= 0x5F)
kono
parents: 67
diff changeset
90 {
kono
parents: 67
diff changeset
91 /* Found (case 1). */
kono
parents: 67
diff changeset
92 *prefix_len = 2;
kono
parents: 67
diff changeset
93 *head = r + 1;
kono
parents: 67
diff changeset
94 return c;
kono
parents: 67
diff changeset
95 }
kono
parents: 67
diff changeset
96 if (0x80 <= c && c <= 0x9F)
kono
parents: 67
diff changeset
97 {
kono
parents: 67
diff changeset
98 /* Found (case 2). */
kono
parents: 67
diff changeset
99 *prefix_len = 1;
kono
parents: 67
diff changeset
100 *head = r + 1;
kono
parents: 67
diff changeset
101 return c - 0x40;
kono
parents: 67
diff changeset
102 }
kono
parents: 67
diff changeset
103 ++r;
kono
parents: 67
diff changeset
104 escaped = c == 0x1B;
kono
parents: 67
diff changeset
105 }
kono
parents: 67
diff changeset
106 }
kono
parents: 67
diff changeset
107
kono
parents: 67
diff changeset
108 /* Find the terminator of an escape sequence.
kono
parents: 67
diff changeset
109 str should be the value stored in *head by a previous successful
kono
parents: 67
diff changeset
110 call to find_esc_head().
kono
parents: 67
diff changeset
111 Returns 0 if no such sequence can be found. */
kono
parents: 67
diff changeset
112 static int
kono
parents: 67
diff changeset
113 find_esc_terminator (const char **term, const char *str)
kono
parents: 67
diff changeset
114 {
kono
parents: 67
diff changeset
115 int c;
kono
parents: 67
diff changeset
116 const char *r = str;
kono
parents: 67
diff changeset
117
kono
parents: 67
diff changeset
118 for (;;)
kono
parents: 67
diff changeset
119 {
kono
parents: 67
diff changeset
120 c = (unsigned char) *r;
kono
parents: 67
diff changeset
121 if (c == 0)
kono
parents: 67
diff changeset
122 {
kono
parents: 67
diff changeset
123 /* Not found. */
kono
parents: 67
diff changeset
124 return 0;
kono
parents: 67
diff changeset
125 }
kono
parents: 67
diff changeset
126 if (0x40 <= c && c <= 0x7E)
kono
parents: 67
diff changeset
127 {
kono
parents: 67
diff changeset
128 /* Found. */
kono
parents: 67
diff changeset
129 *term = r;
kono
parents: 67
diff changeset
130 return c;
kono
parents: 67
diff changeset
131 }
kono
parents: 67
diff changeset
132 ++r;
kono
parents: 67
diff changeset
133 }
kono
parents: 67
diff changeset
134 }
kono
parents: 67
diff changeset
135
kono
parents: 67
diff changeset
136 /* Handle a sequence of codes. Sequences that are invalid, reserved,
kono
parents: 67
diff changeset
137 unrecognized or unimplemented are ignored silently.
kono
parents: 67
diff changeset
138 There isn't much we can do because of lameness of Windows consoles. */
kono
parents: 67
diff changeset
139 static void
kono
parents: 67
diff changeset
140 eat_esc_sequence (HANDLE h, int esc_code,
kono
parents: 67
diff changeset
141 const char *esc_head, const char *esc_term)
kono
parents: 67
diff changeset
142 {
kono
parents: 67
diff changeset
143 /* Numbers in an escape sequence cannot be negative, because
kono
parents: 67
diff changeset
144 a minus sign in the middle of it would have terminated it. */
kono
parents: 67
diff changeset
145 long n1, n2;
kono
parents: 67
diff changeset
146 char *eptr, *delim;
kono
parents: 67
diff changeset
147 CONSOLE_SCREEN_BUFFER_INFO sb;
kono
parents: 67
diff changeset
148 COORD cr;
kono
parents: 67
diff changeset
149 /* ED and EL parameters. */
kono
parents: 67
diff changeset
150 DWORD cnt, step;
kono
parents: 67
diff changeset
151 long rows;
kono
parents: 67
diff changeset
152 /* SGR parameters. */
kono
parents: 67
diff changeset
153 WORD attrib_add, attrib_rm;
kono
parents: 67
diff changeset
154 const char *param;
kono
parents: 67
diff changeset
155
kono
parents: 67
diff changeset
156 switch (MAKEWORD (esc_code, *esc_term))
kono
parents: 67
diff changeset
157 {
kono
parents: 67
diff changeset
158 /* ESC [ n1 'A'
kono
parents: 67
diff changeset
159 Move the cursor up by n1 characters. */
kono
parents: 67
diff changeset
160 case MAKEWORD ('[', 'A'):
kono
parents: 67
diff changeset
161 if (esc_head == esc_term)
kono
parents: 67
diff changeset
162 n1 = 1;
kono
parents: 67
diff changeset
163 else
kono
parents: 67
diff changeset
164 {
kono
parents: 67
diff changeset
165 n1 = strtol (esc_head, &eptr, 10);
kono
parents: 67
diff changeset
166 if (eptr != esc_term)
kono
parents: 67
diff changeset
167 break;
kono
parents: 67
diff changeset
168 }
kono
parents: 67
diff changeset
169
kono
parents: 67
diff changeset
170 if (GetConsoleScreenBufferInfo (h, &sb))
kono
parents: 67
diff changeset
171 {
kono
parents: 67
diff changeset
172 cr = sb.dwCursorPosition;
kono
parents: 67
diff changeset
173 /* Stop at the topmost boundary. */
kono
parents: 67
diff changeset
174 if (cr.Y > n1)
kono
parents: 67
diff changeset
175 cr.Y -= n1;
kono
parents: 67
diff changeset
176 else
kono
parents: 67
diff changeset
177 cr.Y = 0;
kono
parents: 67
diff changeset
178 SetConsoleCursorPosition (h, cr);
kono
parents: 67
diff changeset
179 }
kono
parents: 67
diff changeset
180 break;
kono
parents: 67
diff changeset
181
kono
parents: 67
diff changeset
182 /* ESC [ n1 'B'
kono
parents: 67
diff changeset
183 Move the cursor down by n1 characters. */
kono
parents: 67
diff changeset
184 case MAKEWORD ('[', 'B'):
kono
parents: 67
diff changeset
185 if (esc_head == esc_term)
kono
parents: 67
diff changeset
186 n1 = 1;
kono
parents: 67
diff changeset
187 else
kono
parents: 67
diff changeset
188 {
kono
parents: 67
diff changeset
189 n1 = strtol (esc_head, &eptr, 10);
kono
parents: 67
diff changeset
190 if (eptr != esc_term)
kono
parents: 67
diff changeset
191 break;
kono
parents: 67
diff changeset
192 }
kono
parents: 67
diff changeset
193
kono
parents: 67
diff changeset
194 if (GetConsoleScreenBufferInfo (h, &sb))
kono
parents: 67
diff changeset
195 {
kono
parents: 67
diff changeset
196 cr = sb.dwCursorPosition;
kono
parents: 67
diff changeset
197 /* Stop at the bottommost boundary. */
kono
parents: 67
diff changeset
198 if (sb.dwSize.Y - cr.Y > n1)
kono
parents: 67
diff changeset
199 cr.Y += n1;
kono
parents: 67
diff changeset
200 else
kono
parents: 67
diff changeset
201 cr.Y = sb.dwSize.Y;
kono
parents: 67
diff changeset
202 SetConsoleCursorPosition (h, cr);
kono
parents: 67
diff changeset
203 }
kono
parents: 67
diff changeset
204 break;
kono
parents: 67
diff changeset
205
kono
parents: 67
diff changeset
206 /* ESC [ n1 'C'
kono
parents: 67
diff changeset
207 Move the cursor right by n1 characters. */
kono
parents: 67
diff changeset
208 case MAKEWORD ('[', 'C'):
kono
parents: 67
diff changeset
209 if (esc_head == esc_term)
kono
parents: 67
diff changeset
210 n1 = 1;
kono
parents: 67
diff changeset
211 else
kono
parents: 67
diff changeset
212 {
kono
parents: 67
diff changeset
213 n1 = strtol (esc_head, &eptr, 10);
kono
parents: 67
diff changeset
214 if (eptr != esc_term)
kono
parents: 67
diff changeset
215 break;
kono
parents: 67
diff changeset
216 }
kono
parents: 67
diff changeset
217
kono
parents: 67
diff changeset
218 if (GetConsoleScreenBufferInfo (h, &sb))
kono
parents: 67
diff changeset
219 {
kono
parents: 67
diff changeset
220 cr = sb.dwCursorPosition;
kono
parents: 67
diff changeset
221 /* Stop at the rightmost boundary. */
kono
parents: 67
diff changeset
222 if (sb.dwSize.X - cr.X > n1)
kono
parents: 67
diff changeset
223 cr.X += n1;
kono
parents: 67
diff changeset
224 else
kono
parents: 67
diff changeset
225 cr.X = sb.dwSize.X;
kono
parents: 67
diff changeset
226 SetConsoleCursorPosition (h, cr);
kono
parents: 67
diff changeset
227 }
kono
parents: 67
diff changeset
228 break;
kono
parents: 67
diff changeset
229
kono
parents: 67
diff changeset
230 /* ESC [ n1 'D'
kono
parents: 67
diff changeset
231 Move the cursor left by n1 characters. */
kono
parents: 67
diff changeset
232 case MAKEWORD ('[', 'D'):
kono
parents: 67
diff changeset
233 if (esc_head == esc_term)
kono
parents: 67
diff changeset
234 n1 = 1;
kono
parents: 67
diff changeset
235 else
kono
parents: 67
diff changeset
236 {
kono
parents: 67
diff changeset
237 n1 = strtol (esc_head, &eptr, 10);
kono
parents: 67
diff changeset
238 if (eptr != esc_term)
kono
parents: 67
diff changeset
239 break;
kono
parents: 67
diff changeset
240 }
kono
parents: 67
diff changeset
241
kono
parents: 67
diff changeset
242 if (GetConsoleScreenBufferInfo (h, &sb))
kono
parents: 67
diff changeset
243 {
kono
parents: 67
diff changeset
244 cr = sb.dwCursorPosition;
kono
parents: 67
diff changeset
245 /* Stop at the leftmost boundary. */
kono
parents: 67
diff changeset
246 if (cr.X > n1)
kono
parents: 67
diff changeset
247 cr.X -= n1;
kono
parents: 67
diff changeset
248 else
kono
parents: 67
diff changeset
249 cr.X = 0;
kono
parents: 67
diff changeset
250 SetConsoleCursorPosition (h, cr);
kono
parents: 67
diff changeset
251 }
kono
parents: 67
diff changeset
252 break;
kono
parents: 67
diff changeset
253
kono
parents: 67
diff changeset
254 /* ESC [ n1 'E'
kono
parents: 67
diff changeset
255 Move the cursor to the beginning of the n1-th line downwards. */
kono
parents: 67
diff changeset
256 case MAKEWORD ('[', 'E'):
kono
parents: 67
diff changeset
257 if (esc_head == esc_term)
kono
parents: 67
diff changeset
258 n1 = 1;
kono
parents: 67
diff changeset
259 else
kono
parents: 67
diff changeset
260 {
kono
parents: 67
diff changeset
261 n1 = strtol (esc_head, &eptr, 10);
kono
parents: 67
diff changeset
262 if (eptr != esc_term)
kono
parents: 67
diff changeset
263 break;
kono
parents: 67
diff changeset
264 }
kono
parents: 67
diff changeset
265
kono
parents: 67
diff changeset
266 if (GetConsoleScreenBufferInfo (h, &sb))
kono
parents: 67
diff changeset
267 {
kono
parents: 67
diff changeset
268 cr = sb.dwCursorPosition;
kono
parents: 67
diff changeset
269 cr.X = 0;
kono
parents: 67
diff changeset
270 /* Stop at the bottommost boundary. */
kono
parents: 67
diff changeset
271 if (sb.dwSize.Y - cr.Y > n1)
kono
parents: 67
diff changeset
272 cr.Y += n1;
kono
parents: 67
diff changeset
273 else
kono
parents: 67
diff changeset
274 cr.Y = sb.dwSize.Y;
kono
parents: 67
diff changeset
275 SetConsoleCursorPosition (h, cr);
kono
parents: 67
diff changeset
276 }
kono
parents: 67
diff changeset
277 break;
kono
parents: 67
diff changeset
278
kono
parents: 67
diff changeset
279 /* ESC [ n1 'F'
kono
parents: 67
diff changeset
280 Move the cursor to the beginning of the n1-th line upwards. */
kono
parents: 67
diff changeset
281 case MAKEWORD ('[', 'F'):
kono
parents: 67
diff changeset
282 if (esc_head == esc_term)
kono
parents: 67
diff changeset
283 n1 = 1;
kono
parents: 67
diff changeset
284 else
kono
parents: 67
diff changeset
285 {
kono
parents: 67
diff changeset
286 n1 = strtol (esc_head, &eptr, 10);
kono
parents: 67
diff changeset
287 if (eptr != esc_term)
kono
parents: 67
diff changeset
288 break;
kono
parents: 67
diff changeset
289 }
kono
parents: 67
diff changeset
290
kono
parents: 67
diff changeset
291 if (GetConsoleScreenBufferInfo (h, &sb))
kono
parents: 67
diff changeset
292 {
kono
parents: 67
diff changeset
293 cr = sb.dwCursorPosition;
kono
parents: 67
diff changeset
294 cr.X = 0;
kono
parents: 67
diff changeset
295 /* Stop at the topmost boundary. */
kono
parents: 67
diff changeset
296 if (cr.Y > n1)
kono
parents: 67
diff changeset
297 cr.Y -= n1;
kono
parents: 67
diff changeset
298 else
kono
parents: 67
diff changeset
299 cr.Y = 0;
kono
parents: 67
diff changeset
300 SetConsoleCursorPosition (h, cr);
kono
parents: 67
diff changeset
301 }
kono
parents: 67
diff changeset
302 break;
kono
parents: 67
diff changeset
303
kono
parents: 67
diff changeset
304 /* ESC [ n1 'G'
kono
parents: 67
diff changeset
305 Move the cursor to the (1-based) n1-th column. */
kono
parents: 67
diff changeset
306 case MAKEWORD ('[', 'G'):
kono
parents: 67
diff changeset
307 if (esc_head == esc_term)
kono
parents: 67
diff changeset
308 n1 = 1;
kono
parents: 67
diff changeset
309 else
kono
parents: 67
diff changeset
310 {
kono
parents: 67
diff changeset
311 n1 = strtol (esc_head, &eptr, 10);
kono
parents: 67
diff changeset
312 if (eptr != esc_term)
kono
parents: 67
diff changeset
313 break;
kono
parents: 67
diff changeset
314 }
kono
parents: 67
diff changeset
315
kono
parents: 67
diff changeset
316 if (GetConsoleScreenBufferInfo (h, &sb))
kono
parents: 67
diff changeset
317 {
kono
parents: 67
diff changeset
318 cr = sb.dwCursorPosition;
kono
parents: 67
diff changeset
319 n1 -= 1;
kono
parents: 67
diff changeset
320 /* Stop at the leftmost or rightmost boundary. */
kono
parents: 67
diff changeset
321 if (n1 < 0)
kono
parents: 67
diff changeset
322 cr.X = 0;
kono
parents: 67
diff changeset
323 else if (n1 > sb.dwSize.X)
kono
parents: 67
diff changeset
324 cr.X = sb.dwSize.X;
kono
parents: 67
diff changeset
325 else
kono
parents: 67
diff changeset
326 cr.X = n1;
kono
parents: 67
diff changeset
327 SetConsoleCursorPosition (h, cr);
kono
parents: 67
diff changeset
328 }
kono
parents: 67
diff changeset
329 break;
kono
parents: 67
diff changeset
330
kono
parents: 67
diff changeset
331 /* ESC [ n1 ';' n2 'H'
kono
parents: 67
diff changeset
332 ESC [ n1 ';' n2 'f'
kono
parents: 67
diff changeset
333 Move the cursor to the (1-based) n1-th row and
kono
parents: 67
diff changeset
334 (also 1-based) n2-th column. */
kono
parents: 67
diff changeset
335 case MAKEWORD ('[', 'H'):
kono
parents: 67
diff changeset
336 case MAKEWORD ('[', 'f'):
kono
parents: 67
diff changeset
337 if (esc_head == esc_term)
kono
parents: 67
diff changeset
338 {
kono
parents: 67
diff changeset
339 /* Both parameters are omitted and set to 1 by default. */
kono
parents: 67
diff changeset
340 n1 = 1;
kono
parents: 67
diff changeset
341 n2 = 1;
kono
parents: 67
diff changeset
342 }
kono
parents: 67
diff changeset
343 else if (!(delim = (char *) memchr (esc_head, ';',
kono
parents: 67
diff changeset
344 esc_term - esc_head)))
kono
parents: 67
diff changeset
345 {
kono
parents: 67
diff changeset
346 /* Only the first parameter is given. The second one is
kono
parents: 67
diff changeset
347 set to 1 by default. */
kono
parents: 67
diff changeset
348 n1 = strtol (esc_head, &eptr, 10);
kono
parents: 67
diff changeset
349 if (eptr != esc_term)
kono
parents: 67
diff changeset
350 break;
kono
parents: 67
diff changeset
351 n2 = 1;
kono
parents: 67
diff changeset
352 }
kono
parents: 67
diff changeset
353 else
kono
parents: 67
diff changeset
354 {
kono
parents: 67
diff changeset
355 /* Both parameters are given. The first one shall be
kono
parents: 67
diff changeset
356 terminated by the semicolon. */
kono
parents: 67
diff changeset
357 n1 = strtol (esc_head, &eptr, 10);
kono
parents: 67
diff changeset
358 if (eptr != delim)
kono
parents: 67
diff changeset
359 break;
kono
parents: 67
diff changeset
360 n2 = strtol (delim + 1, &eptr, 10);
kono
parents: 67
diff changeset
361 if (eptr != esc_term)
kono
parents: 67
diff changeset
362 break;
kono
parents: 67
diff changeset
363 }
kono
parents: 67
diff changeset
364
kono
parents: 67
diff changeset
365 if (GetConsoleScreenBufferInfo (h, &sb))
kono
parents: 67
diff changeset
366 {
kono
parents: 67
diff changeset
367 cr = sb.dwCursorPosition;
kono
parents: 67
diff changeset
368 n1 -= 1;
kono
parents: 67
diff changeset
369 n2 -= 1;
kono
parents: 67
diff changeset
370 /* The cursor position shall be relative to the view coord of
kono
parents: 67
diff changeset
371 the console window, which is usually smaller than the actual
kono
parents: 67
diff changeset
372 buffer. FWIW, the 'appropriate' solution will be shrinking
kono
parents: 67
diff changeset
373 the buffer to match the size of the console window,
kono
parents: 67
diff changeset
374 destroying scrollback in the process. */
kono
parents: 67
diff changeset
375 n1 += sb.srWindow.Top;
kono
parents: 67
diff changeset
376 n2 += sb.srWindow.Left;
kono
parents: 67
diff changeset
377 /* Stop at the topmost or bottommost boundary. */
kono
parents: 67
diff changeset
378 if (n1 < 0)
kono
parents: 67
diff changeset
379 cr.Y = 0;
kono
parents: 67
diff changeset
380 else if (n1 > sb.dwSize.Y)
kono
parents: 67
diff changeset
381 cr.Y = sb.dwSize.Y;
kono
parents: 67
diff changeset
382 else
kono
parents: 67
diff changeset
383 cr.Y = n1;
kono
parents: 67
diff changeset
384 /* Stop at the leftmost or rightmost boundary. */
kono
parents: 67
diff changeset
385 if (n2 < 0)
kono
parents: 67
diff changeset
386 cr.X = 0;
kono
parents: 67
diff changeset
387 else if (n2 > sb.dwSize.X)
kono
parents: 67
diff changeset
388 cr.X = sb.dwSize.X;
kono
parents: 67
diff changeset
389 else
kono
parents: 67
diff changeset
390 cr.X = n2;
kono
parents: 67
diff changeset
391 SetConsoleCursorPosition (h, cr);
kono
parents: 67
diff changeset
392 }
kono
parents: 67
diff changeset
393 break;
kono
parents: 67
diff changeset
394
kono
parents: 67
diff changeset
395 /* ESC [ n1 'J'
kono
parents: 67
diff changeset
396 Erase display. */
kono
parents: 67
diff changeset
397 case MAKEWORD ('[', 'J'):
kono
parents: 67
diff changeset
398 if (esc_head == esc_term)
kono
parents: 67
diff changeset
399 /* This is one of the very few codes whose parameters have
kono
parents: 67
diff changeset
400 a default value of zero. */
kono
parents: 67
diff changeset
401 n1 = 0;
kono
parents: 67
diff changeset
402 else
kono
parents: 67
diff changeset
403 {
kono
parents: 67
diff changeset
404 n1 = strtol (esc_head, &eptr, 10);
kono
parents: 67
diff changeset
405 if (eptr != esc_term)
kono
parents: 67
diff changeset
406 break;
kono
parents: 67
diff changeset
407 }
kono
parents: 67
diff changeset
408
kono
parents: 67
diff changeset
409 if (GetConsoleScreenBufferInfo (h, &sb))
kono
parents: 67
diff changeset
410 {
kono
parents: 67
diff changeset
411 /* The cursor is not necessarily in the console window, which
kono
parents: 67
diff changeset
412 makes the behavior of this code harder to define. */
kono
parents: 67
diff changeset
413 switch (n1)
kono
parents: 67
diff changeset
414 {
kono
parents: 67
diff changeset
415 case 0:
kono
parents: 67
diff changeset
416 /* If the cursor is in or above the window, erase from
kono
parents: 67
diff changeset
417 it to the bottom of the window; otherwise, do nothing. */
kono
parents: 67
diff changeset
418 cr = sb.dwCursorPosition;
kono
parents: 67
diff changeset
419 cnt = sb.dwSize.X - sb.dwCursorPosition.X;
kono
parents: 67
diff changeset
420 rows = sb.srWindow.Bottom - sb.dwCursorPosition.Y;
kono
parents: 67
diff changeset
421 break;
kono
parents: 67
diff changeset
422 case 1:
kono
parents: 67
diff changeset
423 /* If the cursor is in or under the window, erase from
kono
parents: 67
diff changeset
424 it to the top of the window; otherwise, do nothing. */
kono
parents: 67
diff changeset
425 cr.X = 0;
kono
parents: 67
diff changeset
426 cr.Y = sb.srWindow.Top;
kono
parents: 67
diff changeset
427 cnt = sb.dwCursorPosition.X + 1;
kono
parents: 67
diff changeset
428 rows = sb.dwCursorPosition.Y - sb.srWindow.Top;
kono
parents: 67
diff changeset
429 break;
kono
parents: 67
diff changeset
430 case 2:
kono
parents: 67
diff changeset
431 /* Erase the entire window. */
kono
parents: 67
diff changeset
432 cr.X = sb.srWindow.Left;
kono
parents: 67
diff changeset
433 cr.Y = sb.srWindow.Top;
kono
parents: 67
diff changeset
434 cnt = 0;
kono
parents: 67
diff changeset
435 rows = sb.srWindow.Bottom - sb.srWindow.Top + 1;
kono
parents: 67
diff changeset
436 break;
kono
parents: 67
diff changeset
437 default:
kono
parents: 67
diff changeset
438 /* Erase the entire buffer. */
kono
parents: 67
diff changeset
439 cr.X = 0;
kono
parents: 67
diff changeset
440 cr.Y = 0;
kono
parents: 67
diff changeset
441 cnt = 0;
kono
parents: 67
diff changeset
442 rows = sb.dwSize.Y;
kono
parents: 67
diff changeset
443 break;
kono
parents: 67
diff changeset
444 }
kono
parents: 67
diff changeset
445 if (rows < 0)
kono
parents: 67
diff changeset
446 break;
kono
parents: 67
diff changeset
447 cnt += rows * sb.dwSize.X;
kono
parents: 67
diff changeset
448 FillConsoleOutputCharacterW (h, L' ', cnt, cr, &step);
kono
parents: 67
diff changeset
449 FillConsoleOutputAttribute (h, sb.wAttributes, cnt, cr, &step);
kono
parents: 67
diff changeset
450 }
kono
parents: 67
diff changeset
451 break;
kono
parents: 67
diff changeset
452
kono
parents: 67
diff changeset
453 /* ESC [ n1 'K'
kono
parents: 67
diff changeset
454 Erase line. */
kono
parents: 67
diff changeset
455 case MAKEWORD ('[', 'K'):
kono
parents: 67
diff changeset
456 if (esc_head == esc_term)
kono
parents: 67
diff changeset
457 /* This is one of the very few codes whose parameters have
kono
parents: 67
diff changeset
458 a default value of zero. */
kono
parents: 67
diff changeset
459 n1 = 0;
kono
parents: 67
diff changeset
460 else
kono
parents: 67
diff changeset
461 {
kono
parents: 67
diff changeset
462 n1 = strtol (esc_head, &eptr, 10);
kono
parents: 67
diff changeset
463 if (eptr != esc_term)
kono
parents: 67
diff changeset
464 break;
kono
parents: 67
diff changeset
465 }
kono
parents: 67
diff changeset
466
kono
parents: 67
diff changeset
467 if (GetConsoleScreenBufferInfo (h, &sb))
kono
parents: 67
diff changeset
468 {
kono
parents: 67
diff changeset
469 switch (n1)
kono
parents: 67
diff changeset
470 {
kono
parents: 67
diff changeset
471 case 0:
kono
parents: 67
diff changeset
472 /* Erase from the cursor to the end. */
kono
parents: 67
diff changeset
473 cr = sb.dwCursorPosition;
kono
parents: 67
diff changeset
474 cnt = sb.dwSize.X - sb.dwCursorPosition.X;
kono
parents: 67
diff changeset
475 break;
kono
parents: 67
diff changeset
476 case 1:
kono
parents: 67
diff changeset
477 /* Erase from the cursor to the beginning. */
kono
parents: 67
diff changeset
478 cr = sb.dwCursorPosition;
kono
parents: 67
diff changeset
479 cr.X = 0;
kono
parents: 67
diff changeset
480 cnt = sb.dwCursorPosition.X + 1;
kono
parents: 67
diff changeset
481 break;
kono
parents: 67
diff changeset
482 default:
kono
parents: 67
diff changeset
483 /* Erase the entire line. */
kono
parents: 67
diff changeset
484 cr = sb.dwCursorPosition;
kono
parents: 67
diff changeset
485 cr.X = 0;
kono
parents: 67
diff changeset
486 cnt = sb.dwSize.X;
kono
parents: 67
diff changeset
487 break;
kono
parents: 67
diff changeset
488 }
kono
parents: 67
diff changeset
489 FillConsoleOutputCharacterW (h, L' ', cnt, cr, &step);
kono
parents: 67
diff changeset
490 FillConsoleOutputAttribute (h, sb.wAttributes, cnt, cr, &step);
kono
parents: 67
diff changeset
491 }
kono
parents: 67
diff changeset
492 break;
kono
parents: 67
diff changeset
493
kono
parents: 67
diff changeset
494 /* ESC [ n1 ';' n2 'm'
kono
parents: 67
diff changeset
495 Set SGR parameters. Zero or more parameters will follow. */
kono
parents: 67
diff changeset
496 case MAKEWORD ('[', 'm'):
kono
parents: 67
diff changeset
497 attrib_add = 0;
kono
parents: 67
diff changeset
498 attrib_rm = 0;
kono
parents: 67
diff changeset
499 if (esc_head == esc_term)
kono
parents: 67
diff changeset
500 {
kono
parents: 67
diff changeset
501 /* When no parameter is given, reset the console. */
kono
parents: 67
diff changeset
502 attrib_add |= (FOREGROUND_RED | FOREGROUND_GREEN
kono
parents: 67
diff changeset
503 | FOREGROUND_BLUE);
kono
parents: 67
diff changeset
504 attrib_rm = -1; /* Removes everything. */
kono
parents: 67
diff changeset
505 goto sgr_set_it;
kono
parents: 67
diff changeset
506 }
kono
parents: 67
diff changeset
507 param = esc_head;
kono
parents: 67
diff changeset
508 do
kono
parents: 67
diff changeset
509 {
kono
parents: 67
diff changeset
510 /* Parse a parameter. */
kono
parents: 67
diff changeset
511 n1 = strtol (param, &eptr, 10);
kono
parents: 67
diff changeset
512 if (*eptr != ';' && eptr != esc_term)
kono
parents: 67
diff changeset
513 goto sgr_set_it;
kono
parents: 67
diff changeset
514
kono
parents: 67
diff changeset
515 switch (n1)
kono
parents: 67
diff changeset
516 {
kono
parents: 67
diff changeset
517 case 0:
kono
parents: 67
diff changeset
518 /* Reset. */
kono
parents: 67
diff changeset
519 attrib_add |= (FOREGROUND_RED | FOREGROUND_GREEN
kono
parents: 67
diff changeset
520 | FOREGROUND_BLUE);
kono
parents: 67
diff changeset
521 attrib_rm = -1; /* Removes everything. */
kono
parents: 67
diff changeset
522 break;
kono
parents: 67
diff changeset
523 case 1:
kono
parents: 67
diff changeset
524 /* Bold. */
kono
parents: 67
diff changeset
525 attrib_add |= FOREGROUND_INTENSITY;
kono
parents: 67
diff changeset
526 break;
kono
parents: 67
diff changeset
527 case 4:
kono
parents: 67
diff changeset
528 /* Underline. */
kono
parents: 67
diff changeset
529 attrib_add |= COMMON_LVB_UNDERSCORE;
kono
parents: 67
diff changeset
530 break;
kono
parents: 67
diff changeset
531 case 5:
kono
parents: 67
diff changeset
532 /* Blink. */
kono
parents: 67
diff changeset
533 /* XXX: It is not BLINKING at all! */
kono
parents: 67
diff changeset
534 attrib_add |= BACKGROUND_INTENSITY;
kono
parents: 67
diff changeset
535 break;
kono
parents: 67
diff changeset
536 case 7:
kono
parents: 67
diff changeset
537 /* Reverse. */
kono
parents: 67
diff changeset
538 attrib_add |= COMMON_LVB_REVERSE_VIDEO;
kono
parents: 67
diff changeset
539 break;
kono
parents: 67
diff changeset
540 case 22:
kono
parents: 67
diff changeset
541 /* No bold. */
kono
parents: 67
diff changeset
542 attrib_add &= ~FOREGROUND_INTENSITY;
kono
parents: 67
diff changeset
543 attrib_rm |= FOREGROUND_INTENSITY;
kono
parents: 67
diff changeset
544 break;
kono
parents: 67
diff changeset
545 case 24:
kono
parents: 67
diff changeset
546 /* No underline. */
kono
parents: 67
diff changeset
547 attrib_add &= ~COMMON_LVB_UNDERSCORE;
kono
parents: 67
diff changeset
548 attrib_rm |= COMMON_LVB_UNDERSCORE;
kono
parents: 67
diff changeset
549 break;
kono
parents: 67
diff changeset
550 case 25:
kono
parents: 67
diff changeset
551 /* No blink. */
kono
parents: 67
diff changeset
552 /* XXX: It is not BLINKING at all! */
kono
parents: 67
diff changeset
553 attrib_add &= ~BACKGROUND_INTENSITY;
kono
parents: 67
diff changeset
554 attrib_rm |= BACKGROUND_INTENSITY;
kono
parents: 67
diff changeset
555 break;
kono
parents: 67
diff changeset
556 case 27:
kono
parents: 67
diff changeset
557 /* No reverse. */
kono
parents: 67
diff changeset
558 attrib_add &= ~COMMON_LVB_REVERSE_VIDEO;
kono
parents: 67
diff changeset
559 attrib_rm |= COMMON_LVB_REVERSE_VIDEO;
kono
parents: 67
diff changeset
560 break;
kono
parents: 67
diff changeset
561 case 30:
kono
parents: 67
diff changeset
562 case 31:
kono
parents: 67
diff changeset
563 case 32:
kono
parents: 67
diff changeset
564 case 33:
kono
parents: 67
diff changeset
565 case 34:
kono
parents: 67
diff changeset
566 case 35:
kono
parents: 67
diff changeset
567 case 36:
kono
parents: 67
diff changeset
568 case 37:
kono
parents: 67
diff changeset
569 /* Foreground color. */
kono
parents: 67
diff changeset
570 attrib_add &= ~(FOREGROUND_RED | FOREGROUND_GREEN
kono
parents: 67
diff changeset
571 | FOREGROUND_BLUE);
kono
parents: 67
diff changeset
572 n1 -= 30;
kono
parents: 67
diff changeset
573 if (n1 & 1)
kono
parents: 67
diff changeset
574 attrib_add |= FOREGROUND_RED;
kono
parents: 67
diff changeset
575 if (n1 & 2)
kono
parents: 67
diff changeset
576 attrib_add |= FOREGROUND_GREEN;
kono
parents: 67
diff changeset
577 if (n1 & 4)
kono
parents: 67
diff changeset
578 attrib_add |= FOREGROUND_BLUE;
kono
parents: 67
diff changeset
579 attrib_rm |= (FOREGROUND_RED | FOREGROUND_GREEN
kono
parents: 67
diff changeset
580 | FOREGROUND_BLUE);
kono
parents: 67
diff changeset
581 break;
kono
parents: 67
diff changeset
582 case 38:
kono
parents: 67
diff changeset
583 /* Reserved for extended foreground color.
kono
parents: 67
diff changeset
584 Don't know how to handle parameters remaining.
kono
parents: 67
diff changeset
585 Bail out. */
kono
parents: 67
diff changeset
586 goto sgr_set_it;
kono
parents: 67
diff changeset
587 case 39:
kono
parents: 67
diff changeset
588 /* Reset foreground color. */
kono
parents: 67
diff changeset
589 /* Set to grey. */
kono
parents: 67
diff changeset
590 attrib_add |= (FOREGROUND_RED | FOREGROUND_GREEN
kono
parents: 67
diff changeset
591 | FOREGROUND_BLUE);
kono
parents: 67
diff changeset
592 attrib_rm |= (FOREGROUND_RED | FOREGROUND_GREEN
kono
parents: 67
diff changeset
593 | FOREGROUND_BLUE);
kono
parents: 67
diff changeset
594 break;
kono
parents: 67
diff changeset
595 case 40:
kono
parents: 67
diff changeset
596 case 41:
kono
parents: 67
diff changeset
597 case 42:
kono
parents: 67
diff changeset
598 case 43:
kono
parents: 67
diff changeset
599 case 44:
kono
parents: 67
diff changeset
600 case 45:
kono
parents: 67
diff changeset
601 case 46:
kono
parents: 67
diff changeset
602 case 47:
kono
parents: 67
diff changeset
603 /* Background color. */
kono
parents: 67
diff changeset
604 attrib_add &= ~(BACKGROUND_RED | BACKGROUND_GREEN
kono
parents: 67
diff changeset
605 | BACKGROUND_BLUE);
kono
parents: 67
diff changeset
606 n1 -= 40;
kono
parents: 67
diff changeset
607 if (n1 & 1)
kono
parents: 67
diff changeset
608 attrib_add |= BACKGROUND_RED;
kono
parents: 67
diff changeset
609 if (n1 & 2)
kono
parents: 67
diff changeset
610 attrib_add |= BACKGROUND_GREEN;
kono
parents: 67
diff changeset
611 if (n1 & 4)
kono
parents: 67
diff changeset
612 attrib_add |= BACKGROUND_BLUE;
kono
parents: 67
diff changeset
613 attrib_rm |= (BACKGROUND_RED | BACKGROUND_GREEN
kono
parents: 67
diff changeset
614 | BACKGROUND_BLUE);
kono
parents: 67
diff changeset
615 break;
kono
parents: 67
diff changeset
616 case 48:
kono
parents: 67
diff changeset
617 /* Reserved for extended background color.
kono
parents: 67
diff changeset
618 Don't know how to handle parameters remaining.
kono
parents: 67
diff changeset
619 Bail out. */
kono
parents: 67
diff changeset
620 goto sgr_set_it;
kono
parents: 67
diff changeset
621 case 49:
kono
parents: 67
diff changeset
622 /* Reset background color. */
kono
parents: 67
diff changeset
623 /* Set to black. */
kono
parents: 67
diff changeset
624 attrib_add &= ~(BACKGROUND_RED | BACKGROUND_GREEN
kono
parents: 67
diff changeset
625 | BACKGROUND_BLUE);
kono
parents: 67
diff changeset
626 attrib_rm |= (BACKGROUND_RED | BACKGROUND_GREEN
kono
parents: 67
diff changeset
627 | BACKGROUND_BLUE);
kono
parents: 67
diff changeset
628 break;
kono
parents: 67
diff changeset
629 }
kono
parents: 67
diff changeset
630
kono
parents: 67
diff changeset
631 /* Prepare the next parameter. */
kono
parents: 67
diff changeset
632 param = eptr + 1;
kono
parents: 67
diff changeset
633 }
kono
parents: 67
diff changeset
634 while (param != esc_term);
kono
parents: 67
diff changeset
635
kono
parents: 67
diff changeset
636 sgr_set_it:
kono
parents: 67
diff changeset
637 /* 0xFFFF removes everything. If it is not the case,
kono
parents: 67
diff changeset
638 care must be taken to preserve old attributes. */
kono
parents: 67
diff changeset
639 if (attrib_rm != 0xFFFF && GetConsoleScreenBufferInfo (h, &sb))
kono
parents: 67
diff changeset
640 {
kono
parents: 67
diff changeset
641 attrib_add |= sb.wAttributes & ~attrib_rm;
kono
parents: 67
diff changeset
642 }
kono
parents: 67
diff changeset
643 SetConsoleTextAttribute (h, attrib_add);
kono
parents: 67
diff changeset
644 break;
kono
parents: 67
diff changeset
645 }
kono
parents: 67
diff changeset
646 }
kono
parents: 67
diff changeset
647
kono
parents: 67
diff changeset
648 int
kono
parents: 67
diff changeset
649 mingw_ansi_fputs (const char *str, FILE *fp)
kono
parents: 67
diff changeset
650 {
kono
parents: 67
diff changeset
651 const char *read = str;
kono
parents: 67
diff changeset
652 HANDLE h;
kono
parents: 67
diff changeset
653 DWORD mode;
kono
parents: 67
diff changeset
654 int esc_code, prefix_len;
kono
parents: 67
diff changeset
655 const char *esc_head, *esc_term;
kono
parents: 67
diff changeset
656
kono
parents: 67
diff changeset
657 h = (HANDLE) _get_osfhandle (_fileno (fp));
kono
parents: 67
diff changeset
658 if (h == INVALID_HANDLE_VALUE)
kono
parents: 67
diff changeset
659 return EOF;
kono
parents: 67
diff changeset
660
kono
parents: 67
diff changeset
661 /* Don't mess up stdio functions with Windows APIs. */
kono
parents: 67
diff changeset
662 fflush (fp);
kono
parents: 67
diff changeset
663
kono
parents: 67
diff changeset
664 if (GetConsoleMode (h, &mode))
kono
parents: 67
diff changeset
665 /* If it is a console, translate ANSI escape codes as needed. */
kono
parents: 67
diff changeset
666 for (;;)
kono
parents: 67
diff changeset
667 {
kono
parents: 67
diff changeset
668 if ((esc_code = find_esc_head (&prefix_len, &esc_head, read)) == 0)
kono
parents: 67
diff changeset
669 {
kono
parents: 67
diff changeset
670 /* Write all remaining characters, then exit. */
kono
parents: 67
diff changeset
671 write_all (h, read, strlen (read));
kono
parents: 67
diff changeset
672 break;
kono
parents: 67
diff changeset
673 }
kono
parents: 67
diff changeset
674 if (find_esc_terminator (&esc_term, esc_head) == 0)
kono
parents: 67
diff changeset
675 /* Ignore incomplete escape sequences at the moment.
kono
parents: 67
diff changeset
676 FIXME: The escape state shall be cached for further calls
kono
parents: 67
diff changeset
677 to this function. */
kono
parents: 67
diff changeset
678 break;
kono
parents: 67
diff changeset
679 write_all (h, read, esc_head - prefix_len - read);
kono
parents: 67
diff changeset
680 eat_esc_sequence (h, esc_code, esc_head, esc_term);
kono
parents: 67
diff changeset
681 read = esc_term + 1;
kono
parents: 67
diff changeset
682 }
kono
parents: 67
diff changeset
683 else
kono
parents: 67
diff changeset
684 /* If it is not a console, write everything as-is. */
kono
parents: 67
diff changeset
685 write_all (h, read, strlen (read));
kono
parents: 67
diff changeset
686
kono
parents: 67
diff changeset
687 _close ((intptr_t) h);
kono
parents: 67
diff changeset
688 return 1;
kono
parents: 67
diff changeset
689 }
kono
parents: 67
diff changeset
690
kono
parents: 67
diff changeset
691 #endif /* __MINGW32__ */
kono
parents: 67
diff changeset
692
kono
parents: 67
diff changeset
693 static void pp_quoted_string (pretty_printer *, const char *, size_t = -1);
kono
parents: 67
diff changeset
694
kono
parents: 67
diff changeset
695 /* Overwrite the given location/range within this text_info's rich_location.
kono
parents: 67
diff changeset
696 For use e.g. when implementing "+" in client format decoders. */
kono
parents: 67
diff changeset
697
kono
parents: 67
diff changeset
698 void
kono
parents: 67
diff changeset
699 text_info::set_location (unsigned int idx, location_t loc, bool show_caret_p)
kono
parents: 67
diff changeset
700 {
kono
parents: 67
diff changeset
701 gcc_checking_assert (m_richloc);
kono
parents: 67
diff changeset
702 m_richloc->set_range (line_table, idx, loc, show_caret_p);
kono
parents: 67
diff changeset
703 }
kono
parents: 67
diff changeset
704
kono
parents: 67
diff changeset
705 location_t
kono
parents: 67
diff changeset
706 text_info::get_location (unsigned int index_of_location) const
kono
parents: 67
diff changeset
707 {
kono
parents: 67
diff changeset
708 gcc_checking_assert (m_richloc);
kono
parents: 67
diff changeset
709
kono
parents: 67
diff changeset
710 if (index_of_location == 0)
kono
parents: 67
diff changeset
711 return m_richloc->get_loc ();
kono
parents: 67
diff changeset
712 else
kono
parents: 67
diff changeset
713 return UNKNOWN_LOCATION;
kono
parents: 67
diff changeset
714 }
kono
parents: 67
diff changeset
715
kono
parents: 67
diff changeset
716 // Default construct an output buffer.
kono
parents: 67
diff changeset
717
kono
parents: 67
diff changeset
718 output_buffer::output_buffer ()
kono
parents: 67
diff changeset
719 : formatted_obstack (),
kono
parents: 67
diff changeset
720 chunk_obstack (),
kono
parents: 67
diff changeset
721 obstack (&formatted_obstack),
kono
parents: 67
diff changeset
722 cur_chunk_array (),
kono
parents: 67
diff changeset
723 stream (stderr),
kono
parents: 67
diff changeset
724 line_length (),
kono
parents: 67
diff changeset
725 digit_buffer (),
kono
parents: 67
diff changeset
726 flush_p (true)
kono
parents: 67
diff changeset
727 {
kono
parents: 67
diff changeset
728 obstack_init (&formatted_obstack);
kono
parents: 67
diff changeset
729 obstack_init (&chunk_obstack);
kono
parents: 67
diff changeset
730 }
kono
parents: 67
diff changeset
731
kono
parents: 67
diff changeset
732 // Release resources owned by an output buffer at the end of lifetime.
kono
parents: 67
diff changeset
733
kono
parents: 67
diff changeset
734 output_buffer::~output_buffer ()
kono
parents: 67
diff changeset
735 {
kono
parents: 67
diff changeset
736 obstack_free (&chunk_obstack, NULL);
kono
parents: 67
diff changeset
737 obstack_free (&formatted_obstack, NULL);
kono
parents: 67
diff changeset
738 }
kono
parents: 67
diff changeset
739
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
740
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
741 /* Format an integer given by va_arg (ARG, type-specifier T) where
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
742 type-specifier is a precision modifier as indicated by PREC. F is
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
743 a string used to construct the appropriate format-specifier. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
744 #define pp_integer_with_precision(PP, ARG, PREC, T, F) \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
745 do \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
746 switch (PREC) \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
747 { \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
748 case 0: \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
749 pp_scalar (PP, "%" F, va_arg (ARG, T)); \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
750 break; \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
751 \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
752 case 1: \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
753 pp_scalar (PP, "%l" F, va_arg (ARG, long T)); \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
754 break; \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
755 \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
756 case 2: \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
757 pp_scalar (PP, "%" HOST_LONG_LONG_FORMAT F, va_arg (ARG, long long T)); \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
758 break; \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
759 \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
760 default: \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
761 break; \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
762 } \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
763 while (0)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
764
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
765
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
766 /* Subroutine of pp_set_maximum_length. Set up PRETTY-PRINTER's
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
767 internal maximum characters per line. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
768 static void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
769 pp_set_real_maximum_length (pretty_printer *pp)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
770 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
771 /* If we're told not to wrap lines then do the obvious thing. In case
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
772 we'll emit prefix only once per message, it is appropriate
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
773 not to increase unnecessarily the line-length cut-off. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
774 if (!pp_is_wrapping_line (pp)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
775 || pp_prefixing_rule (pp) == DIAGNOSTICS_SHOW_PREFIX_ONCE
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
776 || pp_prefixing_rule (pp) == DIAGNOSTICS_SHOW_PREFIX_NEVER)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
777 pp->maximum_length = pp_line_cutoff (pp);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
778 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
779 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
780 int prefix_length = pp->prefix ? strlen (pp->prefix) : 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
781 /* If the prefix is ridiculously too long, output at least
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
782 32 characters. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
783 if (pp_line_cutoff (pp) - prefix_length < 32)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
784 pp->maximum_length = pp_line_cutoff (pp) + 32;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
785 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
786 pp->maximum_length = pp_line_cutoff (pp);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
787 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
788 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
789
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
790 /* Clear PRETTY-PRINTER's output state. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
791 static inline void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
792 pp_clear_state (pretty_printer *pp)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
793 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
794 pp->emitted_prefix = false;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
795 pp_indentation (pp) = 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
796 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
797
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
798 /* Flush the formatted text of PRETTY-PRINTER onto the attached stream. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
799 void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
800 pp_write_text_to_stream (pretty_printer *pp)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
801 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
802 const char *text = pp_formatted_text (pp);
111
kono
parents: 67
diff changeset
803 #ifdef __MINGW32__
kono
parents: 67
diff changeset
804 mingw_ansi_fputs (text, pp_buffer (pp)->stream);
kono
parents: 67
diff changeset
805 #else
kono
parents: 67
diff changeset
806 fputs (text, pp_buffer (pp)->stream);
kono
parents: 67
diff changeset
807 #endif
kono
parents: 67
diff changeset
808 pp_clear_output_area (pp);
kono
parents: 67
diff changeset
809 }
kono
parents: 67
diff changeset
810
kono
parents: 67
diff changeset
811 /* As pp_write_text_to_stream, but for GraphViz label output.
kono
parents: 67
diff changeset
812
kono
parents: 67
diff changeset
813 Flush the formatted text of pretty-printer PP onto the attached stream.
kono
parents: 67
diff changeset
814 Replace characters in PPF that have special meaning in a GraphViz .dot
kono
parents: 67
diff changeset
815 file.
kono
parents: 67
diff changeset
816
kono
parents: 67
diff changeset
817 This routine is not very fast, but it doesn't have to be as this is only
kono
parents: 67
diff changeset
818 be used by routines dumping intermediate representations in graph form. */
kono
parents: 67
diff changeset
819
kono
parents: 67
diff changeset
820 void
kono
parents: 67
diff changeset
821 pp_write_text_as_dot_label_to_stream (pretty_printer *pp, bool for_record)
kono
parents: 67
diff changeset
822 {
kono
parents: 67
diff changeset
823 const char *text = pp_formatted_text (pp);
kono
parents: 67
diff changeset
824 const char *p = text;
kono
parents: 67
diff changeset
825 FILE *fp = pp_buffer (pp)->stream;
kono
parents: 67
diff changeset
826
kono
parents: 67
diff changeset
827 for (;*p; p++)
kono
parents: 67
diff changeset
828 {
kono
parents: 67
diff changeset
829 bool escape_char;
kono
parents: 67
diff changeset
830 switch (*p)
kono
parents: 67
diff changeset
831 {
kono
parents: 67
diff changeset
832 /* Print newlines as a left-aligned newline. */
kono
parents: 67
diff changeset
833 case '\n':
kono
parents: 67
diff changeset
834 fputs ("\\l", fp);
kono
parents: 67
diff changeset
835 escape_char = true;
kono
parents: 67
diff changeset
836 break;
kono
parents: 67
diff changeset
837
kono
parents: 67
diff changeset
838 /* The following characters are only special for record-shape nodes. */
kono
parents: 67
diff changeset
839 case '|':
kono
parents: 67
diff changeset
840 case '{':
kono
parents: 67
diff changeset
841 case '}':
kono
parents: 67
diff changeset
842 case '<':
kono
parents: 67
diff changeset
843 case '>':
kono
parents: 67
diff changeset
844 case ' ':
kono
parents: 67
diff changeset
845 escape_char = for_record;
kono
parents: 67
diff changeset
846 break;
kono
parents: 67
diff changeset
847
kono
parents: 67
diff changeset
848 /* The following characters always have to be escaped
kono
parents: 67
diff changeset
849 for use in labels. */
kono
parents: 67
diff changeset
850 case '\\':
kono
parents: 67
diff changeset
851 /* There is a bug in some (f.i. 2.36.0) versions of graphiz
kono
parents: 67
diff changeset
852 ( http://www.graphviz.org/mantisbt/view.php?id=2524 ) related to
kono
parents: 67
diff changeset
853 backslash as last char in label. Let's avoid triggering it. */
kono
parents: 67
diff changeset
854 gcc_assert (*(p + 1) != '\0');
kono
parents: 67
diff changeset
855 /* Fall through. */
kono
parents: 67
diff changeset
856 case '"':
kono
parents: 67
diff changeset
857 escape_char = true;
kono
parents: 67
diff changeset
858 break;
kono
parents: 67
diff changeset
859
kono
parents: 67
diff changeset
860 default:
kono
parents: 67
diff changeset
861 escape_char = false;
kono
parents: 67
diff changeset
862 break;
kono
parents: 67
diff changeset
863 }
kono
parents: 67
diff changeset
864
kono
parents: 67
diff changeset
865 if (escape_char)
kono
parents: 67
diff changeset
866 fputc ('\\', fp);
kono
parents: 67
diff changeset
867
kono
parents: 67
diff changeset
868 fputc (*p, fp);
kono
parents: 67
diff changeset
869 }
kono
parents: 67
diff changeset
870
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
871 pp_clear_output_area (pp);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
872 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
873
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
874 /* Wrap a text delimited by START and END into PRETTY-PRINTER. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
875 static void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
876 pp_wrap_text (pretty_printer *pp, const char *start, const char *end)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
877 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
878 bool wrapping_line = pp_is_wrapping_line (pp);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
879
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
880 while (start != end)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
881 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
882 /* Dump anything bordered by whitespaces. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
883 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
884 const char *p = start;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
885 while (p != end && !ISBLANK (*p) && *p != '\n')
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
886 ++p;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
887 if (wrapping_line
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
888 && p - start >= pp_remaining_character_count_for_line (pp))
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
889 pp_newline (pp);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
890 pp_append_text (pp, start, p);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
891 start = p;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
892 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
893
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
894 if (start != end && ISBLANK (*start))
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
895 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
896 pp_space (pp);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
897 ++start;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
898 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
899 if (start != end && *start == '\n')
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
900 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
901 pp_newline (pp);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
902 ++start;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
903 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
904 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
905 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
906
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
907 /* Same as pp_wrap_text but wrap text only when in line-wrapping mode. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
908 static inline void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
909 pp_maybe_wrap_text (pretty_printer *pp, const char *start, const char *end)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
910 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
911 if (pp_is_wrapping_line (pp))
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
912 pp_wrap_text (pp, start, end);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
913 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
914 pp_append_text (pp, start, end);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
915 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
916
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
917 /* Append to the output area of PRETTY-PRINTER a string specified by its
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
918 STARTing character and LENGTH. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
919 static inline void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
920 pp_append_r (pretty_printer *pp, const char *start, int length)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
921 {
111
kono
parents: 67
diff changeset
922 output_buffer_append_r (pp_buffer (pp), start, length);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
923 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
924
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
925 /* Insert enough spaces into the output area of PRETTY-PRINTER to bring
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
926 the column position to the current indentation level, assuming that a
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
927 newline has just been written to the buffer. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
928 void
111
kono
parents: 67
diff changeset
929 pp_indent (pretty_printer *pp)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
930 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
931 int n = pp_indentation (pp);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
932 int i;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
933
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
934 for (i = 0; i < n; ++i)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
935 pp_space (pp);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
936 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
937
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
938 /* The following format specifiers are recognized as being client independent:
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
939 %d, %i: (signed) integer in base ten.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
940 %u: unsigned integer in base ten.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
941 %o: unsigned integer in base eight.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
942 %x: unsigned integer in base sixteen.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
943 %ld, %li, %lo, %lu, %lx: long versions of the above.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
944 %lld, %lli, %llo, %llu, %llx: long long versions.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
945 %wd, %wi, %wo, %wu, %wx: HOST_WIDE_INT versions.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
946 %c: character.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
947 %s: string.
111
kono
parents: 67
diff changeset
948 %p: pointer (printed in a host-dependent manner).
kono
parents: 67
diff changeset
949 %r: if pp_show_color(pp), switch to color identified by const char *.
kono
parents: 67
diff changeset
950 %R: if pp_show_color(pp), reset color.
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
951 %m: strerror(text->err_no) - does not consume a value from args_ptr.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
952 %%: '%'.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
953 %<: opening quote.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
954 %>: closing quote.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
955 %': apostrophe (should only be used in untranslated messages;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
956 translations should use appropriate punctuation directly).
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
957 %.*s: a substring the length of which is specified by an argument
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
958 integer.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
959 %Ns: likewise, but length specified as constant in the format string.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
960 Flag 'q': quote formatted text (must come immediately after '%').
111
kono
parents: 67
diff changeset
961 %Z: Requires two arguments - array of int, and len. Prints elements
kono
parents: 67
diff changeset
962 of the array.
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
963
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
964 Arguments can be used sequentially, or through %N$ resp. *N$
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
965 notation Nth argument after the format string. If %N$ / *N$
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
966 notation is used, it must be used for all arguments, except %m, %%,
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
967 %<, %> and %', which may not have a number, as they do not consume
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
968 an argument. When %M$.*N$s is used, M must be N + 1. (This may
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
969 also be written %M$.*s, provided N is not otherwise used.) The
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
970 format string must have conversion specifiers with argument numbers
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
971 1 up to highest argument; each argument may only be used once.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
972 A format string can have at most 30 arguments. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
973
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
974 /* Formatting phases 1 and 2: render TEXT->format_spec plus
111
kono
parents: 67
diff changeset
975 TEXT->args_ptr into a series of chunks in pp_buffer (PP)->args[].
kono
parents: 67
diff changeset
976 Phase 3 is in pp_output_formatted_text. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
977
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
978 void
111
kono
parents: 67
diff changeset
979 pp_format (pretty_printer *pp, text_info *text)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
980 {
111
kono
parents: 67
diff changeset
981 output_buffer *buffer = pp_buffer (pp);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
982 const char *p;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
983 const char **args;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
984 struct chunk_info *new_chunk_array;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
985
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
986 unsigned int curarg = 0, chunk = 0, argno;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
987 pp_wrapping_mode_t old_wrapping_mode;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
988 bool any_unnumbered = false, any_numbered = false;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
989 const char **formatters[PP_NL_ARGMAX];
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
990
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
991 /* Allocate a new chunk structure. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
992 new_chunk_array = XOBNEW (&buffer->chunk_obstack, struct chunk_info);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
993 new_chunk_array->prev = buffer->cur_chunk_array;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
994 buffer->cur_chunk_array = new_chunk_array;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
995 args = new_chunk_array->args;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
996
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
997 /* Formatting phase 1: split up TEXT->format_spec into chunks in
111
kono
parents: 67
diff changeset
998 pp_buffer (PP)->args[]. Even-numbered chunks are to be output
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
999 verbatim, odd-numbered chunks are format specifiers.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1000 %m, %%, %<, %>, and %' are replaced with the appropriate text at
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1001 this point. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1002
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1003 memset (formatters, 0, sizeof formatters);
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1004
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1005 for (p = text->format_spec; *p; )
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1006 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1007 while (*p != '\0' && *p != '%')
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1008 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1009 obstack_1grow (&buffer->chunk_obstack, *p);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1010 p++;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1011 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1012
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1013 if (*p == '\0')
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1014 break;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1015
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1016 switch (*++p)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1017 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1018 case '\0':
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1019 gcc_unreachable ();
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1020
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1021 case '%':
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1022 obstack_1grow (&buffer->chunk_obstack, '%');
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1023 p++;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1024 continue;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1025
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1026 case '<':
111
kono
parents: 67
diff changeset
1027 {
kono
parents: 67
diff changeset
1028 obstack_grow (&buffer->chunk_obstack,
kono
parents: 67
diff changeset
1029 open_quote, strlen (open_quote));
kono
parents: 67
diff changeset
1030 const char *colorstr
kono
parents: 67
diff changeset
1031 = colorize_start (pp_show_color (pp), "quote");
kono
parents: 67
diff changeset
1032 obstack_grow (&buffer->chunk_obstack, colorstr, strlen (colorstr));
kono
parents: 67
diff changeset
1033 p++;
kono
parents: 67
diff changeset
1034 continue;
kono
parents: 67
diff changeset
1035 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1036
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1037 case '>':
111
kono
parents: 67
diff changeset
1038 {
kono
parents: 67
diff changeset
1039 const char *colorstr = colorize_stop (pp_show_color (pp));
kono
parents: 67
diff changeset
1040 obstack_grow (&buffer->chunk_obstack, colorstr, strlen (colorstr));
kono
parents: 67
diff changeset
1041 }
kono
parents: 67
diff changeset
1042 /* FALLTHRU */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1043 case '\'':
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1044 obstack_grow (&buffer->chunk_obstack,
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1045 close_quote, strlen (close_quote));
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1046 p++;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1047 continue;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1048
111
kono
parents: 67
diff changeset
1049 case 'R':
kono
parents: 67
diff changeset
1050 {
kono
parents: 67
diff changeset
1051 const char *colorstr = colorize_stop (pp_show_color (pp));
kono
parents: 67
diff changeset
1052 obstack_grow (&buffer->chunk_obstack, colorstr,
kono
parents: 67
diff changeset
1053 strlen (colorstr));
kono
parents: 67
diff changeset
1054 p++;
kono
parents: 67
diff changeset
1055 continue;
kono
parents: 67
diff changeset
1056 }
kono
parents: 67
diff changeset
1057
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1058 case 'm':
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1059 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1060 const char *errstr = xstrerror (text->err_no);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1061 obstack_grow (&buffer->chunk_obstack, errstr, strlen (errstr));
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1062 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1063 p++;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1064 continue;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1065
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1066 default:
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1067 /* Handled in phase 2. Terminate the plain chunk here. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1068 obstack_1grow (&buffer->chunk_obstack, '\0');
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1069 gcc_assert (chunk < PP_NL_ARGMAX * 2);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1070 args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1071 break;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1072 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1073
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1074 if (ISDIGIT (*p))
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1075 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1076 char *end;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1077 argno = strtoul (p, &end, 10) - 1;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1078 p = end;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1079 gcc_assert (*p == '$');
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1080 p++;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1081
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1082 any_numbered = true;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1083 gcc_assert (!any_unnumbered);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1084 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1085 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1086 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1087 argno = curarg++;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1088 any_unnumbered = true;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1089 gcc_assert (!any_numbered);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1090 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1091 gcc_assert (argno < PP_NL_ARGMAX);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1092 gcc_assert (!formatters[argno]);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1093 formatters[argno] = &args[chunk];
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1094 do
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1095 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1096 obstack_1grow (&buffer->chunk_obstack, *p);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1097 p++;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1098 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1099 while (strchr ("qwl+#", p[-1]));
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1100
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1101 if (p[-1] == '.')
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1102 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1103 /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1104 (where M == N + 1). */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1105 if (ISDIGIT (*p))
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1106 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1107 do
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1108 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1109 obstack_1grow (&buffer->chunk_obstack, *p);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1110 p++;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1111 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1112 while (ISDIGIT (p[-1]));
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1113 gcc_assert (p[-1] == 's');
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1114 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1115 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1116 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1117 gcc_assert (*p == '*');
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1118 obstack_1grow (&buffer->chunk_obstack, '*');
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1119 p++;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1120
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1121 if (ISDIGIT (*p))
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1122 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1123 char *end;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1124 unsigned int argno2 = strtoul (p, &end, 10) - 1;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1125 p = end;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1126 gcc_assert (argno2 == argno - 1);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1127 gcc_assert (!any_unnumbered);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1128 gcc_assert (*p == '$');
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1129
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1130 p++;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1131 formatters[argno2] = formatters[argno];
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1132 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1133 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1134 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1135 gcc_assert (!any_numbered);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1136 formatters[argno+1] = formatters[argno];
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1137 curarg++;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1138 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1139 gcc_assert (*p == 's');
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1140 obstack_1grow (&buffer->chunk_obstack, 's');
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1141 p++;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1142 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1143 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1144 if (*p == '\0')
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1145 break;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1146
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1147 obstack_1grow (&buffer->chunk_obstack, '\0');
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1148 gcc_assert (chunk < PP_NL_ARGMAX * 2);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1149 args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1150 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1151
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1152 obstack_1grow (&buffer->chunk_obstack, '\0');
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1153 gcc_assert (chunk < PP_NL_ARGMAX * 2);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1154 args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1155 args[chunk] = 0;
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1156
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1157 /* Set output to the argument obstack, and switch line-wrapping and
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1158 prefixing off. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1159 buffer->obstack = &buffer->chunk_obstack;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1160 old_wrapping_mode = pp_set_verbatim_wrapping (pp);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1161
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1162 /* Second phase. Replace each formatter with the formatted text it
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1163 corresponds to. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1164
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1165 for (argno = 0; formatters[argno]; argno++)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1166 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1167 int precision = 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1168 bool wide = false;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1169 bool plus = false;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1170 bool hash = false;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1171 bool quote = false;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1172
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1173 /* We do not attempt to enforce any ordering on the modifier
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1174 characters. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1175
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1176 for (p = *formatters[argno];; p++)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1177 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1178 switch (*p)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1179 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1180 case 'q':
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1181 gcc_assert (!quote);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1182 quote = true;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1183 continue;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1184
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1185 case '+':
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1186 gcc_assert (!plus);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1187 plus = true;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1188 continue;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1189
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1190 case '#':
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1191 gcc_assert (!hash);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1192 hash = true;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1193 continue;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1194
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1195 case 'w':
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1196 gcc_assert (!wide);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1197 wide = true;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1198 continue;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1199
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1200 case 'l':
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1201 /* We don't support precision beyond that of "long long". */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1202 gcc_assert (precision < 2);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1203 precision++;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1204 continue;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1205 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1206 break;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1207 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1208
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1209 gcc_assert (!wide || precision == 0);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1210
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1211 if (quote)
111
kono
parents: 67
diff changeset
1212 {
kono
parents: 67
diff changeset
1213 pp_string (pp, open_quote);
kono
parents: 67
diff changeset
1214 pp_string (pp, colorize_start (pp_show_color (pp), "quote"));
kono
parents: 67
diff changeset
1215 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1216
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1217 switch (*p)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1218 {
111
kono
parents: 67
diff changeset
1219 case 'r':
kono
parents: 67
diff changeset
1220 pp_string (pp, colorize_start (pp_show_color (pp),
kono
parents: 67
diff changeset
1221 va_arg (*text->args_ptr,
kono
parents: 67
diff changeset
1222 const char *)));
kono
parents: 67
diff changeset
1223 break;
kono
parents: 67
diff changeset
1224
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1225 case 'c':
111
kono
parents: 67
diff changeset
1226 {
kono
parents: 67
diff changeset
1227 /* When quoting, print alphanumeric, punctuation, and the space
kono
parents: 67
diff changeset
1228 character unchanged, and all others in hexadecimal with the
kono
parents: 67
diff changeset
1229 "\x" prefix. Otherwise print them all unchanged. */
kono
parents: 67
diff changeset
1230 int chr = va_arg (*text->args_ptr, int);
kono
parents: 67
diff changeset
1231 if (ISPRINT (chr) || !quote)
kono
parents: 67
diff changeset
1232 pp_character (pp, chr);
kono
parents: 67
diff changeset
1233 else
kono
parents: 67
diff changeset
1234 {
kono
parents: 67
diff changeset
1235 const char str [2] = { chr, '\0' };
kono
parents: 67
diff changeset
1236 pp_quoted_string (pp, str, 1);
kono
parents: 67
diff changeset
1237 }
kono
parents: 67
diff changeset
1238 break;
kono
parents: 67
diff changeset
1239 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1240
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1241 case 'd':
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1242 case 'i':
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1243 if (wide)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1244 pp_wide_integer (pp, va_arg (*text->args_ptr, HOST_WIDE_INT));
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1245 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1246 pp_integer_with_precision
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1247 (pp, *text->args_ptr, precision, int, "d");
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1248 break;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1249
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1250 case 'o':
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1251 if (wide)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1252 pp_scalar (pp, "%" HOST_WIDE_INT_PRINT "o",
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1253 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1254 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1255 pp_integer_with_precision
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1256 (pp, *text->args_ptr, precision, unsigned, "o");
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1257 break;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1258
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1259 case 's':
111
kono
parents: 67
diff changeset
1260 if (quote)
kono
parents: 67
diff changeset
1261 pp_quoted_string (pp, va_arg (*text->args_ptr, const char *));
kono
parents: 67
diff changeset
1262 else
kono
parents: 67
diff changeset
1263 pp_string (pp, va_arg (*text->args_ptr, const char *));
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1264 break;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1265
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1266 case 'p':
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1267 pp_pointer (pp, va_arg (*text->args_ptr, void *));
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1268 break;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1269
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1270 case 'u':
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1271 if (wide)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1272 pp_scalar (pp, HOST_WIDE_INT_PRINT_UNSIGNED,
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1273 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1274 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1275 pp_integer_with_precision
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1276 (pp, *text->args_ptr, precision, unsigned, "u");
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1277 break;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1278
111
kono
parents: 67
diff changeset
1279 case 'Z':
kono
parents: 67
diff changeset
1280 {
kono
parents: 67
diff changeset
1281 int *v = va_arg (*text->args_ptr, int *);
kono
parents: 67
diff changeset
1282 unsigned len = va_arg (*text->args_ptr, unsigned);
kono
parents: 67
diff changeset
1283
kono
parents: 67
diff changeset
1284 for (unsigned i = 0; i < len; ++i)
kono
parents: 67
diff changeset
1285 {
kono
parents: 67
diff changeset
1286 pp_scalar (pp, "%i", v[i]);
kono
parents: 67
diff changeset
1287 if (i < len - 1)
kono
parents: 67
diff changeset
1288 {
kono
parents: 67
diff changeset
1289 pp_comma (pp);
kono
parents: 67
diff changeset
1290 pp_space (pp);
kono
parents: 67
diff changeset
1291 }
kono
parents: 67
diff changeset
1292 }
kono
parents: 67
diff changeset
1293 break;
kono
parents: 67
diff changeset
1294 }
kono
parents: 67
diff changeset
1295
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1296 case 'x':
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1297 if (wide)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1298 pp_scalar (pp, HOST_WIDE_INT_PRINT_HEX,
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1299 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1300 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1301 pp_integer_with_precision
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1302 (pp, *text->args_ptr, precision, unsigned, "x");
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1303 break;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1304
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1305 case '.':
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1306 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1307 int n;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1308 const char *s;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1309
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1310 /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1311 (where M == N + 1). The format string should be verified
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1312 already from the first phase. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1313 p++;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1314 if (ISDIGIT (*p))
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1315 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1316 char *end;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1317 n = strtoul (p, &end, 10);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1318 p = end;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1319 gcc_assert (*p == 's');
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1320 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1321 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1322 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1323 gcc_assert (*p == '*');
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1324 p++;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1325 gcc_assert (*p == 's');
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1326 n = va_arg (*text->args_ptr, int);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1327
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1328 /* This consumes a second entry in the formatters array. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1329 gcc_assert (formatters[argno] == formatters[argno+1]);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1330 argno++;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1331 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1332
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1333 s = va_arg (*text->args_ptr, const char *);
111
kono
parents: 67
diff changeset
1334
kono
parents: 67
diff changeset
1335 /* Append the lesser of precision and strlen (s) characters
kono
parents: 67
diff changeset
1336 from the array (which need not be a nul-terminated string).
kono
parents: 67
diff changeset
1337 Negative precision is treated as if it were omitted. */
kono
parents: 67
diff changeset
1338 size_t len = n < 0 ? strlen (s) : strnlen (s, n);
kono
parents: 67
diff changeset
1339
kono
parents: 67
diff changeset
1340 pp_append_text (pp, s, s + len);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1341 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1342 break;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1343
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1344 default:
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1345 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1346 bool ok;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1347
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1348 gcc_assert (pp_format_decoder (pp));
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1349 ok = pp_format_decoder (pp) (pp, text, p,
111
kono
parents: 67
diff changeset
1350 precision, wide, plus, hash, quote,
kono
parents: 67
diff changeset
1351 formatters[argno]);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1352 gcc_assert (ok);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1353 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1354 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1355
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1356 if (quote)
111
kono
parents: 67
diff changeset
1357 {
kono
parents: 67
diff changeset
1358 pp_string (pp, colorize_stop (pp_show_color (pp)));
kono
parents: 67
diff changeset
1359 pp_string (pp, close_quote);
kono
parents: 67
diff changeset
1360 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1361
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1362 obstack_1grow (&buffer->chunk_obstack, '\0');
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1363 *formatters[argno] = XOBFINISH (&buffer->chunk_obstack, const char *);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1364 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1365
111
kono
parents: 67
diff changeset
1366 if (CHECKING_P)
kono
parents: 67
diff changeset
1367 for (; argno < PP_NL_ARGMAX; argno++)
kono
parents: 67
diff changeset
1368 gcc_assert (!formatters[argno]);
kono
parents: 67
diff changeset
1369
kono
parents: 67
diff changeset
1370 /* If the client supplied a postprocessing object, call its "handle"
kono
parents: 67
diff changeset
1371 hook here. */
kono
parents: 67
diff changeset
1372 if (pp->m_format_postprocessor)
kono
parents: 67
diff changeset
1373 pp->m_format_postprocessor->handle (pp);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1374
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1375 /* Revert to normal obstack and wrapping mode. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1376 buffer->obstack = &buffer->formatted_obstack;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1377 buffer->line_length = 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1378 pp_wrapping_mode (pp) = old_wrapping_mode;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1379 pp_clear_state (pp);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1380 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1381
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1382 /* Format of a message pointed to by TEXT. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1383 void
111
kono
parents: 67
diff changeset
1384 pp_output_formatted_text (pretty_printer *pp)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1385 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1386 unsigned int chunk;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1387 output_buffer *buffer = pp_buffer (pp);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1388 struct chunk_info *chunk_array = buffer->cur_chunk_array;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1389 const char **args = chunk_array->args;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1390
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1391 gcc_assert (buffer->obstack == &buffer->formatted_obstack);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1392 gcc_assert (buffer->line_length == 0);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1393
111
kono
parents: 67
diff changeset
1394 /* This is a third phase, first 2 phases done in pp_format_args.
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1395 Now we actually print it. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1396 for (chunk = 0; args[chunk]; chunk++)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1397 pp_string (pp, args[chunk]);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1398
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1399 /* Deallocate the chunk structure and everything after it (i.e. the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1400 associated series of formatted strings). */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1401 buffer->cur_chunk_array = chunk_array->prev;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1402 obstack_free (&buffer->chunk_obstack, chunk_array);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1403 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1404
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1405 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1406 settings needed by BUFFER for a verbatim formatting. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1407 void
111
kono
parents: 67
diff changeset
1408 pp_format_verbatim (pretty_printer *pp, text_info *text)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1409 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1410 /* Set verbatim mode. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1411 pp_wrapping_mode_t oldmode = pp_set_verbatim_wrapping (pp);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1412
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1413 /* Do the actual formatting. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1414 pp_format (pp, text);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1415 pp_output_formatted_text (pp);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1416
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1417 /* Restore previous settings. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1418 pp_wrapping_mode (pp) = oldmode;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1419 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1420
111
kono
parents: 67
diff changeset
1421 /* Flush the content of BUFFER onto the attached stream. This
kono
parents: 67
diff changeset
1422 function does nothing unless pp->output_buffer->flush_p. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1423 void
111
kono
parents: 67
diff changeset
1424 pp_flush (pretty_printer *pp)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1425 {
111
kono
parents: 67
diff changeset
1426 pp_clear_state (pp);
kono
parents: 67
diff changeset
1427 if (!pp->buffer->flush_p)
kono
parents: 67
diff changeset
1428 return;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1429 pp_write_text_to_stream (pp);
111
kono
parents: 67
diff changeset
1430 fflush (pp_buffer (pp)->stream);
kono
parents: 67
diff changeset
1431 }
kono
parents: 67
diff changeset
1432
kono
parents: 67
diff changeset
1433 /* Flush the content of BUFFER onto the attached stream independently
kono
parents: 67
diff changeset
1434 of the value of pp->output_buffer->flush_p. */
kono
parents: 67
diff changeset
1435 void
kono
parents: 67
diff changeset
1436 pp_really_flush (pretty_printer *pp)
kono
parents: 67
diff changeset
1437 {
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1438 pp_clear_state (pp);
111
kono
parents: 67
diff changeset
1439 pp_write_text_to_stream (pp);
kono
parents: 67
diff changeset
1440 fflush (pp_buffer (pp)->stream);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1441 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1442
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1443 /* Sets the number of maximum characters per line PRETTY-PRINTER can
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1444 output in line-wrapping mode. A LENGTH value 0 suppresses
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1445 line-wrapping. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1446 void
111
kono
parents: 67
diff changeset
1447 pp_set_line_maximum_length (pretty_printer *pp, int length)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1448 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1449 pp_line_cutoff (pp) = length;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1450 pp_set_real_maximum_length (pp);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1451 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1452
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1453 /* Clear PRETTY-PRINTER output area text info. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1454 void
111
kono
parents: 67
diff changeset
1455 pp_clear_output_area (pretty_printer *pp)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1456 {
111
kono
parents: 67
diff changeset
1457 obstack_free (pp_buffer (pp)->obstack,
kono
parents: 67
diff changeset
1458 obstack_base (pp_buffer (pp)->obstack));
kono
parents: 67
diff changeset
1459 pp_buffer (pp)->line_length = 0;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1460 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1461
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1462 /* Set PREFIX for PRETTY-PRINTER. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1463 void
111
kono
parents: 67
diff changeset
1464 pp_set_prefix (pretty_printer *pp, const char *prefix)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1465 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1466 pp->prefix = prefix;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1467 pp_set_real_maximum_length (pp);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1468 pp->emitted_prefix = false;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1469 pp_indentation (pp) = 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1470 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1471
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1472 /* Free PRETTY-PRINTER's prefix, a previously malloc()'d string. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1473 void
111
kono
parents: 67
diff changeset
1474 pp_destroy_prefix (pretty_printer *pp)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1475 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1476 if (pp->prefix != NULL)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1477 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1478 free (CONST_CAST (char *, pp->prefix));
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1479 pp->prefix = NULL;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1480 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1481 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1482
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1483 /* Write out PRETTY-PRINTER's prefix. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1484 void
111
kono
parents: 67
diff changeset
1485 pp_emit_prefix (pretty_printer *pp)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1486 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1487 if (pp->prefix != NULL)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1488 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1489 switch (pp_prefixing_rule (pp))
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1490 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1491 default:
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1492 case DIAGNOSTICS_SHOW_PREFIX_NEVER:
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1493 break;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1494
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1495 case DIAGNOSTICS_SHOW_PREFIX_ONCE:
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1496 if (pp->emitted_prefix)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1497 {
111
kono
parents: 67
diff changeset
1498 pp_indent (pp);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1499 break;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1500 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1501 pp_indentation (pp) += 3;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1502 /* Fall through. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1503
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1504 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1505 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1506 int prefix_length = strlen (pp->prefix);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1507 pp_append_r (pp, pp->prefix, prefix_length);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1508 pp->emitted_prefix = true;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1509 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1510 break;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1511 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1512 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1513 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1514
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1515 /* Construct a PRETTY-PRINTER with PREFIX and of MAXIMUM_LENGTH
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1516 characters per line. */
111
kono
parents: 67
diff changeset
1517
kono
parents: 67
diff changeset
1518 pretty_printer::pretty_printer (const char *p, int l)
kono
parents: 67
diff changeset
1519 : buffer (new (XCNEW (output_buffer)) output_buffer ()),
kono
parents: 67
diff changeset
1520 prefix (),
kono
parents: 67
diff changeset
1521 padding (pp_none),
kono
parents: 67
diff changeset
1522 maximum_length (),
kono
parents: 67
diff changeset
1523 indent_skip (),
kono
parents: 67
diff changeset
1524 wrapping (),
kono
parents: 67
diff changeset
1525 format_decoder (),
kono
parents: 67
diff changeset
1526 m_format_postprocessor (NULL),
kono
parents: 67
diff changeset
1527 emitted_prefix (),
kono
parents: 67
diff changeset
1528 need_newline (),
kono
parents: 67
diff changeset
1529 translate_identifiers (true),
kono
parents: 67
diff changeset
1530 show_color ()
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1531 {
111
kono
parents: 67
diff changeset
1532 pp_line_cutoff (this) = l;
kono
parents: 67
diff changeset
1533 /* By default, we emit prefixes once per message. */
kono
parents: 67
diff changeset
1534 pp_prefixing_rule (this) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
kono
parents: 67
diff changeset
1535 pp_set_prefix (this, p);
kono
parents: 67
diff changeset
1536 }
kono
parents: 67
diff changeset
1537
kono
parents: 67
diff changeset
1538 pretty_printer::~pretty_printer ()
kono
parents: 67
diff changeset
1539 {
kono
parents: 67
diff changeset
1540 if (m_format_postprocessor)
kono
parents: 67
diff changeset
1541 delete m_format_postprocessor;
kono
parents: 67
diff changeset
1542 buffer->~output_buffer ();
kono
parents: 67
diff changeset
1543 XDELETE (buffer);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1544 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1545
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1546 /* Append a string delimited by START and END to the output area of
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1547 PRETTY-PRINTER. No line wrapping is done. However, if beginning a
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1548 new line then emit PRETTY-PRINTER's prefix and skip any leading
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1549 whitespace if appropriate. The caller must ensure that it is
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1550 safe to do so. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1551 void
111
kono
parents: 67
diff changeset
1552 pp_append_text (pretty_printer *pp, const char *start, const char *end)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1553 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1554 /* Emit prefix and skip whitespace if we're starting a new line. */
111
kono
parents: 67
diff changeset
1555 if (pp_buffer (pp)->line_length == 0)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1556 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1557 pp_emit_prefix (pp);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1558 if (pp_is_wrapping_line (pp))
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1559 while (start != end && *start == ' ')
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1560 ++start;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1561 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1562 pp_append_r (pp, start, end - start);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1563 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1564
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1565 /* Finishes constructing a NULL-terminated character string representing
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1566 the PRETTY-PRINTED text. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1567 const char *
111
kono
parents: 67
diff changeset
1568 pp_formatted_text (pretty_printer *pp)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1569 {
111
kono
parents: 67
diff changeset
1570 return output_buffer_formatted_text (pp_buffer (pp));
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1571 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1572
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1573 /* Return a pointer to the last character emitted in PRETTY-PRINTER's
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1574 output area. A NULL pointer means no character available. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1575 const char *
111
kono
parents: 67
diff changeset
1576 pp_last_position_in_text (const pretty_printer *pp)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1577 {
111
kono
parents: 67
diff changeset
1578 return output_buffer_last_position_in_text (pp_buffer (pp));
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1579 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1580
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1581 /* Return the amount of characters PRETTY-PRINTER can accept to
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1582 make a full line. Meaningful only in line-wrapping mode. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1583 int
111
kono
parents: 67
diff changeset
1584 pp_remaining_character_count_for_line (pretty_printer *pp)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1585 {
111
kono
parents: 67
diff changeset
1586 return pp->maximum_length - pp_buffer (pp)->line_length;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1587 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1588
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1589
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1590 /* Format a message into BUFFER a la printf. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1591 void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1592 pp_printf (pretty_printer *pp, const char *msg, ...)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1593 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1594 text_info text;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1595 va_list ap;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1596
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1597 va_start (ap, msg);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1598 text.err_no = errno;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1599 text.args_ptr = &ap;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1600 text.format_spec = msg;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1601 pp_format (pp, &text);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1602 pp_output_formatted_text (pp);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1603 va_end (ap);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1604 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1605
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1606
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1607 /* Output MESSAGE verbatim into BUFFER. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1608 void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1609 pp_verbatim (pretty_printer *pp, const char *msg, ...)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1610 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1611 text_info text;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1612 va_list ap;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1613
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1614 va_start (ap, msg);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1615 text.err_no = errno;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1616 text.args_ptr = &ap;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1617 text.format_spec = msg;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1618 pp_format_verbatim (pp, &text);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1619 va_end (ap);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1620 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1621
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1622
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1623
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1624 /* Have PRETTY-PRINTER start a new line. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1625 void
111
kono
parents: 67
diff changeset
1626 pp_newline (pretty_printer *pp)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1627 {
111
kono
parents: 67
diff changeset
1628 obstack_1grow (pp_buffer (pp)->obstack, '\n');
kono
parents: 67
diff changeset
1629 pp_needs_newline (pp) = false;
kono
parents: 67
diff changeset
1630 pp_buffer (pp)->line_length = 0;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1631 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1632
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1633 /* Have PRETTY-PRINTER add a CHARACTER. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1634 void
111
kono
parents: 67
diff changeset
1635 pp_character (pretty_printer *pp, int c)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1636 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1637 if (pp_is_wrapping_line (pp)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1638 && pp_remaining_character_count_for_line (pp) <= 0)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1639 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1640 pp_newline (pp);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1641 if (ISSPACE (c))
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1642 return;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1643 }
111
kono
parents: 67
diff changeset
1644 obstack_1grow (pp_buffer (pp)->obstack, c);
kono
parents: 67
diff changeset
1645 ++pp_buffer (pp)->line_length;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1646 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1647
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1648 /* Append a STRING to the output area of PRETTY-PRINTER; the STRING may
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1649 be line-wrapped if in appropriate mode. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1650 void
111
kono
parents: 67
diff changeset
1651 pp_string (pretty_printer *pp, const char *str)
kono
parents: 67
diff changeset
1652 {
kono
parents: 67
diff changeset
1653 gcc_checking_assert (str);
kono
parents: 67
diff changeset
1654 pp_maybe_wrap_text (pp, str, str + strlen (str));
kono
parents: 67
diff changeset
1655 }
kono
parents: 67
diff changeset
1656
kono
parents: 67
diff changeset
1657 /* Append the leading N characters of STRING to the output area of
kono
parents: 67
diff changeset
1658 PRETTY-PRINTER, quoting in hexadecimal non-printable characters.
kono
parents: 67
diff changeset
1659 Setting N = -1 is as if N were set to strlen (STRING). The STRING
kono
parents: 67
diff changeset
1660 may be line-wrapped if in appropriate mode. */
kono
parents: 67
diff changeset
1661 static void
kono
parents: 67
diff changeset
1662 pp_quoted_string (pretty_printer *pp, const char *str, size_t n /* = -1 */)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1663 {
111
kono
parents: 67
diff changeset
1664 gcc_checking_assert (str);
kono
parents: 67
diff changeset
1665
kono
parents: 67
diff changeset
1666 const char *last = str;
kono
parents: 67
diff changeset
1667 const char *ps;
kono
parents: 67
diff changeset
1668
kono
parents: 67
diff changeset
1669 /* Compute the length if not specified. */
kono
parents: 67
diff changeset
1670 if (n == (size_t) -1)
kono
parents: 67
diff changeset
1671 n = strlen (str);
kono
parents: 67
diff changeset
1672
kono
parents: 67
diff changeset
1673 for (ps = str; n; ++ps, --n)
kono
parents: 67
diff changeset
1674 {
kono
parents: 67
diff changeset
1675 if (ISPRINT (*ps))
kono
parents: 67
diff changeset
1676 continue;
kono
parents: 67
diff changeset
1677
kono
parents: 67
diff changeset
1678 if (last < ps)
kono
parents: 67
diff changeset
1679 pp_maybe_wrap_text (pp, last, ps - 1);
kono
parents: 67
diff changeset
1680
kono
parents: 67
diff changeset
1681 /* Append the hexadecimal value of the character. Allocate a buffer
kono
parents: 67
diff changeset
1682 that's large enough for a 32-bit char plus the hex prefix. */
kono
parents: 67
diff changeset
1683 char buf [11];
kono
parents: 67
diff changeset
1684 int n = sprintf (buf, "\\x%02x", (unsigned char)*ps);
kono
parents: 67
diff changeset
1685 pp_maybe_wrap_text (pp, buf, buf + n);
kono
parents: 67
diff changeset
1686 last = ps + 1;
kono
parents: 67
diff changeset
1687 }
kono
parents: 67
diff changeset
1688
kono
parents: 67
diff changeset
1689 pp_maybe_wrap_text (pp, last, ps);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1690 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1691
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1692 /* Maybe print out a whitespace if needed. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1693
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1694 void
111
kono
parents: 67
diff changeset
1695 pp_maybe_space (pretty_printer *pp)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1696 {
111
kono
parents: 67
diff changeset
1697 if (pp->padding != pp_none)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1698 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1699 pp_space (pp);
111
kono
parents: 67
diff changeset
1700 pp->padding = pp_none;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1701 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1702 }
111
kono
parents: 67
diff changeset
1703
kono
parents: 67
diff changeset
1704 // Add a newline to the pretty printer PP and flush formatted text.
kono
parents: 67
diff changeset
1705
kono
parents: 67
diff changeset
1706 void
kono
parents: 67
diff changeset
1707 pp_newline_and_flush (pretty_printer *pp)
kono
parents: 67
diff changeset
1708 {
kono
parents: 67
diff changeset
1709 pp_newline (pp);
kono
parents: 67
diff changeset
1710 pp_flush (pp);
kono
parents: 67
diff changeset
1711 pp_needs_newline (pp) = false;
kono
parents: 67
diff changeset
1712 }
kono
parents: 67
diff changeset
1713
kono
parents: 67
diff changeset
1714 // Add a newline to the pretty printer PP, followed by indentation.
kono
parents: 67
diff changeset
1715
kono
parents: 67
diff changeset
1716 void
kono
parents: 67
diff changeset
1717 pp_newline_and_indent (pretty_printer *pp, int n)
kono
parents: 67
diff changeset
1718 {
kono
parents: 67
diff changeset
1719 pp_indentation (pp) += n;
kono
parents: 67
diff changeset
1720 pp_newline (pp);
kono
parents: 67
diff changeset
1721 pp_indent (pp);
kono
parents: 67
diff changeset
1722 pp_needs_newline (pp) = false;
kono
parents: 67
diff changeset
1723 }
kono
parents: 67
diff changeset
1724
kono
parents: 67
diff changeset
1725 // Add separator C, followed by a single whitespace.
kono
parents: 67
diff changeset
1726
kono
parents: 67
diff changeset
1727 void
kono
parents: 67
diff changeset
1728 pp_separate_with (pretty_printer *pp, char c)
kono
parents: 67
diff changeset
1729 {
kono
parents: 67
diff changeset
1730 pp_character (pp, c);
kono
parents: 67
diff changeset
1731 pp_space (pp);
kono
parents: 67
diff changeset
1732 }
kono
parents: 67
diff changeset
1733
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1734
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1735 /* The string starting at P has LEN (at least 1) bytes left; if they
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1736 start with a valid UTF-8 sequence, return the length of that
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1737 sequence and set *VALUE to the value of that sequence, and
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1738 otherwise return 0 and set *VALUE to (unsigned int) -1. */
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1739
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1740 static int
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1741 decode_utf8_char (const unsigned char *p, size_t len, unsigned int *value)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1742 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1743 unsigned int t = *p;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1744
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1745 if (len == 0)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1746 abort ();
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1747 if (t & 0x80)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1748 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1749 size_t utf8_len = 0;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1750 unsigned int ch;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1751 size_t i;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1752 for (t = *p; t & 0x80; t <<= 1)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1753 utf8_len++;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1754
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1755 if (utf8_len > len || utf8_len < 2 || utf8_len > 6)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1756 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1757 *value = (unsigned int) -1;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1758 return 0;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1759 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1760 ch = *p & ((1 << (7 - utf8_len)) - 1);
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1761 for (i = 1; i < utf8_len; i++)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1762 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1763 unsigned int u = p[i];
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1764 if ((u & 0xC0) != 0x80)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1765 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1766 *value = (unsigned int) -1;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1767 return 0;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1768 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1769 ch = (ch << 6) | (u & 0x3F);
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1770 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1771 if ( (ch <= 0x7F && utf8_len > 1)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1772 || (ch <= 0x7FF && utf8_len > 2)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1773 || (ch <= 0xFFFF && utf8_len > 3)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1774 || (ch <= 0x1FFFFF && utf8_len > 4)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1775 || (ch <= 0x3FFFFFF && utf8_len > 5)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1776 || (ch >= 0xD800 && ch <= 0xDFFF))
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1777 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1778 *value = (unsigned int) -1;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1779 return 0;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1780 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1781 *value = ch;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1782 return utf8_len;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1783 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1784 else
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1785 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1786 *value = t;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1787 return 1;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1788 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1789 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1790
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1791 /* Allocator for identifier_to_locale and corresponding function to
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1792 free memory. */
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1793
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1794 void *(*identifier_to_locale_alloc) (size_t) = xmalloc;
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1795 void (*identifier_to_locale_free) (void *) = free;
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1796
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1797 /* Given IDENT, an identifier in the internal encoding, return a
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1798 version of IDENT suitable for diagnostics in the locale character
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1799 set: either IDENT itself, or a string, allocated using
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1800 identifier_to_locale_alloc, converted to the locale character set
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1801 and using escape sequences if not representable in the locale
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1802 character set or containing control characters or invalid byte
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1803 sequences. Existing backslashes in IDENT are not doubled, so the
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1804 result may not uniquely specify the contents of an arbitrary byte
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1805 sequence identifier. */
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1806
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1807 const char *
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1808 identifier_to_locale (const char *ident)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1809 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1810 const unsigned char *uid = (const unsigned char *) ident;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1811 size_t idlen = strlen (ident);
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1812 bool valid_printable_utf8 = true;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1813 bool all_ascii = true;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1814 size_t i;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1815
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1816 for (i = 0; i < idlen;)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1817 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1818 unsigned int c;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1819 size_t utf8_len = decode_utf8_char (&uid[i], idlen - i, &c);
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1820 if (utf8_len == 0 || c <= 0x1F || (c >= 0x7F && c <= 0x9F))
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1821 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1822 valid_printable_utf8 = false;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1823 break;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1824 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1825 if (utf8_len > 1)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1826 all_ascii = false;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1827 i += utf8_len;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1828 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1829
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1830 /* If IDENT contains invalid UTF-8 sequences (which may occur with
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1831 attributes putting arbitrary byte sequences in identifiers), or
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1832 control characters, we use octal escape sequences for all bytes
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1833 outside printable ASCII. */
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1834 if (!valid_printable_utf8)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1835 {
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1836 char *ret = (char *) identifier_to_locale_alloc (4 * idlen + 1);
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1837 char *p = ret;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1838 for (i = 0; i < idlen; i++)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1839 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1840 if (uid[i] > 0x1F && uid[i] < 0x7F)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1841 *p++ = uid[i];
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1842 else
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1843 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1844 sprintf (p, "\\%03o", uid[i]);
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1845 p += 4;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1846 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1847 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1848 *p = 0;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1849 return ret;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1850 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1851
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1852 /* Otherwise, if it is valid printable ASCII, or printable UTF-8
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1853 with the locale character set being UTF-8, IDENT is used. */
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1854 if (all_ascii || locale_utf8)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1855 return ident;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1856
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1857 /* Otherwise IDENT is converted to the locale character set if
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1858 possible. */
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1859 #if defined ENABLE_NLS && defined HAVE_LANGINFO_CODESET && HAVE_ICONV
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1860 if (locale_encoding != NULL)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1861 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1862 iconv_t cd = iconv_open (locale_encoding, "UTF-8");
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1863 bool conversion_ok = true;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1864 char *ret = NULL;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1865 if (cd != (iconv_t) -1)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1866 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1867 size_t ret_alloc = 4 * idlen + 1;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1868 for (;;)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1869 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1870 /* Repeat the whole conversion process as needed with
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1871 larger buffers so non-reversible transformations can
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1872 always be detected. */
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1873 ICONV_CONST char *inbuf = CONST_CAST (char *, ident);
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1874 char *outbuf;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1875 size_t inbytesleft = idlen;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1876 size_t outbytesleft = ret_alloc - 1;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1877 size_t iconv_ret;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1878
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1879 ret = (char *) identifier_to_locale_alloc (ret_alloc);
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1880 outbuf = ret;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1881
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1882 if (iconv (cd, 0, 0, 0, 0) == (size_t) -1)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1883 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1884 conversion_ok = false;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1885 break;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1886 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1887
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1888 iconv_ret = iconv (cd, &inbuf, &inbytesleft,
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1889 &outbuf, &outbytesleft);
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1890 if (iconv_ret == (size_t) -1 || inbytesleft != 0)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1891 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1892 if (errno == E2BIG)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1893 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1894 ret_alloc *= 2;
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1895 identifier_to_locale_free (ret);
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1896 ret = NULL;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1897 continue;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1898 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1899 else
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1900 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1901 conversion_ok = false;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1902 break;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1903 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1904 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1905 else if (iconv_ret != 0)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1906 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1907 conversion_ok = false;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1908 break;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1909 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1910 /* Return to initial shift state. */
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1911 if (iconv (cd, 0, 0, &outbuf, &outbytesleft) == (size_t) -1)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1912 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1913 if (errno == E2BIG)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1914 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1915 ret_alloc *= 2;
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1916 identifier_to_locale_free (ret);
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1917 ret = NULL;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1918 continue;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1919 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1920 else
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1921 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1922 conversion_ok = false;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1923 break;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1924 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1925 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1926 *outbuf = 0;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1927 break;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1928 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1929 iconv_close (cd);
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1930 if (conversion_ok)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1931 return ret;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1932 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1933 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1934 #endif
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1935
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1936 /* Otherwise, convert non-ASCII characters in IDENT to UCNs. */
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1937 {
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1938 char *ret = (char *) identifier_to_locale_alloc (10 * idlen + 1);
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1939 char *p = ret;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1940 for (i = 0; i < idlen;)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1941 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1942 unsigned int c;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1943 size_t utf8_len = decode_utf8_char (&uid[i], idlen - i, &c);
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1944 if (utf8_len == 1)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1945 *p++ = uid[i];
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1946 else
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1947 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1948 sprintf (p, "\\U%08x", c);
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1949 p += 10;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1950 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1951 i += utf8_len;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1952 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1953 *p = 0;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1954 return ret;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1955 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1956 }
111
kono
parents: 67
diff changeset
1957
kono
parents: 67
diff changeset
1958 #if CHECKING_P
kono
parents: 67
diff changeset
1959
kono
parents: 67
diff changeset
1960 namespace selftest {
kono
parents: 67
diff changeset
1961
kono
parents: 67
diff changeset
1962 /* Smoketest for pretty_printer. */
kono
parents: 67
diff changeset
1963
kono
parents: 67
diff changeset
1964 static void
kono
parents: 67
diff changeset
1965 test_basic_printing ()
kono
parents: 67
diff changeset
1966 {
kono
parents: 67
diff changeset
1967 pretty_printer pp;
kono
parents: 67
diff changeset
1968 pp_string (&pp, "hello");
kono
parents: 67
diff changeset
1969 pp_space (&pp);
kono
parents: 67
diff changeset
1970 pp_string (&pp, "world");
kono
parents: 67
diff changeset
1971
kono
parents: 67
diff changeset
1972 ASSERT_STREQ ("hello world", pp_formatted_text (&pp));
kono
parents: 67
diff changeset
1973 }
kono
parents: 67
diff changeset
1974
kono
parents: 67
diff changeset
1975 /* Helper function for testing pp_format.
kono
parents: 67
diff changeset
1976 Verify that pp_format (FMT, ...) followed by pp_output_formatted_text
kono
parents: 67
diff changeset
1977 prints EXPECTED, assuming that pp_show_color is SHOW_COLOR. */
kono
parents: 67
diff changeset
1978
kono
parents: 67
diff changeset
1979 static void
kono
parents: 67
diff changeset
1980 assert_pp_format_va (const location &loc, const char *expected,
kono
parents: 67
diff changeset
1981 bool show_color, const char *fmt, va_list *ap)
kono
parents: 67
diff changeset
1982 {
kono
parents: 67
diff changeset
1983 pretty_printer pp;
kono
parents: 67
diff changeset
1984 text_info ti;
kono
parents: 67
diff changeset
1985 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
kono
parents: 67
diff changeset
1986
kono
parents: 67
diff changeset
1987 ti.format_spec = fmt;
kono
parents: 67
diff changeset
1988 ti.args_ptr = ap;
kono
parents: 67
diff changeset
1989 ti.err_no = 0;
kono
parents: 67
diff changeset
1990 ti.x_data = NULL;
kono
parents: 67
diff changeset
1991 ti.m_richloc = &rich_loc;
kono
parents: 67
diff changeset
1992
kono
parents: 67
diff changeset
1993 pp_show_color (&pp) = show_color;
kono
parents: 67
diff changeset
1994 pp_format (&pp, &ti);
kono
parents: 67
diff changeset
1995 pp_output_formatted_text (&pp);
kono
parents: 67
diff changeset
1996 ASSERT_STREQ_AT (loc, expected, pp_formatted_text (&pp));
kono
parents: 67
diff changeset
1997 }
kono
parents: 67
diff changeset
1998
kono
parents: 67
diff changeset
1999 /* Verify that pp_format (FMT, ...) followed by pp_output_formatted_text
kono
parents: 67
diff changeset
2000 prints EXPECTED, with show_color disabled. */
kono
parents: 67
diff changeset
2001
kono
parents: 67
diff changeset
2002 static void
kono
parents: 67
diff changeset
2003 assert_pp_format (const location &loc, const char *expected,
kono
parents: 67
diff changeset
2004 const char *fmt, ...)
kono
parents: 67
diff changeset
2005 {
kono
parents: 67
diff changeset
2006 va_list ap;
kono
parents: 67
diff changeset
2007
kono
parents: 67
diff changeset
2008 va_start (ap, fmt);
kono
parents: 67
diff changeset
2009 assert_pp_format_va (loc, expected, false, fmt, &ap);
kono
parents: 67
diff changeset
2010 va_end (ap);
kono
parents: 67
diff changeset
2011 }
kono
parents: 67
diff changeset
2012
kono
parents: 67
diff changeset
2013 /* As above, but with colorization enabled. */
kono
parents: 67
diff changeset
2014
kono
parents: 67
diff changeset
2015 static void
kono
parents: 67
diff changeset
2016 assert_pp_format_colored (const location &loc, const char *expected,
kono
parents: 67
diff changeset
2017 const char *fmt, ...)
kono
parents: 67
diff changeset
2018 {
kono
parents: 67
diff changeset
2019 /* The tests of colorization assume the default color scheme.
kono
parents: 67
diff changeset
2020 If GCC_COLORS is set, then the colors have potentially been
kono
parents: 67
diff changeset
2021 overridden; skip the test. */
kono
parents: 67
diff changeset
2022 if (getenv ("GCC_COLORS"))
kono
parents: 67
diff changeset
2023 return;
kono
parents: 67
diff changeset
2024
kono
parents: 67
diff changeset
2025 va_list ap;
kono
parents: 67
diff changeset
2026
kono
parents: 67
diff changeset
2027 va_start (ap, fmt);
kono
parents: 67
diff changeset
2028 assert_pp_format_va (loc, expected, true, fmt, &ap);
kono
parents: 67
diff changeset
2029 va_end (ap);
kono
parents: 67
diff changeset
2030 }
kono
parents: 67
diff changeset
2031
kono
parents: 67
diff changeset
2032 /* Helper function for calling testing pp_format,
kono
parents: 67
diff changeset
2033 by calling assert_pp_format with various numbers of arguments.
kono
parents: 67
diff changeset
2034 These exist mostly to avoid having to write SELFTEST_LOCATION
kono
parents: 67
diff changeset
2035 throughout test_pp_format. */
kono
parents: 67
diff changeset
2036
kono
parents: 67
diff changeset
2037 #define ASSERT_PP_FORMAT_1(EXPECTED, FMT, ARG1) \
kono
parents: 67
diff changeset
2038 SELFTEST_BEGIN_STMT \
kono
parents: 67
diff changeset
2039 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
kono
parents: 67
diff changeset
2040 (ARG1)); \
kono
parents: 67
diff changeset
2041 SELFTEST_END_STMT
kono
parents: 67
diff changeset
2042
kono
parents: 67
diff changeset
2043 #define ASSERT_PP_FORMAT_2(EXPECTED, FMT, ARG1, ARG2) \
kono
parents: 67
diff changeset
2044 SELFTEST_BEGIN_STMT \
kono
parents: 67
diff changeset
2045 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
kono
parents: 67
diff changeset
2046 (ARG1), (ARG2)); \
kono
parents: 67
diff changeset
2047 SELFTEST_END_STMT
kono
parents: 67
diff changeset
2048
kono
parents: 67
diff changeset
2049 #define ASSERT_PP_FORMAT_3(EXPECTED, FMT, ARG1, ARG2, ARG3) \
kono
parents: 67
diff changeset
2050 SELFTEST_BEGIN_STMT \
kono
parents: 67
diff changeset
2051 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
kono
parents: 67
diff changeset
2052 (ARG1), (ARG2), (ARG3)); \
kono
parents: 67
diff changeset
2053 SELFTEST_END_STMT
kono
parents: 67
diff changeset
2054
kono
parents: 67
diff changeset
2055 /* Verify that pp_format works, for various format codes. */
kono
parents: 67
diff changeset
2056
kono
parents: 67
diff changeset
2057 static void
kono
parents: 67
diff changeset
2058 test_pp_format ()
kono
parents: 67
diff changeset
2059 {
kono
parents: 67
diff changeset
2060 /* Avoid introducing locale-specific differences in the results
kono
parents: 67
diff changeset
2061 by hardcoding open_quote and close_quote. */
kono
parents: 67
diff changeset
2062 const char *old_open_quote = open_quote;
kono
parents: 67
diff changeset
2063 const char *old_close_quote = close_quote;
kono
parents: 67
diff changeset
2064 open_quote = "`";
kono
parents: 67
diff changeset
2065 close_quote = "'";
kono
parents: 67
diff changeset
2066
kono
parents: 67
diff changeset
2067 /* Verify that plain text is passed through unchanged. */
kono
parents: 67
diff changeset
2068 assert_pp_format (SELFTEST_LOCATION, "unformatted", "unformatted");
kono
parents: 67
diff changeset
2069
kono
parents: 67
diff changeset
2070 /* Verify various individual format codes, in the order listed in the
kono
parents: 67
diff changeset
2071 comment for pp_format above. For each code, we append a second
kono
parents: 67
diff changeset
2072 argument with a known bit pattern (0x12345678), to ensure that we
kono
parents: 67
diff changeset
2073 are consuming arguments correctly. */
kono
parents: 67
diff changeset
2074 ASSERT_PP_FORMAT_2 ("-27 12345678", "%d %x", -27, 0x12345678);
kono
parents: 67
diff changeset
2075 ASSERT_PP_FORMAT_2 ("-5 12345678", "%i %x", -5, 0x12345678);
kono
parents: 67
diff changeset
2076 ASSERT_PP_FORMAT_2 ("10 12345678", "%u %x", 10, 0x12345678);
kono
parents: 67
diff changeset
2077 ASSERT_PP_FORMAT_2 ("17 12345678", "%o %x", 15, 0x12345678);
kono
parents: 67
diff changeset
2078 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%x %x", 0xcafebabe, 0x12345678);
kono
parents: 67
diff changeset
2079 ASSERT_PP_FORMAT_2 ("-27 12345678", "%ld %x", (long)-27, 0x12345678);
kono
parents: 67
diff changeset
2080 ASSERT_PP_FORMAT_2 ("-5 12345678", "%li %x", (long)-5, 0x12345678);
kono
parents: 67
diff changeset
2081 ASSERT_PP_FORMAT_2 ("10 12345678", "%lu %x", (long)10, 0x12345678);
kono
parents: 67
diff changeset
2082 ASSERT_PP_FORMAT_2 ("17 12345678", "%lo %x", (long)15, 0x12345678);
kono
parents: 67
diff changeset
2083 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%lx %x", (long)0xcafebabe,
kono
parents: 67
diff changeset
2084 0x12345678);
kono
parents: 67
diff changeset
2085 ASSERT_PP_FORMAT_2 ("-27 12345678", "%lld %x", (long long)-27, 0x12345678);
kono
parents: 67
diff changeset
2086 ASSERT_PP_FORMAT_2 ("-5 12345678", "%lli %x", (long long)-5, 0x12345678);
kono
parents: 67
diff changeset
2087 ASSERT_PP_FORMAT_2 ("10 12345678", "%llu %x", (long long)10, 0x12345678);
kono
parents: 67
diff changeset
2088 ASSERT_PP_FORMAT_2 ("17 12345678", "%llo %x", (long long)15, 0x12345678);
kono
parents: 67
diff changeset
2089 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%llx %x", (long long)0xcafebabe,
kono
parents: 67
diff changeset
2090 0x12345678);
kono
parents: 67
diff changeset
2091 ASSERT_PP_FORMAT_2 ("-27 12345678", "%wd %x", (HOST_WIDE_INT)-27, 0x12345678);
kono
parents: 67
diff changeset
2092 ASSERT_PP_FORMAT_2 ("-5 12345678", "%wi %x", (HOST_WIDE_INT)-5, 0x12345678);
kono
parents: 67
diff changeset
2093 ASSERT_PP_FORMAT_2 ("10 12345678", "%wu %x", (unsigned HOST_WIDE_INT)10,
kono
parents: 67
diff changeset
2094 0x12345678);
kono
parents: 67
diff changeset
2095 ASSERT_PP_FORMAT_2 ("17 12345678", "%wo %x", (HOST_WIDE_INT)15, 0x12345678);
kono
parents: 67
diff changeset
2096 ASSERT_PP_FORMAT_2 ("0xcafebabe 12345678", "%wx %x", (HOST_WIDE_INT)0xcafebabe,
kono
parents: 67
diff changeset
2097 0x12345678);
kono
parents: 67
diff changeset
2098 ASSERT_PP_FORMAT_2 ("A 12345678", "%c %x", 'A', 0x12345678);
kono
parents: 67
diff changeset
2099 ASSERT_PP_FORMAT_2 ("hello world 12345678", "%s %x", "hello world",
kono
parents: 67
diff changeset
2100 0x12345678);
kono
parents: 67
diff changeset
2101
kono
parents: 67
diff changeset
2102 /* Not nul-terminated. */
kono
parents: 67
diff changeset
2103 char arr[5] = { '1', '2', '3', '4', '5' };
kono
parents: 67
diff changeset
2104 ASSERT_PP_FORMAT_3 ("123 12345678", "%.*s %x", 3, arr, 0x12345678);
kono
parents: 67
diff changeset
2105 ASSERT_PP_FORMAT_3 ("1234 12345678", "%.*s %x", -1, "1234", 0x12345678);
kono
parents: 67
diff changeset
2106 ASSERT_PP_FORMAT_3 ("12345 12345678", "%.*s %x", 7, "12345", 0x12345678);
kono
parents: 67
diff changeset
2107
kono
parents: 67
diff changeset
2108 /* We can't test for %p; the pointer is printed in an implementation-defined
kono
parents: 67
diff changeset
2109 manner. */
kono
parents: 67
diff changeset
2110 ASSERT_PP_FORMAT_2 ("normal colored normal 12345678",
kono
parents: 67
diff changeset
2111 "normal %rcolored%R normal %x",
kono
parents: 67
diff changeset
2112 "error", 0x12345678);
kono
parents: 67
diff changeset
2113 assert_pp_format_colored
kono
parents: 67
diff changeset
2114 (SELFTEST_LOCATION,
kono
parents: 67
diff changeset
2115 "normal \33[01;31m\33[Kcolored\33[m\33[K normal 12345678",
kono
parents: 67
diff changeset
2116 "normal %rcolored%R normal %x", "error", 0x12345678);
kono
parents: 67
diff changeset
2117 /* TODO:
kono
parents: 67
diff changeset
2118 %m: strerror(text->err_no) - does not consume a value from args_ptr. */
kono
parents: 67
diff changeset
2119 ASSERT_PP_FORMAT_1 ("% 12345678", "%% %x", 0x12345678);
kono
parents: 67
diff changeset
2120 ASSERT_PP_FORMAT_1 ("` 12345678", "%< %x", 0x12345678);
kono
parents: 67
diff changeset
2121 ASSERT_PP_FORMAT_1 ("' 12345678", "%> %x", 0x12345678);
kono
parents: 67
diff changeset
2122 ASSERT_PP_FORMAT_1 ("' 12345678", "%' %x", 0x12345678);
kono
parents: 67
diff changeset
2123 ASSERT_PP_FORMAT_3 ("abc 12345678", "%.*s %x", 3, "abcdef", 0x12345678);
kono
parents: 67
diff changeset
2124 ASSERT_PP_FORMAT_2 ("abc 12345678", "%.3s %x", "abcdef", 0x12345678);
kono
parents: 67
diff changeset
2125
kono
parents: 67
diff changeset
2126 /* Verify flag 'q'. */
kono
parents: 67
diff changeset
2127 ASSERT_PP_FORMAT_2 ("`foo' 12345678", "%qs %x", "foo", 0x12345678);
kono
parents: 67
diff changeset
2128 assert_pp_format_colored (SELFTEST_LOCATION,
kono
parents: 67
diff changeset
2129 "`\33[01m\33[Kfoo\33[m\33[K' 12345678", "%qs %x",
kono
parents: 67
diff changeset
2130 "foo", 0x12345678);
kono
parents: 67
diff changeset
2131
kono
parents: 67
diff changeset
2132 /* Verify %Z. */
kono
parents: 67
diff changeset
2133 int v[] = { 1, 2, 3 };
kono
parents: 67
diff changeset
2134 ASSERT_PP_FORMAT_3 ("1, 2, 3 12345678", "%Z %x", v, 3, 0x12345678);
kono
parents: 67
diff changeset
2135
kono
parents: 67
diff changeset
2136 int v2[] = { 0 };
kono
parents: 67
diff changeset
2137 ASSERT_PP_FORMAT_3 ("0 12345678", "%Z %x", v2, 1, 0x12345678);
kono
parents: 67
diff changeset
2138
kono
parents: 67
diff changeset
2139 /* Verify that combinations work, along with unformatted text. */
kono
parents: 67
diff changeset
2140 assert_pp_format (SELFTEST_LOCATION,
kono
parents: 67
diff changeset
2141 "the quick brown fox jumps over the lazy dog",
kono
parents: 67
diff changeset
2142 "the %s %s %s jumps over the %s %s",
kono
parents: 67
diff changeset
2143 "quick", "brown", "fox", "lazy", "dog");
kono
parents: 67
diff changeset
2144 assert_pp_format (SELFTEST_LOCATION, "item 3 of 7", "item %i of %i", 3, 7);
kono
parents: 67
diff changeset
2145 assert_pp_format (SELFTEST_LOCATION, "problem with `bar' at line 10",
kono
parents: 67
diff changeset
2146 "problem with %qs at line %i", "bar", 10);
kono
parents: 67
diff changeset
2147
kono
parents: 67
diff changeset
2148 /* Restore old values of open_quote and close_quote. */
kono
parents: 67
diff changeset
2149 open_quote = old_open_quote;
kono
parents: 67
diff changeset
2150 close_quote = old_close_quote;
kono
parents: 67
diff changeset
2151 }
kono
parents: 67
diff changeset
2152
kono
parents: 67
diff changeset
2153 /* Run all of the selftests within this file. */
kono
parents: 67
diff changeset
2154
kono
parents: 67
diff changeset
2155 void
kono
parents: 67
diff changeset
2156 pretty_print_c_tests ()
kono
parents: 67
diff changeset
2157 {
kono
parents: 67
diff changeset
2158 test_basic_printing ();
kono
parents: 67
diff changeset
2159 test_pp_format ();
kono
parents: 67
diff changeset
2160 }
kono
parents: 67
diff changeset
2161
kono
parents: 67
diff changeset
2162 } // namespace selftest
kono
parents: 67
diff changeset
2163
kono
parents: 67
diff changeset
2164 #endif /* CHECKING_P */