Home | History | Annotate | Download | only in src
      1 /* Keeping a unique copy of strings.
      2 
      3    Copyright (C) 2002-2003, 2008-2012 Free Software Foundation, Inc.
      4 
      5    This file is part of Bison, the GNU Compiler Compiler.
      6 
      7    This program is free software: you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation, either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     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
     18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19 
     20 #ifndef UNIQSTR_H_
     21 # define UNIQSTR_H_
     22 
     23 /*-----------------------------------------.
     24 | Pointers to unique copies of C strings.  |
     25 `-----------------------------------------*/
     26 
     27 typedef char const *uniqstr;
     28 
     29 /* Return the uniqstr for STR.  */
     30 uniqstr uniqstr_new (char const *str);
     31 
     32 /* Return a uniqstr built by vsprintf.  In order to simply concatenate
     33    strings, use UNIQSTR_CONCAT, which is a convenient wrapper around
     34    this function.  */
     35 uniqstr uniqstr_vsprintf (char const *format, ...)
     36   __attribute__ ((__format__ (__printf__, 1, 2)));
     37 
     38 /* Two uniqstr values have the same value iff they are the same.  */
     39 #define UNIQSTR_EQ(USTR1, USTR2) (!!((USTR1) == (USTR2)))
     40 
     41 /* Compare two uniqstr a la strcmp: negative for <, nul for =, and
     42    positive for >.  Undefined order, relies on addresses.  */
     43 #define UNIQSTR_CMP(USTR1, USTR2) ((USTR1) - (USTR2))
     44 
     45 /*--------------------------------------.
     46 | Initializing, destroying, debugging.  |
     47 `--------------------------------------*/
     48 
     49 /* Create the string table.  */
     50 void uniqstrs_new (void);
     51 
     52 /* Die if STR is not a uniqstr.  */
     53 void uniqstr_assert (char const *str);
     54 
     55 /* Free all the memory allocated for symbols.  */
     56 void uniqstrs_free (void);
     57 
     58 /* Report them all.  */
     59 void uniqstrs_print (void);
     60 
     61 /*----------------.
     62 | Concatenation.  |
     63 `----------------*/
     64 
     65 /* Concatenate at most 20 strings and return a uniqstr.  The goal of
     66    this macro is to make the caller's code a little more succinct
     67    without a trivial uniqstr_vsprintf format string to maintain
     68    (for example, "%s%s%s") while still benefitting from gcc's type
     69    checking.  Unfortunately, because of the missing format string in the
     70    macro invocation, the argument number reported by gcc for a bad
     71    argument type is 1 too large.  */
     72 #define UNIQSTR_CONCAT(...)                                            \
     73   uniqstr_vsprintf (UNIQSTR_GEN_FORMAT (__VA_ARGS__,                   \
     74                                         "%s", "%s", "%s", "%s", "%s",  \
     75                                         "%s", "%s", "%s", "%s", "%s",  \
     76                                         "%s", "%s", "%s", "%s", "%s",  \
     77                                         "%s", "%s", "%s", "%s", "%s"), \
     78                     __VA_ARGS__)
     79 
     80 #define UNIQSTR_GEN_FORMAT(F1,  F2,  F3,  F4,  F5,  \
     81                            F6,  F7,  F8,  F9,  F10, \
     82                            F11, F12, F13, F14, F15, \
     83                            F16, F17, F18, F19, F20, \
     84                            ...)                     \
     85   UNIQSTR_GEN_FORMAT_ (__VA_ARGS__,                 \
     86                        "", "", "", "", "",          \
     87                        "", "", "", "", "",          \
     88                        "", "", "", "", "",          \
     89                        "", "", "", "", "")
     90 
     91 #define UNIQSTR_GEN_FORMAT_(F1,  F2,  F3,  F4,  F5,       \
     92                             F6,  F7,  F8,  F9,  F10,      \
     93                             F11, F12, F13, F14, F15,      \
     94                             F16, F17, F18, F19, F20, ...) \
     95   F1  F2  F3  F4  F5  F6  F7  F8  F9  F10                 \
     96   F11 F12 F13 F14 F15 F16 F17 F18 F19 F20
     97 
     98 #endif /* ! defined UNIQSTR_H_ */
     99