Skip to content

Commit

Permalink
[flang][preprocessor] Don't expand INCLUDE under -E by default (#110333)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
klausler authored Sep 30, 2024
1 parent 9b3818e commit 4dfed69
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -6898,6 +6898,8 @@ def module_suffix : Separate<["-"], "module-suffix">, Group<f_Group>, MetaVarNa
HelpText<"Use <suffix> as the suffix for module files (the default value is `.mod`)">;
def fno_reformat : Flag<["-"], "fno-reformat">, Group<Preprocessor_Group>,
HelpText<"Dump the cooked character stream in -E mode">;
def fpreprocess_include_lines : Flag<["-"], "fpreprocess-include-lines">, Group<Preprocessor_Group>,
HelpText<"Treat INCLUDE lines like #include directives in -E mode">;
defm analyzed_objects_for_unparse : OptOutFC1FFlag<"analyzed-objects-for-unparse", "", "Do not use the analyzed objects when unparsing">;

def emit_fir : Flag<["-"], "emit-fir">, Group<Action_Group>,
Expand Down
3 changes: 3 additions & 0 deletions flang/include/flang/Frontend/PreprocessorOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ struct PreprocessorOptions {
// -fno-reformat: Emit cooked character stream as -E output
bool noReformat{false};

// -fpreprocess-include-lines: Treat INCLUDE as #include for -E output
bool preprocessIncludeLines{false};

// -dM: Show macro definitions with -dM -E
bool showMacros{false};

Expand Down
1 change: 1 addition & 0 deletions flang/include/flang/Parser/parsing.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct Options {
bool needProvenanceRangeToCharBlockMappings{false};
Fortran::parser::Encoding encoding{Fortran::parser::Encoding::UTF_8};
bool prescanAndReformat{false}; // -E
bool expandIncludeLinesInPreprocessedOutput{true};
bool showColors{false};
};

Expand Down
6 changes: 6 additions & 0 deletions flang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,8 @@ static void parsePreprocessorArgs(Fortran::frontend::PreprocessorOptions &opts,
: PPMacrosFlag::Exclude;

opts.noReformat = args.hasArg(clang::driver::options::OPT_fno_reformat);
opts.preprocessIncludeLines =
args.hasArg(clang::driver::options::OPT_fpreprocess_include_lines);
opts.noLineDirectives = args.hasArg(clang::driver::options::OPT_P);
opts.showMacros = args.hasArg(clang::driver::options::OPT_dM);
}
Expand Down Expand Up @@ -1486,6 +1488,10 @@ void CompilerInvocation::setFortranOpts() {
}
fortranOptions.fixedFormColumns = frontendOptions.fixedFormColumns;

// -E
fortranOptions.prescanAndReformat =
frontendOptions.programAction == PrintPreprocessedInput;

fortranOptions.features = frontendOptions.features;
fortranOptions.encoding = frontendOptions.encoding;

Expand Down
4 changes: 4 additions & 0 deletions flang/lib/Frontend/FrontendAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ bool FrontendAction::beginSourceFile(CompilerInstance &ci,
getCurrentInput().getIsCUDAFortran());
}

// -fpreprocess-include-lines
invoc.getFortranOpts().expandIncludeLinesInPreprocessedOutput =
invoc.getPreprocessorOpts().preprocessIncludeLines;

// Decide between fixed and free form (if the user didn't express any
// preference, use the file extension to decide)
if (invoc.getFrontendOpts().fortranForm == FortranForm::Unknown) {
Expand Down
2 changes: 2 additions & 0 deletions flang/lib/Parser/parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ const SourceFile *Parsing::Prescan(const std::string &path, Options options) {
messages_, *currentCooked_, preprocessor_, options.features};
prescanner.set_fixedForm(options.isFixedForm)
.set_fixedFormColumnLimit(options.fixedFormColumns)
.set_expandIncludeLines(!options.prescanAndReformat ||
options.expandIncludeLinesInPreprocessedOutput)
.AddCompilerDirectiveSentinel("dir$");
if (options.features.IsEnabled(LanguageFeature::OpenACC)) {
prescanner.AddCompilerDirectiveSentinel("$acc");
Expand Down
3 changes: 3 additions & 0 deletions flang/lib/Parser/prescan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,9 @@ const char *Prescanner::IsFreeFormComment(const char *p) const {
}

std::optional<std::size_t> Prescanner::IsIncludeLine(const char *start) const {
if (!expandIncludeLines_) {
return std::nullopt;
}
const char *p{SkipWhiteSpace(start)};
if (*p == '0' && inFixedForm_ && p == start + 5) {
// Accept " 0INCLUDE" in fixed form.
Expand Down
5 changes: 5 additions & 0 deletions flang/lib/Parser/prescan.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class Prescanner {
Preprocessor &preprocessor() { return preprocessor_; }
common::LanguageFeatureControl &features() { return features_; }

Prescanner &set_expandIncludeLines(bool yes) {
expandIncludeLines_ = yes;
return *this;
}
Prescanner &set_fixedForm(bool yes) {
inFixedForm_ = yes;
return *this;
Expand Down Expand Up @@ -209,6 +213,7 @@ class Prescanner {
Preprocessor &preprocessor_;
AllSources &allSources_;
common::LanguageFeatureControl features_;
bool expandIncludeLines_{true};
bool isNestedInIncludeDirective_{false};
bool backslashFreeFormContinuation_{false};
bool inFixedForm_{false};
Expand Down
2 changes: 1 addition & 1 deletion flang/test/Parser/include.f
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
! RUN: %flang_fc1 -E -I %S/Inputs %s 2>&1 | FileCheck %s
! RUN: %flang_fc1 -E -fpreprocess-include-lines -I %S/Inputs %s 2>&1 | FileCheck %s
include 'include-file'
include "include-file"
include 1_'include-file'
Expand Down

0 comments on commit 4dfed69

Please sign in to comment.