From ff7d3c71262d9dd872a346df2d6656e90cbc63bc Mon Sep 17 00:00:00 2001 From: peter klausler Date: Wed, 4 Apr 2018 08:06:15 -0700 Subject: [PATCH] [flang] Do not allow "-" to denote standard input on INCLUDE/#include. Original-commit: flang-compiler/f18@f645036a16c19d5ba946bc7405197a2205dff751 Reviewed-on: https://github.com/flang-compiler/f18/pull/42 Tree-same-pre-rewrite: false --- flang/lib/parser/preprocessor.cc | 16 +++++++++------- flang/lib/parser/prescan.cc | 24 ++++++++++++------------ flang/lib/parser/prescan.h | 2 +- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/flang/lib/parser/preprocessor.cc b/flang/lib/parser/preprocessor.cc index 07b23e8e5abe..f1ef8dd5a149 100644 --- a/flang/lib/parser/preprocessor.cc +++ b/flang/lib/parser/preprocessor.cc @@ -569,20 +569,22 @@ void Preprocessor::Directive(const TokenSequence &dir, Prescanner *prescanner) { dir.GetTokenProvenance(dirOffset)); return; } + if (include == "-") { + prescanner->Say("#include: can't include standard input"_err_en_US, + dir.GetTokenProvenance(dirOffset)); + return; + } std::stringstream error; const SourceFile *included{allSources_.Open(include, &error)}; if (included == nullptr) { prescanner->Say( MessageFormattedText("#include: %s"_err_en_US, error.str().data()), dir.GetTokenProvenance(dirOffset)); - return; + } else if (included->bytes() > 0) { + ProvenanceRange fileRange{ + allSources_.AddIncludedFile(*included, dir.GetProvenanceRange())}; + Prescanner{*prescanner}.Prescan(fileRange); } - if (included->bytes() == 0) { - return; - } - ProvenanceRange fileRange{ - allSources_.AddIncludedFile(*included, dir.GetProvenanceRange())}; - Prescanner{*prescanner}.Prescan(fileRange); } else { prescanner->Say(MessageFormattedText( "#%s: unknown or unimplemented directive"_err_en_US, diff --git a/flang/lib/parser/prescan.cc b/flang/lib/parser/prescan.cc index 429de34ed34c..b4633ea5b3c1 100644 --- a/flang/lib/parser/prescan.cc +++ b/flang/lib/parser/prescan.cc @@ -540,7 +540,7 @@ std::optional Prescanner::IsIncludeLine(const char *start) const { return {}; } -bool Prescanner::FortranInclude(const char *firstQuote) { +void Prescanner::FortranInclude(const char *firstQuote) { const char *p{firstQuote}; while (*p != '"' && *p != '\'') { ++p; @@ -558,13 +558,17 @@ bool Prescanner::FortranInclude(const char *firstQuote) { } if (*p != quote) { Say("malformed path name string"_err_en_US, GetProvenance(p)); - return true; + return; } for (++p; *p == ' ' || *p == '\t'; ++p) { } if (*p != '\n' && *p != '!') { Say("excess characters after path name"_en_US, GetProvenance(p)); } + if (path == "-") { + Say("cannot INCLUDE standard input"_err_en_US, GetProvenance(p)); + return; + } std::stringstream error; Provenance provenance{GetProvenance(lineStart_)}; AllSources &allSources{cooked_.allSources()}; @@ -579,17 +583,13 @@ bool Prescanner::FortranInclude(const char *firstQuote) { if (included == nullptr) { Say(MessageFormattedText("INCLUDE: %s"_err_en_US, error.str().data()), provenance); - return true; + } else if (included->bytes() > 0) { + ProvenanceRange includeLineRange{ + provenance, static_cast(p - lineStart_)}; + ProvenanceRange fileRange{ + allSources.AddIncludedFile(*included, includeLineRange)}; + Prescanner{*this}.Prescan(fileRange); } - if (included->bytes() == 0) { - return true; - } - ProvenanceRange includeLineRange{ - provenance, static_cast(p - lineStart_)}; - ProvenanceRange fileRange{ - allSources.AddIncludedFile(*included, includeLineRange)}; - Prescanner{*this}.Prescan(fileRange); - return true; } bool Prescanner::IsPreprocessorDirectiveLine(const char *start) const { diff --git a/flang/lib/parser/prescan.h b/flang/lib/parser/prescan.h index 9dc14dd70a3a..81953c4be1ec 100644 --- a/flang/lib/parser/prescan.h +++ b/flang/lib/parser/prescan.h @@ -132,7 +132,7 @@ private: bool IsFixedFormCommentLine(const char *) const; bool IsFreeFormComment(const char *) const; std::optional IsIncludeLine(const char *) const; - bool FortranInclude(const char *quote); + void FortranInclude(const char *quote); bool IsPreprocessorDirectiveLine(const char *) const; const char *FixedFormContinuationLine(); bool FixedFormContinuation();