[flang][driver] Add support for `-cpp/-nocpp`
authorAndrzej Warzynski <andrzej.warzynski@arm.com>
Wed, 7 Apr 2021 11:42:37 +0000 (11:42 +0000)
committerAndrzej Warzynski <andrzej.warzynski@arm.com>
Wed, 7 Apr 2021 13:01:52 +0000 (13:01 +0000)
commitb83a4450c216879d76d78c21a2a0b864fdc2eb3d
tree15a0eb6b6fb6e3790bb17c0d3a404d2244bf3212
parentfc1117df5b875b922ec4d4417404b26abaceaf7b
[flang][driver] Add support for `-cpp/-nocpp`

This patch adds support for the `-cpp` and `-nocpp` flags. The
implemented semantics match f18 (i.e. the "throwaway" driver), but are
different to gfortran. In Flang the preprocessor is always run. Instead,
`-cpp/-nocpp` are used to control whether predefined and command-line
preprocessor macro definitions are enabled or not. In practice this is
sufficient to model gfortran`s `-cpp/-nocpp`.

In the absence of `-cpp/-nocpp`, the driver will use the extension of
the input file to decide whether to include the standard macro
predefinitions. gfortran's documentation [1] was used to decide which
file extension to use for this.

The logic mentioned above was added in FrontendAction::BeginSourceFile.
That's relatively late in the driver set-up, but this roughly where the
name of the input file becomes available. The logic for deciding between
fixed and free form works in a similar way and was also moved to
FrontendAction::BeginSourceFile for consistency (and to reduce
code-duplication).

The `-cpp/-nocpp` flags are respected also when the input is read from
stdin. This is different to:
   * gfortran (behaves as if `-cpp` was used)
   * f18 (behaves as if `-nocpp` was used)

Starting with this patch, file extensions are significant and some test
files had to be renamed to reflect that. Where possible, preprocessor
tests were updated so that they can be shared between `f18` and
`flang-new`. This was implemented on top of adding new test for
`-cpp/-nocpp`.

[1] https://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html

Reviewed By: kiranchandramohan

Differential Revision: https://reviews.llvm.org/D99292
18 files changed:
clang/include/clang/Driver/Options.td
clang/lib/Driver/ToolChains/Flang.cpp
flang/include/flang/Frontend/CompilerInvocation.h
flang/include/flang/Frontend/FrontendOptions.h
flang/include/flang/Frontend/PreprocessorOptions.h
flang/lib/Frontend/CompilerInstance.cpp
flang/lib/Frontend/CompilerInvocation.cpp
flang/lib/Frontend/FrontendAction.cpp
flang/lib/Frontend/FrontendActions.cpp
flang/lib/Frontend/FrontendOptions.cpp
flang/test/Driver/cpp-nocpp-command-line-macro.f90 [new file with mode: 0644]
flang/test/Driver/cpp-nocpp-predefined-macro.F90 [new file with mode: 0644]
flang/test/Driver/driver-help-hidden.f90
flang/test/Driver/driver-help.f90
flang/test/Driver/input-from-stdin.f90
flang/test/Driver/macro-def-undef.F90 [moved from flang/test/Driver/macro-def-undef.f90 with 60% similarity]
flang/test/Driver/macro-multiline.F90 [moved from flang/test/Driver/macro-multiline.f90 with 100% similarity]
flang/test/Driver/predefined-macros-compiler-version.F90 [moved from flang/test/Driver/predefined-macros-compiler-version.f90 with 81% similarity]