Skip to content

[flang][preprocessor] Don't expand INCLUDE under -E by default #110333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -6886,6 +6886,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 @@ -819,6 +819,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 @@ -1479,6 +1481,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
Loading