Bug 94228 - Preprocessor inconsistency for macros when invoked from gfortran
Summary: Preprocessor inconsistency for macros when invoked from gfortran
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 9.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-03-19 20:01 UTC by Mark Paris
Modified: 2020-03-23 16:20 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mark Paris 2020-03-19 20:01:06 UTC
This report details differing behavior for preprocessor directives containing variadic macros when processed directly by cpp

    $ cpp-9 --version
    cpp-9 (Homebrew GCC 9.2.0_3) 9.2.0
    Copyright (C) 2019 Free Software Foundation, Inc.

and when invoked from the gfortran compiler

    $ gfortran-9 --version
    GNU Fortran (Homebrew GCC 9.2.0_3) 9.2.0
    Copyright (C) 2019 Free Software Foundation, Inc.

An MWE is included.

1) The source ('test-function-macro.f90'):

    ! filename: test-function-macro.f90
    ! v0: Thu Mar 19 11:06:40 2020

    program test_function_macro
       implicit none

    #define F(...) print *, 0,__VA_ARGS__
       F('1','2','3')

    end program test_function_macro

2) cpp direct processing:

    $ cpp-9 test-function-macro.f90
    # 1 "test-function-macro.f90"
    # 1 "<built-in>"
    # 1 "<command-line>"
    # 1 "test-function-macro.f90"
    ! filename: test-function-macro.f90
    ! v0: Thu Mar 19 11:06:40 2020

    program test_function_macro
       implicit none


       print *, 0,'1','2','3'

    end program test_function_macro

3) gfortran invocation:

    $ gfortran-9 -cpp test-function-macro.f90
    test-function-macro.f90:7:0:

        7 | #define F(...) print *, 0,__VA_ARGS__
          |
    Error: syntax error in macro parameter list


4) Comments

  i) the behavior in item 2) is the desired behavior
 ii) the behavior in item 3) is undesired
iii) The system is runs mac OS; the behavior is identical whether the above cpp is used or the system cpp is used:

    $ cpp --version
    Apple LLVM version 10.0.1 (clang-1001.0.46.4)
    Target: x86_64-apple-darwin18.7.0
    Thread model: posix
    InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Comment 1 Andrew Pinski 2020-03-19 20:07:19 UTC
gfortran defaults to -traditional-cpp preprocessor mode.
Comment 2 Mark Paris 2020-03-19 21:01:49 UTC
(In reply to Andrew Pinski from comment #1)
> gfortran defaults to -traditional-cpp preprocessor mode.

Thank you for your reply. If I may, I'd like to pose a question that I cannot find the answer to from online sources:

Is it possible to run the preprocessor in the ISO mode, not the traditional mode, when invoked by gfortran?
Comment 3 kargls 2020-03-19 21:48:10 UTC
(In reply to Mark Paris from comment #2)
> (In reply to Andrew Pinski from comment #1)
> > gfortran defaults to -traditional-cpp preprocessor mode.
> 
> Thank you for your reply. If I may, I'd like to pose a question that I
> cannot find the answer to from online sources:
> 
> Is it possible to run the preprocessor in the ISO mode, not the traditional
> mode, when invoked by gfortran?

No.  Newer C, as opposed to older C, uses // for a comment.
Fortran uses // as the concatenation operator.  Run this
through a cpp pre-processor.

character(len=80) :: name = 'john ' // 'Doe'
print *, name
end

 ~/work/bin/cpp a.F
# 1 "a.F"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "a.F"
character(len=80) :: name = 'john '
print *, name
end
Comment 4 Mark Paris 2020-03-19 22:24:10 UTC
(In reply to kargl from comment #3)
> No.  Newer C, as opposed to older C, uses // for a comment.
> Fortran uses // as the concatenation operator.  Run this
> through a cpp pre-processor.
> 
> character(len=80) :: name = 'john ' // 'Doe'
> print *, name
> end
> 
>  ~/work/bin/cpp a.F
> # 1 "a.F"
> # 1 "<built-in>"
> # 1 "<command-line>"
> # 1 "a.F"
> character(len=80) :: name = 'john '
> print *, name
> end

Thank you for your kind reply. I understand that this is an issue of disparate use of the same operator, '//' in C and Fortran.

Is it possible to have cpp recognize the different uses of // by, say, the file name extension of the source being processed?

Links to information about gcc development for this specific possible feature would be appreciated.
Comment 5 Steve Kargl 2020-03-20 01:12:18 UTC
On Thu, Mar 19, 2020 at 10:24:10PM +0000, markwayne1969 at gmail dot com wrote:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94228
> 
> --- Comment #4 from Mark Paris <markwayne1969 at gmail dot com> ---
> (In reply to kargl from comment #3)
> > No.  Newer C, as opposed to older C, uses // for a comment.
> > Fortran uses // as the concatenation operator.  Run this
> > through a cpp pre-processor.
> > 
> > character(len=80) :: name = 'john ' // 'Doe'
> > print *, name
> > end
> > 
> >  ~/work/bin/cpp a.F
> > # 1 "a.F"
> > # 1 "<built-in>"
> > # 1 "<command-line>"
> > # 1 "a.F"
> > character(len=80) :: name = 'john '
> > print *, name
> > end
> 
> Thank you for your kind reply. I understand that this is an issue
 of disparate use of the same operator, '//' in C and Fortran.
> 
> Is it possible to have cpp recognize the different uses of // by,
> say, the file name extension of the source being processed?

Sure.  Anything is possible if someone puts in the time to
write a Fortran specific preprocessor.  AFAIK, none of the
current diminishing number of gfortran contributors is 
working a new preprocessor.  

> Links to information about gcc development for this specific
> possible feature would be appreciated.

I don't know of any gfortran preprocessor projects.  You are 
more then welcomed to clone the git repository and start
such a project.  Having new gfortran contributors would be
healthy for gfortran's future.
Comment 6 Mark Paris 2020-03-23 02:41:23 UTC
(In reply to Steve Kargl from comment #5)
> On Thu, Mar 19, 2020 at 10:24:10PM +0000, markwayne1969 at gmail dot com
> wrote:
> > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94228
> > 
> > --- Comment #4 from Mark Paris <markwayne1969 at gmail dot com> ---
> > (In reply to kargl from comment #3)
> > > No.  Newer C, as opposed to older C, uses // for a comment.
> > > Fortran uses // as the concatenation operator.  Run this
> > > through a cpp pre-processor.
> > > 
> > > character(len=80) :: name = 'john ' // 'Doe'
> > > print *, name
> > > end
> > > 
> > >  ~/work/bin/cpp a.F
> > > # 1 "a.F"
> > > # 1 "<built-in>"
> > > # 1 "<command-line>"
> > > # 1 "a.F"
> > > character(len=80) :: name = 'john '
> > > print *, name
> > > end
> > 
> > Thank you for your kind reply. I understand that this is an issue
>  of disparate use of the same operator, '//' in C and Fortran.
> > 
> > Is it possible to have cpp recognize the different uses of // by,
> > say, the file name extension of the source being processed?
> 
> Sure.  Anything is possible if someone puts in the time to
> write a Fortran specific preprocessor.  AFAIK, none of the
> current diminishing number of gfortran contributors is 
> working a new preprocessor.  
> 
> > Links to information about gcc development for this specific
> > possible feature would be appreciated.
> 
> I don't know of any gfortran preprocessor projects.  You are 
> more then welcomed to clone the git repository and start
> such a project.  Having new gfortran contributors would be
> healthy for gfortran's future.

Thank you, again for your reply. It's not exactly the information I was looking for -- perhaps you're not the right person to ask. If not, you might be able to point me to the correct group.

I was asking whether you knew of any reason that the existing cpp couldn't be adjusted to handle the '//' disparity with a filename extension dependency.

If this is possible, then it seems like a minor revision, as opposed to what you appear to have in mind of writing "a Fortran specific preprocessor," which sounds like a prohibitive undertaking.

Thanks again.
Comment 7 Steve Kargl 2020-03-23 16:20:06 UTC
On Mon, Mar 23, 2020 at 02:41:23AM +0000, markwayne1969 at gmail dot com wrote:
> 
> (In reply to Steve Kargl from comment #5)
> > 
> > > Links to information about gcc development for this specific
> > > possible feature would be appreciated.
> > 
> > I don't know of any gfortran preprocessor projects.  You are 
> > more then welcomed to clone the git repository and start
> > such a project.  Having new gfortran contributors would be
> > healthy for gfortran's future.
> 
> Thank you, again for your reply. It's not exactly the information
> I was looking for -- perhaps you're not the right person to ask.
> If not, you might be able to point me to the correct group.
> 
> I was asking whether you knew of any reason that the existing cpp
> couldn't be adjusted to handle the '//' disparity with a filename
> extension dependency.
> 
> If this is possible, then it seems like a minor revision, as
> opposed to what you appear to have in mind of writing "a Fortran
> specific preprocessor," which sounds like a prohibitive undertaking.
> 

You can ask on gcc@gcc.gnu.org and fortran@gcc.gnu.org.
I doubt that that will be too profitable.  Good luck.