[flang][driver] Add support for `-cpp/-nocpp`

Authored by awarzynski on Apr 7 2021, 4:42 AM.


[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

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

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

Reviewed By: kiranchandramohan

Differential Revision: https://reviews.llvm.org/D99292