Skip to content

Commit 2bcb62b

Browse files
committed
[clang] Add -funique-source-file-output-paths option
There's build setups where source file names aren't necessarily unique (e.g. the same source files can be used for multiple targets) but output paths are. It's theoretically possible to get the build system to pass `-funique-source-file-identifier` with the output path, but it can also be quite complicated to do so. Introduce a driver option that specifies the output path as the unique source identifier to support this.
1 parent 0df3651 commit 2bcb62b

File tree

5 files changed

+35
-8
lines changed

5 files changed

+35
-8
lines changed

clang/docs/UsersManual.rst

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,10 +2341,11 @@ are listed below.
23412341

23422342
When enabled, allows the compiler to assume that each object file
23432343
passed to the linker has a unique identifier. The identifier for
2344-
an object file is either the source file path or the value of the
2345-
argument `-funique-source-file-identifier` if specified. This is
2346-
useful for reducing link times when doing ThinLTO in combination with
2347-
whole-program devirtualization or CFI.
2344+
an object file is either the source file path, the output file path
2345+
if the ``-funique-source-file-output-paths`` argument is passed, or
2346+
the value of the argument ``-funique-source-file-identifier`` if
2347+
specified. This is useful for reducing link times when doing ThinLTO
2348+
in combination with whole-program devirtualization or CFI.
23482349

23492350
The full source path or identifier passed to the compiler must be
23502351
unique. This means that, for example, the following is a usage error:
@@ -2371,9 +2372,14 @@ are listed below.
23712372

23722373
.. option:: -funique-source-file-identifier=IDENTIFIER
23732374

2374-
Used with `-funique-source-file-names` to specify a source file
2375+
Used with ``-funique-source-file-names`` to specify a source file
23752376
identifier.
23762377

2378+
.. option:: -funique-source-file-output-paths
2379+
2380+
Used with ``-funique-source-file-names`` to specify the output path
2381+
as the source file identifier.
2382+
23772383
.. option:: -fforce-emit-vtables
23782384

23792385
In order to improve devirtualization, forces emitting of vtables even in

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,4 +890,7 @@ def warn_drv_gcc_install_dir_libstdcxx : Warning<
890890
"future releases of the clang compiler will prefer GCC installations "
891891
"containing libstdc++ include directories; '%0' would be chosen over '%1'">,
892892
InGroup<DiagGroup<"gcc-install-dir-libstdcxx">>;
893+
894+
def err_drv_no_output_filename : Error<
895+
"option '%0' requires an output file name">;
893896
}

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4354,6 +4354,8 @@ def unique_source_file_identifier_EQ: Joined<["-"], "funique-source-file-identif
43544354
HelpText<"Specify the source file identifier for -funique-source-file-names; "
43554355
"uses the source file path if not specified">,
43564356
MarshallingInfoString<CodeGenOpts<"UniqueSourceFileIdentifier">>;
4357+
def funique_source_file_output_paths: Joined<["-"], "funique-source-file-output-paths">, Group<f_Group>,
4358+
HelpText<"Use the output path as the source file identifier for -funique-source-file-names">;
43574359
def funsigned_bitfields : Flag<["-"], "funsigned-bitfields">, Group<f_Group>;
43584360
def funsigned_char : Flag<["-"], "funsigned-char">, Group<f_Group>;
43594361
def fno_unsigned_char : Flag<["-"], "fno-unsigned-char">;

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7578,11 +7578,21 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
75787578

75797579
if (Args.hasFlag(options::OPT_funique_source_file_names,
75807580
options::OPT_fno_unique_source_file_names, false)) {
7581-
if (Arg *A = Args.getLastArg(options::OPT_unique_source_file_identifier_EQ))
7582-
A->render(Args, CmdArgs);
7583-
else
7581+
Arg *A = Args.getLastArg(options::OPT_unique_source_file_identifier_EQ,
7582+
options::OPT_funique_source_file_output_paths);
7583+
if (!A) {
75847584
CmdArgs.push_back(Args.MakeArgString(
75857585
Twine("-funique-source-file-identifier=") + Input.getBaseInput()));
7586+
} else if (A->getOption().matches(
7587+
options::OPT_funique_source_file_output_paths)) {
7588+
if (Output.isFilename())
7589+
CmdArgs.push_back(Args.MakeArgString(
7590+
Twine("-funique-source-file-identifier=") + Output.getFilename()));
7591+
else
7592+
D.Diag(diag::err_drv_no_output_filename) << A->getSpelling();
7593+
} else {
7594+
A->render(Args, CmdArgs);
7595+
}
75867596
}
75877597

75887598
// Setup statistics file output.

clang/test/Driver/unique-source-file-names.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@
99

1010
// ID: "-cc1"
1111
// ID: "-funique-source-file-identifier=foo"
12+
13+
// RUN: %clang -funique-source-file-names -funique-source-file-output-paths -o out.o -c -### %s 2> %t
14+
// RUN: FileCheck --check-prefix=OUTPUT < %t %s
15+
16+
// OUTPUT: "-cc1"
17+
// OUTPUT: "-funique-source-file-identifier=out.o"

0 commit comments

Comments
 (0)