diff --git a/flang/lib/parser/debug-parser.h b/flang/lib/parser/debug-parser.h index e30ddeb1e7f9..76daaf657a62 100644 --- a/flang/lib/parser/debug-parser.h +++ b/flang/lib/parser/debug-parser.h @@ -23,7 +23,8 @@ public: if (auto context = state->context()) { std::cout << *context; } - state->GetAllSources().Identify(std::cout, state->GetProvenance(), ""); + state->cooked().allSources().Identify( + std::cout, state->GetProvenance(), ""); std::cout << " parser debug: " << std::string{str_, length_} << '\n'; return {Success{}}; } diff --git a/flang/lib/parser/message.cc b/flang/lib/parser/message.cc index 3b69ffc266c3..c8a2aa0082e3 100644 --- a/flang/lib/parser/message.cc +++ b/flang/lib/parser/message.cc @@ -11,9 +11,9 @@ void Message::Emit(std::ostream &o, const AllSources &sources) const { o << " " << message_ << '\n'; } -void Messages::Emit(std::ostream &o, const AllSources &sources) const { +void Messages::Emit(std::ostream &o) const { for (const auto &msg : messages_) { - msg.Emit(o, sources); + msg.Emit(o, allSources_); } } } // namespace parser diff --git a/flang/lib/parser/message.h b/flang/lib/parser/message.h index 4f2fac953c61..b3a3f89f320e 100644 --- a/flang/lib/parser/message.h +++ b/flang/lib/parser/message.h @@ -57,9 +57,10 @@ public: using iterator = list_type::iterator; using const_iterator = list_type::const_iterator; - Messages() {} + explicit Messages(const AllSources &sources) : allSources_{sources} {} Messages(Messages &&that) - : messages_{std::move(that.messages_)}, last_{that.last_} {} + : allSources_{that.allSources_}, messages_{std::move(that.messages_)}, + last_{that.last_} {} Messages &operator=(Messages &&that) { swap(that); return *this; @@ -77,7 +78,10 @@ public: const_iterator cbegin() const { return messages_.cbegin(); } const_iterator cend() const { return messages_.cend(); } + const AllSources &allSources() const { return allSources_; } + void Put(Message &&m) { + CHECK(m.provenance() < allSources_.size()); if (messages_.empty()) { messages_.emplace_front(std::move(m)); last_ = messages_.begin(); @@ -97,9 +101,10 @@ public: } } - void Emit(std::ostream &, const AllSources &) const; + void Emit(std::ostream &) const; private: + const AllSources &allSources_; list_type messages_; iterator last_; // valid iff messages_ nonempty }; diff --git a/flang/lib/parser/parse-state.h b/flang/lib/parser/parse-state.h index f346c9f20d8f..7840822aa22d 100644 --- a/flang/lib/parser/parse-state.h +++ b/flang/lib/parser/parse-state.h @@ -24,10 +24,11 @@ class UserState; class ParseState { public: ParseState(const CookedSource &cooked) - : cooked_{cooked}, p_{&cooked[0]}, remaining_{cooked.size()} {} + : cooked_{cooked}, p_{&cooked[0]}, + remaining_{cooked.size()}, messages_{*cooked.allSources()} {} ParseState(const ParseState &that) - : cooked_{that.cooked_}, p_{that.p_}, - remaining_{that.remaining_}, column_{that.column_}, + : cooked_{that.cooked_}, p_{that.p_}, remaining_{that.remaining_}, + column_{that.column_}, messages_{*that.cooked_.allSources()}, userState_{that.userState_}, inCharLiteral_{that.inCharLiteral_}, inFortran_{that.inFortran_}, inFixedForm_{that.inFixedForm_}, enableOldDebugLines_{that.enableOldDebugLines_}, columns_{that.columns_}, @@ -69,15 +70,16 @@ public: std::memcpy(&that, buffer, bytes); } + const CookedSource &cooked() const { return cooked_; } + int column() const { return column_; } + Messages *messages() { return &messages_; } + bool anyErrorRecovery() const { return anyErrorRecovery_; } void set_anyErrorRecovery() { anyErrorRecovery_ = true; } UserState *userState() const { return userState_; } void set_userState(UserState *u) { userState_ = u; } - int column() const { return column_; } - Messages *messages() { return &messages_; } - MessageContext context() const { return context_; } MessageContext set_context(MessageContext c) { MessageContext was{context_}; @@ -157,7 +159,6 @@ public: bool tabInCurrentLine() const { return tabInCurrentLine_; } - const AllSources &GetAllSources() const { return cooked_.sources(); } const char *GetLocation() const { return p_; } Provenance GetProvenance(const char *at) const { return cooked_.GetProvenance(at).start; diff --git a/flang/lib/parser/preprocessor.cc b/flang/lib/parser/preprocessor.cc index ead236588a43..7f4132a072ca 100644 --- a/flang/lib/parser/preprocessor.cc +++ b/flang/lib/parser/preprocessor.cc @@ -328,12 +328,16 @@ bool Preprocessor::MacroReplacement( if (def.isPredefined()) { std::string name{def.replacement()[0].ToString()}; if (name == "__FILE__") { - result->Put("\""s + prescanner_.GetCurrentPath() + '"'); + result->Put("\""s + + prescanner_.allSources().GetPath( + prescanner_.GetCurrentProvenance()) + + '"'); continue; } if (name == "__LINE__") { std::stringstream ss; - ss << prescanner_.GetCurrentLineNumber(); + ss << prescanner_.allSources().GetLineNumber( + prescanner_.GetCurrentProvenance()); result->Put(ss.str()); continue; } diff --git a/flang/lib/parser/prescan.cc b/flang/lib/parser/prescan.cc index 65df502cbc8e..518471a6a34d 100644 --- a/flang/lib/parser/prescan.cc +++ b/flang/lib/parser/prescan.cc @@ -9,13 +9,14 @@ namespace Fortran { namespace parser { -CookedSource Prescanner::Prescan() { +CookedSource Prescanner::Prescan(AllSources *allSources) { startProvenance_ = 0; - start_ = &allSources_[0]; - limit_ = start_ + allSources_.size(); + start_ = &(*allSources)[0]; + limit_ = start_ + allSources->size(); lineStart_ = start_; + BeginSourceLine(start_); TokenSequence tokens, preprocessed; - CookedSource cooked{allSources_}; + CookedSource cooked{allSources}; while (lineStart_ < limit_) { if (CommentLinesAndPreprocessorDirectives() && lineStart_ >= limit_) { break; @@ -67,14 +68,6 @@ std::optional Prescanner::NextTokenizedLine() { return {std::move(tokens)}; } -std::string Prescanner::GetCurrentPath() const { - return allSources_.GetPath(GetCurrentProvenance()); -} - -int Prescanner::GetCurrentLineNumber() const { - return allSources_.GetLineNumber(GetCurrentProvenance()); -} - void Prescanner::NextLine() { void *vstart{static_cast(const_cast(lineStart_))}; void *v{std::memchr(vstart, '\n', limit_ - lineStart_)}; diff --git a/flang/lib/parser/prescan.h b/flang/lib/parser/prescan.h index bb5e41d04149..819e39476368 100644 --- a/flang/lib/parser/prescan.h +++ b/flang/lib/parser/prescan.h @@ -20,8 +20,8 @@ namespace parser { class Prescanner { public: - Prescanner(Messages &messages, AllSources &allSources) - : messages_{messages}, allSources_{allSources}, preprocessor_{*this} {} + explicit Prescanner(Messages &messages) + : messages_{messages}, preprocessor_{*this} {} Messages &messages() const { return messages_; } bool anyFatalErrors() const { return anyFatalErrors_; } @@ -43,7 +43,9 @@ public: return *this; } - CookedSource Prescan(); + const AllSources &allSources() const { return messages_.allSources(); } + + CookedSource Prescan(AllSources *); std::optional NextTokenizedLine(); Provenance GetCurrentProvenance() const { return GetProvenance(at_); } std::string GetCurrentPath() const; // __FILE__ @@ -96,7 +98,6 @@ private: void PayNewlineDebt(CookedSource *); Messages &messages_; - AllSources &allSources_; Provenance startProvenance_; const char *start_{nullptr}; // beginning of sourceFile_ content diff --git a/flang/lib/parser/provenance.cc b/flang/lib/parser/provenance.cc index 9437427aa8f7..26af66337c30 100644 --- a/flang/lib/parser/provenance.cc +++ b/flang/lib/parser/provenance.cc @@ -199,7 +199,7 @@ ProvenanceRange CookedSource::GetProvenance(const char *at) const { void CookedSource::Marshal() { CHECK(provenanceMap_.size() == buffer_.size()); - provenanceMap_.Put(sources_.AddCompilerInsertion("EOF")); + provenanceMap_.Put(allSources_->AddCompilerInsertion("EOF")); data_.resize(buffer_.size()); char *p{&data_[0]}; for (char ch : buffer_) { diff --git a/flang/lib/parser/provenance.h b/flang/lib/parser/provenance.h index b07b551a9cb7..b70e23af3902 100644 --- a/flang/lib/parser/provenance.h +++ b/flang/lib/parser/provenance.h @@ -109,13 +109,13 @@ private: class CookedSource { public: - explicit CookedSource(AllSources &sources) : sources_{sources} {} + explicit CookedSource(AllSources *sources) : allSources_{sources} {} size_t size() const { return data_.size(); } const char &operator[](size_t n) const { return data_[n]; } const char &at(size_t n) const { return data_.at(n); } - AllSources &sources() const { return sources_; } + AllSources *allSources() const { return allSources_; } ProvenanceRange GetProvenance(const char *) const; void Identify(std::ostream &, const char *) const; @@ -130,7 +130,7 @@ public: void Marshal(); // marshalls all text into one contiguous block private: - AllSources &sources_; + AllSources *allSources_; CharBuffer buffer_; // before Marshal() std::vector data_; // all of it, prescanned and preprocessed OffsetToProvenanceMappings provenanceMap_; diff --git a/flang/tools/f18/f18.cc b/flang/tools/f18/f18.cc index c549abcc2b54..d71ae1aaae89 100644 --- a/flang/tools/f18/f18.cc +++ b/flang/tools/f18/f18.cc @@ -105,15 +105,15 @@ int main(int argc, char *const argv[]) { } Fortran::parser::AllSources allSources{sourceFile}; - Fortran::parser::Messages messages; - Fortran::parser::Prescanner prescanner{messages, allSources}; + Fortran::parser::Messages messages{allSources}; + Fortran::parser::Prescanner prescanner{messages}; Fortran::parser::CookedSource cooked{ prescanner.set_fixedForm(fixedForm) .set_enableBackslashEscapesInCharLiterals(backslashEscapes) .set_fixedFormColumnLimit(columns) .set_enableOldDebugLines(enableOldDebugLines) - .Prescan()}; - messages.Emit(std::cerr, allSources); + .Prescan(&allSources)}; + messages.Emit(std::cerr); if (prescanner.anyFatalErrors()) { return 1; } @@ -157,7 +157,7 @@ int main(int argc, char *const argv[]) { std::cerr << "final position: "; allSources.Identify(std::cerr, state.GetProvenance(), " "); } - state.messages()->Emit(std::cerr, allSources); + state.messages()->Emit(std::cerr); return EXIT_FAILURE; } }