Skip to content

Commit 85451f4

Browse files
authored
[Clang][Driver][LTO] Fix empty stats filename when in LTO mode (#71197)
Previously, if a linker argument (i.e. -Wl) is presented before any input filenames, Gnu driver would use the InputInfo object of that argument to generate stats filename for LTO backend, causing an empty filename. This patch fixes such issue.
1 parent 4a4b857 commit 85451f4

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

clang/lib/Driver/ToolChains/Gnu.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,15 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
535535

536536
if (D.isUsingLTO()) {
537537
assert(!Inputs.empty() && "Must have at least one input.");
538-
addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs[0],
538+
// Find the first filename InputInfo object.
539+
auto Input = llvm::find_if(
540+
Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
541+
if (Input == Inputs.end())
542+
// For a very rare case, all of the inputs to the linker are
543+
// InputArg. If that happens, just use the first InputInfo.
544+
Input = Inputs.begin();
545+
546+
addLTOOptions(ToolChain, Args, CmdArgs, Output, *Input,
539547
D.getLTOMode() == LTOK_Thin);
540548
}
541549

clang/test/Driver/save-stats.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
// CHECK-INVALID: invalid value 'bla' in '-save-stats=bla'
2121

2222
// RUN: %clang -target x86_64-linux-unknown -save-stats -flto -o obj/dir/save-stats.exe %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-LTO
23+
// Previously `-plugin-opt=stats-file` would use empty filename if a linker flag (i.e. -Wl) is presented before any input filename.
24+
// RUN: %clang --target=x86_64-linux-unknown -save-stats -flto -o obj/dir/save-stats.exe -Wl,-plugin-opt=-dummy %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-LTO
2325
// CHECK-LTO: "-stats-file=save-stats.stats"
2426
// CHECK-LTO: "-o" "obj/dir{{/|\\\\}}save-stats.exe"
2527
// CHECK-LTO: "-plugin-opt=stats-file=save-stats.stats"

0 commit comments

Comments
 (0)