This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Target CPP builtins, obsolescence of CPP_PREDEFINES


Zack Weinberg wrote:-

> scheme.  To duplicate it for target #defines, you could just create a
> macro called REGISTER_TARGET_DEFINES called in place of your target
> hook.

Done with this patch.  I prepended the macro with TARGET_ like all
recent hooks have been, calling it TARGET_REGISTER_CPP_BUILTINS
as it does a bit more than macros.

This obsoletes CPP_PREDEFINES, so some complex specs are ready to bite
the dust, and when the targets are all converted, will allow removal
of the __ games in gcc.c (see docs in patch for how I handled this;
I think it's the same as what you did in gcc.c).

Loren James Rittle wrote:-

> It appears that __GXX_WEAK__ is no longer defined in every case that
> it had been

Fixed with this patch.

This patch hasn't actually been tested on a target; I might convert
one of the config directories to use the macro rather than
CPP_PREDEFINES as proof of concept.

This patch also removes flag_ansi from the C++ front end; I noticed
it was unused when doing the flag_iso stuff.

Bootstrapping x86 Linux; I'll apply when the testing completes.

Neil.

	* Makefile.in: Update.
	* c-common.c (flag_iso, flag_undef, cb_register_builtins,
	builtin_define_std): New.
	(c_common_init): Register CPP builtins callback.
	* c-common.h (flag_iso, flag_undef): New.
	* c-decl.c (c_decode_option): Set flag_iso and flag_undef.
	* c-lex.c: Don't include target.h.
	(cb_register_builtins): Move to c-common.c.
	(init_c_lex): Don't register hook here.
	* c-lex.h (builtin_define, builtin_assert, builtin_define_std): New.
	(cpp_define, cpp_assert): Remove.
	* gcc.c (cc1_options): Pass -undef to front end.
	* target-def.h (TARGET_REGISTER_CPP_BUILTINS): Remove.
	(TARGET_INITIALIZER): Update.
	* target.h (struct cpp_reader): Don't predeclare.
	(struct gcc_target): Remove cpp builtin hook.
	* tree.c (default_register_cpp_builtins): Remove.
cp:
	* cp-tree.h (flag_ansi): Remove.
	* decl2.c (flag_ansi): Remove.
	(cxx_decode_option): Set flag_iso and flag_undef.
doc:
	* tm.texi: Update.

============================================================
Index: gcc/Makefile.in
--- gcc/Makefile.in	9 May 2002 12:54:15 -0000	1.869
+++ gcc/Makefile.in	9 May 2002 20:17:36 -0000
@@ -1157,7 +1157,7 @@ c-typeck.o : c-typeck.c $(CONFIG_H) $(SY
 c-lang.o : c-lang.c $(CONFIG_H) $(SYSTEM_H) $(TREE_H) $(C_TREE_H) \
     langhooks.h langhooks-def.h c-common.h
 c-lex.o : c-lex.c $(CONFIG_H) $(SYSTEM_H) $(TREE_H) $(RTL_H) c-lex.h \
-    debug.h $(C_TREE_H) c-common.h $(TARGET_H) \
+    debug.h $(C_TREE_H) c-common.h \
     c-pragma.h input.h intl.h flags.h toplev.h output.h \
     mbchar.h $(CPPLIB_H) $(EXPR_H) $(TM_P_H)
 c-objc-common.o : c-objc-common.c $(CONFIG_H) $(SYSTEM_H) $(TREE_H) \
============================================================
Index: gcc/c-common.c
--- gcc/c-common.c	7 May 2002 21:07:13 -0000	1.319
+++ gcc/c-common.c	9 May 2002 20:17:46 -0000
@@ -184,6 +184,14 @@ tree c_global_trees[CTI_MAX];
 /* Nonzero if prepreprocessing only.  */
 int flag_preprocess_only;
 
+/* Nonzero if an ISO standard was selected.  It rejects macros in the
+   user's namespace.  */
+int flag_iso;
+
+/* Nonzero if -undef was given.  It suppresses target built-in macros
+   and assertions.  */
+int flag_undef;
+
 /* Nonzero means don't recognize the non-ANSI builtin functions.  */
 
 int flag_no_builtin;
@@ -275,6 +283,8 @@ static int if_stack_space = 0;
 /* Stack pointer.  */
 static int if_stack_pointer = 0;
 
+static void cb_register_builtins PARAMS ((cpp_reader *));
+
 static tree handle_packed_attribute	PARAMS ((tree *, tree, tree, int,
 						 bool *));
 static tree handle_nocommon_attribute	PARAMS ((tree *, tree, tree, int,
@@ -4298,6 +4308,75 @@ c_common_post_options ()
     warning ("-Wmissing-format-attribute ignored without -Wformat");
 }
 
+/* Hook that registers front end and target-specific built-ins.  */
+static void
+cb_register_builtins (pfile)
+     cpp_reader *pfile;
+{
+  /* -undef turns off target-specific built-ins.  */
+  if (flag_undef)
+    return;
+
+  if (c_language == clk_cplusplus)
+    {
+      if (SUPPORTS_ONE_ONLY)
+	cpp_define (pfile, "__GXX_WEAK__");
+      else
+	cpp_define (pfile, "__GXX_WEAK__=0");
+    }
+
+  /* A straightforward target hook doesn't work, because of problems
+     linking that hook's body when part of non-C front ends.  */
+#ifdef REGISTER_TARGET_CPP_BUILTINS
+  TARGET_REGISTER_CPP_BUILTINS;
+#endif
+}
+
+/* Pass an object-like macro.  If it doesn't lie in the user's
+   namespace, defines it unconditionally.  Otherwise define a version
+   with two leading underscores, and another version with two leading
+   and trailing underscores, and define the original only if an ISO
+   standard was not nominated.
+
+   e.g. passing "unix" defines "__unix", "__unix__" and possibly
+   "unix".  Passing "_mips" defines "__mips", "__mips__" and possibly
+   "_mips".  */
+void
+builtin_define_std (macro)
+     const char *macro;
+{
+  size_t len = strlen (macro);
+  char *buff = xmalloc (len + 5);
+  char *p = buff + 2;
+  char *q = p + len;
+
+  /* prepend __ (or maybe just _) if in user's namespace.  */
+  memcpy (p, macro, len + 1);
+  if (*p != '_')
+    *--p = '_';
+  if (p[1] != '_' && !ISUPPER (p[1]))
+    *--p = '_';
+  cpp_define (parse_in, p);
+
+  /* If it was in user's namespace...  */
+  if (p != buff + 2)
+    {
+      /* Define the original macro if permitted.  */
+      if (!flag_iso)
+	cpp_define (parse_in, buff + 2);
+
+      /* Define the macro with leading and following __.  */
+      if (q[-1] != '_')
+	*q++ = '_';
+      if (q[-2] != '_')
+	*q++ = '_';
+      *q = '\0';
+      cpp_define (parse_in, p);
+    }
+
+  free (buff);
+}
+
 /* Front end initialization common to C, ObjC and C++.  */
 const char *
 c_common_init (filename)
@@ -4321,6 +4400,10 @@ c_common_init (filename)
      options->unsigned_char = !flag_signed_char; */
 
   options->warn_multichar = warn_multichar;
+
+  /* Register preprocessor built-ins before calls to
+     cpp_main_file.  */
+  cpp_get_callbacks (parse_in)->register_builtins = cb_register_builtins;
 
   /* NULL is passed up to toplev.c and we exit quickly.  */
   if (flag_preprocess_only)
============================================================
Index: gcc/c-common.h
--- gcc/c-common.h	6 May 2002 22:52:57 -0000	1.134
+++ gcc/c-common.h	9 May 2002 20:17:48 -0000
@@ -378,6 +378,14 @@ extern c_language_kind c_language;
 /* Nonzero if prepreprocessing only.  */
 extern int flag_preprocess_only;
 
+/* Nonzero if an ISO standard was selected.  It rejects macros in the
+   user's namespace.  */
+extern int flag_iso;
+
+/* Nonzero if -undef was given.  It suppresses target built-in macros
+   and assertions.  */
+extern int flag_undef;
+
 /* Nonzero means give string constants the type `const char *', rather
    than `char *'.  */
 
============================================================
Index: gcc/c-decl.c
--- gcc/c-decl.c	6 May 2002 22:52:57 -0000	1.324
+++ gcc/c-decl.c	9 May 2002 20:18:05 -0000
@@ -541,6 +541,7 @@ c_decode_option (argc, argv)
 	  flag_no_nonansi_builtin = 1;
 	  flag_noniso_default_format_attributes = 0;
 	  flag_isoc99 = 0;
+	  flag_iso = 1;
 	}
       else if (!strcmp (argstart, "iso9899:199409"))
 	{
@@ -558,6 +559,7 @@ c_decode_option (argc, argv)
 	  flag_noniso_default_format_attributes = 0;
 	  flag_isoc99 = 1;
 	  flag_isoc94 = 1;
+	  flag_iso = 1;
 	}
       else if (!strcmp (argstart, "gnu89"))
 	{
@@ -636,6 +638,8 @@ c_decode_option (argc, argv)
     ;
   else if (!strcmp (p, "-ansi"))
     goto iso_1990;
+  else if (!strcmp (p, "-undef"))
+    flag_undef = 1;
   else if (!strcmp (p, "-Werror-implicit-function-declaration"))
     mesg_implicit_function_declaration = 2;
   else if (!strncmp (p, "-Wformat=", 9))
============================================================
Index: gcc/c-lex.c
--- gcc/c-lex.c	6 May 2002 22:52:57 -0000	1.176
+++ gcc/c-lex.c	9 May 2002 20:18:09 -0000
@@ -39,7 +39,6 @@ Software Foundation, 59 Temple Place - S
 #include "tm_p.h"
 #include "splay-tree.h"
 #include "debug.h"
-#include "target.h"
 
 #ifdef MULTIBYTE_CHARS
 #include "mbchar.h"
@@ -81,7 +80,6 @@ static tree lex_string		PARAMS ((const u
 static tree lex_charconst	PARAMS ((const cpp_token *));
 static void update_header_times	PARAMS ((const char *));
 static int dump_one_header	PARAMS ((splay_tree_node, void *));
-static void cb_register_builtins PARAMS ((cpp_reader *));
 static void cb_line_change     PARAMS ((cpp_reader *, const cpp_token *, int));
 static void cb_ident		PARAMS ((cpp_reader *, unsigned int,
 					 const cpp_string *));
@@ -123,7 +121,6 @@ init_c_lex (filename)
   cb->ident = cb_ident;
   cb->file_change = cb_file_change;
   cb->def_pragma = cb_def_pragma;
-  cb->register_builtins = cb_register_builtins;
 
   /* Set the debug callbacks if we can use them.  */
   if (debug_info_level == DINFO_LEVEL_VERBOSE
@@ -227,25 +224,6 @@ dump_time_statistics ()
 
   splay_tree_foreach (file_info_tree, dump_one_header, 0);
 }
-
-/* Register preprocessor built-ins.  */
-static void
-cb_register_builtins (pfile)
-     cpp_reader *pfile;
-{
-  if (c_language == clk_cplusplus)
-    {
-      if (SUPPORTS_ONE_ONLY)
-	cpp_define (pfile, "__GXX_WEAK__");
-      else
-	cpp_define (pfile, "__GXX_WEAK__=0");
-    }
-
-  (*targetm.register_cpp_builtins) (pfile);
-}
-
-/* Not yet handled: #pragma, #define, #undef.
-   No need to deal with linemarkers under normal conditions.  */
 
 static void
 cb_ident (pfile, line, str)
============================================================
Index: gcc/c-lex.h
--- gcc/c-lex.h	4 May 2002 20:14:58 -0000	1.27
+++ gcc/c-lex.h	9 May 2002 20:18:09 -0000
@@ -34,9 +34,19 @@ extern int indent_level;
 
 struct cpp_reader;
 extern struct cpp_reader* parse_in;
-/* Copied from cpplib.h to avoid target code having to pull in all of
-   cpplib.h.  */
-extern void cpp_define PARAMS ((struct cpp_reader *, const char *));
-extern void cpp_assert PARAMS ((struct cpp_reader *, const char *));
+
+#define builtin_define(TXT) cpp_define (parse_in, TXT)
+#define builtin_assert(TXT) cpp_assert (parse_in, TXT)
+
+/* Pass an object-like macro.  If it doesn't lie in the user's
+   namespace, defines it unconditionally.  Otherwise define a version
+   with two leading underscores, and another version with two leading
+   and trailing underscores, and define the original only if an ISO
+   standard was not nominated.
+
+   e.g. passing "unix" defines "__unix", "__unix__" and possibly
+   "unix".  Passing "_mips" defines "__mips", "__mips__" and possibly
+   "_mips".  */
+extern void builtin_define_std PARAMS ((const char *));
 
 #endif /* ! GCC_C_LEX_H */
============================================================
Index: gcc/gcc.c
--- gcc/gcc.c	23 Apr 2002 22:57:16 -0000	1.312
+++ gcc/gcc.c	9 May 2002 20:18:24 -0000
@@ -701,7 +701,7 @@ static const char *cc1_options =
 "%{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  %1 %{!Q:-quiet} -dumpbase %B %{d*} %{m*} %{a*}\
  %{g*} %{O*} %{W*&pedantic*} %{w} %{std*} %{ansi}\
- %{v:-version} %{pg:-p} %{p} %{f*}\
+ %{v:-version} %{pg:-p} %{p} %{f*} %{undef}\
  %{Qn:-fno-ident} %{--help:--help}\
  %{--target-help:--target-help}\
  %{!fsyntax-only:%{S:%W{o*}%{!o*:-o %b.s}}}\
============================================================
Index: gcc/target-def.h
--- gcc/target-def.h	4 May 2002 20:14:58 -0000	1.22
+++ gcc/target-def.h	9 May 2002 20:18:24 -0000
@@ -179,7 +179,6 @@ Foundation, 59 Temple Place - Suite 330,
 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES default_set_default_type_attributes
 #define TARGET_INSERT_ATTRIBUTES default_insert_attributes
 #define TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P default_function_attribute_inlinable_p
-#define TARGET_REGISTER_CPP_BUILTINS default_register_cpp_builtins
 #define TARGET_MS_BITFIELD_LAYOUT_P default_ms_bitfield_layout_p
 
 /* In builtins.c.  */
@@ -206,7 +205,6 @@ Foundation, 59 Temple Place - Suite 330,
   TARGET_SET_DEFAULT_TYPE_ATTRIBUTES,		\
   TARGET_INSERT_ATTRIBUTES,			\
   TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P,	\
-  TARGET_REGISTER_CPP_BUILTINS,			\
   TARGET_MS_BITFIELD_LAYOUT_P,			\
   TARGET_INIT_BUILTINS,				\
   TARGET_EXPAND_BUILTIN,			\
============================================================
Index: gcc/target.h
--- gcc/target.h	4 May 2002 20:14:58 -0000	1.24
+++ gcc/target.h	9 May 2002 20:18:25 -0000
@@ -44,8 +44,6 @@ Foundation, 59 Temple Place - Suite 330,
    to gradually reduce the amount of conditional compilation that is
    scattered throughout GCC.  */
 
-struct cpp_reader;
-
 struct gcc_target
 {
   /* Functions that output assembler for the target.  */
@@ -199,8 +197,6 @@ struct gcc_target
   /* Return true if FNDECL (which has at least one machine attribute)
      can be inlined despite its machine attributes, false otherwise.  */
   bool (* function_attribute_inlinable_p) PARAMS ((tree fndecl));
-
-  void (* register_cpp_builtins) PARAMS ((struct cpp_reader *));
 
   /* Return true if bitfields in RECORD_TYPE should follow the
      Microsoft Visual C++ bitfield layout rules.  */
============================================================
Index: gcc/tree.c
--- gcc/tree.c	4 May 2002 20:14:59 -0000	1.256
+++ gcc/tree.c	9 May 2002 20:18:35 -0000
@@ -2543,13 +2543,6 @@ build_type_attribute_variant (ttype, att
   return ttype;
 }
 
-/* Default registration of target-specific CPP built-ins.  */
-void
-default_register_cpp_builtins (pfile)
-     struct cpp_reader *pfile ATTRIBUTE_UNUSED;
-{
-}
-
 /* Default value of targetm.comp_type_attributes that always returns 1.  */
 
 int
============================================================
Index: gcc/cp/cp-tree.h
--- gcc/cp/cp-tree.h	6 May 2002 22:53:08 -0000	1.712
+++ gcc/cp/cp-tree.h	9 May 2002 20:18:47 -0000
@@ -1021,12 +1021,6 @@ extern int interface_only, interface_unk
 
 extern int flag_elide_constructors;
 
-/* Nonzero means enable obscure standard features and disable GNU
-   extensions that might cause standard-compliant code to be
-   miscompiled.  */
-
-extern int flag_ansi;
-
 /* Nonzero means that member functions defined in class scope are
    inline by default.  */
 
============================================================
Index: gcc/cp/decl2.c
--- gcc/cp/decl2.c	6 May 2002 22:53:08 -0000	1.531
+++ gcc/cp/decl2.c	9 May 2002 20:18:59 -0000
@@ -136,12 +136,6 @@ int flag_no_gnu_keywords;
 
 int flag_signed_bitfields = 1;
 
-/* Nonzero means enable obscure standard features and disable GNU
-   extensions that might cause standard-compliant code to be
-   miscompiled.  */
-
-int flag_ansi;
-
 /* Nonzero means do emit exported implementations of functions even if
    they can be inlined.  */
 
@@ -699,8 +693,10 @@ cxx_decode_option (argc, argv)
     }
   else if (!strcmp (p, "-E"))
     flag_preprocess_only = 1;
+  else if (!strcmp (p, "-undef"))
+    flag_undef = 1;
   else if (!strcmp (p, "-ansi"))
-    flag_no_nonansi_builtin = 1, flag_ansi = 1,
+    flag_no_nonansi_builtin = 1, flag_iso = 1,
     flag_noniso_default_format_attributes = 0, flag_no_gnu_keywords = 1;
 #ifdef SPEW_DEBUG
   /* Undocumented, only ever used when you're invoking cc1plus by hand, since
============================================================
Index: gcc/doc/tm.texi
--- gcc/doc/tm.texi	7 May 2002 21:07:24 -0000	1.121
+++ gcc/doc/tm.texi	9 May 2002 20:19:29 -0000
@@ -622,19 +622,32 @@ The macro @code{STANDARD_STARTFILE_PREFI
 @c prevent bad page break with this line
 Here are run-time target specifications.
 
-@deftypefn {Target Hook} void TARGET_REGISTER_CPP_BUILTINS (cpp_reader *@var{pfile})
-This macro expands to a target-specific function, called by the C
-family of front ends, that allows you to define preprocessor built-in
-macros and assertions at run-time.
+@table @code
+@findex TARGET_REGISTER_CPP_BUILTINS
+@item TARGET_REGISTER_CPP_BUILTINS
+This macro expands to a block of code that defines target-specific
+built-in preprocessor macros and assertions, using the functions
+@code{builtin_macro}, @code{builtin_macro_std} and
+@code{builtin_assert} declared in @file{c-lex.h}.
 
-Pass the argument (a preprocessor handle) as the first argument to the
-functions @code{cpp_define} and @code{cpp_assert}, declared in
-@file{c-lex.h}.  The second argument is the same as the argument to
-the respective command-line option, for example @code{__mips__} for
-@code{cpp_define}, and @code{cpu=mips} for @code{cpp_assert}.
-@end deftypefn
+@code{builtin_assert} takes a string in the form you pass to the
+command-line option @option{-A}, such as @code{cpu=mips}, and creates
+the assertion.  @code{builtin_macro} takes a string in the form
+accepted by option @option{-D} and unconditionally defines the macro.
+
+@code{builtin_macro_std} takes a string representing the name of an
+object-like macro.  If it doesn't lie in the user's namespace,
+@code{builtin_macro_std} defines it unconditionally.  Otherwise, it
+defines a version with two leading underscores, and another version
+with two leading and trailing underscores, and defines the original
+only if an ISO standard was not requested on the command line.  For
+example, passing @code{unix} defines @code{__unix}, @code{__unix__}
+and possibly @code{unix}; passing @code{_mips} defines @code{__mips},
+@code{__mips__} and possibly @code{_mips}, and passing @code{_ABI64}
+defines only @code{_ABI64}.
+
+This macro obsoletes the @code{CPP_PREDEFINES} target macro.
 
-@table @code
 @findex CPP_PREDEFINES
 @item CPP_PREDEFINES
 Define this to be a string constant containing @option{-D} options to


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]