Jannick Kremer ec149d5ef8
[clang][python][test] Move python binding tests to lit framework (#148802)
As discussed in PR #142353, the current testsuite of the `clang` Python
bindings has several issues:

- It `libclang.so` cannot be loaded into `python` to run the testsuite,
the whole `ninja check-all` aborts.
- The result of running the testsuite isn't report like the `lit`-based
tests, rendering them almost invisible.
- The testsuite is disabled in a non-obvious way (`RUN_PYTHON_TESTS`) in
`tests/CMakeLists.txt`, which again doesn't show up in the test results.

All these issues can be avoided by integrating the Python bindings tests
with `lit`, which is what this patch does:

- The actual test lives in `clang/test/bindings/python/bindings.sh` and
is run by `lit`.
- The current `clang/bindings/python/tests` directory (minus the
now-subperfluous `CMakeLists.txt`) is moved into the same directory.
- The check if `libclang` is loadable (originally from PR #142353) is
now handled via a new `lit` feature, `libclang-loadable`.
- The various ways to disable the tests have been turned into `XFAIL`s
as appropriate. This isn't complete and not completely tested yet.

Tested on `sparc-sun-solaris2.11`, `sparcv9-sun-solaris2.11`,
`i386-pc-solaris2.11`, `amd64-pc-solaris2.11`, `i686-pc-linux-gnu`, and
`x86_64-pc-linux-gnu`.

Co-authored-by: Rainer Orth <ro@gcc.gnu.org>
2025-07-15 12:48:24 +02:00

42 lines
1.7 KiB
INI

def is_libclang_loadable():
# Do not try to run if libclang was built with sanitizers because
# the sanitizer library will likely be loaded too late to perform
# interception and will then fail.
# We could use LD_PRELOAD/DYLD_INSERT_LIBRARIES but this isn't
# portable so its easier just to not run the tests when building
# with ASan.
if config.llvm_use_sanitizer != "":
return False
try:
sys.path.append(os.path.join(config.clang_src_dir, "bindings/python"))
from clang.cindex import Config
conf = Config()
Config.set_library_path(config.clang_lib_dir)
conf.lib
return True
except Exception as e:
# Expected failure modes are considered benign when nothing can be
# done about them.
#
# Cannot load a 32-bit libclang.so into a 64-bit python.
if "wrong ELF class: ELFCLASS32" in str(e):
return False
# If libclang.so is missing, it must have been disabled intentionally,
# e.g. by building with LLVM_ENABLE_PIC=OFF.
elif "No such file or directory" in str(e):
return False
# Unexpected failure modes need to be investigated to either fix an
# underlying bug or accept the failure, so return True. This causes
# tests to run and FAIL, drawing developer attention.
else:
print("warning: unhandled failure in is_libclang_loadable: "
+ str(e), file=sys.stderr)
return True
if is_libclang_loadable():
config.available_features.add("libclang-loadable")
config.substitutions.append(('%libdir', config.clang_lib_dir))
config.suffixes = ['.sh']