From e0b363adba5a451083681653ca600d1d6fee9f07 Mon Sep 17 00:00:00 2001 From: evans1629 Date: Mon, 31 Dec 2001 04:24:37 +0000 Subject: [PATCH] Fixed handling of wide character strings (L"test"). --- src/Headers/exprNode.h | 3 +++ src/Headers/lctype.h | 2 ++ src/cscanner.l | 45 +++++++++++++++++++++++++++++++++++-- src/ctype.c | 50 +++++++++++++++++++++++++++++++++++++++++ src/exprNode.c | 20 +++++++++++++++-- src/maketags | 2 +- test/Makefile.am | 10 ++++++++- test/Makefile.in | 10 ++++++++- test/widestrings.c | 17 ++++++++++++++ test/widestrings.expect | 8 +++++++ 10 files changed, 160 insertions(+), 7 deletions(-) create mode 100644 test/widestrings.c create mode 100644 test/widestrings.expect diff --git a/src/Headers/exprNode.h b/src/Headers/exprNode.h index a004add..69fb4ca 100644 --- a/src/Headers/exprNode.h +++ b/src/Headers/exprNode.h @@ -278,6 +278,9 @@ extern exprNode extern exprNode exprNode_stringLiteral (/*@only@*/ cstring p_t, /*@only@*/ fileloc p_loc) /*@*/ ; +extern /*@only@*/ exprNode + exprNode_wideStringLiteral (/*@only@*/ cstring p_t, /*@only@*/ fileloc p_loc) /*@*/ ; + /* ** No surrounding quotes. */ diff --git a/src/Headers/lctype.h b/src/Headers/lctype.h index f563c81..34b1777 100644 --- a/src/Headers/lctype.h +++ b/src/Headers/lctype.h @@ -264,6 +264,8 @@ extern ctype ctype_makeFunction (ctype p_base, /*@only@*/ uentryList p_p) /*@*/ extern ctype ctype_makeNFParamsFunction (ctype p_base, /*@only@*/ uentryList p_p) /*@*/ ; extern ctype ctype_makePointer (ctype p_c); extern ctype ctype_makeRawFunction (ctype p_base, /*@only@*/ uentryList p_p); +extern ctype ctype_makeWideString (void) /*@modifies internalState@*/ ; +extern bool ctype_isWideString (ctype p_c) /*@*/ ; extern ctype ctype_newBase (ctype p_c, ctype p_p) /*@*/ ; extern ctype ctype_realType (ctype p_c) /*@*/ ; diff --git a/src/cscanner.l b/src/cscanner.l index eddfd1d..b519f79 100644 --- a/src/cscanner.l +++ b/src/cscanner.l @@ -95,7 +95,8 @@ static bool continueLine = FALSE; static int ninput (void); static char processChar (void); static double processFloat (void); -static /*@only@*/ exprNode processString (void); +static /*@only@*/ exprNode processString (void) ; +static /*@only@*/ exprNode processWideString (void) ; static long processDec (void); static long processHex (void); static long processOctal (void); @@ -272,7 +273,7 @@ static void setTokLengthT (size_t len) "static" { setTokLength (6); RETURN_TOK (QSTATIC); } \"(\\.|[^\\"])*\"([ \t\n]*\"(\\.|[^\\"])*\")* { RETURN_EXPR (processString ()); } -L\"(\\.|[^\\"])*\"([ \t\n]*\"(\\.|[^\\"])*\")* { RETURN_EXPR (processString ()); } +L\"(\\.|[^\\"])*\"([ \t\n]*\"(\\.|[^\\"])*\")* { RETURN_EXPR (processWideString ()); } "out" { return (processSpec (QOUT)); } "in" { return (processSpec (QIN)); } "partial" { return (processSpec (QPARTIAL)); } @@ -2894,6 +2895,46 @@ static /*@only@*/ exprNode processString () return (res); } +/* +** process a wide character string L"...." +*/ + +static /*@only@*/ exprNode processWideString () +{ + exprNode res; + fileloc loc; + char *nl = strchr (yytext, '\n'); + cstring ns; + + llassert (*yytext == 'L'); + yytext++; + + ns = cstring_fromCharsNew (yytext); + + if (nl == NULL) + { + loc = fileloc_copy (g_currentloc); + addColumn (cstring_length (ns)); + } + else + { + char *lastnl = nl; + + loc = fileloc_copy (g_currentloc); + + context_incLineno (); + + while ((nl = strchr ((nl + 1), '\n')) != NULL) + { + context_incLineno (); + lastnl = nl; + } + } + + res = exprNode_wideStringLiteral (ns, loc); + return (res); +} + static char processChar () { diff --git a/src/ctype.c b/src/ctype.c index 0c5778c..afc38f8 100644 --- a/src/ctype.c +++ b/src/ctype.c @@ -381,6 +381,56 @@ ctype_baseArrayPtr (ctype c) } } +/* +** wchar_t * +*/ + +ctype +ctype_makeWideString () +{ + static ctype res = ctype_unknown; + + if (ctype_isUnknown (res)) + { + ctype wchart; + + if (usymtab_existsType (cstring_makeLiteralTemp ("wchar_t"))) + { + wchart = uentry_getAbstractType (usymtab_lookup (cstring_makeLiteralTemp ("wchar_t"))); + } + else + { + wchart = ctype_char; + } + + res = ctype_makePointer (wchart); + } + + return res; +} + +bool +ctype_isWideString (ctype c) +{ + if (ctype_isPointer (c)) + { + ctype ct = ctype_baseArrayPtr (c); + + if (usymtab_existsType (cstring_makeLiteralTemp ("wchar_t"))) + { + return (ct == uentry_getAbstractType (usymtab_lookup (cstring_makeLiteralTemp ("wchar_t")))); + } + else + { + return FALSE; + } + } + else + { + return FALSE; + } +} + ctype ctype_getReturnType (ctype c) { diff --git a/src/exprNode.c b/src/exprNode.c index 3547de0..ca9c767 100644 --- a/src/exprNode.c +++ b/src/exprNode.c @@ -843,6 +843,15 @@ exprNode_rawStringLiteral (/*@only@*/ cstring t, /*@only@*/ fileloc loc) return (e); /* s released */ } +/*@only@*/ exprNode +exprNode_wideStringLiteral (/*@only@*/ cstring t, /*@only@*/ fileloc loc) +{ + exprNode res = exprNode_stringLiteral (t, loc); + res->typ = ctype_makeWideString (); + + return res; +} + /*@only@*/ exprNode exprNode_stringLiteral (/*@only@*/ cstring t, /*@only@*/ fileloc loc) { @@ -4702,7 +4711,7 @@ ctype sizeof_resultType (void) } else { - sizet = ctype_ulint; + sizet = ctype_ulint; } } return sizet; @@ -9316,7 +9325,14 @@ static /*@only@*/ cstring exprNode_doUnparse (exprNode e) break; case XPR_STRINGLITERAL: - ret = message ("\"%s\"", exprData_getLiteral (data)); + if (ctype_isWideString (e->typ)) + { + ret = message ("L\"%s\"", exprData_getLiteral (data)); + } + else + { + ret = message ("\"%s\"", exprData_getLiteral (data)); + } break; case XPR_NUMLIT: diff --git a/src/maketags b/src/maketags index 6b41559..beab1b0 100755 --- a/src/maketags +++ b/src/maketags @@ -1,4 +1,4 @@ #!/bin/sh -etags *.y Headers/* *.c *.l *.i || echo "Error creating TAGS file (ignoring)" +etags *.y *.l *.c *.i Headers/* || echo "Error creating TAGS file (ignoring)" diff --git a/test/Makefile.am b/test/Makefile.am index 28bd1ad..4e26591 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -52,7 +52,7 @@ UNITTESTS = \ postnotnull preds prefixes printflike rc refcounts release repexpose \ returned sharing slovaknames specclauses special stack staticarray strings \ structassign typequals ud ulstypes union unioninit unreachable unsignedcompare \ - unused ullint utypes void + unused ullint utypes void widestrings UNITEXPECTS = $(addsuffix .expect, $(UNITTESTS)) INTEGTESTS = db1 db2 db3 @@ -761,6 +761,14 @@ utypes: void: ${SPLINTRN} void.c -expect 2 +### +### 2001-12-30: Problems with wide character strings reported by Nelson Beebe +### + +.PHONY: widestrings +widestrings: + ${SPLINTRN} widestrings.c -expect 2 + ### ### New since 2.5q: ### diff --git a/test/Makefile.in b/test/Makefile.in index 342691e..c75fc3e 100644 --- a/test/Makefile.in +++ b/test/Makefile.in @@ -121,7 +121,7 @@ UNITTESTS = \ postnotnull preds prefixes printflike rc refcounts release repexpose \ returned sharing slovaknames specclauses special stack staticarray strings \ structassign typequals ud ulstypes union unioninit unreachable unsignedcompare \ - unused ullint utypes void + unused ullint utypes void widestrings UNITEXPECTS = $(addsuffix .expect, $(UNITTESTS)) @@ -1569,6 +1569,14 @@ utypes: void: ${SPLINTRN} void.c -expect 2 +### +### 2001-12-30: Problems with wide character strings reported by Nelson Beebe +### + +.PHONY: widestrings +widestrings: + ${SPLINTRN} widestrings.c -expect 2 + ### ### New since 2.5q: ### diff --git a/test/widestrings.c b/test/widestrings.c new file mode 100644 index 0000000..db55347 --- /dev/null +++ b/test/widestrings.c @@ -0,0 +1,17 @@ +/* Provided by Nelson Beebe */ +#include +#include +#include + +int main(void) +{ + (void)wprintf(L"English: Hello, world!\n"); + (void)printf(L"English: Hello, world!\n"); /* error */ + (void)wprintf("English: Hello, world!\n"); /* error */ + + (void)wprintf(L"Russian: Ð^×аÑ^ÀегиÑ^ÁÑ^ÂÑ^ÀиÑ^ÀÑ^ÃйÑ^ÂеÑ^ÁÑ^Ì Ñ^ÁейÑ^ÇаÑ^Á на Ð^ÔеÑ^ÁÑ^ÏÑ^ÂÑ^ÃÑ^Î Ð^ÜеждÑ^ÃнаÑ^ÀоднÑ^ÃÑ^Î Ð^ÚонÑ^ÄеÑ^ÀенÑ^ÆиÑ^Î...\n"); + (void)wprintf(L"Greek: Σὲ γνÏ^ÉÏ^ÁίζÏ^É á¼^ÀÏ^Àὸ Ï^Äὴν κόÏ^Èη...\n"); + (void)wprintf(L"Georgian: á^Ã^Òá^Ã^×á^îá^Ã^Ýá^Ã^Õá^Ã^× á^Ã^Ðá^îá^Ã^Úá^Ã^Ðá^Ã^Õá^Ã^Ô á^Ã^Òá^Ã^Ðá^Ã^Øá^Ã^Ðá^àá^Ã^Ýá^Ã^× á^àá^Ã^Ôá^Ã^Òá^Ã^Øá^áá^âá^àá^Ã^Ðá^êá^Ã^Øá^Ã^Ð Unicode-á^Ã^Øá^á á^Ã^Ûá^Ã^Ôá^Ã^Ðá^Ã^×á^Ã^Ô á^áá^Ã^Ðá^Ã^Ôá^àá^Ã^×á^Ã^Ðá^èá^Ã^Ýá^àá^Ã^Øá^áá^Ã^Ý\n"); + return (EXIT_SUCCESS); +} + diff --git a/test/widestrings.expect b/test/widestrings.expect new file mode 100644 index 0000000..03c7fa9 --- /dev/null +++ b/test/widestrings.expect @@ -0,0 +1,8 @@ + +widestrings.c: (in function main) +widestrings.c:9:16: Function printf expects arg 1 to be char * gets wchar_t *: + L"English: Hello, world!\n" +widestrings.c:10:17: Function wprintf expects arg 1 to be wchar_t * gets char + *: "English: Hello, world!\n" + +Finished checking --- 2 code warnings, as expected -- 2.45.1