~ubuntu-branches/ubuntu/breezy/gettext/breezy

« back to all changes in this revision

Viewing changes to gettext-tools/src/format-gcc-internal.c

  • Committer: Bazaar Package Importer
  • Author(s): Santiago Vila
  • Date: 2004-03-14 17:40:02 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040314174002-p1ad5ldve1hqzhye
Tags: 0.14.1-2
* Added libexpat1-dev to Build-Depends, for glade support.
* Added libc0.1-dev to Build-Depends, for GNU/kFreeBSD.
* Removed special-casing of knetbsd-gnu in debian/rules.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GCC internal format strings.
 
2
   Copyright (C) 2003-2004 Free Software Foundation, Inc.
 
3
   Written by Bruno Haible <bruno@clisp.org>, 2003.
 
4
 
 
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 2, or (at your option)
 
8
   any later version.
 
9
 
 
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.
 
14
 
 
15
   You should have received a copy of the GNU General Public License
 
16
   along with this program; if not, write to the Free Software Foundation,
 
17
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
18
 
 
19
#ifdef HAVE_CONFIG_H
 
20
# include <config.h>
 
21
#endif
 
22
 
 
23
#include <stdbool.h>
 
24
#include <stdlib.h>
 
25
 
 
26
#include "format.h"
 
27
#include "c-ctype.h"
 
28
#include "xalloc.h"
 
29
#include "xerror.h"
 
30
#include "format-invalid.h"
 
31
#include "error.h"
 
32
#include "error-progname.h"
 
33
#include "gettext.h"
 
34
 
 
35
#define _(str) gettext (str)
 
36
 
 
37
/* GCC internal format strings consist of language frontend independent
 
38
   format directives, implemented in gcc-3.3/gcc/diagnostic.c (function
 
39
   output_format), plus some frontend dependent extensions:
 
40
     - for the C/ObjC frontend in gcc-3.3/gcc/c-objc-common.c
 
41
     - for the C++ frontend in gcc-3.3/gcc/cp/error.c
 
42
   Taking these together, GCC internal format strings are specified as follows.
 
43
   A directive
 
44
   - starts with '%',
 
45
   - is optionally followed by a size specifier 'l',
 
46
   - is optionally followed by '+' (only the specifiers of gcc/cp/error.c),
 
47
   - is optionally followed by '#' (only the specifiers of gcc/cp/error.c),
 
48
   - is finished by a specifier
 
49
 
 
50
       - '%', that needs no argument,
 
51
       - 'c', that needs a character argument,
 
52
       - 's', that needs a string argument,
 
53
       - 'i', 'd', that need a signed integer argument,
 
54
       - 'o', 'u', 'x', that need an unsigned integer argument,
 
55
       - '.*s', that needs a signed integer argument and a string argument,
 
56
       - 'H', that needs a 'location_t *' argument,
 
57
         [see gcc/diagnostic.c]
 
58
 
 
59
       - 'D', that needs a general declaration argument,
 
60
       - 'F', that needs a function declaration argument,
 
61
       - 'T', that needs a type argument,
 
62
         [see gcc/c-objc-common.c and gcc/cp/error.c]
 
63
 
 
64
       - 'A', that needs a function argument list argument,
 
65
       - 'C', that needs a tree code argument,
 
66
       - 'E', that needs an expression argument,
 
67
       - 'L', that needs a language argument,
 
68
       - 'O', that needs a binary operator argument,
 
69
       - 'P', that needs a function parameter argument,
 
70
       - 'Q', that needs an assignment operator argument,
 
71
       - 'V', that needs a const/volatile qualifier argument.
 
72
         [see gcc/cp/error.c]
 
73
 */
 
74
 
 
75
enum format_arg_type
 
76
{
 
77
  FAT_NONE              = 0,
 
78
  /* Basic types */
 
79
  FAT_INTEGER           = 1,
 
80
  FAT_CHAR              = 2,
 
81
  FAT_STRING            = 3,
 
82
  FAT_LOCATION          = 4,
 
83
  FAT_TREE              = 5,
 
84
  FAT_TREE_CODE         = 6,
 
85
  FAT_LANGUAGES         = 7,
 
86
  /* Flags */
 
87
  FAT_UNSIGNED          = 1 << 3,
 
88
  FAT_SIZE_LONG         = 1 << 4,
 
89
  FAT_TREE_DECL         = 1 << 5,
 
90
  FAT_TREE_FUNCDECL     = 2 << 5,
 
91
  FAT_TREE_TYPE         = 3 << 5,
 
92
  FAT_TREE_ARGUMENT     = 4 << 5,
 
93
  FAT_TREE_EXPRESSION   = 5 << 5,
 
94
  FAT_TREE_CV           = 6 << 5,
 
95
  FAT_TREE_CODE_BINOP   = 1 << 8,
 
96
  FAT_TREE_CODE_ASSOP   = 2 << 8,
 
97
  FAT_FUNCPARAM         = 1 << 10
 
98
};
 
99
 
 
100
struct unnumbered_arg
 
101
{
 
102
  enum format_arg_type type;
 
103
};
 
104
 
 
105
struct spec
 
106
{
 
107
  unsigned int directives;
 
108
  unsigned int unnumbered_arg_count;
 
109
  unsigned int allocated;
 
110
  struct unnumbered_arg *unnumbered;
 
111
};
 
112
 
 
113
 
 
114
static void *
 
115
format_parse (const char *format, bool translated, char **invalid_reason)
 
116
{
 
117
  struct spec spec;
 
118
  struct spec *result;
 
119
 
 
120
  spec.directives = 0;
 
121
  spec.unnumbered_arg_count = 0;
 
122
  spec.allocated = 0;
 
123
  spec.unnumbered = NULL;
 
124
 
 
125
  for (; *format != '\0';)
 
126
    if (*format++ == '%')
 
127
      {
 
128
        /* A directive.  */
 
129
        enum format_arg_type size;
 
130
 
 
131
        spec.directives++;
 
132
 
 
133
        /* Parse size.  */
 
134
        size = 0;
 
135
        if (*format == 'l')
 
136
          {
 
137
            format++;
 
138
            size = FAT_SIZE_LONG;
 
139
          }
 
140
 
 
141
        if (*format != '%')
 
142
          {
 
143
            enum format_arg_type type;
 
144
 
 
145
            if (*format == 'c')
 
146
              type = FAT_CHAR;
 
147
            else if (*format == 's')
 
148
              type = FAT_STRING;
 
149
            else if (*format == 'i' || *format == 'd')
 
150
              type = FAT_INTEGER | size;
 
151
            else if (*format == 'o' || *format == 'u' || *format == 'x')
 
152
              type = FAT_INTEGER | FAT_UNSIGNED | size;
 
153
            else if (*format == '.' && format[1] == '*' && format[2] == 's')
 
154
              {
 
155
                if (spec.allocated == spec.unnumbered_arg_count)
 
156
                  {
 
157
                    spec.allocated = 2 * spec.allocated + 1;
 
158
                    spec.unnumbered = (struct unnumbered_arg *) xrealloc (spec.unnumbered, spec.allocated * sizeof (struct unnumbered_arg));
 
159
                  }
 
160
                spec.unnumbered[spec.unnumbered_arg_count].type = FAT_INTEGER;
 
161
                spec.unnumbered_arg_count++;
 
162
                type = FAT_STRING;
 
163
              }
 
164
            else if (*format == 'H')
 
165
              type = FAT_LOCATION;
 
166
            else
 
167
              {
 
168
                if (*format == '+')
 
169
                  format++;
 
170
                if (*format == '#')
 
171
                  format++;
 
172
                if (*format == 'D')
 
173
                  type = FAT_TREE | FAT_TREE_DECL;
 
174
                else if (*format == 'F')
 
175
                  type = FAT_TREE | FAT_TREE_FUNCDECL;
 
176
                else if (*format == 'T')
 
177
                  type = FAT_TREE | FAT_TREE_TYPE;
 
178
                else if (*format == 'A')
 
179
                  type = FAT_TREE | FAT_TREE_ARGUMENT;
 
180
                else if (*format == 'C')
 
181
                  type = FAT_TREE_CODE;
 
182
                else if (*format == 'E')
 
183
                  type = FAT_TREE | FAT_TREE_EXPRESSION;
 
184
                else if (*format == 'L')
 
185
                  type = FAT_LANGUAGES;
 
186
                else if (*format == 'O')
 
187
                  type = FAT_TREE_CODE | FAT_TREE_CODE_BINOP;
 
188
                else if (*format == 'P')
 
189
                  type = FAT_INTEGER | FAT_FUNCPARAM;
 
190
                else if (*format == 'Q')
 
191
                  type = FAT_TREE_CODE | FAT_TREE_CODE_ASSOP;
 
192
                else if (*format == 'V')
 
193
                  type = FAT_TREE | FAT_TREE_CV;
 
194
                else
 
195
                  {
 
196
                    *invalid_reason =
 
197
                      (*format == '\0'
 
198
                       ? INVALID_UNTERMINATED_DIRECTIVE ()
 
199
                       : (*format == 'c'
 
200
                          || *format == 's'
 
201
                          || *format == 'i' || *format == 'd'
 
202
                          || *format == 'o' || *format == 'u' || *format == 'x'
 
203
                          || *format == 'H'
 
204
                          ? xasprintf (_("In the directive number %u, flags are not allowed before '%c'."), spec.directives, *format)
 
205
                          : INVALID_CONVERSION_SPECIFIER (spec.directives,
 
206
                                                          *format)));
 
207
                    goto bad_format;
 
208
                  }
 
209
              }
 
210
 
 
211
            if (spec.allocated == spec.unnumbered_arg_count)
 
212
              {
 
213
                spec.allocated = 2 * spec.allocated + 1;
 
214
                spec.unnumbered = (struct unnumbered_arg *) xrealloc (spec.unnumbered, spec.allocated * sizeof (struct unnumbered_arg));
 
215
              }
 
216
            spec.unnumbered[spec.unnumbered_arg_count].type = type;
 
217
            spec.unnumbered_arg_count++;
 
218
          }
 
219
 
 
220
        format++;
 
221
      }
 
222
 
 
223
  result = (struct spec *) xmalloc (sizeof (struct spec));
 
224
  *result = spec;
 
225
  return result;
 
226
 
 
227
 bad_format:
 
228
  if (spec.unnumbered != NULL)
 
229
    free (spec.unnumbered);
 
230
  return NULL;
 
231
}
 
232
 
 
233
static void
 
234
format_free (void *descr)
 
235
{
 
236
  struct spec *spec = (struct spec *) descr;
 
237
 
 
238
  if (spec->unnumbered != NULL)
 
239
    free (spec->unnumbered);
 
240
  free (spec);
 
241
}
 
242
 
 
243
static int
 
244
format_get_number_of_directives (void *descr)
 
245
{
 
246
  struct spec *spec = (struct spec *) descr;
 
247
 
 
248
  return spec->directives;
 
249
}
 
250
 
 
251
static bool
 
252
format_check (const lex_pos_ty *pos, void *msgid_descr, void *msgstr_descr,
 
253
              bool equality, bool noisy, const char *pretty_msgstr)
 
254
{
 
255
  struct spec *spec1 = (struct spec *) msgid_descr;
 
256
  struct spec *spec2 = (struct spec *) msgstr_descr;
 
257
  bool err = false;
 
258
  unsigned int i;
 
259
 
 
260
  /* Check the argument types are the same.  */
 
261
  if (equality
 
262
      ? spec1->unnumbered_arg_count != spec2->unnumbered_arg_count
 
263
      : spec1->unnumbered_arg_count < spec2->unnumbered_arg_count)
 
264
    {
 
265
      if (noisy)
 
266
        {
 
267
          error_with_progname = false;
 
268
          error_at_line (0, 0, pos->file_name, pos->line_number,
 
269
                         _("number of format specifications in 'msgid' and '%s' does not match"),
 
270
                         pretty_msgstr);
 
271
          error_with_progname = true;
 
272
        }
 
273
      err = true;
 
274
    }
 
275
  else
 
276
    for (i = 0; i < spec2->unnumbered_arg_count; i++)
 
277
      if (spec1->unnumbered[i].type != spec2->unnumbered[i].type)
 
278
        {
 
279
          if (noisy)
 
280
            {
 
281
              error_with_progname = false;
 
282
              error_at_line (0, 0, pos->file_name, pos->line_number,
 
283
                             _("format specifications in 'msgid' and '%s' for argument %u are not the same"),
 
284
                             pretty_msgstr, i + 1);
 
285
              error_with_progname = true;
 
286
            }
 
287
          err = true;
 
288
        }
 
289
 
 
290
  return err;
 
291
}
 
292
 
 
293
 
 
294
struct formatstring_parser formatstring_gcc_internal =
 
295
{
 
296
  format_parse,
 
297
  format_free,
 
298
  format_get_number_of_directives,
 
299
  format_check
 
300
};
 
301
 
 
302
 
 
303
#ifdef TEST
 
304
 
 
305
/* Test program: Print the argument list specification returned by
 
306
   format_parse for strings read from standard input.  */
 
307
 
 
308
#include <stdio.h>
 
309
#include "getline.h"
 
310
 
 
311
static void
 
312
format_print (void *descr)
 
313
{
 
314
  struct spec *spec = (struct spec *) descr;
 
315
  unsigned int i;
 
316
 
 
317
  if (spec == NULL)
 
318
    {
 
319
      printf ("INVALID");
 
320
      return;
 
321
    }
 
322
 
 
323
  printf ("(");
 
324
  for (i = 0; i < spec->unnumbered_arg_count; i++)
 
325
    {
 
326
      if (i > 0)
 
327
        printf (" ");
 
328
      if (spec->unnumbered[i].type & FAT_UNSIGNED)
 
329
        printf ("[unsigned]");
 
330
      if (spec->unnumbered[i].type & FAT_SIZE_LONG)
 
331
        printf ("[long]");
 
332
      switch (spec->unnumbered[i].type & ~(FAT_UNSIGNED | FAT_SIZE_LONG))
 
333
        {
 
334
        case FAT_INTEGER:
 
335
          printf ("i");
 
336
          break;
 
337
        case FAT_INTEGER | FAT_FUNCPARAM:
 
338
          printf ("P");
 
339
          break;
 
340
        case FAT_CHAR:
 
341
          printf ("c");
 
342
          break;
 
343
        case FAT_STRING:
 
344
          printf ("s");
 
345
          break;
 
346
        case FAT_LOCATION:
 
347
          printf ("H");
 
348
          break;
 
349
        case FAT_TREE | FAT_TREE_DECL:
 
350
          printf ("D");
 
351
          break;
 
352
        case FAT_TREE | FAT_TREE_FUNCDECL:
 
353
          printf ("F");
 
354
          break;
 
355
        case FAT_TREE | FAT_TREE_TYPE:
 
356
          printf ("T");
 
357
          break;
 
358
        case FAT_TREE | FAT_TREE_ARGUMENT:
 
359
          printf ("A");
 
360
          break;
 
361
        case FAT_TREE | FAT_TREE_EXPRESSION:
 
362
          printf ("E");
 
363
          break;
 
364
        case FAT_TREE | FAT_TREE_CV:
 
365
          printf ("V");
 
366
          break;
 
367
        case FAT_TREE_CODE:
 
368
          printf ("C");
 
369
          break;
 
370
        case FAT_TREE_CODE | FAT_TREE_CODE_BINOP:
 
371
          printf ("O");
 
372
          break;
 
373
        case FAT_TREE_CODE | FAT_TREE_CODE_ASSOP:
 
374
          printf ("Q");
 
375
          break;
 
376
        case FAT_LANGUAGES:
 
377
          printf ("L");
 
378
          break;
 
379
        default:
 
380
          abort ();
 
381
        }
 
382
    }
 
383
  printf (")");
 
384
}
 
385
 
 
386
int
 
387
main ()
 
388
{
 
389
  for (;;)
 
390
    {
 
391
      char *line = NULL;
 
392
      size_t line_size = 0;
 
393
      int line_len;
 
394
      char *invalid_reason;
 
395
      void *descr;
 
396
 
 
397
      line_len = getline (&line, &line_size, stdin);
 
398
      if (line_len < 0)
 
399
        break;
 
400
      if (line_len > 0 && line[line_len - 1] == '\n')
 
401
        line[--line_len] = '\0';
 
402
 
 
403
      invalid_reason = NULL;
 
404
      descr = format_parse (line, false, &invalid_reason);
 
405
 
 
406
      format_print (descr);
 
407
      printf ("\n");
 
408
      if (descr == NULL)
 
409
        printf ("%s\n", invalid_reason);
 
410
 
 
411
      free (invalid_reason);
 
412
      free (line);
 
413
    }
 
414
 
 
415
  return 0;
 
416
}
 
417
 
 
418
/*
 
419
 * For Emacs M-x compile
 
420
 * Local Variables:
 
421
 * compile-command: "/bin/sh ../libtool --mode=link gcc -o a.out -static -O -g -Wall -I.. -I../lib -I../intl -DHAVE_CONFIG_H -DTEST format-gcc-internal.c ../lib/libgettextlib.la"
 
422
 * End:
 
423
 */
 
424
 
 
425
#endif /* TEST */