[flang-commits] [flang] 43d2ef2 - [flang][lowering] propagate location info of macro expansions (#67446)

via flang-commits flang-commits at lists.llvm.org
Tue Sep 26 11:33:06 PDT 2023


Author: jeanPerier
Date: 2023-09-26T20:33:01+02:00
New Revision: 43d2ef2856fc3373068c020efa11a933477e11fa

URL: https://github.com/llvm/llvm-project/commit/43d2ef2856fc3373068c020efa11a933477e11fa
DIFF: https://github.com/llvm/llvm-project/commit/43d2ef2856fc3373068c020efa11a933477e11fa.diff

LOG: [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.

Added: 
    flang/test/Lower/macro-debug-file-loc.f90

Modified: 
    flang/lib/Lower/Bridge.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index 2fb3c2c3818d42d..ee838b3b4a546b9 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<std::pair<Fortran::parser::SourcePosition,
-                                  Fortran::parser::SourcePosition>>
-              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<Fortran::parser::ProvenanceRange> provenance =
+              cooked->GetProvenanceRange(block)) {
+        if (std::optional<Fortran::parser::SourcePosition> 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 000000000000000..a47c9aae178530e
--- /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<contract> : () -> () loc(#[[CALL_LOC:.*]])
+  call CMD(foo)
+end subroutine
+! CHECK: #[[CALL_LOC]] = loc("{{.*}}macro-debug-file-loc.f90":11:3)


        


More information about the flang-commits mailing list