Skip to content

Commit c27e8f4

Browse files
committed
[Driver] Fix flang driver preprocessor issue
The -save-temps option has been causing the driver to run flang1/flang2 on the input file twice, and the fortran-preprocessor.f90 test was unable to detect the error, because it had used -c instead of -x in a couple of RUN commands. This patch fixes the test and the driver to make sure that the frontend is not invoked multiple times with -save-temps.
1 parent d07a1b8 commit c27e8f4

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5367,8 +5367,17 @@ class ToolSelector final {
53675367
/// are appended to \a CollapsedOffloadAction.
53685368
void combineWithPreprocessor(const Tool *T, ActionList &Inputs,
53695369
ActionList &CollapsedOffloadAction) {
5370+
#ifdef ENABLE_CLASSIC_FLANG
5371+
// flang1 always combines preprocessing and compilation.
5372+
// Do not return early even when -save-temps is used.
5373+
if (!T || !T->hasIntegratedCPP() ||
5374+
(strcmp(T->getName(), "classic-flang") &&
5375+
!canCollapsePreprocessorAction()))
5376+
return;
5377+
#else
53705378
if (!T || !canCollapsePreprocessorAction() || !T->hasIntegratedCPP())
53715379
return;
5380+
#endif
53725381

53735382
// Attempt to get a preprocessor action dependence.
53745383
ActionList PreprocessJobOffloadActions;

clang/lib/Driver/ToolChains/ClassicFlang.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace tools {
2828
class LLVM_LIBRARY_VISIBILITY ClassicFlang : public Tool {
2929
public:
3030
ClassicFlang(const ToolChain &TC)
31-
: Tool("flang:frontend",
31+
: Tool("classic-flang",
3232
"Fortran frontend to LLVM", TC) {}
3333

3434
bool hasGoodDiagnostics() const override { return true; }

clang/test/Driver/fortran-preprocessor.f90

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
! -cpp should preprocess as it goes, regardless of input file extension
44
! RUN: %flang -cpp -c -DHELLO="hello all" -### %s 2>&1 | FileCheck %s --check-prefixes=ALL,CPP,PP
5-
! RUN: %flang -cpp -c -DHELLO="hello all" -### -c f95-cpp-input %s 2>&1 | FileCheck %s --check-prefixes=ALL,CPP,PP
5+
! RUN: %flang -cpp -c -DHELLO="hello all" -### -x f95-cpp-input %s 2>&1 | FileCheck %s --check-prefixes=ALL,CPP,PP
66
! -E should preprocess then stop, regardless of input file extension
77
! RUN: %flang -E -DHELLO="hello all" -### %s 2>&1 | FileCheck %s --check-prefixes=ALL,E,PPONLY
88
! RUN: %flang -E -DHELLO="hello all" -### -x f95-cpp-input %s 2>&1 | FileCheck %s --check-prefixes=ALL,E,PPONLY
@@ -17,9 +17,9 @@
1717

1818
! Test -save-temps does not break things (same codepath as -traditional-cpp bug above)
1919
! RUN: %flang -E -DHELLO="hello all" -save-temps -### %s 2>&1 | FileCheck %s --check-prefixes=ALL,E,PPONLY
20-
! RUN: %flang -E -DHELLO="hello all" -save-temps -### -c f95-cpp-input %s 2>&1 | FileCheck %s --check-prefixes=ALL,E,PPONLY
21-
! RUN: %flang -cpp -c -DHELLO="hello all" -save-temps -### %s 2>&1 | FileCheck %s --check-prefixes=ALL,CPP
22-
! RUN: %flang -cpp -c -DHELLO="hello all" -save-temps -### -c f95-cpp-input %s 2>&1 | FileCheck %s --check-prefixes=ALL,CPP
20+
! RUN: %flang -E -DHELLO="hello all" -save-temps -### -x f95-cpp-input %s 2>&1 | FileCheck %s --check-prefixes=ALL,E,PPONLY
21+
! RUN: %flang -cpp -c -DHELLO="hello all" -save-temps -### %s 2>&1 | FileCheck %s --check-prefixes=ALL,CPP,PP
22+
! RUN: %flang -cpp -c -DHELLO="hello all" -save-temps -### -x f95-cpp-input %s 2>&1 | FileCheck %s --check-prefixes=ALL,CPP,PP
2323

2424
! Test for the correct cmdline flags
2525
! Consume up to flang1 line
@@ -31,13 +31,14 @@
3131
! E-DAG: "-es"
3232
! E-DAG: "-preprocess"
3333

34-
! flang1 should only be called once!
35-
! ALL-NOT: "{{.*}}flang1"
36-
3734
! CPP should continue to build object
3835
! PP: "{{.*}}flang2"
3936
! PPONLY-NOT: "{{.*}}flang2"
4037

38+
! flang1 and flang2 should only be called at most once!
39+
! ALL-NOT: "{{.*}}flang1"
40+
! ALL-NOT: "{{.*}}flang2"
41+
4142
! These commands should never call a linker!
4243
! ALL-NOT: "{{.*}}ld"
4344

0 commit comments

Comments
 (0)