Skip to content

Commit 569e4fa

Browse files
authored
Defer linker input unused and no input files warnings to clang. NFC (#20905)
Split out from #20884
1 parent 9ab492d commit 569e4fa

File tree

3 files changed

+38
-45
lines changed

3 files changed

+38
-45
lines changed

emcc.py

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,13 @@ def run(args):
534534
''')
535535
return 0
536536

537+
## Process argument and setup the compiler
538+
state = EmccState(args)
539+
options, newargs = phase_parse_arguments(state)
540+
541+
if not shared.SKIP_SUBPROCS:
542+
shared.check_sanity()
543+
537544
if '--version' in args:
538545
print(version_string())
539546
print('''\
@@ -566,13 +573,6 @@ def run(args):
566573
print(shared.shlex_join(parts[1:]))
567574
return 0
568575

569-
## Process argument and setup the compiler
570-
state = EmccState(args)
571-
options, newargs = phase_parse_arguments(state)
572-
573-
if not shared.SKIP_SUBPROCS:
574-
shared.check_sanity()
575-
576576
if 'EMMAKEN_NO_SDK' in os.environ:
577577
exit_with_error('EMMAKEN_NO_SDK is no longer supported. The standard -nostdlib and -nostdinc flags should be used instead')
578578

@@ -618,9 +618,6 @@ def run(args):
618618
print(libname)
619619
return 0
620620

621-
if not input_files and not state.link_flags:
622-
exit_with_error('no input files')
623-
624621
if options.reproduce:
625622
create_reproduce_file(options.reproduce, args)
626623

@@ -641,8 +638,6 @@ def run(args):
641638
return link.run(linker_inputs, options, state, newargs)
642639
else:
643640
logger.debug('stopping after compile phase')
644-
for flag in state.link_flags:
645-
diagnostics.warning('unused-command-line-argument', "argument unused during compilation: '%s'" % flag[1])
646641
return 0
647642

648643

@@ -670,9 +665,9 @@ def phase_parse_arguments(state):
670665
newargs = diagnostics.capture_warnings(newargs)
671666

672667
for i in range(len(newargs)):
673-
if newargs[i] in ('-l', '-L', '-I'):
674-
# Scan for individual -l/-L/-I arguments and concatenate the next arg on
675-
# if there is no suffix
668+
if newargs[i] in ('-l', '-L', '-I', '-z'):
669+
# Scan for flags that can be written as either one or two arguments
670+
# and normalize them to the single argument form.
676671
newargs[i] += newargs[i + 1]
677672
newargs[i + 1] = ''
678673

@@ -764,33 +759,24 @@ def phase_setup(options, state, newargs):
764759
input_files.append((i, arg))
765760
elif arg.startswith('-L'):
766761
state.add_link_flag(i, arg)
767-
newargs[i] = ''
768762
elif arg.startswith('-l'):
769763
state.add_link_flag(i, arg)
770-
newargs[i] = ''
771764
elif arg == '-z':
772765
state.add_link_flag(i, newargs[i])
773766
state.add_link_flag(i + 1, newargs[i + 1])
774-
newargs[i] = ''
775-
newargs[i + 1] = ''
776767
elif arg.startswith('-z'):
777768
state.add_link_flag(i, newargs[i])
778-
newargs[i] = ''
779769
elif arg.startswith('-Wl,'):
780770
# Multiple comma separated link flags can be specified. Create fake
781771
# fractional indices for these: -Wl,a,b,c,d at index 4 becomes:
782772
# (4, a), (4.25, b), (4.5, c), (4.75, d)
783773
link_flags_to_add = arg.split(',')[1:]
784774
for flag_index, flag in enumerate(link_flags_to_add):
785775
state.add_link_flag(i + float(flag_index) / len(link_flags_to_add), flag)
786-
newargs[i] = ''
787776
elif arg == '-Xlinker':
788777
state.add_link_flag(i + 1, newargs[i + 1])
789-
newargs[i] = ''
790-
newargs[i + 1] = ''
791778
elif arg == '-s':
792779
state.add_link_flag(i, newargs[i])
793-
newargs[i] = ''
794780
elif arg == '-':
795781
input_files.append((i, arg))
796782
newargs[i] = ''
@@ -950,11 +936,6 @@ def get_clang_output_extension(state):
950936

951937
@ToolchainProfiler.profile_block('compile inputs')
952938
def phase_compile_inputs(options, state, newargs, input_files):
953-
def is_link_flag(flag):
954-
if flag in ('-nostdlib', '-nostartfiles', '-nolibc', '-nodefaultlibs'):
955-
return True
956-
return flag.startswith(('-l', '-L', '-Wl,'))
957-
958939
if shared.run_via_emxx:
959940
compiler = [shared.CLANG_CXX]
960941
else:
@@ -964,7 +945,7 @@ def is_link_flag(flag):
964945
logger.debug('using compiler wrapper: %s', config.COMPILER_WRAPPER)
965946
compiler.insert(0, config.COMPILER_WRAPPER)
966947

967-
compile_args = [a for a in newargs if a and not is_link_flag(a)]
948+
compile_args = newargs
968949
system_libs.ensure_sysroot()
969950

970951
def get_language_mode(args):
@@ -1002,7 +983,7 @@ def get_clang_command_asm():
1002983
# with -MF! (clang seems to not recognize it)
1003984
logger.debug(('just preprocessor ' if state.has_dash_E else 'just dependencies: ') + ' '.join(cmd))
1004985
shared.check_call(cmd)
1005-
return []
986+
sys.exit(0)
1006987

1007988
# Precompiled headers support
1008989
if state.mode == Mode.PCH:
@@ -1015,7 +996,7 @@ def get_clang_command_asm():
1015996
cmd += ['-o', options.output_file]
1016997
logger.debug(f"running (for precompiled headers): {cmd[0]} {' '.join(cmd[1:])}")
1017998
shared.check_call(cmd)
1018-
return []
999+
sys.exit(0)
10191000

10201001
if state.mode == Mode.COMPILE_ONLY:
10211002
inputs = [i[1] for i in input_files]
@@ -1027,18 +1008,24 @@ def get_clang_command_asm():
10271008
cmd += ['-o', options.output_file]
10281009
if get_file_suffix(options.output_file) == '.bc' and not settings.LTO and '-emit-llvm' not in state.orig_args:
10291010
diagnostics.warning('emcc', '.bc output file suffix used without -flto or -emit-llvm. Consider using .o extension since emcc will output an object file, not a bitcode file')
1011+
ext = get_clang_output_extension(state)
10301012
shared.check_call(cmd)
1031-
if not options.output_file:
1032-
# Rename object files to match --default-obj-ext
1033-
# TODO: Remove '--default-obj-ext' to reduce this complexity
1034-
ext = get_clang_output_extension(state)
1035-
if options.default_object_extension != ext:
1036-
for i in inputs:
1037-
output = unsuffixed_basename(i) + ext
1038-
new_output = unsuffixed_basename(i) + options.default_object_extension
1039-
shutil.move(output, new_output)
1040-
return []
1013+
if not options.output_file and options.default_object_extension != ext:
1014+
for i in inputs:
1015+
output = unsuffixed_basename(i) + ext
1016+
new_output = unsuffixed_basename(i) + options.default_object_extension
1017+
shutil.move(output, new_output)
1018+
sys.exit(0)
1019+
1020+
# In COMPILE_AND_LINK we need to compile source files too, but we also need to
1021+
# filter out the link flags
1022+
1023+
def is_link_flag(flag):
1024+
if flag in ('-nostdlib', '-nostartfiles', '-nolibc', '-nodefaultlibs', '-s'):
1025+
return True
1026+
return flag.startswith(('-l', '-L', '-Wl,', '-z'))
10411027

1028+
compile_args = [a for a in compile_args if a and not is_link_flag(a)]
10421029
linker_inputs = []
10431030
seen_names = {}
10441031

test/test_other.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11458,7 +11458,7 @@ def test_linker_flags_pass_through(self):
1145811458

1145911459
def test_linker_flags_unused(self):
1146011460
err = self.run_process([EMXX, test_file('hello_world.cpp'), '-c', '-lbar'], stderr=PIPE).stderr
11461-
self.assertContained("warning: argument unused during compilation: '-lbar' [-Wunused-command-line-argument]", err)
11461+
self.assertContained("warning: -lbar: 'linker' input unused [-Wunused-command-line-argument]", err)
1146211462

1146311463
def test_linker_input_unused(self):
1146411464
self.run_process([EMXX, '-c', test_file('hello_world.cpp')])
@@ -14279,3 +14279,7 @@ def test_strndup(self):
1427914279

1428014280
def test_errar(self):
1428114281
self.do_other_test('test_errar.c')
14282+
14283+
def test_no_input_files(self):
14284+
err = self.expect_fail([EMCC, '-c'])
14285+
self.assertContained('clang: error: no input files', err)

test/test_sanity.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,13 @@ def do(self, command, env=None):
142142
def check_working(self, command, expected=None):
143143
if type(command) is not list:
144144
command = [command]
145+
if command == [EMCC]:
146+
command = [EMCC, '--version']
145147
if expected is None:
146148
if command[0] == EMCC or (len(command) >= 2 and command[1] == EMCC):
147-
expected = 'no input files'
149+
expected = 'emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld)'
148150
else:
149-
expected = "could not find the following tests: blahblah"
151+
expected = 'could not find the following tests: blahblah'
150152

151153
output = self.do(command)
152154
self.assertContained(expected, output)

0 commit comments

Comments
 (0)