@@ -939,24 +939,59 @@ class FirConverter : public Fortran::lower::AbstractConverter {
939939 return mlir::UnknownLoc::get (&getMLIRContext ());
940940 }
941941
942+ static mlir::Location genLocation (Fortran::parser::SourcePosition pos,
943+ mlir::MLIRContext &ctx) {
944+ llvm::SmallString<256 > path (*pos.path );
945+ llvm::sys::fs::make_absolute (path);
946+ llvm::sys::path::remove_dots (path);
947+ return mlir::FileLineColLoc::get (&ctx, path.str (), pos.line , pos.column );
948+ }
949+
942950 // / Generate a `Location` from the `CharBlock`.
943951 mlir::Location
944952 genLocation (const Fortran::parser::CharBlock &block) override final {
953+ mlir::Location mainLocation = genUnknownLocation ();
945954 if (const Fortran::parser::AllCookedSources *cooked =
946955 bridge.getCookedSource ()) {
947956 if (std::optional<Fortran::parser::ProvenanceRange> provenance =
948957 cooked->GetProvenanceRange (block)) {
949958 if (std::optional<Fortran::parser::SourcePosition> filePos =
950- cooked->allSources ().GetSourcePosition (provenance->start ())) {
951- llvm::SmallString<256 > filePath (*filePos->path );
952- llvm::sys::fs::make_absolute (filePath);
953- llvm::sys::path::remove_dots (filePath);
954- return mlir::FileLineColLoc::get (&getMLIRContext (), filePath.str (),
955- filePos->line , filePos->column );
959+ cooked->allSources ().GetSourcePosition (provenance->start ()))
960+ mainLocation = genLocation (*filePos, getMLIRContext ());
961+
962+ llvm::SmallVector<mlir::Location> locs;
963+ locs.push_back (mainLocation);
964+
965+ llvm::SmallVector<fir::LocationKindAttr> locAttrs;
966+ locAttrs.push_back (fir::LocationKindAttr::get (&getMLIRContext (),
967+ fir::LocationKind::Base));
968+
969+ // Gather include location information if any.
970+ Fortran::parser::ProvenanceRange *prov = &*provenance;
971+ while (prov) {
972+ if (std::optional<Fortran::parser::ProvenanceRange> include =
973+ cooked->allSources ().GetInclusionInfo (*prov)) {
974+ if (std::optional<Fortran::parser::SourcePosition> incPos =
975+ cooked->allSources ().GetSourcePosition (include->start ())) {
976+ locs.push_back (genLocation (*incPos, getMLIRContext ()));
977+ locAttrs.push_back (fir::LocationKindAttr::get (
978+ &getMLIRContext (), fir::LocationKind::Inclusion));
979+ }
980+ prov = &*include;
981+ } else {
982+ prov = nullptr ;
983+ }
984+ }
985+ if (locs.size () > 1 ) {
986+ assert (locs.size () == locAttrs.size () &&
987+ " expect as many attributes as locations" );
988+ return mlir::FusedLocWith<fir::LocationKindArrayAttr>::get (
989+ &getMLIRContext (), locs,
990+ fir::LocationKindArrayAttr::get (&getMLIRContext (), locAttrs));
956991 }
957992 }
958993 }
959- return genUnknownLocation () ;
994+ return mainLocation ;
960995 }
961996
962997 const Fortran::semantics::Scope &getCurrentScope () override final {
0 commit comments