This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
cpplib: don't modify the IStable for '$' in identifiers
- To: gcc-patches at gcc dot gnu dot org
- Subject: cpplib: don't modify the IStable for '$' in identifiers
- From: Zack Weinberg <zack at wolery dot cumb dot org>
- Date: Fri, 3 Mar 2000 17:42:53 -0800
This patch changes the definitions of is_idchar and is_idstart so they
check dollars_in_ident directly, rather than expecting '$' to appear
in the IStable. That, in turn, means we can make the IStable
read-only data, and eliminate the last bit of cpplib internal state
maintained outside the cpp_reader structure.
There's still a dependence on the globals user_label_prefix and
std_prefix. This is only an issue if you use extremely obscure
options. '$' in identifiers changes with -ansi, which is not that
uncommon. Also, those globals are not really under cpplib's control;
they belong to the back end and the driver.
Also, a couple of unrelated static data structures are now constant.
I can't bootstrap because of the strlensi problem, but I have compiled
cpplib and verified that $ is still handled properly in both modes.
zw
* cpplib.h (_dollar_ok): New macro.
(is_idchar, is_idstart): Use it.
(IStable): Rename to _cpp_IStable. Declare it const if
gcc >=2.7 or C99. Delete all references to FAKE_CONST.
(is_idchar, is_idstart, is_numchar, is_numstart, is_hspace,
is_space): Update for renamed IStable.
* cppinit.c: Delete all references to FAKE_CONST and CAT
macros. Define init_IStable as empty macro if gcc >=2.7 or
C99. Change TABLE() to ISTABLE and hardcode name of table.
(cpp_start_read): Don't change the IStable based on
dollars_in_ident.
* cpphash.c (unsafe_chars): Add pfile argument. All callers
changed. Handle '$' for char1 correctly.
* cpplib.c (cpp_get_token): Use is_numchar when parsing numbers.
* cppexp.c (tokentab2): Make const.
(cpp_lex): Make toktab const.
* cppinit.c (include_defaults_array): Make const.
(initialize_standard_includes): Make default_include const.
===================================================================
Index: cppexp.c
--- cppexp.c 2000/03/01 00:57:08 1.34
+++ cppexp.c 2000/03/04 01:35:15
@@ -403,7 +403,7 @@ struct token {
int token;
};
-static struct token tokentab2[] = {
+static const struct token tokentab2[] = {
{"&&", ANDAND},
{"||", OROR},
{"<<", LSH},
@@ -424,7 +424,7 @@ cpp_lex (pfile, skip_evaluation)
cpp_reader *pfile;
int skip_evaluation;
{
- struct token *toktab;
+ const struct token *toktab;
enum cpp_token token;
struct operation op;
U_CHAR *tok_start, *tok_end;
===================================================================
Index: cpphash.c
--- cpphash.c 2000/03/02 20:14:32 1.44
+++ cpphash.c 2000/03/04 01:35:16
@@ -35,7 +35,7 @@ static int comp_def_part PARAMS ((int,
int, int));
static void push_macro_expansion PARAMS ((cpp_reader *,
U_CHAR *, int, HASHNODE *));
-static int unsafe_chars PARAMS ((int, int));
+static int unsafe_chars PARAMS ((cpp_reader *, int, int));
static int macro_cleanup PARAMS ((cpp_buffer *, cpp_reader *));
static enum cpp_token macarg PARAMS ((cpp_reader *, int));
static struct tm *timestamp PARAMS ((cpp_reader *));
@@ -1317,7 +1317,7 @@ macroexpand (pfile, hp)
U_CHAR *expanded = ARG_BASE + arg->expanded;
if (!ap->raw_before && totlen > 0 && arg->expand_length
&& !CPP_TRADITIONAL (pfile)
- && unsafe_chars (xbuf[totlen - 1], expanded[0]))
+ && unsafe_chars (pfile, xbuf[totlen - 1], expanded[0]))
{
xbuf[totlen++] = '\r';
xbuf[totlen++] = ' ';
@@ -1328,7 +1328,7 @@ macroexpand (pfile, hp)
if (!ap->raw_after && totlen > 0 && offset < defn->length
&& !CPP_TRADITIONAL (pfile)
- && unsafe_chars (xbuf[totlen - 1], exp[offset]))
+ && unsafe_chars (pfile, xbuf[totlen - 1], exp[offset]))
{
xbuf[totlen++] = '\r';
xbuf[totlen++] = ' ';
@@ -1382,7 +1382,8 @@ macroexpand (pfile, hp)
could cause mis-tokenization. */
static int
-unsafe_chars (c1, c2)
+unsafe_chars (pfile, c1, c2)
+ cpp_reader *pfile;
int c1, c2;
{
switch (c1)
@@ -1397,6 +1398,11 @@ unsafe_chars (c1, c2)
return 1; /* could extend a pre-processing number */
goto letter;
+ case '$':
+ if (CPP_OPTIONS (pfile)->dollars_in_ident)
+ goto letter;
+ return 0;
+
case 'L':
if (c2 == '\'' || c2 == '\"')
return 1; /* Could turn into L"xxx" or L'xxx'. */
@@ -1468,7 +1474,7 @@ push_macro_expansion (pfile, xbuf, xbuf_
{
int c1 = mbuf->rlimit[-3];
int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
- if (c2 == EOF || !unsafe_chars (c1, c2))
+ if (c2 == EOF || !unsafe_chars (pfile, c1, c2))
mbuf->rlimit -= 2;
}
}
===================================================================
Index: cppinit.c
--- cppinit.c 2000/03/04 00:11:40 1.48
+++ cppinit.c 2000/03/04 01:35:16
@@ -22,7 +22,6 @@ Foundation, 59 Temple Place - Suite 330,
#include "config.h"
#include "system.h"
-#define FAKE_CONST
#include "cpplib.h"
#include "cpphash.h"
#include "output.h"
@@ -103,7 +102,7 @@ static const char * const known_suffixes
All these directories are treated as `system' include directories
(they are not subject to pedantic warnings in some cases). */
-static struct default_include
+struct default_include
{
const char *fname; /* The name of the directory. */
const char *component; /* The component containing the directory
@@ -112,8 +111,9 @@ static struct default_include
int cxx_aware; /* Includes in this directory don't need to
be wrapped in extern "C" when compiling
C++. */
-}
-include_defaults_array[]
+};
+
+static const struct default_include include_defaults_array[]
#ifdef INCLUDE_DEFAULTS
= INCLUDE_DEFAULTS;
#else
@@ -203,25 +203,22 @@ static void new_pending_define PARAMS (
/* Fourth argument to append_include_chain: chain to use */
enum { QUOTE = 0, BRACKET, SYSTEM, AFTER };
-
-/* If gcc is in use (stage2/stage3) we can make this table initialized data. */
-#ifdef __STDC__
-#define CAT(a, b) a##b
-#else
-#define CAT(a, b) a/**/b
-#endif
-#if (GCC_VERSION >= 2007)
-#define TABLE(id) static inline void CAT(init_, id) PARAMS ((void)) {} \
-unsigned char id[256] = {
-#define s(p, v) [p] = v,
+/* If we have designated initializers (GCC >2.7, or C99) this table
+ can be initialized, constant data. Otherwise, it has to be filled
+ in at runtime. */
+
+#if (GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L)
+#define init_IStable() /* nothing */
+#define ISTABLE const unsigned char _cpp_IStable[256] = {
#define END };
+#define s(p, v) [p] = v,
#else
-#define TABLE(id) unsigned char id[256] = { 0 }; \
-static void CAT(init_,id) PARAMS ((void)) { \
-unsigned char *x = id;
-#define s(p, v) x[p] = v;
+#define ISTABLE unsigned char _cpp_IStable[256] = { 0 }; \
+ static void init_IStable PARAMS ((void)) { \
+ unsigned char *x = id;
#define END }
+#define s(p, v) x[p] = v;
#endif
#define A(x) s(x, ISidnum|ISidstart)
@@ -229,7 +226,7 @@ unsigned char *x = id;
#define H(x) s(x, IShspace|ISspace)
#define S(x) s(x, ISspace)
-TABLE (IStable)
+ISTABLE
A('_')
A('a') A('b') A('c') A('d') A('e') A('f') A('g') A('h') A('i')
@@ -251,10 +248,9 @@ END
#undef N
#undef H
#undef S
-#undef TABLE
-#undef END
#undef s
-#undef CAT
+#undef ISTABLE
+#undef END
/* Given a colon-separated list of file names PATH,
add all the names to the search path for include files. */
@@ -668,7 +664,7 @@ initialize_standard_includes (pfile)
{
cpp_options *opts = CPP_OPTIONS (pfile);
char *path;
- struct default_include *p = include_defaults_array;
+ const struct default_include *p;
char *specd_prefix = opts->include_prefix;
/* Several environment variables may add to the include search path.
@@ -793,14 +789,10 @@ cpp_start_read (pfile, fname)
preprocessing. */
if (opts->preprocessed)
pfile->no_macro_expand++;
-
- /* Now that we know dollars_in_ident, we can initialize the syntax
- tables. */
+
+ /* Set up the IStable. This doesn't do anything if we were compiled
+ with a compiler that supports C99 designated initializers. */
init_IStable ();
- /* XXX Get rid of code that depends on this, then IStable can
- be truly const. */
- if (opts->dollars_in_ident)
- IStable['$'] = ISidstart|ISidnum;
/* Set up the include search path now. */
if (! opts->no_standard_includes)
===================================================================
Index: cpplib.c
--- cpplib.c 2000/03/03 00:09:21 1.123
+++ cpplib.c 2000/03/04 01:35:16
@@ -2615,7 +2615,7 @@ cpp_get_token (pfile)
c = PEEKC ();
if (c == EOF)
break;
- if (!is_idchar(c) && c != '.'
+ if (!is_numchar(c) && c != '.'
&& ((c2 != 'e' && c2 != 'E'
&& ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
|| (c != '+' && c != '-')))
@@ -2640,7 +2640,7 @@ cpp_get_token (pfile)
c = GETC();
if (c == EOF)
goto chill_number_eof;
- if (!is_idchar(c))
+ if (!is_numchar(c))
break;
CPP_PUTC (pfile, c);
}
===================================================================
Index: cpplib.h
--- cpplib.h 2000/03/04 00:11:40 1.62
+++ cpplib.h 2000/03/04 01:35:17
@@ -612,22 +612,23 @@ enum node_type {
#define IShspace 0x08 /* ' ' \t \f \v */
#define ISspace 0x10 /* ' ' \t \f \v \n */
-#define is_idchar(x) (IStable[x] & ISidnum)
-#define is_numchar(x) (IStable[x] & ISidnum)
-#define is_idstart(x) (IStable[x] & ISidstart)
-#define is_numstart(x) (IStable[x] & ISnumstart)
-#define is_hspace(x) (IStable[x] & IShspace)
-#define is_space(x) (IStable[x] & ISspace)
+#define _dollar_ok(x) ((x) == '$' && CPP_OPTIONS (pfile)->dollars_in_ident)
-/* This table is not really `const', but it is only modified at
- initialization time, in a separate translation unit from the rest
- of the library. We let the rest of the library think it is `const'
- to get better code and some additional compile-time checks. */
-#ifndef FAKE_CONST
-#define FAKE_CONST const
+#define is_idchar(x) ((_cpp_IStable[x] & ISidnum) || _dollar_ok(x))
+#define is_idstart(x) ((_cpp_IStable[x] & ISidstart) || _dollar_ok(x))
+#define is_numchar(x) (_cpp_IStable[x] & ISidnum)
+#define is_numstart(x) (_cpp_IStable[x] & ISnumstart)
+#define is_hspace(x) (_cpp_IStable[x] & IShspace)
+#define is_space(x) (_cpp_IStable[x] & ISspace)
+
+/* This table is constant if it can be initialized at compile time,
+ which is the case if cpp was compiled with GCC >=2.7, or another
+ compiler that supports C99. */
+#if (GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L)
+extern const unsigned char _cpp_IStable[256];
+#else
+extern unsigned char _cpp_IStable[256];
#endif
-extern FAKE_CONST unsigned char IStable[256];
-#undef FAKE_CONST
/* Stack of conditionals currently in progress
(including both successful and failing conditionals). */