[m-dev.] for review: automatic header file inclusion guards.

Tyson Dowd trd at cs.mu.OZ.AU
Wed Jan 24 12:27:09 AEDT 2001


Hi,

Here's a solution to the multiple header file inclusion I've been
working on.  It puts guards around each foreign_decl in the .h files
that get generated, and makes sure the guards are the same for each
file.

With this change, you can remove the ML_CFLOAT_CHOICEPOINT_GUARD
in extras/clpr and it compiles fine in hlc grades.

===================================================================


Estimated hours taken: 3

Implement automatic header file multiple inclusion guards.

Note that this should not affect the LLDS backend because it doesn't
use header files to implement foreign_decls, so there is no chance of
multiple inclusions.

compiler/intermod.m:
	Output 
		:- pragma source_file("...").
		#linenum
	before :- pragma foreign_decl(...).

	This gives us the original context for foreign_decls, which is
	unique.
	
compiler/mercury_to_mercury.m:
	Output prog_contexts using pragma source_file and #line as above.

compiler/mlds_to_c.m:
	Output header guards based on the context of the foreign_decl.
	Although the same foreign_decl might get pasted into multiple
	.h files and included from different places, it will always have
	the same original context, so we can avoid including it twice.


Index: compiler/intermod.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/intermod.m,v
retrieving revision 1.90
diff -u -r1.90 intermod.m
--- compiler/intermod.m	2000/12/11 04:52:33	1.90
+++ compiler/intermod.m	2001/01/23 09:35:11
@@ -1152,8 +1152,9 @@
 
 intermod__write_foreign_decl([]) --> [].
 intermod__write_foreign_decl(
-		[foreign_decl_code(Language, Header, _) | Headers]) -->
+		[foreign_decl_code(Language, Header, Context) | Headers]) -->
         intermod__write_foreign_decl(Headers),
+	mercury_output_prog_context(Context),
         mercury_output_pragma_foreign_decl(Language, Header).
 
 :- pred intermod__write_types(assoc_list(type_id, hlds_type_defn)::in,
Index: compiler/mercury_to_mercury.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mercury_to_mercury.m,v
retrieving revision 1.181
diff -u -r1.181 mercury_to_mercury.m
--- compiler/mercury_to_mercury.m	2000/11/23 04:32:40	1.181
+++ compiler/mercury_to_mercury.m	2001/01/23 21:42:47
@@ -227,6 +227,10 @@
 	io__state).
 :- mode mercury_output_instance_methods(in, di, uo) is det.
 
+	% Output the given context 
+:- pred mercury_output_prog_context(prog_context, io__state, io__state).
+:- mode mercury_output_prog_context(in, di, uo) is det.
+
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
 
@@ -2194,6 +2198,11 @@
 escape_special_char('\b', 'b').
 
 %-----------------------------------------------------------------------------%
+
+mercury_output_prog_context(Context) -->
+	{ Context = context(File, Line) },
+	mercury_output_pragma_source_file(File),
+	io__format("#%d\n", [i(Line)]).
 
 	% Output the given pragma source_file declaration
 :- pred mercury_output_pragma_source_file(string, io__state, io__state).
Index: compiler/mlds_to_c.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_c.m,v
retrieving revision 1.77
diff -u -r1.77 mlds_to_c.m
--- compiler/mlds_to_c.m	2001/01/17 17:37:17	1.77
+++ compiler/mlds_to_c.m	2001/01/24 01:01:44
@@ -48,7 +48,7 @@
 :- import_module prog_data, prog_out, type_util, error_util.
 
 :- import_module bool, int, string, library, list.
-:- import_module assoc_list, term, std_util, require.
+:- import_module assoc_list, dir, term, std_util, require.
 
 %-----------------------------------------------------------------------------%
 
@@ -495,20 +495,40 @@
 	{ ForeignCode = mlds__foreign_code(RevHeaderCode, _RevBodyCode,
 		ExportDefns) },
 	{ HeaderCode = list__reverse(RevHeaderCode) },
+
 	io__write_list(HeaderCode, "\n", mlds_output_c_hdr_decl(Indent)),
 	io__write_string("\n"),
 	io__write_list(ExportDefns, "\n",
 			mlds_output_pragma_export_decl(ModuleName, Indent)).
 
+
 :- pred mlds_output_c_hdr_decl(indent,
 	foreign_decl_code, io__state, io__state).
 :- mode mlds_output_c_hdr_decl(in, in, di, uo) is det.
 
-mlds_output_c_hdr_decl(_Indent, foreign_decl_code(Lang, Code, Context)) -->
+mlds_output_c_hdr_decl(Indent, foreign_decl_code(Lang, Code, Context)) -->
 		% only output C code in the C header file.
 	( { Lang = c } ->
+		{ Context = context(File, Line) },
+		{ llds_out__name_mangle(File, MangledFile) },
+		{ MangledContext = string__format("%s__%d",
+			[s(MangledFile), i(Line)]) },
+		io__write_string("#ifndef MR_HEADER_GUARD_foreign_decl_"),
+		io__write_string(MangledContext),
+		io__nl,
+		mlds_indent(Indent),
+		io__write_string("#define MR_HEADER_GUARD_foreign_decl_"),
+		io__write_string(MangledContext),
+		io__nl,
+		io__nl,
+
 		mlds_output_context(mlds__make_context(Context)),
-		io__write_string(Code)
+		io__write_string(Code),
+
+		io__nl,
+		io__write_string("#endif /* MR_HEADER_GUARD_foreign_decl_"),
+		io__write_string(MangledContext),
+		io__write_string(" */\n")
 	;
 		{ sorry(this_file, "foreign code other than C") }
 	).
Index: extras/clpr/cfloat.m
===================================================================
RCS file: /home/mercury1/repository/clpr/cfloat.m,v
retrieving revision 1.33
diff -u -r1.33 cfloat.m
--- extras/clpr/cfloat.m	2000/12/22 00:36:37	1.33
+++ extras/clpr/cfloat.m	2001/01/24 00:58:50
@@ -378,8 +378,6 @@
 ** Its entries store the values of the CLP(R) global variables, including
 ** the `CLPR_trtop' variable, which points to the top of the CLP(R) trail.
 */
-#ifndef ML_CFLOAT_CHOICEPOINT_GUARD
-#define ML_CFLOAT_CHOICEPOINT_GUARD
 typedef struct ML_cfloat_choicepoint {
 	int stamp;
 	int slack_id;
@@ -388,7 +386,6 @@
 	NL_EQN_ptr nl_eqn_top;
 	struct ML_cfloat_choicepoint *next;
 } ML_cfloat_choicepoint;
-#endif
 
 #define ML_cfloat_maybe_trail_solver()				\\
@@ -502,9 +499,6 @@
 
 :- pragma c_header_code("
 
-#ifndef ML_CFLOAT_HEADER_GUARD
-#define ML_CFLOAT_HEADER_GUARD
-
 typedef MR_Integer ML_svar;
 
 /*
@@ -874,8 +868,6 @@
 	)
 
 #endif
-
-#endif /* ML_CFLOAT_HEADER_GUARD */
 
 ").
 


-- 
       Tyson Dowd           # 
                            #  Surreal humour isn't everyone's cup of fur.
     trd at cs.mu.oz.au        # 
http://www.cs.mu.oz.au/~trd #
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list