Skip to content

[flang][preprocessing] Handle #include after & line continuation #93382

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
Jun 3, 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
19 changes: 10 additions & 9 deletions flang/lib/Parser/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -749,17 +749,18 @@ void Preprocessor::Directive(const TokenSequence &dir, Prescanner &prescanner) {
}
std::string buf;
llvm::raw_string_ostream error{buf};
const SourceFile *included{
allSources_.Open(include, error, std::move(prependPath))};
if (!included) {
if (const SourceFile *
included{allSources_.Open(include, error, std::move(prependPath))}) {
if (included->bytes() > 0) {
ProvenanceRange fileRange{
allSources_.AddIncludedFile(*included, dir.GetProvenanceRange())};
Prescanner{prescanner, /*isNestedInIncludeDirective=*/true}
.set_encoding(included->encoding())
.Prescan(fileRange);
}
} else {
prescanner.Say(dir.GetTokenProvenanceRange(j), "#include: %s"_err_en_US,
error.str());
} else if (included->bytes() > 0) {
ProvenanceRange fileRange{
allSources_.AddIncludedFile(*included, dir.GetProvenanceRange())};
Prescanner{prescanner}
.set_encoding(included->encoding())
.Prescan(fileRange);
}
} else {
prescanner.Say(dir.GetTokenProvenanceRange(dirOffset),
Expand Down
54 changes: 33 additions & 21 deletions flang/lib/Parser/prescan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ Prescanner::Prescanner(Messages &messages, CookedSource &cooked,
backslashFreeFormContinuation_{preprocessor.AnyDefinitions()},
encoding_{allSources_.encoding()} {}

Prescanner::Prescanner(const Prescanner &that)
Prescanner::Prescanner(const Prescanner &that, bool isNestedInIncludeDirective)
: messages_{that.messages_}, cooked_{that.cooked_},
preprocessor_{that.preprocessor_}, allSources_{that.allSources_},
features_{that.features_},
isNestedInIncludeDirective_{isNestedInIncludeDirective},
backslashFreeFormContinuation_{that.backslashFreeFormContinuation_},
inFixedForm_{that.inFixedForm_},
fixedFormColumnLimit_{that.fixedFormColumnLimit_},
Expand Down Expand Up @@ -104,11 +105,14 @@ void Prescanner::Statement() {
NextLine();
return;
case LineClassification::Kind::ConditionalCompilationDirective:
case LineClassification::Kind::IncludeDirective:
case LineClassification::Kind::DefinitionDirective:
case LineClassification::Kind::PreprocessorDirective:
preprocessor_.Directive(TokenizePreprocessorDirective(), *this);
return;
case LineClassification::Kind::IncludeDirective:
preprocessor_.Directive(TokenizePreprocessorDirective(), *this);
afterIncludeDirective_ = true;
return;
case LineClassification::Kind::CompilerDirective: {
directiveSentinel_ = line.sentinel;
CHECK(InCompilerDirective());
Expand Down Expand Up @@ -213,10 +217,7 @@ void Prescanner::Statement() {
Say(preprocessed->GetProvenanceRange(),
"Preprocessed line resembles a preprocessor directive"_warn_en_US);
}
preprocessed->ToLowerCase()
.CheckBadFortranCharacters(messages_, *this)
.CheckBadParentheses(messages_)
.Emit(cooked_);
CheckAndEmitLine(preprocessed->ToLowerCase(), newlineProvenance);
break;
case LineClassification::Kind::CompilerDirective:
if (preprocessed->HasRedundantBlanks()) {
Expand All @@ -228,10 +229,9 @@ void Prescanner::Statement() {
NormalizeCompilerDirectiveCommentMarker(*preprocessed);
preprocessed->ToLowerCase();
SourceFormChange(preprocessed->ToString());
preprocessed->ClipComment(*this, true /* skip first ! */)
.CheckBadFortranCharacters(messages_, *this)
.CheckBadParentheses(messages_)
.Emit(cooked_);
CheckAndEmitLine(preprocessed->ToLowerCase().ClipComment(
*this, true /* skip first ! */),
newlineProvenance);
break;
case LineClassification::Kind::Source:
if (inFixedForm_) {
Expand All @@ -246,14 +246,11 @@ void Prescanner::Statement() {
preprocessed->RemoveRedundantBlanks();
}
}
preprocessed->ToLowerCase()
.ClipComment(*this)
.CheckBadFortranCharacters(messages_, *this)
.CheckBadParentheses(messages_)
.Emit(cooked_);
CheckAndEmitLine(
preprocessed->ToLowerCase().ClipComment(*this), newlineProvenance);
break;
}
} else {
} else { // no macro replacement
if (line.kind == LineClassification::Kind::CompilerDirective) {
while (CompilerDirectiveContinuation(tokens, line.sentinel)) {
newlineProvenance = GetCurrentProvenance();
Expand All @@ -266,16 +263,29 @@ void Prescanner::Statement() {
EnforceStupidEndStatementRules(tokens);
}
}
tokens.CheckBadFortranCharacters(messages_, *this)
.CheckBadParentheses(messages_)
.Emit(cooked_);
CheckAndEmitLine(tokens, newlineProvenance);
}
directiveSentinel_ = nullptr;
}

void Prescanner::CheckAndEmitLine(
TokenSequence &tokens, Provenance newlineProvenance) {
tokens.CheckBadFortranCharacters(messages_, *this);
// Parenthesis nesting check does not apply while any #include is
// active, nor on the lines before and after a top-level #include.
// Applications play shenanigans with line continuation before and
// after #include'd subprogram argument lists.
if (!isNestedInIncludeDirective_ && !omitNewline_ &&
!afterIncludeDirective_) {
tokens.CheckBadParentheses(messages_);
}
tokens.Emit(cooked_);
if (omitNewline_) {
omitNewline_ = false;
} else {
cooked_.Put('\n', newlineProvenance);
afterIncludeDirective_ = false;
}
directiveSentinel_ = nullptr;
}

TokenSequence Prescanner::TokenizePreprocessorDirective() {
Expand Down Expand Up @@ -985,7 +995,9 @@ void Prescanner::FortranInclude(const char *firstQuote) {
provenance, static_cast<std::size_t>(p - nextLine_)};
ProvenanceRange fileRange{
allSources_.AddIncludedFile(*included, includeLineRange)};
Prescanner{*this}.set_encoding(included->encoding()).Prescan(fileRange);
Prescanner{*this, /*isNestedInIncludeDirective=*/false}
.set_encoding(included->encoding())
.Prescan(fileRange);
}
}

Expand Down
7 changes: 6 additions & 1 deletion flang/lib/Parser/prescan.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ class Prescanner {
public:
Prescanner(Messages &, CookedSource &, Preprocessor &,
common::LanguageFeatureControl);
Prescanner(const Prescanner &);
Prescanner(const Prescanner &, bool isNestedInIncludeDirective);
Prescanner(const Prescanner &) = delete;
Prescanner(Prescanner &&) = delete;

const AllSources &allSources() const { return allSources_; }
AllSources &allSources() { return allSources_; }
Expand Down Expand Up @@ -155,6 +157,7 @@ class Prescanner {
common::LanguageFeature::ClassicCComments)));
}

void CheckAndEmitLine(TokenSequence &, Provenance newlineProvenance);
void LabelField(TokenSequence &);
void EnforceStupidEndStatementRules(const TokenSequence &);
void SkipToEndOfLine();
Expand Down Expand Up @@ -198,6 +201,7 @@ class Prescanner {
Preprocessor &preprocessor_;
AllSources &allSources_;
common::LanguageFeatureControl features_;
bool isNestedInIncludeDirective_{false};
bool backslashFreeFormContinuation_{false};
bool inFixedForm_{false};
int fixedFormColumnLimit_{72};
Expand All @@ -206,6 +210,7 @@ class Prescanner {
int prescannerNesting_{0};
int continuationLines_{0};
bool isPossibleMacroCall_{false};
bool afterIncludeDirective_{false};

Provenance startProvenance_;
const char *start_{nullptr}; // beginning of current source file content
Expand Down
1 change: 1 addition & 0 deletions flang/test/Preprocessing/args.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.14159 &
6 changes: 6 additions & 0 deletions flang/test/Preprocessing/include-args.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
! RUN: %flang -E %s 2>&1 | FileCheck %s
! CHECK: call foo(3.14159)
call foo (&
#include "args.h"
)
end
Loading