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

via flang-commits flang-commits at lists.llvm.org
Tue Sep 26 08:44:31 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-fir-hlfir

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/67446.diff


2 Files Affected:

- (modified) flang/lib/Lower/Bridge.cpp (+10-10) 
- (added) flang/test/Lower/macro-debug-file-loc.f90 (+13) 


``````````diff
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)

``````````

</details>


https://github.com/llvm/llvm-project/pull/67446


More information about the flang-commits mailing list