diff --git a/flang/lib/Parser/provenance.cpp b/flang/lib/Parser/provenance.cpp index 27cc1ebf84f8..e2364d3f8a56 100644 --- a/flang/lib/Parser/provenance.cpp +++ b/flang/lib/Parser/provenance.cpp @@ -435,11 +435,15 @@ std::optional CookedSource::GetProvenanceRange( return std::nullopt; } ProvenanceRange first{provenanceMap_.Map(cookedRange.begin() - &data_[0])}; - if (cookedRange.size() <= first.size()) { + if (cookedRange.size() <= first.size()) { // always true when empty return first.Prefix(cookedRange.size()); } - ProvenanceRange last{provenanceMap_.Map(cookedRange.end() - &data_[0])}; - return {ProvenanceRange{first.start(), last.start() - first.start()}}; + ProvenanceRange last{provenanceMap_.Map(cookedRange.end() - 1 - &data_[0])}; + if (first.start() <= last.start()) { + return {ProvenanceRange{first.start(), last.start() - first.start() + 1}}; + } else { + return std::nullopt; + } } std::optional CookedSource::GetCharBlock(