Re: [RFC] Fix for bug#163666 (skelimpl.patch)



diff -urpN ORBit2.orig/ChangeLog ORBit2/ChangeLog
--- ORBit2.orig/ChangeLog	2005-01-13 10:38:29.878823564 +0100
+++ ORBit2/ChangeLog	2005-01-13 10:54:34.617028673 +0100
@@ -1,5 +1,24 @@
 2005-01-13  Jules Colding  <colding omesc com>
 
+	* src/idl-compiler/orbit-idl-c-skelimpl.c (prev_char_is_nl): New function
+	that implements check of previously written character to the output file.
+	Excessive output of newline characters are fixed by checking the last
+	character written: If it is '\n' then do not write another '\n'.
+	(cbe_ski_do_list): The correct fix for exessive newline output.
+	
+	Generation of skelimpl files has been fixed by protecting definitions and
+	declarations with #ifdef blocks. This is *not* the right fix. The right fix 
+	would be to generate header files with the proper extern declarations. The
+	current "fix" has the drawback of forcing the developer to include the 
+	skelimpl files in inheritance order: Most common ancestor first, then 
+	progressively later descendants. The right fix requires more redesign of 
+	the code than I would like to do now, so this hack must suffice for the time 
+	being. 
+
+	* src/idl-compiler/orbit-idl-c-backend.c (out_for_pass): Change the opening
+	mode of output files to "w+". This is needed to support the correct fix for 
+	excessive newline output in skelimpl files.
+
 	* src/idl-compiler/orbit-idl-c-skelimpl.c: I had a really hard time parsing 
 	the code with the former indentation level of 2, so I changed it to a more
 	reasonable 8.
diff -urpN ORBit2.orig/src/idl-compiler/orbit-idl-c-backend.c ORBit2/src/idl-compiler/orbit-idl-c-backend.c
--- ORBit2.orig/src/idl-compiler/orbit-idl-c-backend.c	2005-01-01 10:21:17.000000000 +0100
+++ ORBit2/src/idl-compiler/orbit-idl-c-backend.c	2005-01-13 09:31:38.000000000 +0100
@@ -153,7 +153,7 @@ out_for_pass (const char    *input_filen
 		output_full_path = g_build_path (G_DIR_SEPARATOR_S, rinfo->output_directory, output_filename, NULL);
 		g_free (output_filename);
 
-		fp = fopen (output_full_path, "w");
+		fp = fopen (output_full_path, "w+");
 		if (fp == NULL)
 			g_error ("failed to fopen '%s': %s\n", output_full_path, strerror(errno));
 
diff -urpN ORBit2.orig/src/idl-compiler/orbit-idl-c-skelimpl.c ORBit2/src/idl-compiler/orbit-idl-c-skelimpl.c
--- ORBit2.orig/src/idl-compiler/orbit-idl-c-skelimpl.c	2005-01-13 10:37:53.290115613 +0100
+++ ORBit2/src/idl-compiler/orbit-idl-c-skelimpl.c	2005-01-13 10:09:05.000000000 +0100
@@ -18,6 +18,8 @@ orbit_idl_output_c_skelimpl(IDL_tree tre
 #include <stdlib.h>
 #include <string.h>
 
+#include <errno.h>
+
 /* Abbreviations used here:
    "cbe" stands for "C backend"
    "hdr" -> "header" (duh :)
@@ -69,7 +71,7 @@ orbit_cbe_write_skelimpl(FILE *outfile, 
 	fprintf(outfile, "#include \"%s.h\"\n", hdrname);
 
 	for(ski.pass = PASS_SERVANTS; ski.pass < PASS_LAST; ski.pass++) {
-		fprintf(outfile, "\n/*** %s ***/\n", passnames[ski.pass]);
+		fprintf(ski.of, "\n/*** %s ***/\n\n", passnames[ski.pass]);
 		orbit_cbe_ski_process_piece(&ski);
 	}
 }
@@ -113,24 +115,48 @@ cbe_ski_do_module(CBESkelImplInfo *ski)
 	cbe_ski_do_list(&subski);
 }
 
+/* Returns 1 if the previous character written to f  */
+/* was '\n', 0 otherwise. */
+static inline unsigned char 
+prev_char_is_nl(FILE *f)
+{
+        char c;
+	long pos;
+	size_t count;
+        unsigned char retv = 0;
+
+	pos = ftell(f);
+	if (pos < sizeof(char)) 
+		return 0; // beginning of file
+
+        if (fseek(f, (-1)*sizeof(char), SEEK_CUR)) 
+		goto out;
+
+	count = fread((void*)&c, sizeof(char), 1, f);
+        if (sizeof(char) == count) 
+		retv = ('\n' == c) ? 1 : 0;
+	
+out:
+	fseek(f, pos, SEEK_SET);
+        return retv;
+}
+
 static void
 cbe_ski_do_list(CBESkelImplInfo *ski)
 {
 	CBESkelImplInfo subski = *ski;
 	IDL_tree curitem;
-	int nl = 0;
 
 	for(curitem = ski->tree; curitem; curitem = IDL_LIST(curitem).next) {
 		subski.tree = IDL_LIST(curitem).data;
 		orbit_cbe_ski_process_piece(&subski);
-		if (nl <= 1) {
-			nl++;
+		if(!prev_char_is_nl(ski->of))
 			fprintf(ski->of, "\n");
-		}
 	}
 }
 
-static void cbe_ski_do_attr_dcl_internal(CBESkelImplInfo *ski, IDL_tree current_interface, gboolean inherited)
+static void 
+cbe_ski_do_attr_dcl_internal(CBESkelImplInfo *ski, IDL_tree current_interface, gboolean inherited)
 {
 	IDL_tree curop, curitem;
 	GString *attrname = g_string_new(NULL);
@@ -211,7 +237,8 @@ static void cbe_ski_do_attr_dcl_internal
 	g_string_free(attrname, TRUE);
 }
 
-static void cbe_ski_do_attr_dcl(CBESkelImplInfo *ski)
+static void 
+cbe_ski_do_attr_dcl(CBESkelImplInfo *ski)
 {
 	cbe_ski_do_attr_dcl_internal(ski, NULL, FALSE);
 }
@@ -226,7 +253,7 @@ static void
 cbe_ski_do_op_dcl(CBESkelImplInfo *ski)
 {
 	/* If you fix anything here, please also fix it in
-	   cbe_ski_interface_print_epv_for_op(), which is almost a
+	   cbe_ski_do_inherited_op_dcl(), which is almost a
 	   cut-and-paste of this routine */
 
 	char *id, *id2;
@@ -237,15 +264,27 @@ cbe_ski_do_op_dcl(CBESkelImplInfo *ski)
 	switch(ski->pass) {
 	case PASS_PROTOS:
 	case PASS_IMPLSTUBS:
-		fprintf(ski->of, "static ");
-		orbit_cbe_write_param_typespec(ski->of, ski->tree);
-    
-		id = IDL_ns_ident_to_qstring(IDL_IDENT_TO_NS(IDL_OP_DCL(ski->tree).ident), "_", 0);
-    
 		curitem = IDL_get_parent_node(ski->tree, IDLN_INTERFACE, &level);
-
 		g_assert(curitem);
+
+		id = IDL_ns_ident_to_qstring(IDL_IDENT_TO_NS(IDL_OP_DCL(ski->tree).ident), "_", 0);    
 		id2 = IDL_ns_ident_to_qstring(IDL_IDENT_TO_NS(IDL_INTERFACE(curitem).ident), "_", 0);
+
+		/* protect with #ifdef block  */
+		if(PASS_PROTOS == ski->pass) 
+			fprintf(ski->of, "#if !defined(_decl_impl_");
+		else
+			fprintf(ski->of, "#if !defined(_impl_");
+		fprintf(ski->of, "%s_)\n", id);
+
+		if(PASS_PROTOS == ski->pass) 
+			fprintf(ski->of, "#define _decl_impl_");
+		else
+			fprintf(ski->of, "#define _impl_");
+		fprintf(ski->of, "%s_ 1\n", id);
+
+		fprintf(ski->of, "static ");
+		orbit_cbe_write_param_typespec(ski->of, ski->tree);    
 		fprintf(ski->of, "\nimpl_%s(impl_POA_%s *servant,\n", id, id2);
 		g_free(id); g_free(id2);
     
@@ -274,14 +313,14 @@ cbe_ski_do_op_dcl(CBESkelImplInfo *ski)
 				fprintf(ski->of, " /* ------   insert method code here   ------ */\n");
 				fprintf(ski->of, " /* ------ ---------- end ------------ ------ */\n");
 			}
-			fprintf(ski->of, "}\n\n");
+			fprintf(ski->of, "}\n");
 		} else /* PASS_PROTOS */
 			fprintf(ski->of, ";\n");
-		break; /* End PASS_PROTOS | PASS_IMPLSTUBS */
 
+		fprintf(ski->of, "#endif\n\n"); /* end of protective #ifdef block */
+		break; /* End PASS_PROTOS | PASS_IMPLSTUBS */
 	case PASS_EPVS:
-		id = IDL_ns_ident_to_qstring(IDL_IDENT_TO_NS(IDL_OP_DCL(ski->tree).ident),
-					     "_", 0);
+		id = IDL_ns_ident_to_qstring(IDL_IDENT_TO_NS(IDL_OP_DCL(ski->tree).ident), "_", 0);
 		fprintf(ski->of, "(gpointer)&impl_%s,\n", id);
 		g_free(id);
 		break;
@@ -308,13 +347,25 @@ cbe_ski_do_inherited_op_dcl(CBESkelImplI
 	switch(ski->pass) {
 	case PASS_PROTOS:
 	case PASS_IMPLSTUBS:
-		fprintf(ski->of, "static ");
-		orbit_cbe_write_param_typespec(ski->of, ski->tree);
-    
 		curitem = IDL_get_parent_node(ski->tree, IDLN_INTERFACE, &level);
 		g_assert(curitem);
 
-		fprintf(ski->of, "\nimpl_%s_%s(impl_POA_%s *servant,\n", id, IDL_IDENT(ident).str, id);
+		/* protect with #ifdef block  */
+		if(PASS_PROTOS == ski->pass) 
+			fprintf(ski->of, "#if !defined(_decl_impl_");
+		else
+			fprintf(ski->of, "#if !defined(_impl_");
+		fprintf(ski->of, "%s_%s_)\n", id, IDL_IDENT(ident).str);
+
+		if(PASS_PROTOS == ski->pass) 
+			fprintf(ski->of, "#define _decl_impl_");
+		else
+			fprintf(ski->of, "#define _impl_");
+		fprintf(ski->of, "%s_%s_ 1\n", id, IDL_IDENT(ident).str);
+
+		fprintf(ski->of, "static ");
+		orbit_cbe_write_param_typespec(ski->of, ski->tree);
+    		fprintf(ski->of, "\nimpl_%s_%s(impl_POA_%s *servant,\n", id, IDL_IDENT(ident).str, id);
     
 		op = ski->tree;
 		for(curitem = IDL_OP_DCL(ski->tree).parameter_dcls;
@@ -341,11 +392,12 @@ cbe_ski_do_inherited_op_dcl(CBESkelImplI
 				fprintf(ski->of, " /* ------   insert method code here   ------ */\n");
 				fprintf(ski->of, " /* ------ ---------- end ------------ ------ */\n");
 			}
-			fprintf(ski->of, "}\n\n");
+			fprintf(ski->of, "}\n");
 		} else /* PASS_PROTOS */
 			fprintf(ski->of, ";\n");
-		break; /* End PASS_PROTOS | PASS_IMPLSTUBS */
 
+		fprintf(ski->of, "#endif\n\n"); /* end of protective #ifdef block */
+		break; /* End PASS_PROTOS | PASS_IMPLSTUBS */
 	case PASS_EPVS:
 		ident=IDL_OP_DCL(ski->tree).ident;
 		g_assert(ident);
@@ -402,8 +454,10 @@ cbe_ski_do_inherited_methods(IDL_tree in
 					     "_", 0);
 		inherit_id = IDL_ns_ident_to_qstring(IDL_IDENT_TO_NS(IDL_INTERFACE(interface).ident),
 						     "_", 0);
-		fprintf(ski->of, "static POA_%s__epv impl_%s_%s_epv = {\nNULL, /* _private */\n",
-			inherit_id, id, inherit_id);
+		/* protect with #ifdef block  */
+		fprintf(ski->of, "#if !defined(_impl_%s_%s_epv_)\n", id, inherit_id);
+		fprintf(ski->of, "#define _impl_%s_%s_epv_ 1\n", id, inherit_id);
+		fprintf(ski->of, "static POA_%s__epv impl_%s_%s_epv = {\nNULL, /* _private */\n", inherit_id, id, inherit_id);
 	}
 
 	for(curitem = IDL_INTERFACE(interface).body; curitem; curitem=IDL_LIST(curitem).next) {
@@ -422,7 +476,8 @@ cbe_ski_do_inherited_methods(IDL_tree in
 	}
 
 	if(ski->pass==PASS_EPVS) {
-		fprintf(ski->of, "};");
+		fprintf(ski->of, "};\n"); 
+		fprintf(ski->of, "#endif\n"); /* end of protective #ifdef block */
 
 		g_free(id);
 		g_free(inherit_id);
@@ -439,46 +494,65 @@ cbe_ski_do_interface(CBESkelImplInfo *sk
 
 	switch(ski->pass) {
 	case PASS_SERVANTS:
+		/* protect with #ifdef block  */
+		fprintf(ski->of, "#if !defined(_typedef_impl_POA_%s_)\n", id);
+		fprintf(ski->of, "#define _typedef_impl_POA_%s_ 1\n", id);
+
 		fprintf(ski->of, "typedef struct {\nPOA_%s servant;\nPortableServer_POA poa;\n", id);
 		subski.tree = IDL_INTERFACE(ski->tree).body;
 		cbe_ski_do_list(&subski);
-		IDL_tree_traverse_parents(ski->tree, (GFunc)&cbe_ski_do_inherited_methods,
-					  ski);
+		IDL_tree_traverse_parents(ski->tree, (GFunc)&cbe_ski_do_inherited_methods, ski);
 		fprintf(ski->of, "   /* ------ add private attributes here ------ */\n");
 		fprintf(ski->of, "   /* ------ ---------- end ------------ ------ */\n");
-		fprintf(ski->of, "} impl_POA_%s;\n\n", id);
+		fprintf(ski->of, "} impl_POA_%s;\n", id);
+
+		fprintf(ski->of, "#endif\n\n"); /* end of protective #ifdef block */
 		break;
 	case PASS_EPVS:
-		fprintf(ski->of,
-			"static PortableServer_ServantBase__epv impl_%s_base_epv = {\n",
-			id);
+		/* protect with #ifdef block  */
+		fprintf(ski->of, "#if !defined(_impl_%s_base_epv_)\n", id);
+		fprintf(ski->of, "#define _impl_%s_base_epv_ 1\n", id);
+		fprintf(ski->of, "static PortableServer_ServantBase__epv impl_%s_base_epv = {\n", id);
 		fprintf(ski->of, "NULL,             /* _private data */\n");
 		fprintf(ski->of, "(gpointer) & impl_%s__destroy, /* finalize routine */\n", id);
 		fprintf(ski->of, "NULL,             /* default_POA routine */\n");
 		fprintf(ski->of, "};\n");
+		fprintf(ski->of, "#endif\n\n"); /* end of protective #ifdef block */
+
+		/* protect with #ifdef block  */
+		fprintf(ski->of, "#if !defined(_impl_%s_epv_)\n", id);
+		fprintf(ski->of, "#define _impl_%s_epv_ 1\n", id);
 		fprintf(ski->of, "static POA_%s__epv impl_%s_epv = {\nNULL, /* _private */\n", id, id);
 		subski.tree = IDL_INTERFACE(ski->tree).body;
 		cbe_ski_do_list(&subski);
-		fprintf(ski->of, "};");
-		IDL_tree_traverse_parents(ski->tree, (GFunc)&cbe_ski_do_inherited_methods,
-					  ski);
+		fprintf(ski->of, "};\n");
+		fprintf(ski->of, "#endif\n\n"); /* end of protective #ifdef block */
+
+		IDL_tree_traverse_parents(ski->tree, (GFunc)&cbe_ski_do_inherited_methods, ski);
 		break;
 	case PASS_VEPVS:
+		/* protect with #ifdef block  */
+		fprintf(ski->of, "#if !defined(_impl_%s_vepv_)\n", id);
+		fprintf(ski->of, "#define _impl_%s_vepv_ 1\n", id);
+
 		fprintf(ski->of, "static POA_%s__vepv impl_%s_vepv = {\n", id, id);
 		fprintf(ski->of, "&impl_%s_base_epv,\n", id);
-		IDL_tree_traverse_parents(ski->tree, (GFunc)&cbe_ski_do_interface_vepv_entry,
-					  ski);
-		fprintf(ski->of, "};");
+		IDL_tree_traverse_parents(ski->tree, (GFunc)&cbe_ski_do_interface_vepv_entry, ski);
+		fprintf(ski->of, "};\n");
+
+		fprintf(ski->of, "#endif\n\n"); /* end of protective #ifdef block */
 		break;
 	case PASS_IMPLSTUBS:
+		/* protect __create with #ifdef block  */
+		fprintf(ski->of, "#if !defined(_impl_%s__create_)\n", id);
+		fprintf(ski->of, "#define _impl_%s__create_ 1\n", id);
 		fprintf(ski->of, "static %s impl_%s__create(PortableServer_POA poa, CORBA_Environment *ev)\n", id, id);
 		fprintf(ski->of, "{\n%s retval;\nimpl_POA_%s *newservant;\nPortableServer_ObjectId *objid;\n\n", id, id);
 		fprintf(ski->of, "newservant = g_new0(impl_POA_%s, 1);\n", id);
 		fprintf(ski->of, "newservant->servant.vepv = &impl_%s_vepv;\n", id);
 		fprintf(ski->of, "newservant->poa = (PortableServer_POA) CORBA_Object_duplicate((CORBA_Object)poa, ev);\n");
 		fprintf(ski->of, "POA_%s__init((PortableServer_Servant)newservant, ev);\n", id);
-    
-		fprintf(ski->of, "   /* Before servant is going to be activated all\n");
+    		fprintf(ski->of, "   /* Before servant is going to be activated all\n");
 		fprintf(ski->of, "    * private attributes must be initialized.  */\n"); 
 		fprintf(ski->of, "\n");
 		fprintf(ski->of, "   /* ------ init private attributes here ------ */\n");
@@ -487,29 +561,37 @@ cbe_ski_do_interface(CBESkelImplInfo *sk
 		fprintf(ski->of, "objid = PortableServer_POA_activate_object(poa, newservant, ev);\n");
 		fprintf(ski->of, "CORBA_free(objid);\n");
 		fprintf(ski->of, "retval = PortableServer_POA_servant_to_reference(poa, newservant, ev);\n");
-		fprintf(ski->of, "\nreturn retval;\n}\n\n");
+		fprintf(ski->of, "\nreturn retval;\n}\n");
+		fprintf(ski->of, "#endif\n\n"); /* end of protective #ifdef block */
+
+		/* protect __destroy with #ifdef block  */
+		fprintf(ski->of, "#if !defined(_impl_%s__destroy_)\n", id);
+		fprintf(ski->of, "#define _impl_%s__destroy_ 1\n", id);
 		fprintf(ski->of, "static void\nimpl_%s__destroy(impl_POA_%s *servant, CORBA_Environment *ev)\n{\n", id, id);
 		fprintf(ski->of, "    CORBA_Object_release ((CORBA_Object) servant->poa, ev);\n\n");
 		fprintf(ski->of, "    /* No further remote method calls are delegated to \n");
 		fprintf(ski->of, "    * servant and you may free your private attributes. */\n");
-
 		fprintf(ski->of, "   /* ------ free private attributes here ------ */\n");
 		fprintf(ski->of, "   /* ------ ---------- end ------------- ------ */\n");
 		fprintf(ski->of, "\nPOA_%s__fini((PortableServer_Servant)servant, ev);\n", id);
 		fprintf(ski->of, "\ng_free (servant);\n");
-		fprintf(ski->of, "}\n\n");
+		fprintf(ski->of, "}\n");
+		fprintf(ski->of, "#endif\n\n"); /* end of protective #ifdef block */
 
 		subski.tree = IDL_INTERFACE(ski->tree).body;
 		cbe_ski_do_list(&subski);
-		IDL_tree_traverse_parents(ski->tree, (GFunc)&cbe_ski_do_inherited_methods,
-					  ski);
+		IDL_tree_traverse_parents(ski->tree, (GFunc)&cbe_ski_do_inherited_methods, ski);
 		break;
 	case PASS_PROTOS:
+		/* protect __destroy declaration with #ifdef block  */
+		fprintf(ski->of, "#if !defined(_decl_impl_%s__destroy_)\n", id);
+		fprintf(ski->of, "#define _decl_impl_%s__destroy_ 1\n", id);
 		fprintf(ski->of, "static void impl_%s__destroy(impl_POA_%s *servant,\nCORBA_Environment *ev);\n", id, id);
+		fprintf(ski->of, "#endif\n\n"); /* end of protective #ifdef block */
+
 		subski.tree = IDL_INTERFACE(ski->tree).body;
 		cbe_ski_do_list(&subski);
-		IDL_tree_traverse_parents(ski->tree, (GFunc)&cbe_ski_do_inherited_methods,
-					  ski);
+		IDL_tree_traverse_parents(ski->tree, (GFunc)&cbe_ski_do_inherited_methods, ski);
 		break;
 	default:
 		break;




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]