1 /* GFC (GNU Fortran Compiler) internal format strings.
2 Copyright (C) 2003-2009 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2009.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include "xvasprintf.h"
29 #include "format-invalid.h"
32 #define _(str) gettext (str)
34 /* GFC internal format strings consist of format directives that are specific
35 to the GNU Fortran Compiler frontend of GCC, implemented in
36 gcc-4.3.3/gcc/fortran/error.c (function error_print).
40 - either is finished by '%', that needs no argument,
41 - or is continued like this:
42 - optionally 'm$' where m is a positive integer,
43 - finished by a specifier
44 - 'C', that needs no argument but uses a particular variable
45 (but for the purposes of 'm$' numbering it consumes an
46 argument nevertheless, of 'void' type),
47 - 'L', that needs a 'locus *' argument,
48 - 'i', 'd', that need a signed integer argument,
49 - 'u', that needs an unsigned integer argument,
50 - 'li', 'ld', that need a signed long integer argument,
51 - 'lu', that needs an unsigned long integer argument,
52 - 'c', that needs a character argument,
53 - 's', that needs a string argument.
55 Numbered ('%m$') and unnumbered argument specifications can be used in the
56 same string. The effect of '%m$' is to set the current argument number to
57 m. The current argument number is incremented after processing a directive.
59 When numbered argument specifications are used, specifying the Nth argument
60 requires that all the leading arguments, from the first to the (N-1)th, are
61 specified in the format string. */
73 FAT_UNSIGNED = 1 << 3,
74 FAT_SIZE_LONG = 1 << 4,
76 FAT_SIZE_MASK = FAT_SIZE_LONG
79 typedef int format_arg_type_t;
81 typedef enum format_arg_type format_arg_type_t;
87 format_arg_type_t type;
92 format_arg_type_t type;
97 unsigned int directives;
98 unsigned int unnumbered_arg_count;
99 struct unnumbered_arg *unnumbered;
100 bool uses_currentloc;
103 /* Locale independent test for a decimal digit.
104 Argument can be 'char' or 'unsigned char'. (Whereas the argument of
105 <ctype.h> isdigit must be an 'unsigned char'.) */
107 #define isdigit(c) ((unsigned int) ((c) - '0') < 10)
111 numbered_arg_compare (const void *p1, const void *p2)
113 unsigned int n1 = ((const struct numbered_arg *) p1)->number;
114 unsigned int n2 = ((const struct numbered_arg *) p2)->number;
116 return (n1 > n2 ? 1 : n1 < n2 ? -1 : 0);
120 format_parse (const char *format, bool translated, char *fdi,
121 char **invalid_reason)
123 const char *const format_start = format;
125 unsigned int numbered_arg_count;
126 unsigned int allocated;
127 struct numbered_arg *numbered;
132 numbered_arg_count = 0;
135 spec.uses_currentloc = false;
138 for (; *format != '\0';)
139 if (*format++ == '%')
142 FDI_SET (format - 1, FMTDIR_START);
147 format_arg_type_t type;
149 if (isdigit (*format))
151 const char *f = format;
156 m = 10 * m + (*f - '0');
159 while (isdigit (*f));
165 *invalid_reason = INVALID_ARGNO_0 (spec.directives);
166 FDI_SET (f, FMTDIR_ERROR);
177 spec.uses_currentloc = true;
179 else if (*format == 'L')
181 else if (*format == 'c')
183 else if (*format == 's')
187 format_arg_type_t size = 0;
192 size = FAT_SIZE_LONG;
195 if (*format == 'i' || *format == 'd')
196 type = FAT_INTEGER | size;
197 else if (*format == 'u')
198 type = FAT_INTEGER | FAT_UNSIGNED | size;
203 *invalid_reason = INVALID_UNTERMINATED_DIRECTIVE ();
204 FDI_SET (format - 1, FMTDIR_ERROR);
209 INVALID_CONVERSION_SPECIFIER (spec.directives, *format);
210 FDI_SET (format, FMTDIR_ERROR);
216 if (allocated == numbered_arg_count)
218 allocated = 2 * allocated + 1;
219 numbered = (struct numbered_arg *) xrealloc (numbered, allocated * sizeof (struct numbered_arg));
221 numbered[numbered_arg_count].number = number;
222 numbered[numbered_arg_count].type = type;
223 numbered_arg_count++;
228 FDI_SET (format, FMTDIR_END);
233 /* Sort the numbered argument array, and eliminate duplicates. */
234 if (numbered_arg_count > 1)
239 qsort (numbered, numbered_arg_count,
240 sizeof (struct numbered_arg), numbered_arg_compare);
242 /* Remove duplicates: Copy from i to j, keeping 0 <= j <= i. */
244 for (i = j = 0; i < numbered_arg_count; i++)
245 if (j > 0 && numbered[i].number == numbered[j-1].number)
247 format_arg_type_t type1 = numbered[i].type;
248 format_arg_type_t type2 = numbered[j-1].type;
249 format_arg_type_t type_both;
255 /* Incompatible types. */
256 type_both = FAT_NONE;
259 INVALID_INCOMPATIBLE_ARG_TYPES (numbered[i].number);
263 numbered[j-1].type = type_both;
269 numbered[j].number = numbered[i].number;
270 numbered[j].type = numbered[i].type;
274 numbered_arg_count = j;
276 /* *invalid_reason has already been set above. */
280 /* Verify that the format strings uses all arguments up to the highest
285 for (i = 0; i < numbered_arg_count; i++)
286 if (numbered[i].number != i + 1)
289 xasprintf (_("The string refers to argument number %u but ignores argument number %u."), numbered[i].number, i + 1);
294 /* So now the numbered arguments array is equivalent to a sequence
295 of unnumbered arguments. Eliminate the FAT_VOID placeholders. */
299 spec.unnumbered_arg_count = 0;
300 for (i = 0; i < numbered_arg_count; i++)
301 if (numbered[i].type != FAT_VOID)
302 spec.unnumbered_arg_count++;
304 if (spec.unnumbered_arg_count > 0)
308 spec.unnumbered = XNMALLOC (spec.unnumbered_arg_count, struct unnumbered_arg);
310 for (i = 0; i < numbered_arg_count; i++)
311 if (numbered[i].type != FAT_VOID)
312 spec.unnumbered[j++].type = numbered[i].type;
315 spec.unnumbered = NULL;
319 result = XMALLOC (struct spec);
324 if (numbered != NULL)
330 format_free (void *descr)
332 struct spec *spec = (struct spec *) descr;
334 if (spec->unnumbered != NULL)
335 free (spec->unnumbered);
340 format_get_number_of_directives (void *descr)
342 struct spec *spec = (struct spec *) descr;
344 return spec->directives;
348 format_check (void *msgid_descr, void *msgstr_descr, bool equality,
349 formatstring_error_logger_t error_logger,
350 const char *pretty_msgid, const char *pretty_msgstr)
352 struct spec *spec1 = (struct spec *) msgid_descr;
353 struct spec *spec2 = (struct spec *) msgstr_descr;
357 /* Check the argument types are the same. */
359 ? spec1->unnumbered_arg_count != spec2->unnumbered_arg_count
360 : spec1->unnumbered_arg_count < spec2->unnumbered_arg_count)
363 error_logger (_("number of format specifications in '%s' and '%s' does not match"),
364 pretty_msgid, pretty_msgstr);
368 for (i = 0; i < spec2->unnumbered_arg_count; i++)
369 if (spec1->unnumbered[i].type != spec2->unnumbered[i].type)
372 error_logger (_("format specifications in '%s' and '%s' for argument %u are not the same"),
373 pretty_msgid, pretty_msgstr, i + 1);
377 /* Check that the use of currentloc is the same. */
378 if (spec1->uses_currentloc != spec2->uses_currentloc)
382 if (spec1->uses_currentloc)
383 error_logger (_("'%s' uses %%C but '%s' doesn't"),
384 pretty_msgid, pretty_msgstr);
386 error_logger (_("'%s' does not use %%C but '%s' uses %%C"),
387 pretty_msgid, pretty_msgstr);
396 struct formatstring_parser formatstring_gfc_internal =
400 format_get_number_of_directives,
408 /* Test program: Print the argument list specification returned by
409 format_parse for strings read from standard input. */
414 format_print (void *descr)
416 struct spec *spec = (struct spec *) descr;
426 for (i = 0; i < spec->unnumbered_arg_count; i++)
430 if (spec->unnumbered[i].type & FAT_UNSIGNED)
431 printf ("[unsigned]");
432 switch (spec->unnumbered[i].type & FAT_SIZE_MASK)
442 switch (spec->unnumbered[i].type & ~(FAT_UNSIGNED | FAT_SIZE_MASK))
461 if (spec->uses_currentloc)
471 size_t line_size = 0;
473 char *invalid_reason;
476 line_len = getline (&line, &line_size, stdin);
479 if (line_len > 0 && line[line_len - 1] == '\n')
480 line[--line_len] = '\0';
482 invalid_reason = NULL;
483 descr = format_parse (line, false, NULL, &invalid_reason);
485 format_print (descr);
488 printf ("%s\n", invalid_reason);
490 free (invalid_reason);
498 * For Emacs M-x compile
500 * compile-command: "/bin/sh ../libtool --tag=CC --mode=link gcc -o a.out -static -O -g -Wall -I.. -I../gnulib-lib -I../intl -DHAVE_CONFIG_H -DTEST format-gfc-internal.c ../gnulib-lib/libgettextlib.la"