Skip to content

Commit eceaa34

Browse files
committed
[flang] Ensure -x f95-cpp-input enables -cpp
This change ensures that specifying `-x f95-cpp-input` automatically enables `-cpp`, making `flang`'s behavior consistent with `gfortran`. `flang/test/Driver/input-from-stdin/input-from-stdin.f90` changes because the driver assumes `-x f95-cpp-input` for `<stdin>`. Therefore, after this patch, Fortran source that comes from `<stdin>` will now be preprocessed.
1 parent 3753b14 commit eceaa34

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,12 @@ static void parsePreprocessorArgs(Fortran::frontend::PreprocessorOptions &opts,
863863
(currentArg->getOption().matches(clang::driver::options::OPT_cpp))
864864
? PPMacrosFlag::Include
865865
: PPMacrosFlag::Exclude;
866+
// Enable -cpp based on -x unless explicitly disabled with -nocpp
867+
if (opts.macrosFlag != PPMacrosFlag::Exclude)
868+
if (const auto *dashX = args.getLastArg(clang::driver::options::OPT_x))
869+
opts.macrosFlag = llvm::StringSwitch<PPMacrosFlag>(dashX->getValue())
870+
.Case("f95-cpp-input", PPMacrosFlag::Include)
871+
.Default(opts.macrosFlag);
866872

867873
opts.noReformat = args.hasArg(clang::driver::options::OPT_fno_reformat);
868874
opts.preprocessIncludeLines =
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
program main
2+
print *, __FILE__, __LINE__
3+
end
4+
5+
! This test verifies that `flang`'s `-x` options behave like `gfortran`'s.
6+
! Specifically:
7+
! - `-x f95` should process the file based on its extension unless overridden.
8+
! - `-x f95-cpp-input` should behave like `-x f95` but with preprocessing
9+
! (`-cpp`) enabled unless overridden.
10+
11+
! ---
12+
! Ensure the file is treated as fixed-form unless explicitly set otherwise
13+
! ---
14+
! RUN: not %flang -Werror -fsyntax-only -x f95 -cpp %s 2>&1 | FileCheck --check-prefix=SCAN-ERROR %s
15+
! RUN: not %flang -Werror -fsyntax-only -x f95-cpp-input %s 2>&1 | FileCheck --check-prefix=SCAN-ERROR %s
16+
17+
! SCAN-ERROR: error
18+
19+
! RUN: %flang -Werror -fsyntax-only -x f95 -cpp -ffree-form %s 2>&1 | FileCheck --check-prefix=NO-SCAN-ERROR --allow-empty %s
20+
! RUN: %flang -Werror -fsyntax-only -x f95-cpp-input -ffree-form %s 2>&1 | FileCheck --check-prefix=NO-SCAN-ERROR --allow-empty %s
21+
22+
! NO-SCAN-ERROR-NOT: error
23+
24+
! ---
25+
! Ensure `-cpp` is not enabled by default unless explicitly requested
26+
! ---
27+
! RUN: not %flang -Werror -fsyntax-only -x f95 -ffree-form %s 2>&1 | FileCheck --check-prefix=SEMA-ERROR %s
28+
! RUN: not %flang -Werror -fsyntax-only -x f95-cpp-input -nocpp -ffree-form %s 2>&1 | FileCheck --check-prefix=SEMA-ERROR %s
29+
30+
! SEMA-ERROR: error
31+
32+
! RUN: %flang -Werror -fsyntax-only -x f95 -cpp -ffree-form %s 2>&1 | FileCheck --check-prefix=NO-SEMA-ERROR --allow-empty %s
33+
! RUN: %flang -Werror -fsyntax-only -x f95-cpp-input -ffree-form %s 2>&1 | FileCheck --check-prefix=NO-SEMA-ERROR --allow-empty %s
34+
35+
! NO-SEMA-ERROR-NOT: error

flang/test/Driver/input-from-stdin/input-from-stdin.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
! Input type is implicit
77
! RUN: cat %s | %flang -E -cpp - | FileCheck %s --check-prefix=PP-NOT-DEFINED
88
! RUN: cat %s | %flang -DNEW -E -cpp - | FileCheck %s --check-prefix=PP-DEFINED
9-
! RUN: cat %s | %flang -DNEW -E - | FileCheck %s --check-prefix=PP-NOT-DEFINED
9+
! RUN: cat %s | %flang -DNEW -E - | FileCheck %s --check-prefix=PP-DEFINED
1010
! RUN: cat %s | %flang -DNEW -E -nocpp - | FileCheck %s --check-prefix=PP-NOT-DEFINED
1111

1212
! Input type is explicit

0 commit comments

Comments
 (0)