1 /* GCC internal format strings.
2 Copyright (C) 2003-2009 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2003.
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 /* GCC internal format strings consist of language frontend independent
35 format directives, implemented in gcc-4.3.0/gcc/pretty-print.c (function
36 pp_base_format), plus some frontend dependent extensions:
37 - for the C/ObjC frontend
38 in gcc-4.3.0/gcc/c-objc-common.c (function c_tree_printer)
39 - for the C++ frontend
40 in gcc-4.3.0/gcc/cp/error.c (function cp_printer)
41 Taking these together, GCC internal format strings are specified as follows.
45 - either is finished by one of these:
46 - '%', '<', '>', "'", that need no argument,
47 - 'm', that needs no argument but looks at an err_no variable,
48 - or is continued like this:
49 - optionally 'm$' where m is a positive integer,
50 - optionally any number of flags:
52 'l' (up to twice) or 'w' (once only) (exclusive),
55 - finished by a specifier
57 - 'c', that needs a character argument,
58 - 's', that needs a string argument,
59 - '.NNNs', where NNN is a nonempty digit sequence, that needs a
61 - '.*NNN$s' where NNN is a positive integer and NNN = m - 1, that
62 needs a signed integer argument at position NNN and a string
64 - '.*s', that needs a signed integer argument and a string argument,
65 - 'i', 'd', that need a signed integer argument of the specified
67 - 'o', 'u', 'x', that need an unsigned integer argument of the
69 - 'p', that needs a 'void *' argument,
70 - 'H', that needs a 'location_t *' argument,
71 - 'J', that needs a general declaration argument,
72 - 'K', that needs a statement argument,
73 [see gcc/pretty-print.c]
75 - 'D', that needs a general declaration argument,
76 - 'F', that needs a function declaration argument,
77 - 'T', that needs a type argument,
78 - 'E', that needs an expression argument,
79 [see gcc/c-objc-common.c and gcc/cp/error.c]
81 - 'A', that needs a function argument list argument,
82 - 'C', that needs a tree code argument,
83 - 'L', that needs a language argument,
84 - 'O', that needs a binary operator argument,
85 - 'P', that needs a function parameter argument,
86 - 'Q', that needs an assignment operator argument,
87 - 'V', that needs a const/volatile qualifier argument.
90 Numbered ('%m$' or '*m$') and unnumbered argument specifications cannot
91 be used in the same string. */
106 FAT_UNSIGNED = 1 << 4,
107 FAT_SIZE_LONG = 1 << 5,
108 FAT_SIZE_LONGLONG = 2 << 5,
109 FAT_SIZE_WIDE = 3 << 5,
110 FAT_TREE_DECL = 1 << 7,
111 FAT_TREE_STATEMENT = 2 << 7,
112 FAT_TREE_FUNCDECL = 3 << 7,
113 FAT_TREE_TYPE = 4 << 7,
114 FAT_TREE_ARGUMENT = 5 << 7,
115 FAT_TREE_EXPRESSION = 6 << 7,
116 FAT_TREE_CV = 7 << 7,
117 FAT_TREE_CODE_BINOP = 1 << 10,
118 FAT_TREE_CODE_ASSOP = 2 << 10,
119 FAT_FUNCPARAM = 1 << 12,
121 FAT_SIZE_MASK = (FAT_SIZE_LONG | FAT_SIZE_LONGLONG | FAT_SIZE_WIDE)
124 typedef int format_arg_type_t;
126 typedef enum format_arg_type format_arg_type_t;
132 format_arg_type_t type;
137 unsigned int directives;
138 unsigned int numbered_arg_count;
139 unsigned int allocated;
140 struct numbered_arg *numbered;
144 /* Locale independent test for a decimal digit.
145 Argument can be 'char' or 'unsigned char'. (Whereas the argument of
146 <ctype.h> isdigit must be an 'unsigned char'.) */
148 #define isdigit(c) ((unsigned int) ((c) - '0') < 10)
152 numbered_arg_compare (const void *p1, const void *p2)
154 unsigned int n1 = ((const struct numbered_arg *) p1)->number;
155 unsigned int n2 = ((const struct numbered_arg *) p2)->number;
157 return (n1 > n2 ? 1 : n1 < n2 ? -1 : 0);
161 format_parse (const char *format, bool translated, char *fdi,
162 char **invalid_reason)
164 const char *const format_start = format;
166 unsigned int unnumbered_arg_count;
170 spec.numbered_arg_count = 0;
172 spec.numbered = NULL;
173 spec.uses_err_no = false;
174 unnumbered_arg_count = 0;
176 for (; *format != '\0';)
177 if (*format++ == '%')
180 FDI_SET (format - 1, FMTDIR_START);
183 if (*format == '%' || *format == '<' || *format == '>'
186 else if (*format == 'm')
187 spec.uses_err_no = true;
190 unsigned int number = 0;
191 unsigned int flag_q = 0;
192 unsigned int flag_l = 0;
193 unsigned int flag_w = 0;
194 unsigned int flag_plus = 0;
195 unsigned int flag_sharp = 0;
196 format_arg_type_t size;
197 format_arg_type_t type;
199 if (isdigit (*format))
201 const char *f = format;
206 m = 10 * m + (*f - '0');
209 while (isdigit (*f));
215 *invalid_reason = INVALID_ARGNO_0 (spec.directives);
216 FDI_SET (f, FMTDIR_ERROR);
224 /* Parse flags and size. */
235 if (flag_l > 1 || flag_w)
240 if (flag_w > 0 || flag_l)
255 *invalid_reason = xasprintf (_("In the directive number %u, the flags combination is invalid."), spec.directives);
256 FDI_SET (format, FMTDIR_ERROR);
263 size = (flag_l == 2 ? FAT_SIZE_LONGLONG :
264 flag_l == 1 ? FAT_SIZE_LONG :
265 flag_w ? FAT_SIZE_WIDE :
270 else if (*format == 's')
272 else if (*format == '.')
276 if (isdigit (*format))
280 while (isdigit (*format));
286 *invalid_reason = INVALID_UNTERMINATED_DIRECTIVE ();
287 FDI_SET (format - 1, FMTDIR_ERROR);
292 xasprintf (_("In the directive number %u, a precision is not allowed before '%c'."), spec.directives, *format);
293 FDI_SET (format, FMTDIR_ERROR);
300 else if (*format == '*')
302 unsigned int precision_number = 0;
306 if (isdigit (*format))
308 const char *f = format;
313 m = 10 * m + (*f - '0');
316 while (isdigit (*f));
322 *invalid_reason = INVALID_WIDTH_ARGNO_0 (spec.directives);
323 FDI_SET (f, FMTDIR_ERROR);
326 if (unnumbered_arg_count > 0 || number == 0)
328 *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
329 FDI_SET (f, FMTDIR_ERROR);
334 *invalid_reason = xasprintf (_("In the directive number %u, the argument number for the precision must be equal to %u."), spec.directives, number - 1);
335 FDI_SET (f, FMTDIR_ERROR);
338 precision_number = m;
343 if (precision_number)
345 /* Numbered argument. */
347 /* Numbered and unnumbered specifications are exclusive. */
348 if (unnumbered_arg_count > 0)
350 *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
351 FDI_SET (format - 1, FMTDIR_ERROR);
355 if (spec.allocated == spec.numbered_arg_count)
357 spec.allocated = 2 * spec.allocated + 1;
358 spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
360 spec.numbered[spec.numbered_arg_count].number = precision_number;
361 spec.numbered[spec.numbered_arg_count].type = FAT_INTEGER;
362 spec.numbered_arg_count++;
366 /* Unnumbered argument. */
368 /* Numbered and unnumbered specifications are exclusive. */
369 if (spec.numbered_arg_count > 0)
371 *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
372 FDI_SET (format - 1, FMTDIR_ERROR);
376 if (spec.allocated == unnumbered_arg_count)
378 spec.allocated = 2 * spec.allocated + 1;
379 spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
381 spec.numbered[unnumbered_arg_count].number = unnumbered_arg_count + 1;
382 spec.numbered[unnumbered_arg_count].type = FAT_INTEGER;
383 unnumbered_arg_count++;
392 *invalid_reason = INVALID_UNTERMINATED_DIRECTIVE ();
393 FDI_SET (format - 1, FMTDIR_ERROR);
398 xasprintf (_("In the directive number %u, a precision specification is not allowed before '%c'."), spec.directives, *format);
399 FDI_SET (format, FMTDIR_ERROR);
406 *invalid_reason = xasprintf (_("In the directive number %u, the precision specification is invalid."), spec.directives);
407 FDI_SET (*format == '\0' ? format - 1 : format,
412 else if (*format == 'i' || *format == 'd')
413 type = FAT_INTEGER | size;
414 else if (*format == 'o' || *format == 'u' || *format == 'x')
415 type = FAT_INTEGER | FAT_UNSIGNED | size;
416 else if (*format == 'p')
418 else if (*format == 'H')
420 else if (*format == 'J')
421 type = FAT_TREE | FAT_TREE_DECL;
422 else if (*format == 'K')
423 type = FAT_TREE | FAT_TREE_STATEMENT;
427 type = FAT_TREE | FAT_TREE_DECL;
428 else if (*format == 'F')
429 type = FAT_TREE | FAT_TREE_FUNCDECL;
430 else if (*format == 'T')
431 type = FAT_TREE | FAT_TREE_TYPE;
432 else if (*format == 'E')
433 type = FAT_TREE | FAT_TREE_EXPRESSION;
434 else if (*format == 'A')
435 type = FAT_TREE | FAT_TREE_ARGUMENT;
436 else if (*format == 'C')
437 type = FAT_TREE_CODE;
438 else if (*format == 'L')
439 type = FAT_LANGUAGES;
440 else if (*format == 'O')
441 type = FAT_TREE_CODE | FAT_TREE_CODE_BINOP;
442 else if (*format == 'P')
443 type = FAT_INTEGER | FAT_FUNCPARAM;
444 else if (*format == 'Q')
445 type = FAT_TREE_CODE | FAT_TREE_CODE_ASSOP;
446 else if (*format == 'V')
447 type = FAT_TREE | FAT_TREE_CV;
452 *invalid_reason = INVALID_UNTERMINATED_DIRECTIVE ();
453 FDI_SET (format - 1, FMTDIR_ERROR);
460 || *format == 'i' || *format == 'd'
461 || *format == 'o' || *format == 'u' || *format == 'x'
463 ? xasprintf (_("In the directive number %u, flags are not allowed before '%c'."), spec.directives, *format)
464 : INVALID_CONVERSION_SPECIFIER (spec.directives,
466 FDI_SET (format, FMTDIR_ERROR);
474 /* Numbered argument. */
476 /* Numbered and unnumbered specifications are exclusive. */
477 if (unnumbered_arg_count > 0)
479 *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
480 FDI_SET (format, FMTDIR_ERROR);
484 if (spec.allocated == spec.numbered_arg_count)
486 spec.allocated = 2 * spec.allocated + 1;
487 spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
489 spec.numbered[spec.numbered_arg_count].number = number;
490 spec.numbered[spec.numbered_arg_count].type = type;
491 spec.numbered_arg_count++;
495 /* Unnumbered argument. */
497 /* Numbered and unnumbered specifications are exclusive. */
498 if (spec.numbered_arg_count > 0)
500 *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
501 FDI_SET (format, FMTDIR_ERROR);
505 if (spec.allocated == unnumbered_arg_count)
507 spec.allocated = 2 * spec.allocated + 1;
508 spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
510 spec.numbered[unnumbered_arg_count].number = unnumbered_arg_count + 1;
511 spec.numbered[unnumbered_arg_count].type = type;
512 unnumbered_arg_count++;
516 FDI_SET (format, FMTDIR_END);
521 /* Convert the unnumbered argument array to numbered arguments. */
522 if (unnumbered_arg_count > 0)
523 spec.numbered_arg_count = unnumbered_arg_count;
524 /* Sort the numbered argument array, and eliminate duplicates. */
525 else if (spec.numbered_arg_count > 1)
530 qsort (spec.numbered, spec.numbered_arg_count,
531 sizeof (struct numbered_arg), numbered_arg_compare);
533 /* Remove duplicates: Copy from i to j, keeping 0 <= j <= i. */
535 for (i = j = 0; i < spec.numbered_arg_count; i++)
536 if (j > 0 && spec.numbered[i].number == spec.numbered[j-1].number)
538 format_arg_type_t type1 = spec.numbered[i].type;
539 format_arg_type_t type2 = spec.numbered[j-1].type;
540 format_arg_type_t type_both;
546 /* Incompatible types. */
547 type_both = FAT_NONE;
550 INVALID_INCOMPATIBLE_ARG_TYPES (spec.numbered[i].number);
554 spec.numbered[j-1].type = type_both;
560 spec.numbered[j].number = spec.numbered[i].number;
561 spec.numbered[j].type = spec.numbered[i].type;
565 spec.numbered_arg_count = j;
567 /* *invalid_reason has already been set above. */
571 result = XMALLOC (struct spec);
576 if (spec.numbered != NULL)
577 free (spec.numbered);
582 format_free (void *descr)
584 struct spec *spec = (struct spec *) descr;
586 if (spec->numbered != NULL)
587 free (spec->numbered);
592 format_get_number_of_directives (void *descr)
594 struct spec *spec = (struct spec *) descr;
596 return spec->directives;
600 format_check (void *msgid_descr, void *msgstr_descr, bool equality,
601 formatstring_error_logger_t error_logger,
602 const char *pretty_msgid, const char *pretty_msgstr)
604 struct spec *spec1 = (struct spec *) msgid_descr;
605 struct spec *spec2 = (struct spec *) msgstr_descr;
608 if (spec1->numbered_arg_count + spec2->numbered_arg_count > 0)
611 unsigned int n1 = spec1->numbered_arg_count;
612 unsigned int n2 = spec2->numbered_arg_count;
614 /* Check the argument names are the same.
615 Both arrays are sorted. We search for the first difference. */
616 for (i = 0, j = 0; i < n1 || j < n2; )
618 int cmp = (i >= n1 ? 1 :
620 spec1->numbered[i].number > spec2->numbered[j].number ? 1 :
621 spec1->numbered[i].number < spec2->numbered[j].number ? -1 :
627 error_logger (_("a format specification for argument %u, as in '%s', doesn't exist in '%s'"),
628 spec2->numbered[j].number, pretty_msgstr,
638 error_logger (_("a format specification for argument %u doesn't exist in '%s'"),
639 spec1->numbered[i].number, pretty_msgstr);
649 /* Check the argument types are the same. */
651 for (i = 0, j = 0; j < n2; )
653 if (spec1->numbered[i].number == spec2->numbered[j].number)
655 if (spec1->numbered[i].type != spec2->numbered[j].type)
658 error_logger (_("format specifications in '%s' and '%s' for argument %u are not the same"),
659 pretty_msgid, pretty_msgstr,
660 spec2->numbered[j].number);
671 /* Check that the use of err_no is the same. */
672 if (spec1->uses_err_no != spec2->uses_err_no)
676 if (spec1->uses_err_no)
677 error_logger (_("'%s' uses %%m but '%s' doesn't"),
678 pretty_msgid, pretty_msgstr);
680 error_logger (_("'%s' does not use %%m but '%s' uses %%m"),
681 pretty_msgid, pretty_msgstr);
690 struct formatstring_parser formatstring_gcc_internal =
694 format_get_number_of_directives,
702 /* Test program: Print the argument list specification returned by
703 format_parse for strings read from standard input. */
708 format_print (void *descr)
710 struct spec *spec = (struct spec *) descr;
722 for (i = 0; i < spec->numbered_arg_count; i++)
724 unsigned int number = spec->numbered[i].number;
730 for (; last < number; last++)
732 if (spec->numbered[i].type & FAT_UNSIGNED)
733 printf ("[unsigned]");
734 switch (spec->numbered[i].type & FAT_SIZE_MASK)
741 case FAT_SIZE_LONGLONG:
742 printf ("[long long]");
745 printf ("[host-wide]");
750 switch (spec->numbered[i].type & ~(FAT_UNSIGNED | FAT_SIZE_MASK))
755 case FAT_INTEGER | FAT_FUNCPARAM:
770 case FAT_TREE | FAT_TREE_DECL:
773 case FAT_TREE | FAT_TREE_STATEMENT:
776 case FAT_TREE | FAT_TREE_FUNCDECL:
779 case FAT_TREE | FAT_TREE_TYPE:
782 case FAT_TREE | FAT_TREE_ARGUMENT:
785 case FAT_TREE | FAT_TREE_EXPRESSION:
788 case FAT_TREE | FAT_TREE_CV:
794 case FAT_TREE_CODE | FAT_TREE_CODE_BINOP:
797 case FAT_TREE_CODE | FAT_TREE_CODE_ASSOP:
809 if (spec->uses_err_no)
819 size_t line_size = 0;
821 char *invalid_reason;
824 line_len = getline (&line, &line_size, stdin);
827 if (line_len > 0 && line[line_len - 1] == '\n')
828 line[--line_len] = '\0';
830 invalid_reason = NULL;
831 descr = format_parse (line, false, NULL, &invalid_reason);
833 format_print (descr);
836 printf ("%s\n", invalid_reason);
838 free (invalid_reason);
846 * For Emacs M-x compile
848 * 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-gcc-internal.c ../gnulib-lib/libgettextlib.la"