bison-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Bug in #include guards


From: Alexander Belopolsky
Subject: Bug in #include guards
Date: Mon, 24 Sep 2001 21:20:35 -0400 (EDT)

When bison is ran with --file-prefix=/full/path/foo --defines options,
it creates invalid #include guards with '/' in macro defs:

#ifndef /FULL/PATH/FOO_TAB_H
# define /FULL/PATH/FOO_TAB_H
...

in foo.tab.h

The patch below fixes the problem by mangling file names replacing
invalid characters with their hexadecimal codes.


Index: files.c
===================================================================
RCS file: /cvsroot/bison/bison/src/files.c,v
retrieving revision 1.51
diff -r1.51 files.c
88a89,138
> |  Returns 1 if c is a valid character in a preprocesssor macro, |
> |  0 otherwise.                                                  |
> `---------------------------------------------------------------*/
> 
> static int
> is_valid_in_macro(int c)
> {
>   return isalnum(c);
> }
> 
> 
> /*---------------------------------------------------------------.
> |  Compute the length of the string after all invalid characters |
> |  are replaced with _XX_.                                       |
> `---------------------------------------------------------------*/
> 
> static int
> mangled_length(const char* str)
> {
>   int res = 0;
>   while (*str) {
>     if (is_valid_in_macro(*str++))
>       res++;    /* copied as is */
>     else
>       res += 4; /* replaced by _XX_ */
>   }
>   return res;
> }
> 
> 
> /*---------------------------------------------------------------.
> |  If c is a valid character, append it to the string, otherwise |
> |  append _XX_, where XX is hexadecimal represantation of c.     |
> |  Returns pointer to the end of resulting string.               |
> `---------------------------------------------------------------*/
> 
> static char*
> put_mangled(char* str, char c)
> {
>   if (is_valid_in_macro(c)) {
>     *str++ = c;
>     *str = '\0';
>   } else {
>     str += sprintf(str, "_%02X_", (unsigned int)c);
>   }
> 
>   return str;
> }
> 
> /*---------------------------------------------------------------.
96,97c146,147
<   int ite;
<   char *macro_name;
---
>   static const char* macro_prefix = "INCLUDED";
>   char *macro_name, *p;
99,113c149,161
<   macro_name = XMALLOC (char,
<                       strlen (base_name) +
<                       strlen (header_extension) + 1);
< 
<   stpcpy (macro_name, base_name);
<   strcat (macro_name, header_extension);
< 
<   for (ite = 0; macro_name[ite]; ite++)
<     if (macro_name[ite] == '.')
<       macro_name[ite] = '_';
<     else
<       {
<       if (islower (macro_name[ite]))
<         macro_name[ite] -= ('a' - 'A');
<       }
---
>   p = macro_name = XMALLOC (char, 
>                           strlen (macro_prefix) +
>                           mangled_length (base_name) + 
>                           mangled_length (header_extension) + 1);
>   p = stpcpy (p, macro_prefix);
>   while (*base_name) {
>     p = put_mangled (p, *base_name++);
>   }
> 
>   while (*header_extension) {
>     p = put_mangled (p, *header_extension++);
>   }
>   

--
Alexander Belopolsky | Renaissance Technologies Corp.
600 Route 25A | East Setauket | NY 11733-1249
PH: 631-444-7125 | FAX: 631-444-7009





reply via email to

[Prev in Thread] Current Thread [Next in Thread]