Skip to content

Commit 43d2ef2

Browse files
authored
[flang][lowering] propagate location info of macro expansions (#67446)
Currently flang-new -g is failing when compiling code containing a call in a macro to a function defined in the same file. The verification added in https://reviews.llvm.org/D157447 is valid, flang lowering was failing to propagate location information in code from macro expansion because GetSourcePositionRange does not work with them (it fails to come with an end location), but we do not need a range for the MLIR location, only the start. Use GetSourcePosition instead that works with code from macro expansion. Note that the source location is the one of the statement where the macro appeared, if needed some FusedLocation could be later built to keep a link to the macro location in the debug info.
1 parent ebfea26 commit 43d2ef2

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

flang/lib/Lower/Bridge.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -809,16 +809,16 @@ class FirConverter : public Fortran::lower::AbstractConverter {
809809
genLocation(const Fortran::parser::CharBlock &block) override final {
810810
if (const Fortran::parser::AllCookedSources *cooked =
811811
bridge.getCookedSource()) {
812-
if (std::optional<std::pair<Fortran::parser::SourcePosition,
813-
Fortran::parser::SourcePosition>>
814-
loc = cooked->GetSourcePositionRange(block)) {
815-
// loc is a pair (begin, end); use the beginning position
816-
Fortran::parser::SourcePosition &filePos = loc->first;
817-
llvm::SmallString<256> filePath(*filePos.path);
818-
llvm::sys::fs::make_absolute(filePath);
819-
llvm::sys::path::remove_dots(filePath);
820-
return mlir::FileLineColLoc::get(&getMLIRContext(), filePath.str(),
821-
filePos.line, filePos.column);
812+
if (std::optional<Fortran::parser::ProvenanceRange> provenance =
813+
cooked->GetProvenanceRange(block)) {
814+
if (std::optional<Fortran::parser::SourcePosition> filePos =
815+
cooked->allSources().GetSourcePosition(provenance->start())) {
816+
llvm::SmallString<256> filePath(*filePos->path);
817+
llvm::sys::fs::make_absolute(filePath);
818+
llvm::sys::path::remove_dots(filePath);
819+
return mlir::FileLineColLoc::get(&getMLIRContext(), filePath.str(),
820+
filePos->line, filePos->column);
821+
}
822822
}
823823
}
824824
return genUnknownLocation();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
! Test that the expanded macros have the location information
2+
! RUN: %flang_fc1 -mmlir --mlir-print-debuginfo -emit-fir -o - %s | FileCheck %s
3+
4+
#define CMD(fname) fname()
5+
6+
subroutine foo()
7+
end subroutine
8+
9+
subroutine test()
10+
! CHECK: fir.call @_QPfoo() fastmath<contract> : () -> () loc(#[[CALL_LOC:.*]])
11+
call CMD(foo)
12+
end subroutine
13+
! CHECK: #[[CALL_LOC]] = loc("{{.*}}macro-debug-file-loc.f90":11:3)

0 commit comments

Comments
 (0)