[m-rev.] for review: do not output trigraphs in string literals

Ian MacLarty maclarty at cs.mu.OZ.AU
Tue Aug 30 19:56:48 AEST 2005


For review by anyone.

Estimated hours taken: 0.5
Branches main, 0.12

Do not output trigraphs in string literals in generated C code.

compiler/c_util.m:
	Break strings containing trigraphs into multiple chunks, so the
	C compiler doesn't convert the trigraphs into other characters.
	For example "??-" is converted to "?" "?-" in the generated C code.

tests/hard_coded/Mmakefile:
tests/hard_coded/trigraphs.exp:
tests/hard_coded/trigraphs.m:
	Add a regression test.  Previously this test generated incorrect
	output.

Index: compiler/c_util.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/c_util.m,v
retrieving revision 1.25
diff -u -r1.25 c_util.m
--- compiler/c_util.m	12 Apr 2005 01:53:54 -0000	1.25
+++ compiler/c_util.m	30 Aug 2005 09:52:21 -0000
@@ -209,9 +209,6 @@
 %
 % String and character handling.
 %
-% XXX we should check to ensure that we don't accidentally generate
-%     trigraph sequences in string literals.
-%

 c_util__output_quoted_string(S0, !IO) :-
 	c_util__output_quoted_multi_string(string__length(S0), S0, !IO).
@@ -239,6 +236,30 @@
 			% to access chars beyond the first NUL
 		string__unsafe_index(S, Cur, Char),
 		c_util__output_quoted_char(Char, !IO),
+
+		%
+		% Check for trigraph sequences in string literals.
+		% We break the trigraph by breaking the string into
+		% multiple chunks.  For example "??-" gets converted to
+		% "?" "?-".
+		%
+		(
+			Char = '?',
+			Cur < Len + 2
+		->
+			(
+				string__unsafe_index(S, Cur + 1, '?'),
+				string__unsafe_index(S, Cur + 2, ThirdChar),
+				is_trigraph_char(ThirdChar)
+			->
+				io__write_string("\" \"", !IO)
+			;
+				true
+			)
+		;
+			true
+		),
+
 		output_quoted_multi_string_2(Cur + 1, Len, S, !IO)
 	;
 		true
@@ -283,6 +304,20 @@
 c_util__escape_special_char('\v', 'v').
 c_util__escape_special_char('\r', 'r').
 c_util__escape_special_char('\f', 'f').
+
+	% Succeed if the given character, prefixed with "??", is a trigraph.
+	%
+:- pred is_trigraph_char(char::in) is semidet.
+
+is_trigraph_char('(').
+is_trigraph_char(')').
+is_trigraph_char('<').
+is_trigraph_char('>').
+is_trigraph_char('=').
+is_trigraph_char('/').
+is_trigraph_char('\'').
+is_trigraph_char('!').
+is_trigraph_char('-').

 	% This succeeds iff the specified character is allowed as an (unescaped)
 	% character in standard-conforming C source code.
Index: tests/hard_coded/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/Mmakefile,v
retrieving revision 1.262
diff -u -r1.262 Mmakefile
--- tests/hard_coded/Mmakefile	29 Aug 2005 03:22:30 -0000	1.262
+++ tests/hard_coded/Mmakefile	30 Aug 2005 06:28:04 -0000
@@ -182,6 +182,7 @@
 	transform_value \
 	trans_intermod_user_equality \
 	transitive_inst_type \
+	trigraphs \
 	tuple_test \
 	tuple_test \
 	type_ctor_desc \
Index: tests/hard_coded/trigraphs.exp
===================================================================
RCS file: tests/hard_coded/trigraphs.exp
diff -N tests/hard_coded/trigraphs.exp
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/hard_coded/trigraphs.exp	30 Aug 2005 06:58:11 -0000
@@ -0,0 +1 @@
+??(  ??)  ??<  ??>  ??=  ??/n  ??'  ??!  ??-
Index: tests/hard_coded/trigraphs.m
===================================================================
RCS file: tests/hard_coded/trigraphs.m
diff -N tests/hard_coded/trigraphs.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/hard_coded/trigraphs.m	30 Aug 2005 06:25:44 -0000
@@ -0,0 +1,13 @@
+:- module trigraphs.
+
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+:- implementation.
+
+main(!IO) :-
+	io.write_string("??(  ??)  ??<  ??>  ??=  ??/n  ??'  ??!  ??-", !IO),
+	nl(!IO).

--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list