]> git.saurik.com Git - bison.git/blame - src/muscle_tab.c
Fix push parsing memory leak reported by Brandon Lucia at
[bison.git] / src / muscle_tab.c
CommitLineData
e00b6826
PE
1/* Muscle table manager for Bison.
2
279cabb6 3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
05ac60f3 4 Foundation, Inc.
f753cd62
MA
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
8 Bison is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 Bison is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Bison; see the file COPYING. If not, write to
0fb669f9
PE
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
f753cd62 22
2cec9080 23#include <config.h>
f753cd62 24#include "system.h"
8322e8f5
PE
25
26#include <hash.h>
27#include <quotearg.h>
28
9611cfa2 29#include "complain.h"
f753cd62
MA
30#include "files.h"
31#include "muscle_tab.h"
f508cb0a 32#include "getargs.h"
f753cd62 33
eb095650
PE
34/* A key-value pair, along with storage that can be reclaimed when
35 this pair is no longer needed. */
8322e8f5
PE
36typedef struct
37{
eb095650
PE
38 char const *key;
39 char const *value;
40 char *storage;
8322e8f5 41} muscle_entry;
592e8d4d
AD
42
43/* An obstack used to create some entries. */
44struct obstack muscle_obstack;
45
beda758b
AD
46/* Initial capacity of muscles hash table. */
47#define HT_INITIAL_CAPACITY 257
f753cd62 48
04098407 49static struct hash_table *muscle_table = NULL;
f753cd62 50
beda758b
AD
51static bool
52hash_compare_muscles (void const *x, void const *y)
f753cd62 53{
8322e8f5
PE
54 muscle_entry const *m1 = x;
55 muscle_entry const *m2 = y;
5dd5fd4a 56 return strcmp (m1->key, m2->key) == 0;
f753cd62
MA
57}
58
233a88ad
PE
59static size_t
60hash_muscle (const void *x, size_t tablesize)
f753cd62 61{
8322e8f5 62 muscle_entry const *m = x;
beda758b 63 return hash_string (m->key, tablesize);
f753cd62
MA
64}
65
592e8d4d
AD
66/*-----------------------------------------------------------------.
67| Create the MUSCLE_TABLE, and initialize it with default values. |
68| Also set up the MUSCLE_OBSTACK. |
69`-----------------------------------------------------------------*/
70
eb095650
PE
71static void
72muscle_entry_free (void *entry)
73{
74 muscle_entry *mentry = entry;
75 free (mentry->storage);
76 free (mentry);
77}
78
f753cd62
MA
79void
80muscle_init (void)
81{
ae7453f2
AD
82 /* Initialize the muscle obstack. */
83 obstack_init (&muscle_obstack);
84
beda758b 85 muscle_table = hash_initialize (HT_INITIAL_CAPACITY, NULL, hash_muscle,
eb095650 86 hash_compare_muscles, muscle_entry_free);
f753cd62
MA
87
88 /* Version and input file. */
ae7453f2 89 MUSCLE_INSERT_STRING ("version", VERSION);
48b16bbc 90 MUSCLE_INSERT_C_STRING ("file_name", grammar_file);
f753cd62
MA
91}
92
592e8d4d
AD
93
94/*------------------------------------------------------------.
95| Free all the memory consumed by the muscle machinery only. |
96`------------------------------------------------------------*/
97
98void
99muscle_free (void)
100{
101 hash_free (muscle_table);
102 obstack_free (&muscle_obstack, NULL);
103}
104
105
106
ae7453f2
AD
107/*------------------------------------------------------------.
108| Insert (KEY, VALUE). If KEY already existed, overwrite the |
109| previous value. |
110`------------------------------------------------------------*/
111
a870c567 112void
eb095650 113muscle_insert (char const *key, char const *value)
f753cd62 114{
8322e8f5 115 muscle_entry probe;
da2a7671 116 muscle_entry *entry;
e9bca3ad 117
ae7453f2
AD
118 probe.key = key;
119 entry = hash_lookup (muscle_table, &probe);
beda758b
AD
120
121 if (!entry)
122 {
123 /* First insertion in the hash. */
da2a7671 124 entry = xmalloc (sizeof *entry);
beda758b
AD
125 entry->key = key;
126 hash_insert (muscle_table, entry);
127 }
4502eadc
JD
128 else
129 free (entry->storage);
beda758b 130 entry->value = value;
4502eadc 131 entry->storage = NULL;
f753cd62
MA
132}
133
ae7453f2
AD
134
135/*-------------------------------------------------------------------.
ff5150d9
PE
136| Append VALUE to the current value of KEY. If KEY did not already |
137| exist, create it. Use MUSCLE_OBSTACK. De-allocate the previously |
138| associated value. Copy VALUE and SEPARATOR. |
ae7453f2
AD
139`-------------------------------------------------------------------*/
140
141void
142muscle_grow (const char *key, const char *val, const char *separator)
143{
8322e8f5
PE
144 muscle_entry probe;
145 muscle_entry *entry = NULL;
ae7453f2
AD
146
147 probe.key = key;
148 entry = hash_lookup (muscle_table, &probe);
149
150 if (!entry)
151 {
152 /* First insertion in the hash. */
da2a7671 153 entry = xmalloc (sizeof *entry);
ae7453f2
AD
154 entry->key = key;
155 hash_insert (muscle_table, entry);
eb095650 156 entry->value = entry->storage = xstrdup (val);
ae7453f2
AD
157 }
158 else
159 {
160 /* Grow the current value. */
161 char *new_val;
ae7453f2 162 obstack_sgrow (&muscle_obstack, entry->value);
eb095650 163 free (entry->storage);
ae7453f2
AD
164 obstack_sgrow (&muscle_obstack, separator);
165 obstack_sgrow (&muscle_obstack, val);
166 obstack_1grow (&muscle_obstack, 0);
167 new_val = obstack_finish (&muscle_obstack);
eb095650 168 entry->value = entry->storage = xstrdup (new_val);
ae7453f2
AD
169 obstack_free (&muscle_obstack, new_val);
170 }
171}
172
173
cd3684cf
AD
174/*------------------------------------------------------------------.
175| Append VALUE to the current value of KEY, using muscle_grow. But |
176| in addition, issue a synchronization line for the location LOC. |
177`------------------------------------------------------------------*/
178
179void
180muscle_code_grow (const char *key, const char *val, location loc)
181{
182 char *extension = NULL;
05ac60f3 183 obstack_fgrow1 (&muscle_obstack, "]b4_syncline(%d, [[", loc.start.line);
cd3684cf
AD
184 MUSCLE_OBSTACK_SGROW (&muscle_obstack,
185 quotearg_style (c_quoting_style, loc.start.file));
186 obstack_sgrow (&muscle_obstack, "]])[\n");
187 obstack_sgrow (&muscle_obstack, val);
188 obstack_1grow (&muscle_obstack, 0);
189 extension = obstack_finish (&muscle_obstack);
190 muscle_grow (key, extension, "");
eb095650 191 obstack_free (&muscle_obstack, extension);
cd3684cf
AD
192}
193
194
ae7453f2
AD
195void muscle_pair_list_grow (const char *muscle,
196 const char *a1, const char *a2)
197{
66d30cd4 198 char *pair;
7ecec4dd
JD
199 obstack_sgrow (&muscle_obstack, "[[[");
200 MUSCLE_OBSTACK_SGROW (&muscle_obstack, a1);
201 obstack_sgrow (&muscle_obstack, "]], [[");
202 MUSCLE_OBSTACK_SGROW (&muscle_obstack, a2);
203 obstack_sgrow (&muscle_obstack, "]]]");
ae7453f2 204 obstack_1grow (&muscle_obstack, 0);
66d30cd4
AD
205 pair = obstack_finish (&muscle_obstack);
206 muscle_grow (muscle, pair, ",\n");
207 obstack_free (&muscle_obstack, pair);
ae7453f2
AD
208}
209
7eb8a0bc
JD
210
211/*----------------------------------------------------------------------------.
212| Find the value of muscle KEY. Unlike MUSCLE_FIND, this is always reliable |
213| to determine whether KEY has a value. |
214`----------------------------------------------------------------------------*/
215
216char const *
217muscle_find_const (char const *key)
218{
219 muscle_entry probe;
220 muscle_entry *result = NULL;
221
222 probe.key = key;
223 result = hash_lookup (muscle_table, &probe);
224 if (result)
225 return result->value;
226 return NULL;
227}
228
229
4502eadc
JD
230/*----------------------------------------------------------------------------.
231| Find the value of muscle KEY. Abort if muscle_insert was invoked more |
232| recently than muscle_grow for KEY since muscle_find can't return a |
233| char const *. |
234`----------------------------------------------------------------------------*/
ae7453f2 235
ff5150d9 236char *
7eb8a0bc 237muscle_find (char const *key)
f753cd62 238{
8322e8f5
PE
239 muscle_entry probe;
240 muscle_entry *result = NULL;
e9bca3ad 241
ae7453f2
AD
242 probe.key = key;
243 result = hash_lookup (muscle_table, &probe);
4502eadc
JD
244 if (result)
245 {
246 aver (result->value == result->storage);
247 return result->storage;
248 }
249 return NULL;
f753cd62 250}
be2a1a68
AD
251
252
9611cfa2
JD
253void
254muscle_boundary_grow (char const *key, boundary bound)
255{
256 char *extension;
257 MUSCLE_OBSTACK_SGROW (&muscle_obstack, bound.file);
258 obstack_1grow (&muscle_obstack, ':');
259 obstack_fgrow1 (&muscle_obstack, "%d", bound.line);
260 obstack_1grow (&muscle_obstack, '.');
261 obstack_fgrow1 (&muscle_obstack, "%d", bound.column);
262 obstack_1grow (&muscle_obstack, '\0');
263 extension = obstack_finish (&muscle_obstack);
264 muscle_grow (key, extension, "");
265 obstack_free (&muscle_obstack, extension);
266}
267
268void
269muscle_location_grow (char const *key, location loc)
270{
271 muscle_grow (key, "[[", "");
272 muscle_boundary_grow (key, loc.start);
273 muscle_grow (key, "]], [[", "");
274 muscle_boundary_grow (key, loc.end);
275 muscle_grow (key, "]]", "");
276}
277
f124d423
JD
278#define MUSCLE_COMMON_DECODE(Value) \
279 case '$': \
280 aver (*++(Value) == ']'); \
281 aver (*++(Value) == '['); \
282 obstack_sgrow (&muscle_obstack, "$"); \
283 break; \
284 case '@': \
285 switch (*++(Value)) \
286 { \
287 case '@': obstack_sgrow (&muscle_obstack, "@" ); break; \
288 case '{': obstack_sgrow (&muscle_obstack, "[" ); break; \
289 case '}': obstack_sgrow (&muscle_obstack, "]" ); break; \
290 default: aver (false); break; \
291 } \
292 break; \
293 default: \
294 obstack_1grow (&muscle_obstack, *(Value)); \
295 break;
296
297/* Reverse of MUSCLE_OBSTACK_SGROW. */
298static char *
299muscle_string_decode (char const *key)
300{
301 char const *value;
302 char *value_decoded;
303 char *result;
304
305 value = muscle_find_const (key);
306 if (!value)
307 return NULL;
308 do {
309 switch (*value)
310 {
311 MUSCLE_COMMON_DECODE (value)
312 case '[':
313 case ']':
314 aver (false);
315 break;
316 }
317 } while (*value++);
318 value_decoded = obstack_finish (&muscle_obstack);
319 result = xstrdup (value_decoded);
320 obstack_free (&muscle_obstack, value_decoded);
321 return result;
322}
323
9611cfa2
JD
324/* Reverse of muscle_location_grow. */
325static location
326muscle_location_decode (char const *key)
327{
328 location loc;
329 char const *value = muscle_find_const (key);
330 aver (value);
331 aver (*value == '[');
332 aver (*++value == '[');
333 while (*++value)
334 switch (*value)
335 {
f124d423 336 MUSCLE_COMMON_DECODE (value)
9611cfa2
JD
337 case '[':
338 aver (false);
339 break;
340 case ']':
341 {
342 char *boundary_str;
343 aver (*++value == ']');
344 obstack_1grow (&muscle_obstack, '\0');
345 boundary_str = obstack_finish (&muscle_obstack);
346 switch (*++value)
347 {
348 case ',':
349 boundary_set_from_string (&loc.start, boundary_str);
350 obstack_free (&muscle_obstack, boundary_str);
351 aver (*++value == ' ');
352 aver (*++value == '[');
353 aver (*++value == '[');
354 break;
355 case '\0':
356 boundary_set_from_string (&loc.end, boundary_str);
357 obstack_free (&muscle_obstack, boundary_str);
358 return loc;
359 break;
360 default:
361 aver (false);
362 break;
363 }
364 }
365 break;
9611cfa2
JD
366 }
367 aver (false);
368 return loc;
369}
370
371void
372muscle_user_name_list_grow (char const *key, char const *user_name,
373 location loc)
374{
375 muscle_grow (key, "[[[[", ",");
376 muscle_grow (key, user_name, "");
377 muscle_grow (key, "]], ", "");
378 muscle_location_grow (key, loc);
379 muscle_grow (key, "]]", "");
380}
381
382#define MUSCLE_USER_NAME_CONVERT(NAME, PREFIX, USER_NAME, SUFFIX) \
383do { \
384 char *tmp; \
385 size_t length = strlen ((USER_NAME)); \
386 tmp = xmalloc (sizeof (PREFIX) - 1 + length + sizeof (SUFFIX)); \
387 strcpy (tmp, (PREFIX)); \
388 strcpy (tmp + sizeof (PREFIX) - 1, (USER_NAME)); \
389 strcpy (tmp + sizeof (PREFIX) - 1 + length, (SUFFIX)); \
390 (NAME) = uniqstr_new (tmp); \
391 free (tmp); \
392} while (0)
393
394void
395muscle_percent_define_insert (char const *variable, location variable_loc,
396 char const *value)
397{
398 char const *name;
399 char const *loc_name;
400
401 MUSCLE_USER_NAME_CONVERT (name, "percent_define(", variable, ")");
402 MUSCLE_USER_NAME_CONVERT (loc_name, "percent_define_loc(", variable, ")");
403
404 if (muscle_find_const (name))
405 {
406 warn_at (variable_loc, _("%s `%s' redefined"),
407 "%define variable", variable);
408 warn_at (muscle_location_decode (loc_name), _("previous definition"));
409 }
410 MUSCLE_INSERT_STRING (name, value);
411
412 muscle_insert (loc_name, "");
413 muscle_location_grow (loc_name, variable_loc);
414 muscle_user_name_list_grow ("percent_define_user_variables", variable,
415 variable_loc);
416}
417
f124d423
JD
418char *
419muscle_percent_define_get (char const *variable)
420{
421 char const *name;
422 char const *usage_name;
423 char *value;
424
425 MUSCLE_USER_NAME_CONVERT (name, "percent_define(", variable, ")");
426 MUSCLE_USER_NAME_CONVERT (usage_name, "percent_define_bison_variables(",
427 variable, ")");
428
429 muscle_insert (usage_name, "");
430 value = muscle_string_decode (name);
431 if (!value)
432 value = xstrdup ("");
433 return value;
434}
435
436bool
437muscle_percent_define_ifdef (char const *variable)
438{
439 char const *name;
440 char const *usage_name;
441 char const *value;
442
443 MUSCLE_USER_NAME_CONVERT (name, "percent_define(", variable, ")");
444 MUSCLE_USER_NAME_CONVERT (usage_name, "percent_define_bison_variables(",
445 variable, ")");
446
447 value = muscle_find_const (name);
448 if (value)
449 {
450 muscle_insert (usage_name, "");
451 return true;
452 }
453
454 return false;
455}
456
9611cfa2
JD
457bool
458muscle_percent_define_flag_if (char const *variable)
459{
460 char const *name;
461 char const *loc_name;
462 char const *usage_name;
9611cfa2
JD
463 bool result = false;
464
465 MUSCLE_USER_NAME_CONVERT (name, "percent_define(", variable, ")");
466 MUSCLE_USER_NAME_CONVERT (loc_name, "percent_define_loc(", variable, ")");
467 MUSCLE_USER_NAME_CONVERT (usage_name, "percent_define_bison_variables(",
468 variable, ")");
469
f124d423 470 if (muscle_percent_define_ifdef (variable))
9611cfa2 471 {
f124d423 472 char *value = muscle_percent_define_get (variable);
9611cfa2
JD
473 if (value[0] == '\0' || 0 == strcmp (value, "true"))
474 result = true;
475 else if (0 == strcmp (value, "false"))
476 result = false;
477 else if (!muscle_find_const (usage_name))
478 complain_at(muscle_location_decode (loc_name),
479 _("invalid value for %%define boolean variable `%s'"),
480 variable);
f124d423 481 free (value);
9611cfa2
JD
482 }
483 else
484 fatal(_("undefined %%define variable `%s' passed to muscle_percent_define_flag_if"),
485 variable);
486
9611cfa2
JD
487 return result;
488}
489
490void
491muscle_percent_define_default (char const *variable, char const *value)
492{
493 char const *name;
494 char const *loc_name;
495 MUSCLE_USER_NAME_CONVERT (name, "percent_define(", variable, ")");
496 MUSCLE_USER_NAME_CONVERT (loc_name, "percent_define_loc(", variable, ")");
497 if (!muscle_find_const (name))
498 {
499 location loc;
500 MUSCLE_INSERT_STRING (name, value);
501 loc.start.file = loc.end.file = "[Bison:muscle_percent_define_default]";
502 loc.start.line = loc.start.column = 0;
503 loc.end.line = loc.end.column = 0;
504 muscle_insert (loc_name, "");
505 muscle_location_grow (loc_name, loc);
506 }
507}
508
f124d423 509void
b1a81613 510muscle_percent_define_check_values (char const * const *values)
f124d423 511{
b1a81613
JD
512 for (; *values; ++values)
513 {
514 char const *variable = *values;
515 char const *name;
516 char const *loc_name;
517 char *value;
518
519 MUSCLE_USER_NAME_CONVERT (name, "percent_define(", variable, ")");
520 MUSCLE_USER_NAME_CONVERT (loc_name, "percent_define_loc(", variable, ")");
521
522 value = muscle_string_decode (name);
523 if (value)
524 {
525 bool valid = false;
526 for (++values; *values; ++values)
527 {
528 if (0 == strcmp (value, *values))
529 {
530 valid = true;
531 while (*values)
532 ++values;
533 break;
534 }
535 }
536 if (!valid)
537 complain_at(muscle_location_decode (loc_name),
538 _("invalid value for %%define variable `%s': `%s'"),
539 variable, value);
540 free (value);
541 }
542 else
543 fatal(_("undefined %%define variable `%s' passed to muscle_percent_define_check_values"),
544 variable);
545 }
f124d423
JD
546}
547
9611cfa2
JD
548void
549muscle_percent_code_grow (char const *qualifier, location qualifier_loc,
550 char const *code, location code_loc)
551{
552 char const *name;
553 MUSCLE_USER_NAME_CONVERT (name, "percent_code(", qualifier, ")");
554 muscle_code_grow (name, code, code_loc);
555 muscle_user_name_list_grow ("percent_code_user_qualifiers", qualifier,
556 qualifier_loc);
557}
558
559
ae7453f2
AD
560/*------------------------------------------------.
561| Output the definition of ENTRY as a m4_define. |
562`------------------------------------------------*/
be2a1a68 563
e00b6826 564static inline bool
8322e8f5 565muscle_m4_output (muscle_entry *entry, FILE *out)
be2a1a68
AD
566{
567 fprintf (out, "m4_define([b4_%s],\n", entry->key);
ae7453f2 568 fprintf (out, "[[%s]])\n\n\n", entry->value);
e00b6826
PE
569 return true;
570}
571
572static bool
573muscle_m4_output_processor (void *entry, void *out)
574{
575 return muscle_m4_output (entry, out);
be2a1a68
AD
576}
577
578
ae7453f2
AD
579/*----------------------------------------------------------------.
580| Output the definition of all the current muscles into a list of |
581| m4_defines. |
582`----------------------------------------------------------------*/
be2a1a68
AD
583
584void
585muscles_m4_output (FILE *out)
586{
e00b6826 587 hash_do_for_each (muscle_table, muscle_m4_output_processor, out);
be2a1a68 588}