[flang] Fix extra "./" prefix in source file paths (#186212)

When a relative file search path in the current working directory was
passed to LocateSourceFile, an extra `./` was appended, producing paths
like "././test.F90". This caused the `__FILE__` macro to expand
incorrectly and did not match the behavior of other Fortran compilers
such as gfortran. This caused failures when running an external test
suites that parses `__FILE__` to generate test log filenames.

In LocateSourceFile and LocateSourceFileAll: do not append the search
path if it is the current working directory (`.`). In all other cases,
append the search path to the file name (original behavior).

The test expectation in getsymbols02.f90 is updated because the old
CHECK patterns matched the buggy "././" output.
This commit is contained in:
Caroline Newcombe 2026-03-18 10:35:17 -05:00 committed by GitHub
parent a083e19efe
commit b37f8a54a0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 6 deletions

View file

@ -64,8 +64,15 @@ std::optional<std::string> LocateSourceFile(
return name;
}
for (const std::string &dir : searchPath) {
llvm::SmallString<128> path{dir};
llvm::sys::path::append(path, name);
llvm::SmallString<128> path;
// If the file is found in the current directory, don't append the
// directory path. This preserves the user's format.
if (dir == ".") {
path = name;
} else {
path = dir;
llvm::sys::path::append(path, name);
}
bool isDir{false};
auto er = llvm::sys::fs::is_directory(path, isDir);
if (!er && !isDir) {
@ -82,8 +89,15 @@ std::vector<std::string> LocateSourceFileAll(
}
std::vector<std::string> result;
for (const std::string &dir : searchPath) {
llvm::SmallString<128> path{dir};
llvm::sys::path::append(path, name);
llvm::SmallString<128> path;
// If the file is found in the current directory, don't append the
// directory path. This preserves the user's format.
if (dir == ".") {
path = name;
} else {
path = dir;
llvm::sys::path::append(path, name);
}
bool isDir{false};
auto er = llvm::sys::fs::is_directory(path, isDir);
if (!er && !isDir) {

View file

@ -10,5 +10,5 @@ ENDPROGRAM
! RUN: %flang_fc1 -fsyntax-only %S/Inputs/getsymbols02-a.f90
! RUN: %flang_fc1 -fsyntax-only %S/Inputs/getsymbols02-b.f90
! RUN: %flang_fc1 -fget-symbols-sources %s 2>&1 | FileCheck %s
! CHECK: callget5: .{{[/\\]}}mm2b.mod,
! CHECK: get5: .{{[/\\]}}.{{[/\\]}}mm2a.mod,
! CHECK: callget5: mm2b.mod,
! CHECK: get5: mm2a.mod,