Skip to content

Commit baebc11

Browse files
committed
[clang][driver] Set the input type to Fortran when reading from stdin
This patch makes sure that for the following invocation of the new Flang driver, clangDriver sets the input type to Fortran: ``` flang-new -E - ``` This change does not affect `clang`, i.e. for the following invocation the input type is set to C: ``` clang -E - ``` This change leverages the fact that for `flang-new` the driver is in Flang mode. Differential Revision: https://reviews.llvm.org/D96777
1 parent 0c8b26b commit baebc11

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

clang/lib/Driver/Driver.cpp

+14-9
Original file line numberDiff line numberDiff line change
@@ -2191,15 +2191,20 @@ void Driver::BuildInputs(const ToolChain &TC, DerivedArgList &Args,
21912191

21922192
// stdin must be handled specially.
21932193
if (memcmp(Value, "-", 2) == 0) {
2194-
// If running with -E, treat as a C input (this changes the builtin
2195-
// macros, for example). This may be overridden by -ObjC below.
2196-
//
2197-
// Otherwise emit an error but still use a valid type to avoid
2198-
// spurious errors (e.g., no inputs).
2199-
if (!Args.hasArgNoClaim(options::OPT_E) && !CCCIsCPP())
2200-
Diag(IsCLMode() ? clang::diag::err_drv_unknown_stdin_type_clang_cl
2201-
: clang::diag::err_drv_unknown_stdin_type);
2202-
Ty = types::TY_C;
2194+
if (IsFlangMode()) {
2195+
Ty = types::TY_Fortran;
2196+
} else {
2197+
// If running with -E, treat as a C input (this changes the
2198+
// builtin macros, for example). This may be overridden by -ObjC
2199+
// below.
2200+
//
2201+
// Otherwise emit an error but still use a valid type to avoid
2202+
// spurious errors (e.g., no inputs).
2203+
if (!Args.hasArgNoClaim(options::OPT_E) && !CCCIsCPP())
2204+
Diag(IsCLMode() ? clang::diag::err_drv_unknown_stdin_type_clang_cl
2205+
: clang::diag::err_drv_unknown_stdin_type);
2206+
Ty = types::TY_C;
2207+
}
22032208
} else {
22042209
// Otherwise lookup by extension.
22052210
// Fallback is C if invoked as C preprocessor, C++ if invoked with

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

+9-6
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@
55
!--------------------------
66
! FLANG DRIVER (flang-new)
77
!--------------------------
8-
! TODO: Add support for `flang-new -`
9-
! Currently `bin/flang-new -E -` defaults to `-x c` and e.g. F90 is not allowed
10-
! in `-x <input-type>` (see `clang::driver::types::canTypeBeUserSpecified` in
11-
! Types.cpp)
8+
! Input type is implicit
9+
! RUN: cat %s | flang-new -E - | FileCheck %s --check-prefix=PP-NOT-DEFINED
10+
! RUN: cat %s | flang-new -DNEW -E - | FileCheck %s --check-prefix=PP-DEFINED
11+
12+
! Input type is explicit
13+
! RUN: cat %s | flang-new -E -x f95-cpp-input - | FileCheck %s --check-prefix=PP-NOT-DEFINED
14+
! RUN: cat %s | flang-new -DNEW -E -x f95-cpp-input - | FileCheck %s --check-prefix=PP-DEFINED
1215

1316
!---------------------------------------
1417
! FLANG FRONTEND DRIVER (flang-new -fc1)
1518
!---------------------------------------
16-
! Test `-E` - for the corresponding frontend actions the driver relies on the prescanner API to handle file I/O
19+
! Test `-E`: for the corresponding frontend actions the driver relies on the prescanner API to handle file I/O
1720
! RUN: cat %s | flang-new -fc1 -E | FileCheck %s --check-prefix=PP-NOT-DEFINED
1821
! RUN: cat %s | flang-new -fc1 -DNEW -E | FileCheck %s --check-prefix=PP-DEFINED
1922

20-
! Test `-test-io` - for the corresponding frontend action (`InputOutputTestAction`) the driver handles the file I/O on its own
23+
! Test `-test-io`: for the corresponding frontend action (`InputOutputTestAction`) the driver handles the file I/O on its own
2124
! the corresponding action (`PrintPreprocessedAction`)
2225
! RUN: cat %s | flang-new -fc1 -test-io | FileCheck %s --check-prefix=IO --match-full-lines
2326
! RUN: cat %s | flang-new -fc1 -DNEW -test-io | FileCheck %s --check-prefix=IO --match-full-lines

0 commit comments

Comments
 (0)