diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp index 2fb3c2c3818d4..ee838b3b4a546 100644 --- a/flang/lib/Lower/Bridge.cpp +++ b/flang/lib/Lower/Bridge.cpp @@ -809,16 +809,16 @@ class FirConverter : public Fortran::lower::AbstractConverter { genLocation(const Fortran::parser::CharBlock &block) override final { if (const Fortran::parser::AllCookedSources *cooked = bridge.getCookedSource()) { - if (std::optional> - loc = cooked->GetSourcePositionRange(block)) { - // loc is a pair (begin, end); use the beginning position - Fortran::parser::SourcePosition &filePos = loc->first; - llvm::SmallString<256> filePath(*filePos.path); - llvm::sys::fs::make_absolute(filePath); - llvm::sys::path::remove_dots(filePath); - return mlir::FileLineColLoc::get(&getMLIRContext(), filePath.str(), - filePos.line, filePos.column); + if (std::optional provenance = + cooked->GetProvenanceRange(block)) { + if (std::optional filePos = + cooked->allSources().GetSourcePosition(provenance->start())) { + llvm::SmallString<256> filePath(*filePos->path); + llvm::sys::fs::make_absolute(filePath); + llvm::sys::path::remove_dots(filePath); + return mlir::FileLineColLoc::get(&getMLIRContext(), filePath.str(), + filePos->line, filePos->column); + } } } return genUnknownLocation(); diff --git a/flang/test/Lower/macro-debug-file-loc.f90 b/flang/test/Lower/macro-debug-file-loc.f90 new file mode 100644 index 0000000000000..a47c9aae17853 --- /dev/null +++ b/flang/test/Lower/macro-debug-file-loc.f90 @@ -0,0 +1,13 @@ +! Test that the expanded macros have the location information +! RUN: %flang_fc1 -mmlir --mlir-print-debuginfo -emit-fir -o - %s | FileCheck %s + +#define CMD(fname) fname() + +subroutine foo() +end subroutine + +subroutine test() + ! CHECK: fir.call @_QPfoo() fastmath : () -> () loc(#[[CALL_LOC:.*]]) + call CMD(foo) +end subroutine +! CHECK: #[[CALL_LOC]] = loc("{{.*}}macro-debug-file-loc.f90":11:3)