diff: llds_out.m output #line directives

Fergus Henderson fjh at kryten.cs.mu.OZ.AU
Mon Sep 22 04:24:57 AEST 1997


compiler/llds_out.m:
	Output proper `#line' directives.
	(The line numbers are still slightly off, because we 
	use the term_context of the start of the `:- pragma c_code...'
	declaration, rather than the term_context of the actual C code
	string.  But it is a lot better than nothing.)

Index: llds_out.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/llds_out.m,v
retrieving revision 1.55
diff -u -r1.55 llds_out.m
--- 1.55	1997/08/25 17:48:26
+++ llds_out.m	1997/09/21 18:17:02
@@ -442,8 +442,10 @@
 	;
 		[]
 	),
+	output_set_line_num(Context),
 	io__write_string(C_Code),
-	io__write_string("\n").
+	io__write_string("\n"),
+	output_reset_line_num.
 
 output_c_module(c_export(PragmaExports), DeclSet, DeclSet, _BaseName) -->
 	output_exported_c_functions(PragmaExports).
@@ -470,12 +472,14 @@
 	( { PrintComments = yes } ->
 		io__write_string("/* "),
 		prog_out__write_context(Context),
-		io__write_string(" pragma(c_code) */\n")
+		io__write_string(" pragma(c_header_code) */\n")
 	;
 		[]
 	),
+	output_set_line_num(Context),
 	io__write_string(Code),
 	io__write_string("\n"),
+	output_reset_line_num,
 	output_c_header_include_lines_2(Hs).
 
 :- pred output_exported_c_functions(list(string), io__state, io__state).
@@ -1042,10 +1046,21 @@
 	%	<the C code itself>
 	%	<assignment to the output regs of the corresponding locals>
 	% }
-	%
-	% The printing of the #line directives is currently disabled;
-	% they are printed as comments instead.
 output_instruction(pragma_c(Decls, Inputs, C_Code, Outputs, Context), _) -->
+	io__write_string("\t{\n"),
+	output_pragma_decls(Decls),
+	output_pragma_inputs(Inputs),
+	output_set_line_num(Context),
+	io__write_string("{\t\t"),
+	io__write_string(C_Code),
+	io__write_string(";}\n"),
+	output_reset_line_num,
+	output_pragma_outputs(Outputs),
+	io__write_string("\n\t}\n").
+
+:- pred output_set_line_num(term__context, io__state, io__state).
+:- mode output_set_line_num(in, di, uo) is det.
+output_set_line_num(Context) -->
 	{ term__context_file(Context, File) },
 	{ term__context_line(Context, Line) },
 	% The context is unfortunately bogus for pragma_c_codes inlined
@@ -1054,35 +1069,35 @@
 		{ Line > 0 },
 		{ File \= "" }
 	->
-		io__write_string("/* #line "),
+		io__write_string("#line "),
 		io__write_int(Line),
 		io__write_string(" """),
 		io__write_string(File),
-		io__write_string(""" */\n")
+		io__write_string("""\n")
 	;
 		[]
-	),
-	io__write_string("\t{\n"),
-	output_pragma_decls(Decls),
-	output_pragma_inputs(Inputs),
-	io__write_string("\t\t"),
-	io__write_string(C_Code),
-	io__write_string("\n"),
-	output_pragma_outputs(Outputs),
-	io__write_string("\n\t}\n").
-%	% We want to generate another #line directive to reset the C compiler's
-%	% idea of what it is processing back to the file we are generating.
-%	% However, that would require us to pass down here the filename and
-%	% line count. This current fudge depends on the code *we* generate
-%	% never getting any errors or warnings.
-%	(
-%		{ Line > 0 },
-%		{ File \= "" }
-%	->
-%		io__write_string("#line 1 ""xxx.c""\n")
-%	;
-%		[]
-%	).
+	).
+
+:- pred output_reset_line_num(io__state, io__state).
+:- mode output_reset_line_num(di, uo) is det.
+output_reset_line_num -->
+	% We want to generate another #line directive to reset the C compiler's
+	% idea of what it is processing back to the file we are generating.
+	io__get_output_line_number(Line),
+	io__output_stream_name(FileName),
+	(
+		{ Line > 0 },
+		{ FileName \= "" }
+	->
+		io__write_string("#line "),
+		{ NextLine is Line + 1 },
+		io__write_int(NextLine),
+		io__write_string(" """),
+		io__write_string(FileName),
+		io__write_string("""\n")
+	;
+		[]
+	).
 
 	% Output the local variable declarations at the top of the 
 	% pragma_c_code code.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3         |     -- the last words of T. S. Garp.



More information about the developers mailing list