* cppexp.c: Don't include cpphash.h.
(parse_charconst, cpp_lex): Use cpp_defined.
(cpp_lex): Use get_directive_token throughout. Remove
unnecessary cases from switch. Move assertion-handling code
down to OTHER case.
(cpp_parse_expr): If we see '+' or '-', check the context to
determine if they are unary or binary operators. Streamline
the jumps a bit. Do not call skip_rest_of_line.
* cpplib.c: Make skip_rest_of_line and cpp_skip_hspace
static. Export get_directive_token. Update commentary.
(cpp_defined): New function.
(do_define): Remove reference to T_PCSTRING. Call
free_definition to release memory for old definition, when
redefining a macro.
(eval_if_expression): Set only_seen_white to 0 before calling
cpp_parse_expr. Call skip_rest_of_line after it returns.
(cpp_read_check_assertion): Don't preserve a pointer into the
token buffer across a call to cpp_get_token.
* Makefile.in (cppexp.o): Don't depend on cpphash.h.
* cppfiles.c (redundant_include_p): Use cpp_defined.
* cpphash.c (free_definition): New function.
(delete_macro): Use it. Update commentary.
* cpphash.h: Typedef HASHNODE here. Prototype cpp_lookup and
free_definition.
* cpplib.h: Don't typedef HASHNODE here. Delete T_PCSTRING
from enum node_type. Prototype cpp_defined and get_directive_token.
Don't prototype cpp_lookup, skip_rest_of_line, or cpp_skip_hspace.
* fix-header.c (check_macro_names): Use cpp_defined.
(read_scan_file): Set inhibit_warnings and inhibit_errors in
the options structure.
From-SVN: r31908
+2000-02-10 Zack Weinberg <zack@wolery.cumb.org>
+
+ * cppexp.c: Don't include cpphash.h.
+ (parse_charconst, cpp_lex): Use cpp_defined.
+ (cpp_lex): Use get_directive_token throughout. Remove
+ unnecessary cases from switch. Move assertion-handling code
+ down to OTHER case.
+ (cpp_parse_expr): If we see '+' or '-', check the context to
+ determine if they are unary or binary operators. Streamline
+ the jumps a bit. Do not call skip_rest_of_line.
+
+ * cpplib.c: Make skip_rest_of_line and cpp_skip_hspace
+ static. Export get_directive_token. Update commentary.
+ (cpp_defined): New function.
+ (do_define): Remove reference to T_PCSTRING. Call
+ free_definition to release memory for old definition, when
+ redefining a macro.
+ (eval_if_expression): Set only_seen_white to 0 before calling
+ cpp_parse_expr. Call skip_rest_of_line after it returns.
+ (cpp_read_check_assertion): Don't preserve a pointer into the
+ token buffer across a call to cpp_get_token.
+
+ * Makefile.in (cppexp.o): Don't depend on cpphash.h.
+ * cppfiles.c (redundant_include_p): Use cpp_defined.
+ * cpphash.c (free_definition): New function.
+ (delete_macro): Use it. Update commentary.
+ * cpphash.h: Typedef HASHNODE here. Prototype cpp_lookup and
+ free_definition.
+ * cpplib.h: Don't typedef HASHNODE here. Delete T_PCSTRING
+ from enum node_type. Prototype cpp_defined and get_directive_token.
+ Don't prototype cpp_lookup, skip_rest_of_line, or cpp_skip_hspace.
+
+ * fix-header.c (check_macro_names): Use cpp_defined.
+ (read_scan_file): Set inhibit_warnings and inhibit_errors in
+ the options structure.
+
2000-02-10 Franz Sirl <Franz.Sirl-kernel@lauterbach.com>
* c-pragma.c (maximum_field_alignment): Remove duplicate declaration.
cpphash.o: cpphash.c $(CONFIG_H) cpplib.h intl.h system.h cpphash.h
cppalloc.o: cppalloc.c $(CONFIG_H) cpplib.h intl.h system.h
cpperror.o: cpperror.c $(CONFIG_H) cpplib.h intl.h system.h
-cppexp.o: cppexp.c $(CONFIG_H) cpplib.h intl.h system.h cpphash.h
+cppexp.o: cppexp.c $(CONFIG_H) cpplib.h intl.h system.h
cppfiles.o: cppfiles.c $(CONFIG_H) cpplib.h intl.h system.h
cppinit.o: cppinit.c $(CONFIG_H) cpplib.h intl.h system.h \
#include "config.h"
#include "system.h"
#include "cpplib.h"
-#include "cpphash.h"
#ifdef MULTIBYTE_CHARS
#include <locale.h>
/* If char type is signed, sign-extend the constant. */
num_bits = num_chars * width;
- if (cpp_lookup (pfile, (const U_CHAR *)"__CHAR_UNSIGNED__",
- sizeof ("__CHAR_UNSIGNED__")-1)
+ if (cpp_defined (pfile, (const U_CHAR *)"__CHAR_UNSIGNED__",
+ sizeof ("__CHAR_UNSIGNED__")-1)
|| ((result >> (num_bits - 1)) & 1) == 0)
op.value = result & ((unsigned HOST_WIDEST_INT) ~0
>> (HOST_BITS_PER_WIDEST_INT - num_bits));
cpp_reader *pfile;
int skip_evaluation;
{
- U_CHAR c;
struct token *toktab;
enum cpp_token token;
struct operation op;
U_CHAR *tok_start, *tok_end;
- int old_written;
-
- retry:
+ long old_written;
old_written = CPP_WRITTEN (pfile);
- cpp_skip_hspace (pfile);
- c = CPP_BUF_PEEK (CPP_BUFFER (pfile));
- if (c == '#')
- {
- op.op = INT;
- op.value = cpp_read_check_assertion (pfile);
- return op;
- }
-
- if (c == '\n')
- {
- op.op = 0;
- return op;
- }
+ token = get_directive_token (pfile);
- token = cpp_get_token (pfile);
tok_start = pfile->token_buffer + old_written;
tok_end = CPP_PWRITTEN (pfile);
- pfile->limit = tok_start;
+ CPP_SET_WRITTEN (pfile, old_written);
switch (token)
{
case CPP_EOF: /* Should not happen ... */
case CPP_VSPACE:
op.op = 0;
return op;
- case CPP_POP:
- if (CPP_BUFFER (pfile)->fname != NULL)
- {
- op.op = 0;
- return op;
- }
- cpp_pop_buffer (pfile);
- goto retry;
- case CPP_HSPACE:
- case CPP_COMMENT:
- goto retry;
case CPP_NUMBER:
return parse_number (pfile, tok_start, tok_end);
case CPP_STRING:
- cpp_error (pfile, "string constants not allowed in #if expressions");
+ cpp_error (pfile, "string constants are not allowed in #if expressions");
op.op = ERROR;
return op;
case CPP_CHAR:
else
{
int paren = 0, len;
- cpp_buffer *ip = CPP_BUFFER (pfile);
U_CHAR *tok;
- HASHNODE *hp;
- cpp_skip_hspace (pfile);
- if (*ip->cur == '(')
+ pfile->no_macro_expand++;
+ token = get_directive_token (pfile);
+ if (token == CPP_LPAREN)
{
paren++;
- ip->cur++; /* Skip over the paren */
- cpp_skip_hspace (pfile);
+ CPP_SET_WRITTEN (pfile, old_written);
+ token = get_directive_token (pfile);
}
- if (!is_idstart(*ip->cur))
- goto oops;
- if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '"'))
+ if (token != CPP_NAME)
goto oops;
- tok = ip->cur;
- while (is_idchar(*ip->cur))
- ++ip->cur;
- len = ip->cur - tok;
- cpp_skip_hspace (pfile);
+
+ tok = pfile->token_buffer + old_written;
+ len = CPP_PWRITTEN (pfile) - tok;
+ if (cpp_defined (pfile, tok, len))
+ op.value = 1;
+
if (paren)
{
- if (*ip->cur != ')')
+ if (get_directive_token (pfile) != CPP_RPAREN)
goto oops;
- ++ip->cur;
- }
- hp = cpp_lookup (pfile, tok, len);
- if (hp != NULL)
- {
- if (hp->type == T_POISON)
- cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
- else
- op.value = 1;
}
+ CPP_SET_WRITTEN (pfile, old_written);
+ pfile->no_macro_expand--;
}
return op;
oops:
+ CPP_SET_WRITTEN (pfile, old_written);
+ pfile->no_macro_expand--;
cpp_error (pfile, "`defined' without an identifier");
return op;
op.op = toktab->token;
return op;
}
+ else if (tok_start + 1 == tok_end && *tok_start == '#')
+ {
+ CPP_FORWARD (CPP_BUFFER (pfile), -1);
+ op.op = INT;
+ op.value = cpp_read_check_assertion (pfile);
+ return op;
+ }
/* fall through */
default:
op.op = *tok_start;
cpp_ice (pfile, "cpp_lex returns a NAME");
goto syntax_error;
case INT: case CHAR:
- top->value = op.value;
- top->unsignedp = op.unsignedp;
goto set_value;
case 0:
lprio = 0; goto maybe_reduce;
case '+': case '-':
- /* Is this correct if unary ? FIXME */
- flags = RIGHT_OPERAND_REQUIRED;
- lprio = PLUS_PRIO; rprio = lprio + 1; goto maybe_reduce;
+ if (top->flags & HAVE_VALUE)
+ {
+ lprio = PLUS_PRIO;
+ goto binop;
+ }
+ /* else fall through */
case '!': case '~':
flags = RIGHT_OPERAND_REQUIRED;
rprio = UNARY_PRIO; lprio = rprio + 1; goto maybe_reduce;
goto maybe_reduce;
case ERROR:
goto syntax_error;
- binop:
- flags = LEFT_OPERAND_REQUIRED|RIGHT_OPERAND_REQUIRED;
- rprio = lprio + 1;
- goto maybe_reduce;
default:
cpp_error (pfile, "invalid character in #if");
goto syntax_error;
cpp_error (pfile, "syntax error in #if");
goto syntax_error;
}
+ top->value = op.value;
+ top->unsignedp = op.unsignedp;
top->flags |= HAVE_VALUE;
continue;
+ binop:
+ flags = LEFT_OPERAND_REQUIRED|RIGHT_OPERAND_REQUIRED;
+ rprio = lprio + 1;
+
maybe_reduce:
/* Push an operator, and check if we can reduce now. */
while (top->rprio > lprio)
syntax_error:
if (stack != init_stack)
free (stack);
- skip_rest_of_line (pfile);
return 0;
}
included again if the string is the name of a defined macro. */
return (i->control_macro
&& (i->control_macro[0] == '\0'
- || cpp_lookup (pfile, i->control_macro, -1)))
+ || cpp_defined (pfile, i->control_macro, -1)))
? (struct include_hash *)-1 : i;
return 0;
return (HASHNODE *) 0;
}
+/* Free a DEFINITION structure. Used by delete_macro, and by
+ do_define when redefining macros. */
+
+void
+free_definition (d)
+ DEFINITION *d;
+{
+ struct reflist *ap, *nextap;
+
+ for (ap = d->pattern; ap != NULL; ap = nextap)
+ {
+ nextap = ap->next;
+ free (ap);
+ }
+ if (d->nargs >= 0)
+ free (d->argnames);
+ free (d);
+}
+
/*
* Delete a hash node. Some weirdness to free junk from macros.
* More such weirdness will have to be added if you define more hash
* types that need it.
*/
-/* Note that the DEFINITION of a macro is removed from the hash table
- but its storage is not freed. This would be a storage leak
- except that it is not reasonable to keep undefining and redefining
- large numbers of macros many times.
- In any case, this is necessary, because a macro can be #undef'd
- in the middle of reading the arguments to a call to it.
- If #undef freed the DEFINITION, that would crash. */
-
void
delete_macro (hp)
HASHNODE *hp;
*hp->bucket_hdr = hp->next;
if (hp->type == T_MACRO)
- {
- DEFINITION *d = hp->value.defn;
- struct reflist *ap, *nextap;
-
- for (ap = d->pattern; ap != NULL; ap = nextap)
- {
- nextap = ap->next;
- free (ap);
- }
- if (d->nargs >= 0)
- free (d->argnames);
- free (d);
- }
+ free_definition (hp->value.defn);
free (hp);
}
struct hashnode *aschain; /* #assert */
};
+typedef struct hashnode HASHNODE;
struct hashnode {
struct hashnode *next; /* double links for easy deletion */
struct hashnode *prev;
extern HASHNODE *cpp_install PARAMS ((cpp_reader *, const U_CHAR *, int,
enum node_type, const char *));
+extern HASHNODE *cpp_lookup PARAMS ((cpp_reader *, const U_CHAR *, int));
+extern void free_definition PARAMS ((DEFINITION *));
extern void delete_macro PARAMS ((HASHNODE *));
extern MACRODEF create_definition PARAMS ((U_CHAR *, U_CHAR *,
static int skip_comment PARAMS ((cpp_reader *, int));
static int copy_comment PARAMS ((cpp_reader *, int));
static void copy_rest_of_line PARAMS ((cpp_reader *));
+static void skip_rest_of_line PARAMS ((cpp_reader *));
+static void cpp_skip_hspace PARAMS ((cpp_reader *));
static int handle_directive PARAMS ((cpp_reader *));
static void pass_thru_directive PARAMS ((const U_CHAR *, size_t,
cpp_reader *,
const struct directive *));
-static enum cpp_token get_directive_token PARAMS ((cpp_reader *));
static int read_line_number PARAMS ((cpp_reader *, int *));
static U_CHAR *detect_if_not_defined PARAMS ((cpp_reader *));
static int consider_directive_while_skipping PARAMS ((cpp_reader *,
CPP_SET_WRITTEN (pfile, old_written);
}
-/* Process the string STR as if it appeared as the body of a #define
- If STR is just an identifier, define it with value 1.
- If STR has anything after the identifier, then it should
- be identifier=definition. */
-
-/* Process the string STR as if it appeared as the body of a #define
+/* Process the string STR as if it appeared as the body of a #define.
If STR is just an identifier, define it with value 1.
If STR has anything after the identifier, then it should
be identifier=definition. */
}
}
+/* Determine whether the identifier ID, of length LEN, is a defined macro. */
+int
+cpp_defined (pfile, id, len)
+ cpp_reader *pfile;
+ const U_CHAR *id;
+ int len;
+{
+ HASHNODE *hp = cpp_lookup (pfile, id, len);
+ if (hp && hp->type == T_POISON)
+ {
+ cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
+ return 0;
+ }
+ return (hp != NULL);
+}
static enum cpp_token
null_underflow (pfile)
/* Skip whitespace \-newline and comments. Does not macro-expand. */
-void
+static void
cpp_skip_hspace (pfile)
cpp_reader *pfile;
{
the scan itself. >75% of calls to copy_r_o_l are from here or
skip_if_group, which means the common case is to copy stuff into the
token_buffer only to discard it. */
-void
+static void
skip_rest_of_line (pfile)
cpp_reader *pfile;
{
if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen)) != NULL)
{
int ok = 0;
- /* Redefining a precompiled key is ok. */
- if (hp->type == T_PCSTRING)
- ok = 1;
/* Redefining a poisoned identifier is even worse than `not ok'. */
- else if (hp->type == T_POISON)
+ if (hp->type == T_POISON)
ok = -1;
/* Redefining a macro is ok if the definitions are the same. */
else if (hp->type == T_MACRO)
{
/* Replace the old definition. */
hp->type = new_type;
+ free_definition (hp->value.defn);
hp->value.defn = mdef.defn;
}
}
/* Like cpp_get_token, except that it does not read past end-of-line.
Also, horizontal space is skipped, and macros are popped. */
-static enum cpp_token
+enum cpp_token
get_directive_token (pfile)
cpp_reader *pfile;
{
HOST_WIDEST_INT value;
long old_written = CPP_WRITTEN (pfile);
+ /* Work around bug in cpp_get_token where it may mistake an
+ assertion for a directive. */
+ pfile->only_seen_white = 0;
+
value = cpp_parse_expr (pfile);
+ skip_rest_of_line (pfile);
CPP_SET_WRITTEN (pfile, old_written); /* Pop */
return value;
return CPP_NAME;
}
- /* If macro wants an arglist, verify that a '(' follows.
- first skip all whitespace, copying it to the output
- after the macro name. Then, if there is no '(',
- decide this is not a macro call and leave things that way. */
+ /* If macro wants an arglist, verify that a '(' follows. */
if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
{
int macbuf_whitespace = 0;
cpp_read_check_assertion (pfile)
cpp_reader *pfile;
{
- U_CHAR *name = CPP_PWRITTEN (pfile);
+ U_CHAR *name;
int result;
- HASHNODE *hp;
+ long written = CPP_WRITTEN (pfile);
FORWARD (1); /* Skip '#' */
cpp_skip_hspace (pfile);
result = 0;
else
{
- hp = cpp_lookup (pfile, name, CPP_PWRITTEN (pfile) - name);
- result = (hp != 0);
+ name = pfile->token_buffer + written;
+ result = cpp_defined (pfile, name, CPP_PWRITTEN (pfile) - name);
}
- pfile->limit = name;
+ CPP_SET_WRITTEN (pfile, written);
return result;
}
-/* Remember the current position of PFILE. */
+/* Remember the current position of PFILE so it may be returned to
+ after looking ahead a bit.
+
+ Note that when you set a mark, you _must_ return to that mark. You
+ may not forget about it and continue parsing. You may not pop a
+ buffer with an active mark. You may not call CPP_BUMP_LINE while a
+ mark is active. */
static void
parse_set_mark (pfile)
extern int cpp_handle_option PARAMS ((cpp_reader *, int, char **));
extern int cpp_handle_options PARAMS ((cpp_reader *, int, char **));
extern enum cpp_token cpp_get_token PARAMS ((cpp_reader *));
-extern void cpp_skip_hspace PARAMS((cpp_reader *));
extern enum cpp_token cpp_get_non_space_token PARAMS ((cpp_reader *));
+extern enum cpp_token get_directive_token PARAMS ((cpp_reader *));
/* This frees resources used by PFILE. */
extern void cpp_cleanup PARAMS ((cpp_reader *PFILE));
Applying cpp_get_token repeatedly yields a stream of pre-processor
tokens. Usually, there is only one cpp_reader object active. */
-struct hashnode;
-typedef struct hashnode HASHNODE;
-
struct cpp_reader
{
parse_underflow_t get_token;
/* Hash table of macros and assertions. See cpphash.c */
#define HASHSIZE 1403
- HASHNODE **hashtab;
+ struct hashnode **hashtab;
/* Hash table of other included files. See cppfiles.c */
#define ALL_INCLUDE_HASHSIZE 71
T_CONST, /* Constant string, used by `__SIZE_TYPE__' etc */
T_MACRO, /* macro defined by `#define' */
T_DISABLED, /* macro temporarily turned off for rescan */
- T_PCSTRING, /* precompiled string (hashval is KEYDEF *) */
T_POISON, /* defined with `#pragma poison' */
T_UNUSED /* Used for something not defined. */
};
extern cpp_buffer *cpp_push_buffer PARAMS ((cpp_reader *,
unsigned char *, long));
extern cpp_buffer *cpp_pop_buffer PARAMS ((cpp_reader *));
-extern HASHNODE *cpp_lookup PARAMS ((cpp_reader *, const U_CHAR *, int));
+extern int cpp_defined PARAMS ((cpp_reader *, const U_CHAR *, int));
extern void cpp_reader_init PARAMS ((cpp_reader *));
extern void cpp_options_init PARAMS ((cpp_options *));
extern int cpp_start_read PARAMS ((cpp_reader *, char *));
extern int cpp_read_check_assertion PARAMS ((cpp_reader *));
-extern void skip_rest_of_line PARAMS ((cpp_reader *));
extern void cpp_finish PARAMS ((cpp_reader *));
extern void quote_string PARAMS ((cpp_reader *, const char *));
{
while (*names)
{
- if (cpp_lookup (pfile, names, -1))
+ if (cpp_defined (pfile, names, -1))
recognized_macro (names);
names += strlen (names) + 1;
}
cpp_reader_init (&scan_in);
scan_in.opts = &scan_options;
cpp_options_init (&scan_options);
+ /* We are going to be scanning a header file out of its proper context,
+ so ignore warnings and errors. */
+ scan_options.inhibit_warnings = 1;
+ scan_options.inhibit_errors = 1;
i = cpp_handle_options (&scan_in, argc, argv);
if (i < argc && ! CPP_FATAL_ERRORS (&scan_in))
cpp_fatal (&scan_in, "Invalid option `%s'", argv[i]);
--- /dev/null
+/* { dg-do preprocess } */
+
+/* Test for proper handling of unary minus in #if. */
+
+#if !(-1)
+#error Error /* { dg-bogus "Error" "case !(-1)" } */
+#endif
+
+#if !-1
+#error Error /* { dg-bogus "Error" "case !-1" } */
+#endif
+
+#if -1
+#else
+#error Error /* { dg-bogus "Error" "case -1" } */
+#endif
--- /dev/null
+/* { dg-do preprocess } */
+
+/* Test for proper handling of unary plus in #if. */
+
+#if !(+1)
+#error Error /* { dg-bogus "Error" "case !(+1)" } */
+#endif
+
+#if !+1
+#error Error /* { dg-bogus "Error" "case !+1" } */
+#endif
+
+#if +1
+#else
+#error Error /* { dg-bogus "Error" "case +1" } */
+#endif