[flang-commits] [flang] [flang] Remove directory path from compiler remarks (PR #212669)

John Otken via flang-commits flang-commits at lists.llvm.org
Wed Aug 12 09:59:46 PDT 2026


https://github.com/jotken updated https://github.com/llvm/llvm-project/pull/212669

>From 92d8f11341c63cbe464c81e6aaa1ff83aacdd728 Mon Sep 17 00:00:00 2001
From: John Otken <john at otken.com>
Date: Tue, 28 Jul 2026 21:17:23 -0500
Subject: [PATCH 1/4] [flang] Remove directory path from compiler remarks

flang prefixes optimization remarks with the full path to the sourcefile.  This is not handled by some test frameworks that compare compiler output to a reference.  Change remarks to only display the source filename.

$ flang -O3 -Rpass=loop-vectorize constr26.f90
home/users/john/test/constr26.f90:6:3: remark: vectorized loop (vectorization width: 4, interleaved count: 1) [-Rpass=loop-vectorize]

after this change:

$ flang -O3 -Rpass=loop-vectorize constr26.f90
constr26.f90:6:3: remark: vectorized loop (vectorization width: 4, interleaved count: 1) [-Rpass=loop-vectorize]

gnu fortran output:

$ gfortran -march=native -fopt-info-vec -O3 constr26.f90
constr26.f90:6:0: note: loop vectorized

Co-authored-by: John Otken john.otken at hpe.com
Assisted-by: Copilot and Claude Opus 4.8.
---
 flang/lib/Frontend/TextDiagnosticPrinter.cpp | 18 ++++++++----------
 flang/test/Driver/optimization-remark.f90    |  7 +++++++
 2 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/flang/lib/Frontend/TextDiagnosticPrinter.cpp b/flang/lib/Frontend/TextDiagnosticPrinter.cpp
index 33d54d9ad0b9c..bcc5635c56f3f 100644
--- a/flang/lib/Frontend/TextDiagnosticPrinter.cpp
+++ b/flang/lib/Frontend/TextDiagnosticPrinter.cpp
@@ -55,10 +55,10 @@ static void printRemarkOption(llvm::raw_ostream &os,
 
 // For remarks only, if we are receiving a message of this format
 // [file location with line and column];;[path to file];;[the remark message]
-// then print the absolute file path, line and column number.
+// then print the base file name, line and column number.
 void TextDiagnosticPrinter::printLocForRemarks(
     llvm::raw_svector_ostream &diagMessageStream, llvm::StringRef &diagMsg) {
-  // split incoming string to get the absolute path and filename in the
+  // split incoming string to get the location and filename in the
   // case we are receiving optimization remarks from BackendRemarkConsumer
   diagMsg = diagMessageStream.str();
   llvm::StringRef delimiter = ";;";
@@ -73,19 +73,17 @@ void TextDiagnosticPrinter::printLocForRemarks(
   // tokens will always be of size 2 in the case of optimization
   // remark message received
   if (tokens.size() == 2) {
-    // Extract absolute path
-    llvm::SmallString<128> absPath = llvm::sys::path::relative_path(tokens[1]);
-    llvm::sys::path::remove_filename(absPath);
-    // Add the last separator before the file name
-    llvm::sys::path::append(absPath, llvm::sys::path::get_separator());
-    llvm::sys::path::make_preferred(absPath);
+    // tokens[0] is of the form [file path]:[line]:[column]. Strip any leading
+    // directory so that only the base file name is shown, matching how errors
+    // and warnings are displayed.
+    llvm::StringRef loc = llvm::sys::path::filename(tokens[0]);
 
     // Used for changing only the bold attribute
     if (diagOpts.showColors(os.has_colors()))
       os.changeColor(llvm::raw_ostream::SAVEDCOLOR, true);
 
-    // Print path, file name, line and column
-    os << absPath << tokens[0] << ": ";
+    // Print base file name, line and column
+    os << loc << ": ";
   }
 }
 
diff --git a/flang/test/Driver/optimization-remark.f90 b/flang/test/Driver/optimization-remark.f90
index 76b794720e593..743287a34cff2 100644
--- a/flang/test/Driver/optimization-remark.f90
+++ b/flang/test/Driver/optimization-remark.f90
@@ -82,3 +82,10 @@ subroutine swap_real(a1, a2)
     end do
 
 end subroutine swap_real
+
+! Check that only the base file name (not the full path passed on the command
+! line) is displayed in remarks, matching how errors and warnings are shown.
+! The RUN and CHECK lines are placed at the end of the file so that adding them
+! does not shift the source line numbers referenced by the checks above.
+! RUN: %flang %s -O2 -Rpass -S %{output} 2>&1 | FileCheck %s --check-prefix=BASENAME
+! BASENAME: {{^}}optimization-remark.f90:{{[0-9]+}}:{{[0-9]+}}: remark:

>From cf5d31a25031297f4053457c9fb754e47ce1d742 Mon Sep 17 00:00:00 2001
From: John Otken <john at otken.com>
Date: Mon, 3 Aug 2026 17:54:54 -0500
Subject: [PATCH 2/4] Handle relative paths in remarks the same as clang (e.g.,
 ../vec.f90 or foo/bar/vec.f90).

---
 flang/lib/Frontend/TextDiagnosticPrinter.cpp | 19 +++++++++++--------
 flang/lib/Lower/Bridge.cpp                   |  4 +++-
 flang/test/Driver/optimization-remark.f90    | 17 +++++++++++++----
 3 files changed, 27 insertions(+), 13 deletions(-)

diff --git a/flang/lib/Frontend/TextDiagnosticPrinter.cpp b/flang/lib/Frontend/TextDiagnosticPrinter.cpp
index bcc5635c56f3f..9b5f939611b25 100644
--- a/flang/lib/Frontend/TextDiagnosticPrinter.cpp
+++ b/flang/lib/Frontend/TextDiagnosticPrinter.cpp
@@ -21,7 +21,6 @@
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/ErrorHandling.h"
-#include "llvm/Support/Path.h"
 #include "llvm/Support/raw_ostream.h"
 
 using namespace Fortran::frontend;
@@ -55,7 +54,8 @@ static void printRemarkOption(llvm::raw_ostream &os,
 
 // For remarks only, if we are receiving a message of this format
 // [file location with line and column];;[path to file];;[the remark message]
-// then print the base file name, line and column number.
+// then print the file path (as given on the command line), line and column
+// number.
 void TextDiagnosticPrinter::printLocForRemarks(
     llvm::raw_svector_ostream &diagMessageStream, llvm::StringRef &diagMsg) {
   // split incoming string to get the location and filename in the
@@ -73,17 +73,20 @@ void TextDiagnosticPrinter::printLocForRemarks(
   // tokens will always be of size 2 in the case of optimization
   // remark message received
   if (tokens.size() == 2) {
-    // tokens[0] is of the form [file path]:[line]:[column]. Strip any leading
-    // directory so that only the base file name is shown, matching how errors
-    // and warnings are displayed.
-    llvm::StringRef loc = llvm::sys::path::filename(tokens[0]);
+    // tokens[0] has the form [base file name]:[line]:[column] and tokens[1] is
+    // the full path to the file. Display the full path (as presented on the
+    // command line) with the line and column, matching Clang: relative paths
+    // (e.g. ../vec.f90, dir/vec.f90) stay relative, and absolute paths are
+    // shown in full. The ":[line]:[column]" suffix is taken from tokens[0].
+    llvm::StringRef path = tokens[1];
+    llvm::StringRef lineAndColumn = tokens[0].substr(tokens[0].find(':'));
 
     // Used for changing only the bold attribute
     if (diagOpts.showColors(os.has_colors()))
       os.changeColor(llvm::raw_ostream::SAVEDCOLOR, true);
 
-    // Print base file name, line and column
-    os << loc << ": ";
+    // Print file path, line and column
+    os << path << lineAndColumn << ": ";
   }
 }
 
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index ed8b256f47fd4..cb21ef0d35be1 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -1141,7 +1141,9 @@ class FirConverter : public Fortran::lower::AbstractConverter {
   static mlir::Location genLocation(Fortran::parser::SourcePosition pos,
                                     mlir::MLIRContext &ctx) {
     llvm::SmallString<256> path(*pos.path);
-    llvm::sys::fs::make_absolute(path);
+    // Preserve the file path as it was presented on the command line (matching
+    // Clang): relative paths stay relative and absolute paths stay absolute.
+    // Only normalize '.' components; do not force the path to be absolute.
     llvm::sys::path::remove_dots(path);
     return mlir::FileLineColLoc::get(&ctx, path.str(), pos.line, pos.column);
   }
diff --git a/flang/test/Driver/optimization-remark.f90 b/flang/test/Driver/optimization-remark.f90
index 743287a34cff2..8f17b19971e8f 100644
--- a/flang/test/Driver/optimization-remark.f90
+++ b/flang/test/Driver/optimization-remark.f90
@@ -83,9 +83,18 @@ subroutine swap_real(a1, a2)
 
 end subroutine swap_real
 
-! Check that only the base file name (not the full path passed on the command
-! line) is displayed in remarks, matching how errors and warnings are shown.
+! Check that the file path is displayed as it was presented on the command
+! line, matching how Clang prints optimization remarks: an absolute path is
+! shown in full and relative paths are kept relative.
 ! The RUN and CHECK lines are placed at the end of the file so that adding them
 ! does not shift the source line numbers referenced by the checks above.
-! RUN: %flang %s -O2 -Rpass -S %{output} 2>&1 | FileCheck %s --check-prefix=BASENAME
-! BASENAME: {{^}}optimization-remark.f90:{{[0-9]+}}:{{[0-9]+}}: remark:
+
+! Absolute path: %s is an absolute path, so the full path is displayed.
+! RUN: %flang %s -O2 -Rpass -S %{output} 2>&1 | FileCheck %s -DFILE=%s --check-prefix=ABSPATH
+! ABSPATH: {{^}}[[FILE]]:{{[0-9]+}}:{{[0-9]+}}: remark:
+
+! Relative path in a subdirectory: the relative path is displayed as given.
+! RUN: rm -rf %t && mkdir -p %t/sub
+! RUN: cp %s %t/sub/vec.f90
+! RUN: cd %t && %flang sub/vec.f90 -O2 -Rpass -S -o %t/vec.s 2>&1 | FileCheck %s --check-prefix=SUBDIR
+! SUBDIR: {{^}}sub/vec.f90:{{[0-9]+}}:{{[0-9]+}}: remark:

>From 09936ee2780608d157798d373cefc42c08a8058f Mon Sep 17 00:00:00 2001
From: John Otken <john.otken at gmail.com>
Date: Wed, 5 Aug 2026 16:45:23 -0500
Subject: [PATCH 3/4] Update flang/test/Driver/optimization-remark.f90

Co-authored-by: Tarun Prabhu <tarunprabhu at gmail.com>
---
 flang/test/Driver/optimization-remark.f90 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flang/test/Driver/optimization-remark.f90 b/flang/test/Driver/optimization-remark.f90
index 8f17b19971e8f..9ddc6590d441e 100644
--- a/flang/test/Driver/optimization-remark.f90
+++ b/flang/test/Driver/optimization-remark.f90
@@ -96,5 +96,5 @@ end subroutine swap_real
 ! Relative path in a subdirectory: the relative path is displayed as given.
 ! RUN: rm -rf %t && mkdir -p %t/sub
 ! RUN: cp %s %t/sub/vec.f90
-! RUN: cd %t && %flang sub/vec.f90 -O2 -Rpass -S -o %t/vec.s 2>&1 | FileCheck %s --check-prefix=SUBDIR
+! RUN: cd %t && %flang sub/vec.f90 -O2 -Rpass -S %{output} 2>&1 | FileCheck %s --check-prefix=SUBDIR
 ! SUBDIR: {{^}}sub/vec.f90:{{[0-9]+}}:{{[0-9]+}}: remark:

>From 2059e676de8f4384d3ae0d0cc210628de5e564de Mon Sep 17 00:00:00 2001
From: John Otken <john.otken at gmail.com>
Date: Wed, 12 Aug 2026 11:59:35 -0500
Subject: [PATCH 4/4] Update ReleaseNotes.md

---
 flang/docs/ReleaseNotes.md | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/flang/docs/ReleaseNotes.md b/flang/docs/ReleaseNotes.md
index 888da4d58b868..2af304ee87590 100644
--- a/flang/docs/ReleaseNotes.md
+++ b/flang/docs/ReleaseNotes.md
@@ -33,6 +33,11 @@ page](https://llvm.org/releases/).
 
 ## Non-comprehensive list of changes in this release
 
+- Change source path in -Rpass remarks (e.g., -Rpass=loop-vectorize) from a
+  (mostly) full path to clang's behavior which is to use the source filename
+  as specified on the command line (except that ./foo.f90 removes the ./
+  prefix).
+
 ## New Compiler Flags
 
 ## Windows Support



More information about the flang-commits mailing list