Skip to content

Commit 15d7f77

Browse files
RichBarton-Armbryanpkc
authored andcommitted
Fix flang driver preprocessor issue
Fix a bug where flang -E -save-temps input.F90 would preprocess the input file twice. Add a new test for preprocessor behaviour that would expose the bug and updated fortran-phases test for new behaviour. Also added -emit-flang-llvm to that test for completeness. Signed-off-by: Richard Barton <[email protected]>
1 parent bad00a6 commit 15d7f77

File tree

4 files changed

+109
-38
lines changed

4 files changed

+109
-38
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -400,14 +400,7 @@ phases::ID Driver::getFinalPhase(const DerivedArgList &DAL,
400400
(PhaseArg = DAL.getLastArg(options::OPT_M, options::OPT_MM)) ||
401401
(PhaseArg = DAL.getLastArg(options::OPT__SLASH_P)) ||
402402
CCGenDiagnostics) {
403-
#ifdef ENABLE_CLASSIC_FLANG
404-
if (IsFlangMode())
405-
FinalPhase = phases::Compile;
406-
else
407-
FinalPhase = phases::Preprocess;
408-
#else
409403
FinalPhase = phases::Preprocess;
410-
#endif
411404

412405
// --precompile only runs up to precompilation.
413406
// Options that cause the output of C++20 compiled module interfaces or
@@ -2946,7 +2939,11 @@ void Driver::BuildInputs(const ToolChain &TC, DerivedArgList &Args,
29462939
if (memcmp(Value, "-", 2) == 0) {
29472940
if (IsFlangMode()) {
29482941
#ifdef ENABLE_CLASSIC_FLANG
2949-
Ty = types::TY_C;
2942+
// If running with -E, treat as needing preprocessing
2943+
if (!Args.hasArgNoClaim(options::OPT_E))
2944+
Ty = types::TY_PP_F_FreeForm;
2945+
else
2946+
Ty = types::TY_F_FreeForm;
29502947
#else
29512948
Ty = types::TY_Fortran;
29522949
#endif
@@ -2973,6 +2970,16 @@ void Driver::BuildInputs(const ToolChain &TC, DerivedArgList &Args,
29732970
// idea of what .s is.
29742971
if (const char *Ext = strrchr(Value, '.'))
29752972
Ty = TC.LookupTypeForExtension(Ext + 1);
2973+
#ifdef ENABLE_CLASSIC_FLANG
2974+
// If called with -E, treat the inputs as needing preprocessing
2975+
// regardless of extension
2976+
if (IsFlangMode() && Args.hasArgNoClaim(options::OPT_E)) {
2977+
if (Ty == types::TY_PP_F_FreeForm)
2978+
Ty = types::TY_F_FreeForm;
2979+
else if (Ty == types::TY_PP_F_FixedForm)
2980+
Ty = types::TY_F_FixedForm;
2981+
}
2982+
#endif
29762983

29772984
if (Ty == types::TY_INVALID) {
29782985
if (IsCLMode() && (Args.hasArgNoClaim(options::OPT_E) || CCGenDiagnostics))

clang/lib/Driver/ToolChains/ClassicFlang.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ void ClassicFlang::ConstructJob(Compilation &C, const JobAction &JA,
8080
// Check file type sanity
8181
assert(types::isAcceptedByFlang(InputType) && "Can only accept Fortran");
8282

83-
if (Args.hasArg(options::OPT_fsyntax_only)) {
84-
// For -fsyntax-only produce temp files only
83+
if (Args.hasArg(options::OPT_fsyntax_only, options::OPT_E)) {
84+
// For -fsyntax-only and -E produce temp files only
8585
Stem = C.getDriver().GetTemporaryPath("", "");
8686
} else {
8787
OutFile = Output.getFilename();

clang/test/Driver/fortran-phases.f90

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
! RUN: %flang -ccc-print-phases -S 2>&1 %s | FileCheck %s --check-prefix=AONLY-NOPP
88
! RUN: %flang -ccc-print-phases -c -emit-llvm 2>&1 %s | FileCheck %s --check-prefix=LLONLY-NOPP
99
! RUN: %flang -ccc-print-phases -S -emit-llvm 2>&1 %s | FileCheck %s --check-prefix=LLONLY-NOPP
10+
! RUN: %flang -ccc-print-phases -emit-flang-llvm 2>&1 %s | FileCheck %s --check-prefix=FLLONLY-NOPP
1011
! RUN: %flang -ccc-print-phases -fsyntax-only 2>&1 %s | FileCheck %s --check-prefix=SONLY-NOPP
1112
! RUN: %flang -ccc-print-phases -E 2>&1 %s | FileCheck %s --check-prefix=PPONLY-NOPP
1213

@@ -16,6 +17,7 @@
1617
! RUN: %flang -ccc-print-phases -S 2>&1 -x f95-cpp-input %s | FileCheck %s --check-prefix=AONLY
1718
! RUN: %flang -ccc-print-phases -c -emit-llvm 2>&1 -x f95-cpp-input %s | FileCheck %s --check-prefix=LLONLY
1819
! RUN: %flang -ccc-print-phases -S -emit-llvm 2>&1 -x f95-cpp-input %s | FileCheck %s --check-prefix=LLONLY
20+
! RUN: %flang -ccc-print-phases -emit-flang-llvm 2>&1 -x f95-cpp-input %s | FileCheck %s --check-prefix=FLLONLY
1921
! RUN: %flang -ccc-print-phases -fsyntax-only 2>&1 -x f95-cpp-input %s | FileCheck %s --check-prefix=SONLY
2022
! RUN: %flang -ccc-print-phases -E 2>&1 -x f95-cpp-input %s | FileCheck %s --check-prefix=PPONLY
2123

@@ -29,31 +31,39 @@
2931
! CONLY-NOPP: 1: compiler, {0}, ir
3032
! CONLY-NOPP: 2: backend, {1}, assembler
3133
! CONLY-NOPP: 3: assembler, {2}, object
32-
! CONLY-NOPP-NOT: 4: linker, {3}, image
34+
! CONLY-NOPP-NOT: {{.*}}: linker, {{{.*}}}, image
3335

3436
! AONLY-NOPP: 0: input, {{.*}}, f95
3537
! AONLY-NOPP: 1: compiler, {0}, ir
3638
! AONLY-NOPP: 2: backend, {1}, assembler
37-
! AONLY-NOPP-NOT: 3: assembler, {2}, object
38-
! AONLY-NOPP-NOT: 4: linker, {3}, image
39+
! AONLY-NOPP-NOT: {{.*}}: assembler, {{{.*}}}, object
40+
! AONLY-NOPP-NOT: {{.*}}: linker, {{{.*}}}, image
3941

4042
! LLONLY-NOPP: 0: input, {{.*}}, f95
4143
! LLONLY-NOPP: 1: compiler, {0}, ir
42-
! LLONLY-NOPP-NOT: 2: backend, {1}, assembler
43-
! LLONLY-NOPP-NOT: 3: assembler, {2}, object
44-
! LLONLY-NOPP-NOT: 4: linker, {3}, image
44+
! LLONLY-NOPP-NOT: {{.*}}: backend, {{{.*}}}, assembler
45+
! LLONLY-NOPP-NOT: {{.*}}: assembler, {{{.*}}}, object
46+
! LLONLY-NOPP-NOT: {{.*}}: linker, {{{.*}}}, image
47+
48+
! FLLONLY-NOPP: 0: input, {{.*}}, f95
49+
! FLLONLY-NOPP: 1: compiler, {0}, ir
50+
! FLLONLY-NOPP-NOT: {{.*}}: backend, {{{.*}}}, assembler
51+
! FLLONLY-NOPP-NOT: {{.*}}: assembler, {{{.*}}}, object
52+
! FLLONLY-NOPP-NOT: {{.*}}: linker, {{{.*}}}, image
4553

4654
! SONLY-NOPP: 0: input, {{.*}}, f95
47-
! SONLY-NOPP-NOT: 1: compiler, {0}, ir
48-
! SONLY-NOPP-NOT: 2: backend, {1}, assembler
49-
! SONLY-NOPP-NOT: 3: assembler, {2}, object
50-
! SONLY-NOPP-NOT: 4: linker, {3}, image
55+
! SONLY-NOPP-NOT: {{.*}}: compiler, {{{.*}}}, ir
56+
! SONLY-NOPP-NOT: {{.*}}: backend, {{{.*}}}, assembler
57+
! SONLY-NOPP-NOT: {{.*}}: assembler, {{{.*}}}, object
58+
! SONLY-NOPP-NOT: {{.*}}: linker, {{{.*}}}, image
5159

60+
! flang always preprocesses with -E regardless of file extension
5261
! PPONLY-NOPP: 0: input, {{.*}}, f95
53-
! PPONLY-NOPP: 1: compiler, {0}, ir
54-
! PPONLY-NOPP-NOT: 2: backend, {1}, assembler
55-
! PPONLY-NOPP-NOT: 3: assembler, {2}, object
56-
! PPONLY-NOPP-NOT: 4: linker, {3}, image
62+
! PPONLY-NOPP: 1: preprocessor, {0}, f95
63+
! PPONLY-NOPP-NOT: {{.*}}: compiler, {{{.*}}}, ir
64+
! PPONLY-NOPP-NOT: {{.*}}: backend, {{{.*}}}, assembler
65+
! PPONLY-NOPP-NOT: {{.*}}: assembler, {{{.*}}}, object
66+
! PPONLY-NOPP-NOT: {{.*}}: linker, {{{.*}}}, image
5767

5868
! LINK: 0: input, {{.*}}, f95-cpp-input
5969
! LINK: 1: preprocessor, {0}, f95
@@ -67,35 +77,42 @@
6777
! CONLY: 2: compiler, {1}, ir
6878
! CONLY: 3: backend, {2}, assembler
6979
! CONLY: 4: assembler, {3}, object
70-
! CONLY-NOT: 5: linker, {4}, image
80+
! CONLY-NOT: {{.*}}: linker, {{{.*}}}, image
7181

7282
! AONLY: 0: input, {{.*}}, f95-cpp-input
7383
! AONLY: 1: preprocessor, {0}, f95
7484
! AONLY: 2: compiler, {1}, ir
7585
! AONLY: 3: backend, {2}, assembler
76-
! AONLY-NOT: 4: assembler, {3}, object
77-
! AONLY-NOT: 5: linker, {4}, image
86+
! AONLY-NOT: {{.*}}: assembler, {{{.*}}}, object
87+
! AONLY-NOT: {{.*}}: linker, {{{.*}}}, image
7888

7989
! LLONLY: 0: input, {{.*}}, f95-cpp-input
8090
! LLONLY: 1: preprocessor, {0}, f95
8191
! LLONLY: 2: compiler, {1}, ir
82-
! LLONLY-NOT: 3: backend, {2}, assembler
83-
! LLONLY-NOT: 4: assembler, {3}, object
84-
! LLONLY-NOT: 5: linker, {4}, image
92+
! LLONLY-NOT: {{.*}}: backend, {{{.*}}}, assembler
93+
! LLONLY-NOT: {{.*}}: assembler, {{{.*}}}, object
94+
! LLONLY-NOT: {{.*}}: linker, {{{.*}}}, image
95+
96+
! FLLONLY: 0: input, {{.*}}, f95-cpp-input
97+
! FLLONLY: 1: preprocessor, {0}, f95
98+
! FLLONLY: 2: compiler, {1}, ir
99+
! FLLONLY-NOT: {{.*}}: backend, {{{.*}}}, assembler
100+
! FLLONLY-NOT: {{.*}}: assembler, {{{.*}}}, object
101+
! FLLONLY-NOT: {{.*}}: linker, {{{.*}}}, image
85102

86103
! SONLY: 0: input, {{.*}}, f95-cpp-input
87104
! SONLY: 1: preprocessor, {0}, f95
88-
! SONLY-NOT: 2: compiler, {1}, ir
89-
! SONLY-NOT: 3: backend, {2}, assembler
90-
! SONLY-NOT: 4: assembler, {3}, object
91-
! SONLY-NOT: 5: linker, {4}, image
105+
! SONLY-NOT: {{.*}}: compiler, {{{.*}}}, ir
106+
! SONLY-NOT: {{.*}}: backend, {{{.*}}}, assembler
107+
! SONLY-NOT: {{.*}}: assembler, {{{.*}}}, object
108+
! SONLY-NOT: {{.*}}: linker, {{{.*}}}, image
92109

93110
! PPONLY: 0: input, {{.*}}, f95-cpp-input
94111
! PPONLY: 1: preprocessor, {0}, f95
95-
! PPONLY: 2: compiler, {1}, ir
96-
! PPONLY-NOT: 3: backend, {2}, assembler
97-
! PPONLY-NOT: 4: assembler, {3}, object
98-
! PPONLY-NOT: 5: linker, {4}, image
112+
! PPONLY-NOT: {{.*}}: compiler, {{{.*}}}, ir
113+
! PPONLY-NOT: {{.*}}: backend, {{{.*}}}, assembler
114+
! PPONLY-NOT: {{.*}}: assembler, {{{.*}}}, object
115+
! PPONLY-NOT: {{.*}}: linker, {{{.*}}}, image
99116

100117
program hello
101118
write(*, *) "Hello"
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
! REQUIRES: classic_flang
2+
3+
! -cpp should preprocess as it goes, regardless of input file extension
4+
! 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
6+
! -E should preprocess then stop, regardless of input file extension
7+
! RUN: %flang -E -DHELLO="hello all" -### %s 2>&1 | FileCheck %s --check-prefixes=ALL,E,PPONLY
8+
! RUN: %flang -E -DHELLO="hello all" -### -x f95-cpp-input %s 2>&1 | FileCheck %s --check-prefixes=ALL,E,PPONLY
9+
! -cpp and -E are redundant
10+
! RUN: %flang -E -cpp -DHELLO="hello all" -### %s 2>&1 | FileCheck %s --check-prefixes=ALL,E,PPONLY
11+
12+
! Don't link when given linker input
13+
! RUN: %flang -E -cpp -Wl,-rpath=blah -### %s 2>&1 | FileCheck %s --check-prefixes=ALL,E,PPONLY
14+
15+
! Explicitly test this nonsence case causing a bug with LLVM 13/14
16+
! RUN: %flang -E -traditional-cpp -DHELLO="hello all" -x f95-cpp-input -### %s 2>&1 | FileCheck %s --check-prefixes=ALL,E,PPONLY
17+
18+
! Test -save-temps does not break things (same codepath as -traditional-cpp bug above)
19+
! 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
23+
24+
! Test for the correct cmdline flags
25+
! Consume up to flang1 line
26+
! ALL-LABEL: "{{.*}}flang1"
27+
! CPP-NOT: "-es"
28+
! CPP: "-preprocess"
29+
! CPP-NOT: "-es"
30+
31+
! E-DAG: "-es"
32+
! E-DAG: "-preprocess"
33+
34+
! flang1 should only be called once!
35+
! ALL-NOT: "{{.*}}flang1"
36+
37+
! CPP should continue to build object
38+
! PP: "{{.*}}flang2"
39+
! PPONLY-NOT: "{{.*}}flang2"
40+
41+
! These commands should never call a linker!
42+
! ALL-NOT: "{{.*}}ld"
43+
44+
program hello
45+
write(*, *) HELLO
46+
end program hello
47+

0 commit comments

Comments
 (0)