* Makefile.in (cppexp.o): Depend on cpphash.h.
* cppexp.c (cpp_lex): Handle `defined (xxx)' for poisoned xxx.
Include cpphash.h.
* cpphash.c (special_symbol): Handle plain `xxx' for poisoned xxx.
* cpplib.c (do_define): Generalise to handle poisoned definitions,
redefining poisoned identifiers, etc.
(do_undef): Don't allow poisoned identifiers to be undefined.
(do_pragma): Add #pragma poison.
(do_xifdef): Handle `#ifdef xxx' for poisoned xxx.
* cccp.c: Add T_POISON node type.
(special_symbol): Handle `defined(xxx)' and plain `xxx' for
poisoned xxx.
(do_define): Generalise to handle poisoned definitions,
redefining poisoned identifiers, etc.
(do_undef): Don't allow poisoned identifiers to be undefined.
(do_pragma): Add #pragma poison.
(do_xifdef): Handle `#ifdef xxx' for poisoned xxx.
* c-pragma.c (handle_pragma_token): Ignore #pragma poison.
* c-pragma.h: Add ps_poison state. We now always have generic
pragmas.
From-SVN: r29224
+Thu Sep 9 13:46:06 1999 Geoffrey Keating <geoffk@cygnus.com>
+
+ * Makefile.in (cppexp.o): Depend on cpphash.h.
+ * cppexp.c (cpp_lex): Handle `defined (xxx)' for poisoned xxx.
+ Include cpphash.h.
+ * cpphash.c (special_symbol): Handle plain `xxx' for poisoned xxx.
+ * cpplib.c (do_define): Generalise to handle poisoned definitions,
+ redefining poisoned identifiers, etc.
+ (do_undef): Don't allow poisoned identifiers to be undefined.
+ (do_pragma): Add #pragma poison.
+ (do_xifdef): Handle `#ifdef xxx' for poisoned xxx.
+
+ * cccp.c: Add T_POISON node type.
+ (special_symbol): Handle `defined(xxx)' and plain `xxx' for
+ poisoned xxx.
+ (do_define): Generalise to handle poisoned definitions,
+ redefining poisoned identifiers, etc.
+ (do_undef): Don't allow poisoned identifiers to be undefined.
+ (do_pragma): Add #pragma poison.
+ (do_xifdef): Handle `#ifdef xxx' for poisoned xxx.
+
+ * c-pragma.c (handle_pragma_token): Ignore #pragma poison.
+ * c-pragma.h: Add ps_poison state. We now always have generic
+ pragmas.
+
Wed Sep 8 20:30:42 1999 Mark Mitchell <mark@codesourcery.com>
* ggc.h (ggc_alloc): New function.
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
+cppexp.o: cppexp.c $(CONFIG_H) cpplib.h intl.h system.h cpphash.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 \
ret_val = 1; /* Ignore the pragma. */
break;
#endif /* HANDLE_PRAGMA_WEAK */
+
+ case ps_poison:
+ ret_val = 1;
+ break;
}
type = state = ps_start;
#ifdef HANDLE_PRAGMA_WEAK
if (strcmp (string, "weak") == 0)
type = state = ps_weak;
-#endif
+#endif
+ if (strcmp (string, "poison") == 0)
+ type = state = ps_poison;
break;
-
+
#ifdef HANDLE_PRAGMA_WEAK
case ps_weak:
name = permalloc (strlen (string) + 1);
state = ps_bad;
break;
#endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
-
+
+ case ps_poison:
+ if (token && TREE_CODE (token) != IDENTIFIER_NODE)
+ state = ps_bad;
+ break;
+
case ps_bad:
case ps_done:
break;
#endif /* HANDLE_PRAGMA_WEAK */
-#if defined HANDLE_PRAGMA_PACK || defined HANDLE_PRAGMA_WEAK
/* Define HANDLE_GENERIC_PRAGMAS if any kind of front-end pragma
parsing is to be done. The code in GCC's generic C source files
will only look for the definition of this constant. They will
- ignore definitions of HANDLE_PRAGMA_PACK and so on. */
+ ignore definitions of HANDLE_PRAGMA_PACK and so on.
+ With #pragma poison, this is always set. */
#define HANDLE_GENERIC_PRAGMAS 1
-#endif
#ifdef HANDLE_GENERIC_PRAGMAS
ps_push, ps_pushcomma, ps_pushid, ps_pushcomma2,
ps_pop, ps_popcomma,
#endif
+ ps_poison,
ps_bad
};
T_DISABLED, /* macro temporarily turned off for rescan */
T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
T_PCSTRING, /* precompiled string (hashval is KEYDEF *) */
+ T_POISON, /* defined with `#pragma poison' */
T_UNUSED /* Used for something not defined. */
};
|| (hp->type == T_MACRO && hp->value.defn->predefined)))
/* Output a precondition for this macro use. */
fprintf (pcp_outfile, "#define %s\n", hp->name);
- buf = " 1 ";
+ if (hp->type == T_POISON) {
+ error("attempt to use poisoned `%s'.", hp->name);
+ buf = " 0 ";
+ } else {
+ buf = " 1 ";
+ }
}
else
if (pcp_outfile && pcp_inside_if) {
}
break;
+ case T_POISON:
+ error("attempt to use poisoned `%s'.", hp->name);
+ buf = " 0 "; /* Consider poisoned symbol to not be defined */
+ break;
+
oops:
error ("`defined' without an identifier");
{
int hashcode;
MACRODEF mdef;
+ enum node_type newtype = keyword->type == T_DEFINE ? T_MACRO : T_POISON;
/* If this is a precompiler run (with -pcp) pass thru #define directives. */
if (pcp_outfile && op)
/* 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)
+ ok = -1;
+ /* Poisoning anything else is not ok.
+ The poison should always come first. */
+ else if (newtype == T_POISON)
+ ok = 0;
/* Redefining a macro is ok if the definitions are the same. */
else if (hp->type == T_MACRO)
ok = ! compare_defs (mdef.defn, hp->value.defn);
/* Redefining a constant is ok with -D. */
else if (hp->type == T_CONST)
ok = ! done_initializing;
- /* Print the warning if it's not ok. */
- if (!ok) {
- /* If we are passing through #define and #undef directives, do
- that for this re-definition now. */
- if (debug_output && op)
- pass_thru_directive (buf, limit, op, keyword);
-
- pedwarn ("`%.*s' redefined", mdef.symlen, mdef.symnam);
- if (hp->type == T_MACRO)
- pedwarn_with_file_and_line (hp->value.defn->file,
- hp->value.defn->file_len,
- hp->value.defn->line,
- "this is the location of the previous definition");
- }
- /* Replace the old definition. */
- hp->type = T_MACRO;
- hp->value.defn = mdef.defn;
+
+ /* Print the warning or error if it's not ok. */
+ if (ok <= 0)
+ {
+ /* If we are passing through #define and #undef directives, do
+ that for this re-definition now. */
+ if (debug_output && op)
+ pass_thru_directive (buf, limit, op, keyword);
+
+ if (hp->type == T_POISON)
+ error ("redefining poisoned `%.*s'", mdef.symlen, mdef.symnam);
+ else
+ pedwarn ("`%.*s' redefined", mdef.symlen, mdef.symnam);
+ if (hp->type == T_MACRO)
+ pedwarn_with_file_and_line (hp->value.defn->file,
+ hp->value.defn->file_len,
+ hp->value.defn->line,
+ "this is the location of the previous definition");
+ }
+ if (hp->type != T_POISON)
+ {
+ /* Replace the old definition. */
+ hp->type = newtype;
+ hp->value.defn = mdef.defn;
+ }
} else {
/* If we are passing through #define and #undef directives, do
that for this new definition now. */
if (debug_output && op)
pass_thru_directive (buf, limit, op, keyword);
- install (mdef.symnam, mdef.symlen, T_MACRO,
+ install (mdef.symnam, mdef.symlen, newtype,
(char *) mdef.defn, hashcode);
}
}
need to pass through all effective #undef directives. */
if (debug_output && op)
pass_thru_directive (orig_buf, limit, op, keyword);
- if (hp->type != T_MACRO)
- warning ("undefining `%s'", hp->name);
- delete_macro (hp);
+ if (hp->type == T_POISON)
+ error ("cannot undefine poisoned `%s'", hp->name);
+ else {
+ if (hp->type != T_MACRO)
+ warning ("undefining `%s'", hp->name);
+ delete_macro (hp);
+ }
}
if (pedantic) {
static int
do_pragma (buf, limit, op, keyword)
- U_CHAR *buf, *limit ATTRIBUTE_UNUSED;
- FILE_BUF *op ATTRIBUTE_UNUSED;
- struct directive *keyword ATTRIBUTE_UNUSED;
+ U_CHAR *buf, *limit;
+ FILE_BUF *op;
+ struct directive *keyword;
{
SKIP_WHITE_SPACE (buf);
if (!strncmp ((char *) buf, "once", 4)) {
do_once ();
}
+ if (!strncmp (buf, "poison", 6)) {
+ /* Poison these symbols so that all subsequent usage produces an
+ error message. */
+ U_CHAR *p = buf + 6;
+
+ SKIP_WHITE_SPACE (p);
+ while (p < limit)
+ {
+ U_CHAR *end = p;
+
+ while (end < limit && is_idchar[*end])
+ end++;
+ if (end < limit && !is_space[*end])
+ {
+ error ("invalid #pragma poison");
+ return 0;
+ }
+ do_define(p, end, op, keyword);
+ p = end;
+ SKIP_WHITE_SPACE (p);
+ }
+ }
+
if (!strncmp ((char *) buf, "implementation", 14)) {
/* Be quiet about `#pragma implementation' for a file only if it hasn't
been included yet. */
}
}
+ if ((hp != NULL) && (hp->type == T_POISON)) {
+ error("attempt to use poisoned `%s'.", hp->name);
+ hp = NULL;
+ }
skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
if (start_of_file && !skip) {
control_macro = (U_CHAR *) xmalloc (end - buf + 1);
* Concatenation:: Building tokens from parts taken from macro arguments.
* Undefining:: Cancelling a macro's definition.
* Redefining:: Changing a macro's definition.
+* Poisoning:: Ensuring a macro is never defined or used.
* Macro Pitfalls:: Macros can confuse the unwary. Here we explain
several common problems and strange features.
@end menu
arguments or definitions that don't expect arguments. The @samp{#undef}
directive has no effect when used on a name not currently defined as a macro.
-@node Redefining, Macro Pitfalls, Undefining, Macros
+@node Redefining, Poisoning, Undefining, Macros
@subsection Redefining Macros
@cindex redefining macros
Recall that a comment counts as whitespace.
-@node Macro Pitfalls,, Redefining, Macros
+@node Poisoning, Macro Pitfalls, Redefining, Macros
+@subsection Poisoning Macros
+@cindex poisoning macros
+
+Sometimes, there is an identifier that you want to remove completely
+from your program, and make sure that it never creeps back in. To
+enforce this, the @samp{#pragma poison} directive can be used.
+@samp{#pragma poison} is followed by a list of identifiers to poison,
+and takes effect for the rest of the source. You cannot @samp{#undef} a
+poisoned identifier or test to see if it's defined with @samp{#ifdef}.
+
+For example,
+
+@example
+#pragma poison printf sprintf fprintf
+sprintf(some_string, "hello");
+@end example
+
+@noindent
+will produce an error.
+
+@node Macro Pitfalls,, Poisoning, Macros
@subsection Pitfalls and Subtleties of Macros
@cindex problems with macros
@cindex pitfalls of macros
#include "config.h"
#include "system.h"
#include "cpplib.h"
+#include "cpphash.h"
#ifdef MULTIBYTE_CHARS
#include <locale.h>
int paren = 0, len;
cpp_buffer *ip = CPP_BUFFER (pfile);
U_CHAR *tok;
+ HASHNODE *hp;
cpp_skip_hspace (pfile);
if (*ip->cur == '(')
goto oops;
++ip->cur;
}
- if (cpp_lookup (pfile, tok, len, -1))
- op.value = 1;
-
+ hp = cpp_lookup (pfile, tok, len, -1);
+ if (hp != NULL)
+ {
+ if (hp->type == T_POISON)
+ cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
+ else
+ op.value = 1;
+ }
}
return op;
return;
}
+ case T_POISON:
+ cpp_error (pfile, "attempt to use poisoned `%s'.", hp->name);
+ CPP_RESERVE (pfile, 1);
+ CPP_PUTC_Q (pfile, '0');
+ CPP_NUL_TERMINATE_Q (pfile);
+ break;
+
default:
cpp_fatal (pfile, "cpplib internal error: invalid special hash type");
return;
}
/* Process a #define command.
-KEYWORD is the keyword-table entry for #define,
-or NULL for a "predefined" macro. */
+ KEYWORD is the keyword-table entry for #define,
+ or NULL for a "predefined" macro,
+ or the keyword-table entry for #pragma in the case of a #pragma poison. */
static int
do_define (pfile, keyword)
HASHNODE *hp;
long here;
U_CHAR *macro, *buf, *end;
+ enum node_type new_type;
here = CPP_WRITTEN (pfile);
copy_rest_of_line (pfile);
+ if (keyword == NULL || keyword->type == T_DEFINE)
+ new_type = T_MACRO;
+ else
+ new_type = T_POISON;
+
/* Copy out the line so we can pop the token buffer. */
buf = pfile->token_buffer + here;
end = CPP_PWRITTEN (pfile);
/* 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)
+ ok = -1;
/* Redefining a macro is ok if the definitions are the same. */
else if (hp->type == T_MACRO)
ok = ! compare_defs (pfile, mdef.defn, hp->value.defn);
/* Redefining a constant is ok with -D. */
else if (hp->type == T_CONST || hp->type == T_STDC)
ok = ! CPP_OPTIONS (pfile)->done_initializing;
- /* Print the warning if it's not ok. */
- if (!ok)
+ /* Print the warning or error if it's not ok. */
+ if (ok <= 0)
{
- cpp_pedwarn (pfile, "`%.*s' redefined", mdef.symlen, mdef.symnam);
+ if (hp->type == T_POISON)
+ cpp_error (pfile, "redefining poisoned `%.*s'",
+ mdef.symlen, mdef.symnam);
+ else
+ cpp_pedwarn (pfile, "`%.*s' redefined", mdef.symlen, mdef.symnam);
if (hp->type == T_MACRO)
cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file,
hp->value.defn->line,
"this is the location of the previous definition");
}
- /* Replace the old definition. */
- hp->type = T_MACRO;
- hp->value.defn = mdef.defn;
+ if (hp->type != T_POISON)
+ {
+ /* Replace the old definition. */
+ hp->type = new_type;
+ hp->value.defn = mdef.defn;
+ }
}
else
- cpp_install (pfile, mdef.symnam, mdef.symlen, T_MACRO,
+ cpp_install (pfile, mdef.symnam, mdef.symlen, new_type,
(char *) mdef.defn, hashcode);
- if (keyword)
+ if (keyword != NULL && keyword->type == T_DEFINE)
{
if (CPP_OPTIONS (pfile)->debug_output
|| CPP_OPTIONS (pfile)->dump_macros == dump_definitions)
need to pass through all effective #undef commands. */
if (CPP_OPTIONS (pfile)->debug_output && keyword)
pass_thru_directive (name, sym_length, pfile, keyword);
- if (hp->type != T_MACRO)
- cpp_warning (pfile, "undefining `%s'", hp->name);
- delete_macro (hp);
+ if (hp->type == T_POISON)
+ cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
+ else
+ {
+ if (hp->type != T_MACRO)
+ cpp_warning (pfile, "undefining `%s'", hp->name);
+ delete_macro (hp);
+ }
}
return 0;
"`#pragma implementation' for `%s' appears after file is included",
fcopy);
}
+ else if (!strncmp (buf, "poison", 6))
+ {
+ /* Poison these symbols so that all subsequent usage produces an
+ error message. */
+ U_CHAR *p = buf + 6;
+ size_t plen;
+ U_CHAR *syms;
+ int writeit;
+
+ SKIP_WHITE_SPACE (p);
+ plen = strlen(p) + 1;
+
+ syms = (U_CHAR *) alloca (plen);
+ memcpy (syms, p, plen);
+
+ /* As a rule, don't include #pragma poison commands in output,
+ unless the user asks for them. */
+ writeit = (CPP_OPTIONS (pfile)->debug_output
+ || CPP_OPTIONS (pfile)->dump_macros == dump_definitions
+ || CPP_OPTIONS (pfile)->dump_macros == dump_names);
+
+ if (writeit)
+ CPP_SET_WRITTEN (pfile, here);
+ else
+ CPP_SET_WRITTEN (pfile, here-8);
+
+ if (writeit)
+ {
+ CPP_RESERVE (pfile, plen + 7);
+ CPP_PUTS_Q (pfile, "poison", 7);
+ }
+
+ while (*syms != '\0')
+ {
+ U_CHAR *end = syms;
+
+ while (is_idchar[*end])
+ end++;
+
+ if (!is_hor_space[*end] && *end != '\0')
+ {
+ cpp_error (pfile, "invalid #pragma poison directive");
+ return 1;
+ }
+
+ if (cpp_push_buffer (pfile, syms, end - syms) != NULL)
+ {
+ do_define (pfile, keyword);
+ cpp_pop_buffer (pfile);
+ }
+ if (writeit)
+ {
+ CPP_PUTC_Q (pfile, ' ');
+ CPP_PUTS_Q (pfile, syms, end - syms);
+ }
+ syms = end;
+ SKIP_WHITE_SPACE (syms);
+ }
+ }
return 0;
}
control_macro = (U_CHAR *) xmalloc (ident_length + 1);
bcopy (ident, control_macro, ident_length + 1);
}
+ if (hp != NULL && hp->type == T_POISON)
+ {
+ cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
+ skip = !skip;
+ }
}
else
{
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. */
};
+1999-09-08 Geoffrey Keating <geoffk@cygnus.com>
+
+ * noncompile/noncompile.exp: Add poison-1.c.
+ * noncompile/poison-1.c: New file.
+
1999-09-06 Franz Sirl <Franz.Sirl-kernel@lauterbach.com>
* execute/va-arg-9.c: New test.
set groups {passed gcc-noncompile}
postbase_with_opts $src_code $run $groups
+
+# Test poison-1.c
+prebase
+
+set src_code poison-1.c
+set compiler_output ".*c:2:.*c:4:.*c:5:.*c:7:.*c:8:.*c:9:.*c:11:.*c:12:.*c:13:.*c:14:.*c:17:.*c:20:"
+
+set groups {passed gcc-noncompile}
+
+postbase $src_code $run $groups
--- /dev/null
+#pragma poison foo
+foo
+#pragma poison foo2 foo3
+foo2
+foo3
+#pragma poison foo4 foo5
+foo4
+foo5
+#pragma poison +++
+#define foo6 123
+#pragma poison foo6
+#define foo6 345
+#define foo6 456
+#ifdef foo6
+#error hey! foo6 poisoned!
+#endif
+#if defined(foo6)
+#error no, foo6 still poisoned!
+#else
+foo6
+#endif
+#pragma poison