Skip to content

Commit 6a75496

Browse files
committed
[Driver] Define LinkOption and fix forwarded options to GCC for linking
Many driver options are neither 'DriverOption' nor 'LinkerInput'. When gcc is used for linking, these options get forwarded even if they don't have anything to do with linking. Among these options, clang-specific ones can cause gcc to error. Just use 'OPT_Link_Group' and a new flag 'LinkOption' for options which already have a group. gfortran support apparently bit rots (which does not seem to make much sense). XFAIL the test.
1 parent 48c3228 commit 6a75496

File tree

5 files changed

+26
-41
lines changed

5 files changed

+26
-41
lines changed

clang/include/clang/Driver/Options.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ enum ClangFlags {
3333
CC1Option = (1 << 10),
3434
CC1AsOption = (1 << 11),
3535
NoDriverOption = (1 << 12),
36-
Ignored = (1 << 13)
36+
LinkOption = (1 << 13),
37+
Ignored = (1 << 14),
3738
};
3839

3940
enum ID {

clang/include/clang/Driver/Options.td

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ def CC1AsOption : OptionFlag;
5252
// NoDriverOption - This option should not be accepted by the driver.
5353
def NoDriverOption : OptionFlag;
5454

55+
// If an option affects linking, but has a primary group (so Link_Group cannot
56+
// be used), add this flag.
57+
def LinkOption : OptionFlag;
58+
5559
// A short name to show in documentation. The name will be interpreted as rST.
5660
class DocName<string name> { string DocName = name; }
5761

@@ -573,7 +577,7 @@ def config_system_dir_EQ : Joined<["--"], "config-system-dir=">, Flags<[DriverOp
573577
HelpText<"System directory for configuration files">;
574578
def config_user_dir_EQ : Joined<["--"], "config-user-dir=">, Flags<[DriverOption, HelpHidden]>,
575579
HelpText<"User directory for configuration files">;
576-
def coverage : Flag<["-", "--"], "coverage">, Flags<[CoreOption]>;
580+
def coverage : Flag<["-", "--"], "coverage">, Group<Link_Group>, Flags<[CoreOption]>;
577581
def cpp_precomp : Flag<["-"], "cpp-precomp">, Group<clang_ignored_f_Group>;
578582
def current__version : JoinedOrSeparate<["-"], "current_version">;
579583
def cxx_isystem : JoinedOrSeparate<["-"], "cxx-isystem">, Group<clang_i_Group>,
@@ -1747,7 +1751,7 @@ def fpass_plugin_EQ : Joined<["-"], "fpass-plugin=">,
17471751
HelpText<"Load pass plugin from a dynamic shared object file (only with new pass manager).">;
17481752
defm preserve_as_comments : OptOutFFlag<"preserve-as-comments", "",
17491753
"Do not preserve comments in inline assembly">;
1750-
def fprofile_arcs : Flag<["-"], "fprofile-arcs">, Group<f_Group>;
1754+
def fprofile_arcs : Flag<["-"], "fprofile-arcs">, Group<f_Group>, Flags<[LinkOption]>;
17511755
def fno_profile_arcs : Flag<["-"], "fno-profile-arcs">, Group<f_Group>;
17521756
def framework : Separate<["-"], "framework">, Flags<[LinkerInput]>;
17531757
def frandom_seed_EQ : Joined<["-"], "frandom-seed=">, Group<clang_ignored_f_Group>;
@@ -2724,7 +2728,7 @@ def nostdinc : Flag<["-"], "nostdinc">, Flags<[CoreOption]>;
27242728
def nostdlibinc : Flag<["-"], "nostdlibinc">;
27252729
def nostdincxx : Flag<["-"], "nostdinc++">, Flags<[CC1Option]>,
27262730
HelpText<"Disable standard #include directories for the C++ standard library">;
2727-
def nostdlib : Flag<["-"], "nostdlib">;
2731+
def nostdlib : Flag<["-"], "nostdlib">, Group<Link_Group>;
27282732
def nostdlibxx : Flag<["-"], "nostdlib++">;
27292733
def object : Flag<["-"], "object">;
27302734
def o : JoinedOrSeparate<["-"], "o">, Flags<[DriverOption, RenderAsInput, CC1Option, CC1AsOption]>,
@@ -2768,15 +2772,15 @@ def pthread : Flag<["-"], "pthread">, Flags<[CC1Option]>,
27682772
HelpText<"Support POSIX threads in generated code">;
27692773
def no_pthread : Flag<["-"], "no-pthread">, Flags<[CC1Option]>;
27702774
def p : Flag<["-"], "p">;
2771-
def pie : Flag<["-"], "pie">;
2772-
def static_pie : Flag<["-"], "static-pie">;
2775+
def pie : Flag<["-"], "pie">, Group<Link_Group>;
2776+
def static_pie : Flag<["-"], "static-pie">, Group<Link_Group>;
27732777
def read__only__relocs : Separate<["-"], "read_only_relocs">;
27742778
def remap : Flag<["-"], "remap">;
27752779
def rewrite_objc : Flag<["-"], "rewrite-objc">, Flags<[DriverOption,CC1Option]>,
27762780
HelpText<"Rewrite Objective-C source to C++">, Group<Action_Group>;
27772781
def rewrite_legacy_objc : Flag<["-"], "rewrite-legacy-objc">, Flags<[DriverOption]>,
27782782
HelpText<"Rewrite Legacy Objective-C source to C++">;
2779-
def rdynamic : Flag<["-"], "rdynamic">;
2783+
def rdynamic : Flag<["-"], "rdynamic">, Group<Link_Group>;
27802784
def resource_dir : Separate<["-"], "resource-dir">,
27812785
Flags<[DriverOption, CC1Option, CoreOption, HelpHidden]>,
27822786
HelpText<"The directory which holds the compiler resource files">;
@@ -2818,13 +2822,13 @@ def segs__read__only__addr : Separate<["-"], "segs_read_only_addr">;
28182822
def segs__read__write__addr : Separate<["-"], "segs_read_write_addr">;
28192823
def segs__read__ : Joined<["-"], "segs_read_">;
28202824
def shared_libgcc : Flag<["-"], "shared-libgcc">;
2821-
def shared : Flag<["-", "--"], "shared">;
2825+
def shared : Flag<["-", "--"], "shared">, Group<Link_Group>;
28222826
def single__module : Flag<["-"], "single_module">;
28232827
def specs_EQ : Joined<["-", "--"], "specs=">;
28242828
def specs : Separate<["-", "--"], "specs">, Flags<[Unsupported]>;
28252829
def static_libgcc : Flag<["-"], "static-libgcc">;
28262830
def static_libstdcxx : Flag<["-"], "static-libstdc++">;
2827-
def static : Flag<["-", "--"], "static">, Flags<[NoArgumentUnused]>;
2831+
def static : Flag<["-", "--"], "static">, Group<Link_Group>, Flags<[NoArgumentUnused]>;
28282832
def std_default_EQ : Joined<["-"], "std-default=">;
28292833
def std_EQ : Joined<["-", "--"], "std=">, Flags<[CC1Option]>,
28302834
Group<CompileOnly_Group>, HelpText<"Language standard to compile for">,
@@ -3283,8 +3287,8 @@ defm : BooleanFFlag<"keep-inline-functions">, Group<clang_ignored_gcc_optimizati
32833287

32843288
def fprofile_dir : Joined<["-"], "fprofile-dir=">, Group<f_Group>;
32853289

3286-
def fuse_ld_EQ : Joined<["-"], "fuse-ld=">, Group<f_Group>, Flags<[CoreOption]>;
3287-
def ld_path_EQ : Joined<["--"], "ld-path=">;
3290+
def fuse_ld_EQ : Joined<["-"], "fuse-ld=">, Group<f_Group>, Flags<[CoreOption, LinkOption]>;
3291+
def ld_path_EQ : Joined<["--"], "ld-path=">, Group<Link_Group>;
32883292

32893293
defm align_labels : BooleanFFlag<"align-labels">, Group<clang_ignored_gcc_optimization_f_Group>;
32903294
def falign_labels_EQ : Joined<["-"], "falign-labels=">, Group<clang_ignored_gcc_optimization_f_Group>;

clang/lib/Driver/ToolChains/Gnu.cpp

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ using tools::addMultilibFlag;
3838
using tools::addPathIfExists;
3939

4040
static bool forwardToGCC(const Option &O) {
41-
// Don't forward inputs from the original command line. They are added from
42-
// InputInfoList.
43-
return O.getKind() != Option::InputClass &&
44-
!O.hasFlag(options::DriverOption) && !O.hasFlag(options::LinkerInput);
41+
return O.matches(options::OPT_Link_Group) || O.hasFlag(options::LinkOption);
4542
}
4643

4744
// Switch CPU names not recognized by GNU assembler to a close CPU that it does
@@ -76,23 +73,6 @@ void tools::gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
7673
// to get to the assembler.
7774
A->claim();
7875

79-
// Don't forward any -g arguments to assembly steps.
80-
if (isa<AssembleJobAction>(JA) &&
81-
A->getOption().matches(options::OPT_g_Group))
82-
continue;
83-
84-
// Don't forward any -W arguments to assembly and link steps.
85-
if ((isa<AssembleJobAction>(JA) || isa<LinkJobAction>(JA)) &&
86-
A->getOption().matches(options::OPT_W_Group))
87-
continue;
88-
89-
// Don't forward -mno-unaligned-access since GCC doesn't understand
90-
// it and because it doesn't affect the assembly or link steps.
91-
if ((isa<AssembleJobAction>(JA) || isa<LinkJobAction>(JA)) &&
92-
(A->getOption().matches(options::OPT_munaligned_access) ||
93-
A->getOption().matches(options::OPT_mno_unaligned_access)))
94-
continue;
95-
9676
A->render(Args, CmdArgs);
9777
}
9878
}

clang/test/Driver/gcc_forward.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
// RUN: %clang -### %s -target aarch64-none-elf \
2+
// RUN: --coverage -fuse-ld=lld --ld-path=ld -nostdlib -r -rdynamic -static -static-pie \
3+
// RUN: 2>&1 | FileCheck --check-prefix=FORWARD %s
4+
// FORWARD: gcc{{[^"]*}}" "--coverage" "-fuse-ld=lld" "--ld-path=ld" "-nostdlib" "-r" "-rdynamic" "-static" "-static-pie"
5+
16
// Check that we don't try to forward -Xclang or -mlinker-version to GCC.
27
// PR12920 -- Check also we may not forward W_Group options to GCC.
38
//
49
// RUN: %clang -target powerpc-unknown-unknown \
510
// RUN: %s \
611
// RUN: -Wall -Wdocumentation \
712
// RUN: -Xclang foo-bar \
8-
// RUN: -march=x86-64 \
13+
// RUN: -pie -march=x86-64 \
914
// RUN: -mlinker-version=10 -### 2> %t
1015
// RUN: FileCheck < %t %s
1116
//
@@ -15,13 +20,13 @@
1520
// CHECK: "-o" "{{[^"]+}}.o"
1621
//
1722
// gcc as ld.
18-
// CHECK: gcc{{[^"]*}}"
23+
// CHECK: gcc{{[^"]*}}" "-pie"
1924
// CHECK-NOT: "-mlinker-version=10"
2025
// CHECK-NOT: "-Xclang"
2126
// CHECK-NOT: "foo-bar"
2227
// CHECK-NOT: "-Wall"
2328
// CHECK-NOT: "-Wdocumentation"
24-
// CHECK: -march
29+
// CHECK-NOT: -march
2530
// CHECK-NOT: "-mlinker-version=10"
2631
// CHECK-NOT: "-Xclang"
2732
// CHECK-NOT: "foo-bar"
@@ -34,9 +39,3 @@
3439
// RUN: | FileCheck --check-prefix=CHECK-ASM %s
3540
// CHECK-ASM: as
3641
// CHECK-ASM-NOT: "-g"
37-
38-
// Check that we're not forwarding -mno-unaligned-access.
39-
// RUN: %clang -target aarch64-none-elf -mno-unaligned-access %s -### 2>&1 \
40-
// RUN: | FileCheck --check-prefix=CHECK-ARM %s
41-
// CHECK-ARM: gcc{{[^"]*}}"
42-
// CHECK-ARM-NOT: -mno-unaligned-access

clang/test/Driver/gfortran.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
! XFAIL: *
12
! Test that Clang can forward all of the flags which are documented as
23
! being supported by gfortran to GCC when falling back to GCC for
34
! a fortran input file.

0 commit comments

Comments
 (0)