Skip to content

Commit 4dfed69

Browse files
authored
[flang][preprocessor] Don't expand INCLUDE under -E by default (#110333)
Fortran INCLUDE lines have (until now) been treated like #include directives. This isn't how things work with other Fortran compilers when running under the -E option for preprocessing only, so stop doing it by default, and add -fpreprocess-include-lines to turn it back on when desired.
1 parent 9b3818e commit 4dfed69

File tree

9 files changed

+27
-1
lines changed

9 files changed

+27
-1
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6898,6 +6898,8 @@ def module_suffix : Separate<["-"], "module-suffix">, Group<f_Group>, MetaVarNa
68986898
HelpText<"Use <suffix> as the suffix for module files (the default value is `.mod`)">;
68996899
def fno_reformat : Flag<["-"], "fno-reformat">, Group<Preprocessor_Group>,
69006900
HelpText<"Dump the cooked character stream in -E mode">;
6901+
def fpreprocess_include_lines : Flag<["-"], "fpreprocess-include-lines">, Group<Preprocessor_Group>,
6902+
HelpText<"Treat INCLUDE lines like #include directives in -E mode">;
69016903
defm analyzed_objects_for_unparse : OptOutFC1FFlag<"analyzed-objects-for-unparse", "", "Do not use the analyzed objects when unparsing">;
69026904

69036905
def emit_fir : Flag<["-"], "emit-fir">, Group<Action_Group>,

flang/include/flang/Frontend/PreprocessorOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ struct PreprocessorOptions {
5656
// -fno-reformat: Emit cooked character stream as -E output
5757
bool noReformat{false};
5858

59+
// -fpreprocess-include-lines: Treat INCLUDE as #include for -E output
60+
bool preprocessIncludeLines{false};
61+
5962
// -dM: Show macro definitions with -dM -E
6063
bool showMacros{false};
6164

flang/include/flang/Parser/parsing.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct Options {
4040
bool needProvenanceRangeToCharBlockMappings{false};
4141
Fortran::parser::Encoding encoding{Fortran::parser::Encoding::UTF_8};
4242
bool prescanAndReformat{false}; // -E
43+
bool expandIncludeLinesInPreprocessedOutput{true};
4344
bool showColors{false};
4445
};
4546

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,8 @@ static void parsePreprocessorArgs(Fortran::frontend::PreprocessorOptions &opts,
820820
: PPMacrosFlag::Exclude;
821821

822822
opts.noReformat = args.hasArg(clang::driver::options::OPT_fno_reformat);
823+
opts.preprocessIncludeLines =
824+
args.hasArg(clang::driver::options::OPT_fpreprocess_include_lines);
823825
opts.noLineDirectives = args.hasArg(clang::driver::options::OPT_P);
824826
opts.showMacros = args.hasArg(clang::driver::options::OPT_dM);
825827
}
@@ -1486,6 +1488,10 @@ void CompilerInvocation::setFortranOpts() {
14861488
}
14871489
fortranOptions.fixedFormColumns = frontendOptions.fixedFormColumns;
14881490

1491+
// -E
1492+
fortranOptions.prescanAndReformat =
1493+
frontendOptions.programAction == PrintPreprocessedInput;
1494+
14891495
fortranOptions.features = frontendOptions.features;
14901496
fortranOptions.encoding = frontendOptions.encoding;
14911497

flang/lib/Frontend/FrontendAction.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ bool FrontendAction::beginSourceFile(CompilerInstance &ci,
9595
getCurrentInput().getIsCUDAFortran());
9696
}
9797

98+
// -fpreprocess-include-lines
99+
invoc.getFortranOpts().expandIncludeLinesInPreprocessedOutput =
100+
invoc.getPreprocessorOpts().preprocessIncludeLines;
101+
98102
// Decide between fixed and free form (if the user didn't express any
99103
// preference, use the file extension to decide)
100104
if (invoc.getFrontendOpts().fortranForm == FortranForm::Unknown) {

flang/lib/Parser/parsing.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ const SourceFile *Parsing::Prescan(const std::string &path, Options options) {
7575
messages_, *currentCooked_, preprocessor_, options.features};
7676
prescanner.set_fixedForm(options.isFixedForm)
7777
.set_fixedFormColumnLimit(options.fixedFormColumns)
78+
.set_expandIncludeLines(!options.prescanAndReformat ||
79+
options.expandIncludeLinesInPreprocessedOutput)
7880
.AddCompilerDirectiveSentinel("dir$");
7981
if (options.features.IsEnabled(LanguageFeature::OpenACC)) {
8082
prescanner.AddCompilerDirectiveSentinel("$acc");

flang/lib/Parser/prescan.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,9 @@ const char *Prescanner::IsFreeFormComment(const char *p) const {
10311031
}
10321032

10331033
std::optional<std::size_t> Prescanner::IsIncludeLine(const char *start) const {
1034+
if (!expandIncludeLines_) {
1035+
return std::nullopt;
1036+
}
10341037
const char *p{SkipWhiteSpace(start)};
10351038
if (*p == '0' && inFixedForm_ && p == start + 5) {
10361039
// Accept " 0INCLUDE" in fixed form.

flang/lib/Parser/prescan.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ class Prescanner {
4848
Preprocessor &preprocessor() { return preprocessor_; }
4949
common::LanguageFeatureControl &features() { return features_; }
5050

51+
Prescanner &set_expandIncludeLines(bool yes) {
52+
expandIncludeLines_ = yes;
53+
return *this;
54+
}
5155
Prescanner &set_fixedForm(bool yes) {
5256
inFixedForm_ = yes;
5357
return *this;
@@ -209,6 +213,7 @@ class Prescanner {
209213
Preprocessor &preprocessor_;
210214
AllSources &allSources_;
211215
common::LanguageFeatureControl features_;
216+
bool expandIncludeLines_{true};
212217
bool isNestedInIncludeDirective_{false};
213218
bool backslashFreeFormContinuation_{false};
214219
bool inFixedForm_{false};

flang/test/Parser/include.f

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
! RUN: %flang_fc1 -E -I %S/Inputs %s 2>&1 | FileCheck %s
1+
! RUN: %flang_fc1 -E -fpreprocess-include-lines -I %S/Inputs %s 2>&1 | FileCheck %s
22
include 'include-file'
33
include "include-file"
44
include 1_'include-file'

0 commit comments

Comments
 (0)