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]

[PLUGINS] Add 'extern "C"' wrappers to several header files


I am writing a gcc plugin in C++.  In order to use gcc's functions
and objects from C++, they need to be declared as having C linkage.
The attached patch wraps the body of various gcc header files with
  #ifdef __cplusplus
  extern "C" {
  #endif /* __cplusplus */
  ...
  #ifdef __cplusplus
  }
  #endif /* __cplusplus */
I made no attempt to do this systematically: these are the headers
I needed to modify for my plugin to link.  At first sight it might
appear that there is no need to modify gcc, since the plugin can
include header files like this:
  extern "C" {
  #include "flags.h"
  }
But in fact it cannot: some gcc header files include C++ aware
system headers.  For example, flags.h includes real.h, and real.h
includes gmp.h, and gmp.h includes C++ system headers if compiled
from C++.  The result is that if I wrap the inclusion of flags.h in
an 'extern "C"' wrapper then C++ code ends up with C linkage, and
compilation fails (templates particularly don't like having C linkage).

2009-07-21 Duncan Sands <baldrick@free.fr>

	* coretypes.h: Wrap in 'extern "C"' when included by C++.
	* diagnostic.h: Likewise.
	* except.h: Likewise.
	* flags.h: Likewise.
	* gcc-plugin.h: Likewise.
	* gimple.h: Likewise.
	* input.h: Likewise.
	* machmode.h: Likewise.
	* output.h: Likewise.
	* real.h: Likewise.
	* rtl.h: Likewise.
	* target.h: Likewise.
	* toplev.h: Likewise.
	* tree-flow.h: Likewise.
	* tree-pass.h: Likewise.
	* tree.h: Likewise.
	* vec.h: Likewise.
	* configure.ac: Likewise in generated plugin-version.h.
	* configure: Likewise.
	* genflags.c: Likewise in generated insn-flags.h.
	* genpreds.c: Likewise in generated tm-preds.h.
	* opth-gen.awk: Likewise in generated options.h.
Index: gcc.fsf.master/gcc/flags.h
===================================================================
--- gcc.fsf.master.orig/gcc/flags.h	2009-07-21 15:54:27.691059365 +0200
+++ gcc.fsf.master/gcc/flags.h	2009-07-21 15:58:56.391078407 +0200
@@ -26,6 +26,10 @@
 #include "options.h"
 #include "real.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
 enum debug_info_type
 {
   NO_DEBUG,	    /* Write no debug info.  */
@@ -401,4 +405,8 @@
 /* Whether to emit an overflow warning whose code is C.  */
 #define issue_strict_overflow_warning(c) (warn_strict_overflow >= (int) (c))
 
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
 #endif /* ! GCC_FLAGS_H */
Index: gcc.fsf.master/gcc/opth-gen.awk
===================================================================
--- gcc.fsf.master.orig/gcc/opth-gen.awk	2009-07-21 15:54:27.647059508 +0200
+++ gcc.fsf.master/gcc/opth-gen.awk	2009-07-21 15:58:56.411142502 +0200
@@ -66,6 +66,10 @@
 print "#ifndef OPTIONS_H"
 print "#define OPTIONS_H"
 print ""
+print "#ifdef __cplusplus"
+print "extern \"C\" {"
+print "#endif /* __cplusplus */"
+print ""
 print "extern int target_flags;"
 print "extern int target_flags_explicit;"
 print ""
@@ -365,5 +369,9 @@
 print "  N_OPTS"
 print "};"
 print ""
+print "#ifdef __cplusplus"
+print "}"
+print "#endif /* __cplusplus */"
+print ""
 print "#endif /* OPTIONS_H */"
 }
Index: gcc.fsf.master/gcc/tree.h
===================================================================
--- gcc.fsf.master.orig/gcc/tree.h	2009-07-21 15:54:27.699059136 +0200
+++ gcc.fsf.master/gcc/tree.h	2009-07-21 15:58:56.428063472 +0200
@@ -31,6 +31,10 @@
 #include "alias.h"
 #include "options.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
 /* Codes of tree nodes */
 
 #define DEFTREECODE(SYM, STRING, TYPE, NARGS)   SYM,
@@ -5344,4 +5348,8 @@
   for ((arg) = first_const_call_expr_arg ((call), &(iter)); (arg);	\
        (arg) = next_const_call_expr_arg (&(iter)))
 
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
 #endif  /* GCC_TREE_H  */
Index: gcc.fsf.master/gcc/tree-flow.h
===================================================================
--- gcc.fsf.master.orig/gcc/tree-flow.h	2009-07-21 15:54:27.707059465 +0200
+++ gcc.fsf.master/gcc/tree-flow.h	2009-07-21 15:58:56.428063472 +0200
@@ -32,6 +32,10 @@
 #include "ipa-reference.h"
 #include "tree-ssa-alias.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
 
 /* Gimple dataflow datastructure. All publicly available fields shall have
    gimple_ accessor defined in tree-flow-inline.h, all publicly modifiable
@@ -920,4 +924,8 @@
 int least_common_multiple (int, int);
 edge redirect_eh_edge (edge e, basic_block new_bb);
 
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
 #endif /* _TREE_FLOW_H  */
Index: gcc.fsf.master/gcc/gimple.h
===================================================================
--- gcc.fsf.master.orig/gcc/gimple.h	2009-07-21 15:54:27.695060089 +0200
+++ gcc.fsf.master/gcc/gimple.h	2009-07-21 15:58:56.428063472 +0200
@@ -30,6 +30,10 @@
 #include "basic-block.h"
 #include "tree-ssa-operands.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
 DEF_VEC_P(gimple);
 DEF_VEC_ALLOC_P(gimple,heap);
 DEF_VEC_ALLOC_P(gimple,gc);
@@ -4485,4 +4489,8 @@
 
 extern void dump_gimple_statistics (void);
 
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
 #endif  /* GCC_GIMPLE_H */
Index: gcc.fsf.master/gcc/configure
===================================================================
--- gcc.fsf.master.orig/gcc/configure	2009-07-21 15:54:27.747059157 +0200
+++ gcc.fsf.master/gcc/configure	2009-07-21 15:58:56.448060797 +0200
@@ -13982,6 +13982,10 @@
 cat > plugin-version.h <<EOF
 #include "configargs.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
 static char basever[] = "$gcc_BASEVER";
 static char datestamp[] = "$gcc_DATESTAMP";
 static char devphase[] = "$gcc_DEVPHASE";
@@ -13993,6 +13997,10 @@
 static struct plugin_gcc_version gcc_version = {basever, datestamp,
 						devphase, revision,
 						configuration_arguments};
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
 EOF
 
 # Internationalization
Index: gcc.fsf.master/gcc/configure.ac
===================================================================
--- gcc.fsf.master.orig/gcc/configure.ac	2009-07-21 15:54:27.611058722 +0200
+++ gcc.fsf.master/gcc/configure.ac	2009-07-21 15:58:56.468080614 +0200
@@ -1521,6 +1521,10 @@
 cat > plugin-version.h <<EOF
 #include "configargs.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
 static char basever[] = "$gcc_BASEVER";
 static char datestamp[] = "$gcc_DATESTAMP";
 static char devphase[] = "$gcc_DEVPHASE";
@@ -1532,6 +1536,10 @@
 static struct plugin_gcc_version gcc_version = {basever, datestamp,
 						devphase, revision,
 						configuration_arguments};
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
 EOF
 changequote([,])dnl
 
Index: gcc.fsf.master/gcc/coretypes.h
===================================================================
--- gcc.fsf.master.orig/gcc/coretypes.h	2009-07-21 15:54:27.755060255 +0200
+++ gcc.fsf.master/gcc/coretypes.h	2009-07-21 15:58:56.468080614 +0200
@@ -37,6 +37,10 @@
 #ifndef GCC_CORETYPES_H
 #define GCC_CORETYPES_H
 
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
 #ifndef GTY
 #define GTY(x)  /* nothing - marker for gengtype */
 #endif
@@ -117,5 +121,8 @@
 
 #endif
 
-#endif /* coretypes.h */
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
 
+#endif /* coretypes.h */
Index: gcc.fsf.master/gcc/gcc-plugin.h
===================================================================
--- gcc.fsf.master.orig/gcc/gcc-plugin.h	2009-07-21 15:54:27.675058916 +0200
+++ gcc.fsf.master/gcc/gcc-plugin.h	2009-07-21 15:58:56.471080842 +0200
@@ -27,6 +27,10 @@
 #include "config.h"
 #include "system.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
 /* Event names.  Keep in sync with plugin_event_name[].  */
 enum plugin_event
 {
@@ -153,4 +157,8 @@
                                plugin_callback_func callback,
                                void *user_data);
 
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
 #endif /* GCC_PLUGIN_H */
Index: gcc.fsf.master/gcc/target.h
===================================================================
--- gcc.fsf.master.orig/gcc/target.h	2009-07-21 15:54:27.739058757 +0200
+++ gcc.fsf.master/gcc/target.h	2009-07-21 15:58:56.471080842 +0200
@@ -52,6 +52,10 @@
 #include "tm.h"
 #include "insn-modes.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
 /* Types used by the record_gcc_switches() target function.  */
 typedef enum
 {
@@ -1149,4 +1153,8 @@
 /* Each target can provide their own.  */
 extern struct gcc_targetcm targetcm;
 
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
 #endif /* GCC_TARGET_H */
Index: gcc.fsf.master/gcc/toplev.h
===================================================================
--- gcc.fsf.master.orig/gcc/toplev.h	2009-07-21 15:54:27.715059096 +0200
+++ gcc.fsf.master/gcc/toplev.h	2009-07-21 15:58:56.471080842 +0200
@@ -23,6 +23,10 @@
 #include "input.h"
 #include "bversion.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
 /* If non-NULL, return one past-the-end of the matching SUBPART of
    the WHOLE string.  */
 #define skip_leading_substring(whole,  part) \
@@ -216,4 +220,8 @@
 extern const char *get_random_seed (bool);
 extern const char *set_random_seed (const char *);
 
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
 #endif /* ! GCC_TOPLEV_H */
Index: gcc.fsf.master/gcc/tree-pass.h
===================================================================
--- gcc.fsf.master.orig/gcc/tree-pass.h	2009-07-21 15:54:27.735058452 +0200
+++ gcc.fsf.master/gcc/tree-pass.h	2009-07-21 15:58:56.471080842 +0200
@@ -25,6 +25,10 @@
 
 #include "timevar.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
 /* Different tree dump places.  When you add new tree dump places,
    extend the DUMP_FILES array in tree-dump.c.  */
 enum tree_dump_index
@@ -549,4 +553,8 @@
    directly in jump threading, and avoid peeling them next time.  */
 extern bool first_pass_instance;
 
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
 #endif /* GCC_TREE_PASS_H */
Index: gcc.fsf.master/gcc/rtl.h
===================================================================
--- gcc.fsf.master.orig/gcc/rtl.h	2009-07-21 15:54:27.743058852 +0200
+++ gcc.fsf.master/gcc/rtl.h	2009-07-21 15:58:56.471080842 +0200
@@ -30,6 +30,10 @@
 #include "fixed-value.h"
 #include "alias.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
 #undef FFS  /* Some systems predefine this symbol; don't let it interfere.  */
 #undef FLOAT /* Likewise.  */
 #undef ABS /* Likewise.  */
@@ -2375,4 +2379,8 @@
 extern bool optimize_insn_for_size_p (void);
 extern bool optimize_insn_for_speed_p (void);
 
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
 #endif /* ! GCC_RTL_H */
Index: gcc.fsf.master/gcc/genflags.c
===================================================================
--- gcc.fsf.master.orig/gcc/genflags.c	2009-07-21 15:54:27.723059216 +0200
+++ gcc.fsf.master/gcc/genflags.c	2009-07-21 15:58:56.471080842 +0200
@@ -268,6 +268,9 @@
   puts ("   from the machine description file `md'.  */\n");
   puts ("#ifndef GCC_INSN_FLAGS_H");
   puts ("#define GCC_INSN_FLAGS_H\n");
+  puts ("#ifdef __cplusplus");
+  puts ("extern \"C\" {");
+  puts ("#endif /* __cplusplus */\n");
 
   /* Read the machine description.  */
 
@@ -290,7 +293,10 @@
   for (insn_ptr = insns; *insn_ptr; insn_ptr++)
     gen_proto (*insn_ptr);
 
-  puts("\n#endif /* GCC_INSN_FLAGS_H */");
+  puts ("\n#ifdef __cplusplus");
+  puts ("}");
+  puts ("#endif /* __cplusplus */\n");
+  puts ("#endif /* GCC_INSN_FLAGS_H */");
 
   if (have_error || ferror (stdout) || fflush (stdout) || fclose (stdout))
     return FATAL_EXIT_CODE;
Index: gcc.fsf.master/gcc/genpreds.c
===================================================================
--- gcc.fsf.master.orig/gcc/genpreds.c	2009-07-21 15:54:27.643058715 +0200
+++ gcc.fsf.master/gcc/genpreds.c	2009-07-21 15:58:56.471080842 +0200
@@ -1257,6 +1257,8 @@
 \n\
 #ifdef HAVE_MACHINE_MODES");
 
+  puts ("\n#ifdef __cplusplus\nextern \"C\" {\n#endif /* __cplusplus */\n");
+
   FOR_ALL_PREDICATES (p)
     printf ("extern int %s (rtx, enum machine_mode);\n", p->name);
 
@@ -1317,6 +1319,8 @@
 	puts ("#define EXTRA_ADDRESS_CONSTRAINT(c_,s_) false\n");
     }
 
+  puts ("#ifdef __cplusplus\n}\n#endif /* __cplusplus */\n");
+
   puts ("#endif /* tm-preds.h */");
 }
 
Index: gcc.fsf.master/gcc/input.h
===================================================================
--- gcc.fsf.master.orig/gcc/input.h	2009-07-21 15:54:27.711059072 +0200
+++ gcc.fsf.master/gcc/input.h	2009-07-21 15:58:56.471080842 +0200
@@ -24,6 +24,10 @@
 
 #include "line-map.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
 extern GTY(()) struct line_maps *line_table;
 
 /* A value which will never be used to represent a real location.  */
@@ -65,4 +69,8 @@
 #define in_system_header_at(LOC) ((expand_location (LOC)).sysp != 0)
 #define in_system_header (in_system_header_at (input_location))
 
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
 #endif
Index: gcc.fsf.master/gcc/output.h
===================================================================
--- gcc.fsf.master.orig/gcc/output.h	2009-07-21 15:54:27.663059539 +0200
+++ gcc.fsf.master/gcc/output.h	2009-07-21 15:58:56.471080842 +0200
@@ -22,6 +22,10 @@
 #ifndef GCC_OUTPUT_H
 #define GCC_OUTPUT_H
 
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
 /* Initialize data in final at the beginning of a compilation.  */
 extern void init_final (const char *);
 
@@ -654,4 +658,8 @@
 
 #endif
 
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
 #endif /* ! GCC_OUTPUT_H */
Index: gcc.fsf.master/gcc/machmode.h
===================================================================
--- gcc.fsf.master.orig/gcc/machmode.h	2009-07-21 15:54:27.727060148 +0200
+++ gcc.fsf.master/gcc/machmode.h	2009-07-21 15:58:56.471080842 +0200
@@ -23,6 +23,12 @@
 
 /* Make an enum class that gives all the machine modes.  */
 #include "insn-modes.h"
+/* Mode classes.  */
+#include "mode-classes.def"
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
 
 /* Get the name of mode MODE as a string.  */
 
@@ -270,4 +276,8 @@
 /* Target-dependent machine mode initialization - in insn-modes.c.  */
 extern void init_adjust_machine_modes (void);
 
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
 #endif /* not HAVE_MACHINE_MODES */
Index: gcc.fsf.master/gcc/real.h
===================================================================
--- gcc.fsf.master.orig/gcc/real.h	2009-07-21 15:54:27.671059240 +0200
+++ gcc.fsf.master/gcc/real.h	2009-07-21 15:58:56.471080842 +0200
@@ -33,6 +33,10 @@
 #endif
 #include "machmode.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
 /* An expanded form of the represented number.  */
 
 /* Enumerate the special cases of numbers that we encounter.  */
@@ -489,4 +493,9 @@
    number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
    float string.  BUF must be large enough to contain the result.  */
 extern void get_max_float (const struct real_format *, char *, size_t);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
 #endif /* ! GCC_REAL_H */
Index: gcc.fsf.master/gcc/vec.h
===================================================================
--- gcc.fsf.master.orig/gcc/vec.h	2009-07-21 15:54:27.659059304 +0200
+++ gcc.fsf.master/gcc/vec.h	2009-07-21 15:58:56.471080842 +0200
@@ -21,6 +21,10 @@
 #ifndef GCC_VEC_H
 #define GCC_VEC_H
 
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
 /* The macros here implement a set of templated vector types and
    associated interfaces.  These templates are implemented with
    macros, as we're not in C++ land.  The interface functions are
@@ -1278,4 +1282,8 @@
   return ((VEC(T,stack) *) vec_stack_p_reserve_exact_1 (alloc_, space);   \
 }
 
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
 #endif /* GCC_VEC_H */
Index: gcc.fsf.master/gcc/diagnostic.h
===================================================================
--- gcc.fsf.master.orig/gcc/diagnostic.h	2009-07-21 15:54:27.655058790 +0200
+++ gcc.fsf.master/gcc/diagnostic.h	2009-07-21 15:58:56.476060757 +0200
@@ -25,6 +25,10 @@
 #include "pretty-print.h"
 #include "options.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
 /* Constants used to discriminate diagnostics.  */
 typedef enum
 {
@@ -239,4 +243,8 @@
 extern void print_gimple_expr (FILE *, gimple, int, int);
 extern void dump_gimple_stmt (pretty_printer *, gimple, int, int);
 
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
 #endif /* ! GCC_DIAGNOSTIC_H */
Index: gcc.fsf.master/gcc/except.h
===================================================================
--- gcc.fsf.master.orig/gcc/except.h	2009-07-21 15:54:27.639059039 +0200
+++ gcc.fsf.master/gcc/except.h	2009-07-21 15:58:56.476060757 +0200
@@ -22,6 +22,10 @@
 #include "sbitmap.h"
 #include "vecprim.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
 struct function;
 
 /* The type of an exception region.  */
@@ -277,3 +281,7 @@
 extern bitmap must_not_throw_labels (void);
 extern struct eh_region_d *redirect_eh_edge_to_label (struct edge_def *, tree, bool, bool, int);
 extern int get_next_region_sharing_label (int);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
Index: gcc.fsf.master/gcc/ChangeLog
===================================================================
--- gcc.fsf.master.orig/gcc/ChangeLog	2009-07-21 15:59:05.559111301 +0200
+++ gcc.fsf.master/gcc/ChangeLog	2009-07-21 16:23:35.180060964 +0200
@@ -1,3 +1,28 @@
+2009-07-21  Duncan Sands  <baldrick@free.fr>
+
+	* coretypes.h: Wrap in 'extern "C"' when included by C++.
+	* diagnostic.h: Likewise.
+	* except.h: Likewise.
+	* flags.h: Likewise.
+	* gcc-plugin.h: Likewise.
+	* gimple.h: Likewise.
+	* input.h: Likewise.
+	* machmode.h: Likewise.
+	* output.h: Likewise.
+	* real.h: Likewise.
+	* rtl.h: Likewise.
+	* target.h: Likewise.
+	* toplev.h: Likewise.
+	* tree-flow.h: Likewise.
+	* tree-pass.h: Likewise.
+	* tree.h: Likewise.
+	* vec.h: Likewise.
+	* configure.ac: Likewise in generated plugin-version.h.
+	* configure: Likewise.
+	* genflags.c: Likewise in generated insn-flags.h.
+	* genpreds.c: Likewise in generated tm-preds.h.
+	* opth-gen.awk: Likewise in generated options.h.
+
 2009-07-21  Kaz Kojima  <kkojima@gcc.gnu.org>
 
 	* config/sh/sh.c (sh_gimplify_va_arg_expr): Wrap the result

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