Skip to content
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
33 changes: 15 additions & 18 deletions flang/lib/Parser/prescan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,7 @@ void Prescanner::Prescan(ProvenanceRange range) {
while (!IsAtEnd()) {
Statement();
}
if (inFixedForm_ != beganInFixedForm) {
std::string dir{"!dir$ "};
if (beganInFixedForm) {
dir += "fixed";
} else {
dir += "free";
}
dir += '\n';
TokenSequence tokens{dir, allSources_.AddCompilerInsertion(dir).start()};
tokens.Emit(cooked_);
}
inFixedForm_ = beganInFixedForm;
}

void Prescanner::Statement() {
Expand Down Expand Up @@ -324,10 +314,11 @@ void Prescanner::Statement() {
}
NormalizeCompilerDirectiveCommentMarker(*preprocessed);
preprocessed->ToLowerCase();
SourceFormChange(preprocessed->ToString());
CheckAndEmitLine(
preprocessed->ClipComment(*this, true /* skip first ! */),
newlineProvenance);
if (!SourceFormChange(preprocessed->ToString())) {
CheckAndEmitLine(
preprocessed->ClipComment(*this, true /* skip first ! */),
newlineProvenance);
}
break;
case LineClassification::Kind::Source:
if (inFixedForm_) {
Expand Down Expand Up @@ -370,14 +361,16 @@ void Prescanner::Statement() {
}
}
tokens.ToLowerCase();
SourceFormChange(tokens.ToString());
if (!SourceFormChange(tokens.ToString())) {
CheckAndEmitLine(tokens, newlineProvenance);
}
} else { // Kind::Source
tokens.ToLowerCase();
if (inFixedForm_) {
EnforceStupidEndStatementRules(tokens);
}
CheckAndEmitLine(tokens, newlineProvenance);
}
CheckAndEmitLine(tokens, newlineProvenance);
}
directiveSentinel_ = nullptr;
}
Expand Down Expand Up @@ -1774,11 +1767,15 @@ Prescanner::LineClassification Prescanner::ClassifyLine(
return classification;
}

void Prescanner::SourceFormChange(std::string &&dir) {
bool Prescanner::SourceFormChange(std::string &&dir) {
if (dir == "!dir$ free") {
inFixedForm_ = false;
return true;
} else if (dir == "!dir$ fixed") {
inFixedForm_ = true;
return true;
} else {
return false;
}
}

Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Parser/prescan.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ class Prescanner {
LineClassification ClassifyLine(const char *) const;
LineClassification ClassifyLine(
TokenSequence &, Provenance newlineProvenance) const;
void SourceFormChange(std::string &&);
bool SourceFormChange(std::string &&);
bool CompilerDirectiveContinuation(TokenSequence &, const char *sentinel);
bool SourceLineContinuation(TokenSequence &);

Expand Down
8 changes: 8 additions & 0 deletions flang/test/Preprocessing/fixed-free.f
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
!RUN: %flang -E %s 2>&1 | FileCheck %s
!RUN: %flang -fc1 -fsyntax-only %s 2>&1 | FileCheck --allow-empty %s
!CHECK-NOT: dir$
!CHECK-NOT: error:
!dir$ fixed
continue
!dir$ free
end