Skip to content

Commit 8bdbc57

Browse files
authored
[NFC][LLDB] Make it possible to detect if the compiler used in tests supports -fbounds-safety (#169112)
This patch makes it possible to detect in LLDB shell and API tests if `-fbounds-safety` is supported by the compiler used for testing. The motivation behind this is to allow upstreaming swiftlang#11835 but with the tests disabled in upstream because the full implementation of -fbounds-safety isn't available in Clang yet. For shell tests when -fbounds-safety is available the `clang-bounds-safety` feature is available which means tests can be annotated with `# REQUIRES: clang-bounds-safety`. API tests that need -fbounds-safety support in the compiler can use the new `@skipUnlessBoundsSafety` decorator. rdar://165225507
1 parent 54a4da9 commit 8bdbc57

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

lldb/packages/Python/lldbsuite/test/decorators.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,16 @@ def is_compiler_with_address_sanitizer():
10591059
return skipTestIfFn(is_compiler_with_address_sanitizer)(func)
10601060

10611061

1062+
def skipUnlessBoundsSafety(func):
1063+
"""Decorate the item to skip test unless Clang -fbounds-safety is supported."""
1064+
1065+
def is_compiler_with_bounds_safety():
1066+
if not _compiler_supports(lldbplatformutil.getCompiler(), "-fbounds-safety"):
1067+
return "Compiler cannot compile with -fbounds-safety"
1068+
return None
1069+
1070+
return skipTestIfFn(is_compiler_with_bounds_safety)(func)
1071+
10621072
def skipIfAsan(func):
10631073
"""Skip this test if the environment is set up to run LLDB *itself* under ASAN."""
10641074
return skipTestIfFn(is_running_under_asan)(func)

lldb/test/Shell/helper/toolchain.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ def use_support_substitutions(config):
277277
required=True,
278278
use_installed=True,
279279
)
280+
if llvm_config.clang_has_bounds_safety():
281+
llvm_config.lit_config.note("clang has -fbounds-safety support")
282+
config.available_features.add("clang-bounds-safety")
280283

281284
if sys.platform == "win32":
282285
_use_msvc_substitutions(config)

llvm/utils/lit/lit/llvm/config.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,17 @@ def get_process_output(self, command):
293293
except OSError:
294294
self.lit_config.fatal("Could not run process %s" % command)
295295

296+
def check_process_success(self, command):
297+
cp = subprocess.run(
298+
command,
299+
stdout=subprocess.DEVNULL,
300+
stderr=subprocess.DEVNULL,
301+
env=self.config.environment,
302+
)
303+
if cp.returncode == 0:
304+
return True
305+
return False
306+
296307
def feature_config(self, features):
297308
# Ask llvm-config about the specified feature.
298309
arguments = [x for (x, _) in features]
@@ -334,6 +345,25 @@ def get_clang_builtin_include_dir(self, clang):
334345
# Ensure the result is an ascii string, across Python2.5+ - Python3.
335346
return clang_dir
336347

348+
def clang_has_bounds_safety(self, additional_flags=None):
349+
"""
350+
Return True iff `self.config.clang` supports -fbounds-safety
351+
"""
352+
if not self.config.clang:
353+
return False
354+
if not os.path.exists(self.config.clang):
355+
return False
356+
if additional_flags is None:
357+
additional_flags = []
358+
# Invoke the clang driver to see if it supports the `-fbounds-safety`
359+
# flag. Only the downstream implementation has this flag so this is
360+
# a simple way to check if the full implementation is available or not.
361+
cmd = [self.config.clang] + additional_flags
362+
cmd += ["-fbounds-safety", "-###"]
363+
if self.check_process_success(cmd):
364+
return True
365+
return False
366+
337367
# On macOS, LSan is only supported on clang versions 5 and higher
338368
def get_clang_has_lsan(self, clang, triple):
339369
if not clang:

0 commit comments

Comments
 (0)