Bug 30278 - Inconsistencies with backslash handling
Inconsistencies with backslash handling
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: fortran
4.3.0
: P3 normal
: ---
Assigned To: Not yet assigned to anyone
:
Depends on:
Blocks: 19292
  Show dependency treegraph
 
Reported: 2006-12-22 15:13 UTC by Tobias Schlüter
Modified: 2007-01-27 17:16 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-12-23 12:12:23


Attachments
Handle escaped characters if flag_backslash (988 bytes, patch)
2006-12-23 13:17 UTC, Steven Bosscher
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Schlüter 2006-12-22 15:13:32 UTC
A testcase provided by Eef van Beveren, notice the difference between the handling of backslash in the format string and in the regular string:
schluter@pcl247d:~/src> cat backslash.f
      call wrt('\backslash')
      call wrt('\\backslash')
      write (*,100)
      write (*,200)
      stop
 100  format ('\backslash now results in \backslash')
 200  format ('\\backslash now results in \\backslash')
      end
      subroutine wrt(a)
      character*(*) a
      lena = len(a)
      write(*,*) 'results in: ', a(1:lena)
      return
      end

schluter@pcl247d:~/src> gfortran backslash.f
schluter@pcl247d:~/src> ./a.out
 results in:ackslash
 results in: \backslash
\backslash now results in \backslash
\\backslash now results in \\backslash
schluter@pcl247d:~/src> gfortran -fno-backslash backslash.f
schluter@pcl247d:~/src> ./a.out
 results in: \backslash
 results in: \\backslash
\backslash now results in \backslash
\\backslash now results in \\backslash
schluter@pcl247d:~/src> g77 backslash.f
schluter@pcl247d:~/src> ./a.out
 results in:ackslash
 results in: \backslash
ackslash now results inackslash
\backslash now results in \backslash
schluter@pcl247d:~/src>
Comment 1 Steven Bosscher 2006-12-23 12:19:14 UTC
This is what g77 outputs for this test case:
 results in:ackslash
 results in: \backslash
ackslash now results inackslash
\backslash now results in \backslash
Comment 2 Steven Bosscher 2006-12-23 12:42:53 UTC
I suspect that the problem is some difference between the front end and library FORMAT parsers.
Comment 3 Steven Bosscher 2006-12-23 12:52:21 UTC
This not-for-including patch shows that we should match "\\" in io.c and treat it as a single '\\' character.  This is what g77 appears to do, too.

Index: io.c
===================================================================
--- io.c        (revision 120167)
+++ io.c        (working copy)
@@ -905,6 +905,24 @@ gfc_match_format (void)
   check_format ();             /* Guaranteed to succeed */
   gfc_match_eos ();            /* Guaranteed to succeed */

+  {
+    char c1, c2;
+    int i, j;
+    int len = e->value.character.length;
+    for (i = 0; i < len - 1; ++i)
+      {
+       c1 = e->value.character.string[i];
+       c2 = e->value.character.string[i + 1];
+       if (c1 == '\\' && c2 == '\\')
+         {
+           for (j = i; j < len; ++j)
+             e->value.character.string[j] = e->value.character.string[j+1];
+           len--;
+         }
+      }
+    e->value.character.length = len;
+  }
+
   return MATCH_YES;
 }


My understanding of io.c is not quite good, so perhaps someone who understands that code well can have a look at a proper fix.
Comment 4 Tobias Schlüter 2006-12-23 13:12:03 UTC
NB The whole of \-ed symbols would have to be interpreted, conditionalized on -fbackslash.
Comment 5 Steven Bosscher 2006-12-23 13:17:36 UTC
Created attachment 12838 [details]
Handle escaped characters if flag_backslash

Slightly less horrible patch...
Comment 6 Jerry DeLisle 2006-12-24 03:14:40 UTC
With th patch in comment #5, NIST tests go OK.  It matches g77 behavior.

I am thinking though that the default behavior should be -fno-backslash.  With -fno-backslash, gfortran matches intel behavior.  My impression is that escaping with backslashes is non-standard behavior, so we should require a parameter to invoke it.  It could also be tied in with -std=legacy.

If not this, then we should at least do -fno-backslash with -std=f95
Comment 7 kargl 2007-01-27 17:14:18 UTC
Subject: Bug 30278

Author: kargl
Date: Sat Jan 27 17:14:06 2007
New Revision: 121234

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=121234
Log:
2007-01-26  Steven G. Kargl  <kargl@gcc.gnu.org>

        PR fortran/30278
        * gfortran.dg/backslash_3.f: New test.

2007-01-26  Steven Bosscher  <stevenb.gcc@gmail.com>
            Steven G. Kargl <kargl@gcc.gnu.org>

        PR fortran/30278
        * fortran/io.c (next_char): Deal with backslash escaped characters.
        Issue warnings in non -std=gnu cases.
        * fortran/primary.c (next_string_char): Issue warnings in non


Added:
    branches/gcc-4_1-branch/gcc/testsuite/gfortran.dg/backslash_3.f
Modified:
    branches/gcc-4_1-branch/gcc/fortran/ChangeLog
    branches/gcc-4_1-branch/gcc/fortran/io.c
    branches/gcc-4_1-branch/gcc/fortran/primary.c
    branches/gcc-4_1-branch/gcc/testsuite/ChangeLog

Comment 8 kargl 2007-01-27 17:16:03 UTC
Fixed in 4.1, 4.2, and trunk.