diff --git a/flang/lib/parser/message.h b/flang/lib/parser/message.h index 2efd6d8a67d3..370f641ef4c9 100644 --- a/flang/lib/parser/message.h +++ b/flang/lib/parser/message.h @@ -285,6 +285,7 @@ public: CharBlock at() const { return at_; } Messages *messages() const { return messages_; } + bool empty() const { return !messages_ || messages_->empty(); } // Set CharBlock for messages; restore when the returned value is deleted common::Restorer SetLocation(CharBlock at) { @@ -294,6 +295,12 @@ public: return common::ScopedSet(at_, std::move(at)); } + // Diverts messages to another buffer; restored when the returned + // value is deleted. + common::Restorer SetMessages(Messages &buffer) { + return common::ScopedSet(messages_, &buffer); + } + template Message *Say(CharBlock at, A &&... args) { if (messages_ != nullptr) { return &messages_->Say(at, std::forward(args)...); diff --git a/flang/lib/semantics/expression.cc b/flang/lib/semantics/expression.cc index aa6c1a923b1e..07ceb3288695 100644 --- a/flang/lib/semantics/expression.cc +++ b/flang/lib/semantics/expression.cc @@ -885,7 +885,7 @@ MaybeExpr ExpressionAnalyzer::Analyze(const parser::StructureComponent &sc) { "a designator"_err_en_US); } } else if (dtSpec == nullptr || dtSpec->scope() == nullptr) { - CHECK(context_.AnyFatalError()); + CHECK(context_.AnyFatalError() || !foldingContext_.messages().empty()); return std::nullopt; } else if (std::optional dataRef{ ExtractDataRef(std::move(*dtExpr))}) { @@ -1534,7 +1534,7 @@ auto ExpressionAnalyzer::AnalyzeProcedureComponentRef( } } } - CHECK(context_.messages().AnyFatalError()); + CHECK(!GetContextualMessages().empty()); return std::nullopt; } diff --git a/flang/lib/semantics/expression.h b/flang/lib/semantics/expression.h index dfbbcc7c91e9..0c95d43163ad 100644 --- a/flang/lib/semantics/expression.h +++ b/flang/lib/semantics/expression.h @@ -121,16 +121,16 @@ public: using MaybeExpr = std::optional>; explicit ExpressionAnalyzer(semantics::SemanticsContext &sc) : context_{sc} {} + ExpressionAnalyzer(semantics::SemanticsContext &sc, FoldingContext &fc) + : context_{sc}, foldingContext_{fc} {} ExpressionAnalyzer(ExpressionAnalyzer &) = default; semantics::SemanticsContext &context() const { return context_; } - FoldingContext &GetFoldingContext() const { - return context_.foldingContext(); - } + FoldingContext &GetFoldingContext() const { return foldingContext_; } parser::ContextualMessages &GetContextualMessages() { - return GetFoldingContext().messages(); + return foldingContext_.messages(); } template parser::Message *Say(A &&... args) { @@ -341,6 +341,7 @@ private: MaybeExpr MakeFunctionRef(parser::CharBlock intrinsic, ActualArguments &&); semantics::SemanticsContext &context_; + FoldingContext &foldingContext_{context_.foldingContext()}; std::map acImpliedDos_; // values are INTEGER kinds bool fatalErrors_{false}; };