]> git.saurik.com Git - bison.git/blame - src/uniqstr.c
Pacify "gcc -Wunused" for the input function from Flex.
[bison.git] / src / uniqstr.c
CommitLineData
ac72f3e9
PE
1/* Keep a unique copy of strings.
2
b5c212b6 3 Copyright (C) 2002, 2003, 2004, 2005, 2009 Free Software Foundation, Inc.
5bab03fc
PE
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
f16b0819 7 This program is free software: you can redistribute it and/or modify
5bab03fc 8 it under the terms of the GNU General Public License as published by
f16b0819
PE
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
5bab03fc 11
f16b0819 12 This program is distributed in the hope that it will be useful,
5bab03fc
PE
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
f16b0819 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
5bab03fc 19
2cec9080 20#include <config.h>
5bab03fc
PE
21#include "system.h"
22
23#include <error.h>
24#include <hash.h>
25#include <quotearg.h>
26
27#include "uniqstr.h"
28
29/*-----------------------.
30| A uniqstr hash table. |
31`-----------------------*/
32
33/* Initial capacity of uniqstr hash table. */
34#define HT_INITIAL_CAPACITY 257
35
36static struct hash_table *uniqstrs_table = NULL;
37
38/*-------------------------------------.
39| Create the uniqstr for S if needed. |
40`-------------------------------------*/
41
42uniqstr
a737b216 43uniqstr_new (char const *str)
5bab03fc 44{
a737b216 45 uniqstr res = hash_lookup (uniqstrs_table, str);
5bab03fc
PE
46 if (!res)
47 {
48 /* First insertion in the hash. */
a737b216 49 res = xstrdup (str);
b5c212b6
AD
50 if (!hash_insert (uniqstrs_table, res))
51 xalloc_die ();
5bab03fc
PE
52 }
53 return res;
54}
55
56
57/*------------------------------.
58| Abort if S is not a uniqstr. |
59`------------------------------*/
60
61void
a737b216 62uniqstr_assert (char const *str)
5bab03fc 63{
a737b216 64 if (!hash_lookup (uniqstrs_table, str))
5bab03fc 65 {
04098407
PE
66 error (0, 0,
67 "not a uniqstr: %s", quotearg (str));
5bab03fc
PE
68 abort ();
69 }
70}
71
72
73/*--------------------.
74| Print the uniqstr. |
75`--------------------*/
76
ac72f3e9 77static inline bool
a737b216 78uniqstr_print (uniqstr ustr)
5bab03fc 79{
a737b216 80 fprintf (stderr, "%s\n", ustr);
5bab03fc
PE
81 return true;
82}
83
ac72f3e9 84static bool
a737b216 85uniqstr_print_processor (void *ustr, void *null ATTRIBUTE_UNUSED)
ac72f3e9 86{
a737b216 87 return uniqstr_print (ustr);
ac72f3e9
PE
88}
89
5bab03fc
PE
90\f
91/*-----------------------.
92| A uniqstr hash table. |
93`-----------------------*/
94
95static bool
ac72f3e9 96hash_compare_uniqstr (void const *m1, void const *m2)
5bab03fc
PE
97{
98 return strcmp (m1, m2) == 0;
99}
100
233a88ad
PE
101static size_t
102hash_uniqstr (void const *m, size_t tablesize)
5bab03fc
PE
103{
104 return hash_string (m, tablesize);
105}
106
5bab03fc
PE
107/*----------------------------.
108| Create the uniqstrs table. |
109`----------------------------*/
110
111void
112uniqstrs_new (void)
113{
114 uniqstrs_table = hash_initialize (HT_INITIAL_CAPACITY,
115 NULL,
ac72f3e9
PE
116 hash_uniqstr,
117 hash_compare_uniqstr,
118 free);
5bab03fc
PE
119}
120
121
122/*-------------------------------------.
123| Perform a task on all the uniqstrs. |
124`-------------------------------------*/
125
126static void
ac72f3e9 127uniqstrs_do (Hash_processor processor, void *processor_data)
5bab03fc 128{
ac72f3e9 129 hash_do_for_each (uniqstrs_table, processor, processor_data);
5bab03fc
PE
130}
131
132
133/*-----------------.
134| Print them all. |
135`-----------------*/
136
137void
138uniqstrs_print (void)
139{
ac72f3e9 140 uniqstrs_do (uniqstr_print_processor, NULL);
5bab03fc
PE
141}
142
143
144/*--------------------.
145| Free the uniqstrs. |
146`--------------------*/
147
148void
149uniqstrs_free (void)
150{
151 hash_free (uniqstrs_table);
152}