The attached patch fixes this issue. Regression tested on x86-64.
New test case attached. OK for trunk? Regards, Jerry 2015-02-01 Jerry DeLisle <jvdeli...@gcc.gnu.org> PR fortran/64506 * scanner.c (gfc_next_char_literal): Check for '!' and if found, clear the comment and go back and get the next character.
Index: scanner.c =================================================================== --- scanner.c (revision 220307) +++ scanner.c (working copy) @@ -1268,13 +1268,25 @@ restart: c = next_char (); /* Character constants to be continued cannot have commentary - after the '&'. */ + after the '&'. However, there are cases where we may think we + are still in a string and we are looking for a possible + doubled quote and we end up here. See PR64506. */ - if (in_string && c != '\n') + if (in_string) { gfc_current_locus = old_loc; - c = '&'; - goto done; + + if (c == '!') + { + skip_comment_line (); + goto restart; + } + + if (c != '\n') + { + c = '&'; + goto done; + } } if (c != '!' && c != '\n')
! { dg-do run } ! { dg-options "-std=gnu" } ! PR64506 character(25) :: astring 100 format('This format is OK.'& ) 200 format('This format now works.'&!comment ) 300 format('This format now works.'& !comment ) 400 format('This format is OK.' &!comment ) 500 format('This format is OK.' & !comment ) 600 format('This format now works.'''&!comment ) 700 format('This format now works.'''& !comment ) 800 format('This is actually ok.'& !comment ' end' ) write(astring,100) if (astring.ne."This format is OK.") call abort write(astring,200) if (astring.ne."This format now works.") call abort write(astring,300) if (astring.ne."This format now works.") call abort write(astring,400) if (astring.ne."This format is OK.") call abort write(astring,500) if (astring.ne."This format is OK.") call abort write(astring,600) if (astring.ne."This format now works.'") call abort write(astring,700) if (astring.ne."This format now works.'") call abort write(astring,800) if (astring.ne."This is actually ok. end") call abort end