Skip to content

[clang][python][test] Move python binding tests to lit framework #145855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 26, 2025

Conversation

rorth
Copy link
Collaborator

@rorth rorth commented Jun 26, 2025

As discussed in PR #142353, the current testsuite of the clang Python
bindings has several issues:

  • If 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-superfluous CMakeLists.txt) is moved into the same directory.
  • The check if libclang is loadable (originally from PR [clang][python][test] Check if libclang.so is loadable #142353) is
    now handled via a new lit feature, libclang-loadable.
  • The various ways to disable the tests have been turned into XFAILs
    as appropriate.
  • AArch64 doesn't FAIL any longer, so no XFAIL is necessary.
  • It keeps the check-clang-python target for use by the Clang Python
    CI.

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.

@rorth rorth requested a review from DeinAlptraum as a code owner June 26, 2025 08:41
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:as-a-library libclang and C++ API labels Jun 26, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 26, 2025

@llvm/pr-subscribers-github-workflow

@llvm/pr-subscribers-clang

Author: Rainer Orth (rorth)

Changes

As discussed in PR #142353, the current testsuite of the clang Python
bindings has several issues:

  • If 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-superfluous 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 XFAILs
    as appropriate.
  • It keeps the check-clang-python target for use by the Clang Python
    CI.

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.


Full diff: https://github.com/llvm/llvm-project/pull/145855.diff

39 Files Affected:

  • (modified) clang/CMakeLists.txt (-1)
  • (removed) clang/bindings/python/tests/CMakeLists.txt (-66)
  • (modified) clang/test/CMakeLists.txt (+11)
  • (added) clang/test/bindings/python/bindings.sh (+33)
  • (added) clang/test/bindings/python/lit.local.cfg (+41)
  • (renamed) clang/test/bindings/python/tests/init.py ()
  • (renamed) clang/test/bindings/python/tests/cindex/INPUTS/a.inc ()
  • (renamed) clang/test/bindings/python/tests/cindex/INPUTS/b.inc ()
  • (renamed) clang/test/bindings/python/tests/cindex/INPUTS/compile_commands.json ()
  • (renamed) clang/test/bindings/python/tests/cindex/INPUTS/header1.h ()
  • (renamed) clang/test/bindings/python/tests/cindex/INPUTS/header2.h ()
  • (renamed) clang/test/bindings/python/tests/cindex/INPUTS/header3.h ()
  • (renamed) clang/test/bindings/python/tests/cindex/INPUTS/hello.cpp ()
  • (renamed) clang/test/bindings/python/tests/cindex/INPUTS/include.cpp ()
  • (renamed) clang/test/bindings/python/tests/cindex/INPUTS/parse_arguments.c ()
  • (renamed) clang/test/bindings/python/tests/cindex/INPUTS/testfile.c ()
  • (renamed) clang/test/bindings/python/tests/cindex/init.py ()
  • (renamed) clang/test/bindings/python/tests/cindex/test_access_specifiers.py ()
  • (renamed) clang/test/bindings/python/tests/cindex/test_cdb.py ()
  • (renamed) clang/test/bindings/python/tests/cindex/test_code_completion.py ()
  • (renamed) clang/test/bindings/python/tests/cindex/test_comment.py ()
  • (renamed) clang/test/bindings/python/tests/cindex/test_cursor.py ()
  • (renamed) clang/test/bindings/python/tests/cindex/test_cursor_kind.py ()
  • (renamed) clang/test/bindings/python/tests/cindex/test_diagnostics.py ()
  • (renamed) clang/test/bindings/python/tests/cindex/test_enums.py ()
  • (renamed) clang/test/bindings/python/tests/cindex/test_exception_specification_kind.py ()
  • (renamed) clang/test/bindings/python/tests/cindex/test_file.py ()
  • (renamed) clang/test/bindings/python/tests/cindex/test_index.py ()
  • (renamed) clang/test/bindings/python/tests/cindex/test_lib.py ()
  • (renamed) clang/test/bindings/python/tests/cindex/test_linkage.py ()
  • (renamed) clang/test/bindings/python/tests/cindex/test_location.py ()
  • (renamed) clang/test/bindings/python/tests/cindex/test_rewrite.py ()
  • (renamed) clang/test/bindings/python/tests/cindex/test_source_range.py ()
  • (renamed) clang/test/bindings/python/tests/cindex/test_tls_kind.py ()
  • (renamed) clang/test/bindings/python/tests/cindex/test_token_kind.py ()
  • (renamed) clang/test/bindings/python/tests/cindex/test_tokens.py ()
  • (renamed) clang/test/bindings/python/tests/cindex/test_translation_unit.py ()
  • (renamed) clang/test/bindings/python/tests/cindex/test_type.py ()
  • (renamed) clang/test/bindings/python/tests/cindex/util.py ()
diff --git a/clang/CMakeLists.txt b/clang/CMakeLists.txt
index 94607a8e8473c..72386132bce9f 100644
--- a/clang/CMakeLists.txt
+++ b/clang/CMakeLists.txt
@@ -530,7 +530,6 @@ if( CLANG_INCLUDE_TESTS )
     clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
   )
   add_subdirectory(test)
-  add_subdirectory(bindings/python/tests)
 
   if(CLANG_BUILT_STANDALONE)
     umbrella_lit_testsuite_end(check-all)
diff --git a/clang/bindings/python/tests/CMakeLists.txt b/clang/bindings/python/tests/CMakeLists.txt
deleted file mode 100644
index a0ddabc21bb41..0000000000000
--- a/clang/bindings/python/tests/CMakeLists.txt
+++ /dev/null
@@ -1,66 +0,0 @@
-# Test target to run Python test suite from main build.
-
-# Avoid configurations including '-include' from interfering with
-# our tests by setting CLANG_NO_DEFAULT_CONFIG.
-add_custom_target(check-clang-python
-    COMMAND ${CMAKE_COMMAND} -E env
-            CLANG_NO_DEFAULT_CONFIG=1
-            CLANG_LIBRARY_PATH=$<TARGET_FILE_DIR:libclang>
-            "${Python3_EXECUTABLE}" -m unittest discover
-    DEPENDS libclang
-    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/..)
-
-set(RUN_PYTHON_TESTS TRUE)
-set_target_properties(check-clang-python PROPERTIES FOLDER "Clang/Tests")
-
-# Tests require libclang.so which is only built with LLVM_ENABLE_PIC=ON
-if(NOT LLVM_ENABLE_PIC)
-  set(RUN_PYTHON_TESTS FALSE)
-endif()
-
-# 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(NOT LLVM_USE_SANITIZER STREQUAL "")
-  set(RUN_PYTHON_TESTS FALSE)
-endif()
-
-# Tests fail on Windows, and need someone knowledgeable to fix.
-# It's not clear whether it's a test or a valid binding problem.
-if(WIN32)
-  set(RUN_PYTHON_TESTS FALSE)
-endif()
-
-# The Python FFI interface is broken on AIX: https://bugs.python.org/issue38628.
-if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
-  set(RUN_PYTHON_TESTS FALSE)
-endif()
-
-# AArch64, Hexagon, and Sparc have known test failures that need to be
-# addressed.
-# SystemZ has broken Python/FFI interface:
-# https://reviews.llvm.org/D52840#1265716
-if(${LLVM_NATIVE_ARCH} MATCHES "^(AArch64|Hexagon|Sparc|SystemZ)$")
-  set(RUN_PYTHON_TESTS FALSE)
-endif()
-
-# Tests will fail if cross-compiling for a different target, as tests will try
-# to use the host Python3_EXECUTABLE and make FFI calls to functions in target
-# libraries.
-if(CMAKE_CROSSCOMPILING)
-  # FIXME: Consider a solution that allows better control over these tests in
-  # a crosscompiling scenario. e.g. registering them with lit to allow them to
-  # be explicitly skipped via appropriate LIT_ARGS, or adding a mechanism to
-  # allow specifying a python interpreter compiled for the target that could
-  # be executed using qemu-user.
-  message(WARNING "check-clang-python not added to check-all as these tests fail in a cross-build setup")
-  set(RUN_PYTHON_TESTS FALSE)
-endif()
-
-if(RUN_PYTHON_TESTS)
-    set_property(GLOBAL APPEND PROPERTY
-                 LLVM_ALL_ADDITIONAL_TEST_TARGETS check-clang-python)
-endif()
diff --git a/clang/test/CMakeLists.txt b/clang/test/CMakeLists.txt
index 35a8042ac0e0a..fe05e022c88da 100644
--- a/clang/test/CMakeLists.txt
+++ b/clang/test/CMakeLists.txt
@@ -211,6 +211,17 @@ add_custom_target(clang-test)
 add_dependencies(clang-test check-clang)
 set_target_properties(clang-test PROPERTIES FOLDER "Clang/Tests")
 
+# Allow running Clang Python binding tests separately from CI.
+add_lit_testsuite(check-clang-python "Running the Clang Python tests"
+  ${CMAKE_CURRENT_BINARY_DIR}
+  #LIT ${LLVM_LIT}
+  PARAMS ${CLANG_TEST_PARAMS}
+  DEPENDS ${CLANG_TEST_DEPS}
+  ARGS ${CLANG_TEST_EXTRA_ARGS} --filter=bindings.sh
+  # Avoid running tests twice.
+  EXCLUDE_FROM_CHECK_ALL
+  )
+
 # FIXME: This logic can be removed once all buildbots have moved
 # debuginfo-test from clang/test to llvm/projects or monorepo.
 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/debuginfo-tests)
diff --git a/clang/test/bindings/python/bindings.sh b/clang/test/bindings/python/bindings.sh
new file mode 100755
index 0000000000000..f7376d4c7a067
--- /dev/null
+++ b/clang/test/bindings/python/bindings.sh
@@ -0,0 +1,33 @@
+#!/bin/sh
+
+# UNSUPPORTED: !libclang-loadable
+
+# Tests fail on Windows, and need someone knowledgeable to fix.
+# It's not clear whether it's a test or a valid binding problem.
+# XFAIL: target={{.*windows.*}}
+
+# The Python FFI interface is broken on AIX: https://bugs.python.org/issue38628.
+# XFAIL: target={{.*-aix.*}}
+
+# AArch64 and Hexagon have known test failures that need to be addressed.
+# SystemZ has broken Python/FFI interface:
+# https://reviews.llvm.org/D52840#1265716
+# XFAIL: target={{(aarch64|hexagon|s390x)-.*}}
+# python SEGVs on Linux/sparc64 when loading libclang.so.  Seems to be an FFI
+# issue, too.
+# XFAIL: target={{sparc.*-.*-linux.*}}
+
+# Tests will fail if cross-compiling for a different target, as tests will try
+# to use the host Python3_EXECUTABLE and make FFI calls to functions in target
+# libraries.
+#
+# FIXME: Consider a solution that allows better control over these tests in
+# a crosscompiling scenario. e.g. registering them with lit to allow them to
+# be explicitly skipped via appropriate LIT_ARGS, or adding a mechanism to
+# allow specifying a python interpreter compiled for the target that could
+# be executed using qemu-user.
+# XFAIL: !native
+
+# RUN: env PYTHONPATH=%S/../../../bindings/python \
+# RUN:   CLANG_LIBRARY_PATH=%libdir \
+# RUN:   %python -m unittest discover -s %S/tests
diff --git a/clang/test/bindings/python/lit.local.cfg b/clang/test/bindings/python/lit.local.cfg
new file mode 100644
index 0000000000000..cc3bdf8ba97d4
--- /dev/null
+++ b/clang/test/bindings/python/lit.local.cfg
@@ -0,0 +1,41 @@
+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']
diff --git a/clang/bindings/python/tests/__init__.py b/clang/test/bindings/python/tests/__init__.py
similarity index 100%
rename from clang/bindings/python/tests/__init__.py
rename to clang/test/bindings/python/tests/__init__.py
diff --git a/clang/bindings/python/tests/cindex/INPUTS/a.inc b/clang/test/bindings/python/tests/cindex/INPUTS/a.inc
similarity index 100%
rename from clang/bindings/python/tests/cindex/INPUTS/a.inc
rename to clang/test/bindings/python/tests/cindex/INPUTS/a.inc
diff --git a/clang/bindings/python/tests/cindex/INPUTS/b.inc b/clang/test/bindings/python/tests/cindex/INPUTS/b.inc
similarity index 100%
rename from clang/bindings/python/tests/cindex/INPUTS/b.inc
rename to clang/test/bindings/python/tests/cindex/INPUTS/b.inc
diff --git a/clang/bindings/python/tests/cindex/INPUTS/compile_commands.json b/clang/test/bindings/python/tests/cindex/INPUTS/compile_commands.json
similarity index 100%
rename from clang/bindings/python/tests/cindex/INPUTS/compile_commands.json
rename to clang/test/bindings/python/tests/cindex/INPUTS/compile_commands.json
diff --git a/clang/bindings/python/tests/cindex/INPUTS/header1.h b/clang/test/bindings/python/tests/cindex/INPUTS/header1.h
similarity index 100%
rename from clang/bindings/python/tests/cindex/INPUTS/header1.h
rename to clang/test/bindings/python/tests/cindex/INPUTS/header1.h
diff --git a/clang/bindings/python/tests/cindex/INPUTS/header2.h b/clang/test/bindings/python/tests/cindex/INPUTS/header2.h
similarity index 100%
rename from clang/bindings/python/tests/cindex/INPUTS/header2.h
rename to clang/test/bindings/python/tests/cindex/INPUTS/header2.h
diff --git a/clang/bindings/python/tests/cindex/INPUTS/header3.h b/clang/test/bindings/python/tests/cindex/INPUTS/header3.h
similarity index 100%
rename from clang/bindings/python/tests/cindex/INPUTS/header3.h
rename to clang/test/bindings/python/tests/cindex/INPUTS/header3.h
diff --git a/clang/bindings/python/tests/cindex/INPUTS/hello.cpp b/clang/test/bindings/python/tests/cindex/INPUTS/hello.cpp
similarity index 100%
rename from clang/bindings/python/tests/cindex/INPUTS/hello.cpp
rename to clang/test/bindings/python/tests/cindex/INPUTS/hello.cpp
diff --git a/clang/bindings/python/tests/cindex/INPUTS/include.cpp b/clang/test/bindings/python/tests/cindex/INPUTS/include.cpp
similarity index 100%
rename from clang/bindings/python/tests/cindex/INPUTS/include.cpp
rename to clang/test/bindings/python/tests/cindex/INPUTS/include.cpp
diff --git a/clang/bindings/python/tests/cindex/INPUTS/parse_arguments.c b/clang/test/bindings/python/tests/cindex/INPUTS/parse_arguments.c
similarity index 100%
rename from clang/bindings/python/tests/cindex/INPUTS/parse_arguments.c
rename to clang/test/bindings/python/tests/cindex/INPUTS/parse_arguments.c
diff --git a/clang/bindings/python/tests/cindex/INPUTS/testfile.c b/clang/test/bindings/python/tests/cindex/INPUTS/testfile.c
similarity index 100%
rename from clang/bindings/python/tests/cindex/INPUTS/testfile.c
rename to clang/test/bindings/python/tests/cindex/INPUTS/testfile.c
diff --git a/clang/bindings/python/tests/cindex/__init__.py b/clang/test/bindings/python/tests/cindex/__init__.py
similarity index 100%
rename from clang/bindings/python/tests/cindex/__init__.py
rename to clang/test/bindings/python/tests/cindex/__init__.py
diff --git a/clang/bindings/python/tests/cindex/test_access_specifiers.py b/clang/test/bindings/python/tests/cindex/test_access_specifiers.py
similarity index 100%
rename from clang/bindings/python/tests/cindex/test_access_specifiers.py
rename to clang/test/bindings/python/tests/cindex/test_access_specifiers.py
diff --git a/clang/bindings/python/tests/cindex/test_cdb.py b/clang/test/bindings/python/tests/cindex/test_cdb.py
similarity index 100%
rename from clang/bindings/python/tests/cindex/test_cdb.py
rename to clang/test/bindings/python/tests/cindex/test_cdb.py
diff --git a/clang/bindings/python/tests/cindex/test_code_completion.py b/clang/test/bindings/python/tests/cindex/test_code_completion.py
similarity index 100%
rename from clang/bindings/python/tests/cindex/test_code_completion.py
rename to clang/test/bindings/python/tests/cindex/test_code_completion.py
diff --git a/clang/bindings/python/tests/cindex/test_comment.py b/clang/test/bindings/python/tests/cindex/test_comment.py
similarity index 100%
rename from clang/bindings/python/tests/cindex/test_comment.py
rename to clang/test/bindings/python/tests/cindex/test_comment.py
diff --git a/clang/bindings/python/tests/cindex/test_cursor.py b/clang/test/bindings/python/tests/cindex/test_cursor.py
similarity index 100%
rename from clang/bindings/python/tests/cindex/test_cursor.py
rename to clang/test/bindings/python/tests/cindex/test_cursor.py
diff --git a/clang/bindings/python/tests/cindex/test_cursor_kind.py b/clang/test/bindings/python/tests/cindex/test_cursor_kind.py
similarity index 100%
rename from clang/bindings/python/tests/cindex/test_cursor_kind.py
rename to clang/test/bindings/python/tests/cindex/test_cursor_kind.py
diff --git a/clang/bindings/python/tests/cindex/test_diagnostics.py b/clang/test/bindings/python/tests/cindex/test_diagnostics.py
similarity index 100%
rename from clang/bindings/python/tests/cindex/test_diagnostics.py
rename to clang/test/bindings/python/tests/cindex/test_diagnostics.py
diff --git a/clang/bindings/python/tests/cindex/test_enums.py b/clang/test/bindings/python/tests/cindex/test_enums.py
similarity index 100%
rename from clang/bindings/python/tests/cindex/test_enums.py
rename to clang/test/bindings/python/tests/cindex/test_enums.py
diff --git a/clang/bindings/python/tests/cindex/test_exception_specification_kind.py b/clang/test/bindings/python/tests/cindex/test_exception_specification_kind.py
similarity index 100%
rename from clang/bindings/python/tests/cindex/test_exception_specification_kind.py
rename to clang/test/bindings/python/tests/cindex/test_exception_specification_kind.py
diff --git a/clang/bindings/python/tests/cindex/test_file.py b/clang/test/bindings/python/tests/cindex/test_file.py
similarity index 100%
rename from clang/bindings/python/tests/cindex/test_file.py
rename to clang/test/bindings/python/tests/cindex/test_file.py
diff --git a/clang/bindings/python/tests/cindex/test_index.py b/clang/test/bindings/python/tests/cindex/test_index.py
similarity index 100%
rename from clang/bindings/python/tests/cindex/test_index.py
rename to clang/test/bindings/python/tests/cindex/test_index.py
diff --git a/clang/bindings/python/tests/cindex/test_lib.py b/clang/test/bindings/python/tests/cindex/test_lib.py
similarity index 100%
rename from clang/bindings/python/tests/cindex/test_lib.py
rename to clang/test/bindings/python/tests/cindex/test_lib.py
diff --git a/clang/bindings/python/tests/cindex/test_linkage.py b/clang/test/bindings/python/tests/cindex/test_linkage.py
similarity index 100%
rename from clang/bindings/python/tests/cindex/test_linkage.py
rename to clang/test/bindings/python/tests/cindex/test_linkage.py
diff --git a/clang/bindings/python/tests/cindex/test_location.py b/clang/test/bindings/python/tests/cindex/test_location.py
similarity index 100%
rename from clang/bindings/python/tests/cindex/test_location.py
rename to clang/test/bindings/python/tests/cindex/test_location.py
diff --git a/clang/bindings/python/tests/cindex/test_rewrite.py b/clang/test/bindings/python/tests/cindex/test_rewrite.py
similarity index 100%
rename from clang/bindings/python/tests/cindex/test_rewrite.py
rename to clang/test/bindings/python/tests/cindex/test_rewrite.py
diff --git a/clang/bindings/python/tests/cindex/test_source_range.py b/clang/test/bindings/python/tests/cindex/test_source_range.py
similarity index 100%
rename from clang/bindings/python/tests/cindex/test_source_range.py
rename to clang/test/bindings/python/tests/cindex/test_source_range.py
diff --git a/clang/bindings/python/tests/cindex/test_tls_kind.py b/clang/test/bindings/python/tests/cindex/test_tls_kind.py
similarity index 100%
rename from clang/bindings/python/tests/cindex/test_tls_kind.py
rename to clang/test/bindings/python/tests/cindex/test_tls_kind.py
diff --git a/clang/bindings/python/tests/cindex/test_token_kind.py b/clang/test/bindings/python/tests/cindex/test_token_kind.py
similarity index 100%
rename from clang/bindings/python/tests/cindex/test_token_kind.py
rename to clang/test/bindings/python/tests/cindex/test_token_kind.py
diff --git a/clang/bindings/python/tests/cindex/test_tokens.py b/clang/test/bindings/python/tests/cindex/test_tokens.py
similarity index 100%
rename from clang/bindings/python/tests/cindex/test_tokens.py
rename to clang/test/bindings/python/tests/cindex/test_tokens.py
diff --git a/clang/bindings/python/tests/cindex/test_translation_unit.py b/clang/test/bindings/python/tests/cindex/test_translation_unit.py
similarity index 100%
rename from clang/bindings/python/tests/cindex/test_translation_unit.py
rename to clang/test/bindings/python/tests/cindex/test_translation_unit.py
diff --git a/clang/bindings/python/tests/cindex/test_type.py b/clang/test/bindings/python/tests/cindex/test_type.py
similarity index 100%
rename from clang/bindings/python/tests/cindex/test_type.py
rename to clang/test/bindings/python/tests/cindex/test_type.py
diff --git a/clang/bindings/python/tests/cindex/util.py b/clang/test/bindings/python/tests/cindex/util.py
similarity index 100%
rename from clang/bindings/python/tests/cindex/util.py
rename to clang/test/bindings/python/tests/cindex/util.py

@rorth
Copy link
Collaborator Author

rorth commented Jun 26, 2025

Compared to PR #142948, this addresses the issues found after the initial commit:

  • Instead of calling llvm-config --libdir (which may not be present on some bots), this uses a new substititution.
  • The cross-compilation case is now XFAILed for !native.

So far, this has only been lightly testing by running ninja check-clang-bindings on amd64-pc-solaris2.11. The cross case is yet untested.

@rorth rorth requested review from DeinAlptraum and removed request for DeinAlptraum June 26, 2025 08:47
@rorth
Copy link
Collaborator Author

rorth commented Jun 26, 2025

With the exception of llvm-clang-win-x-armv7l (another cross build), the remaining failures seem all to be XPASSes on AArch64. I guess it's time to just remove aarch64 from the XFAIL in bindings.sh then, something that would never have been noticed with the old RUN_PYTHON_TESTS approach where the test would have remained disabled permanently. I've augmented my patch locally, but will wait if anything else comes up before pushing.

@DeinAlptraum
Copy link
Contributor

the remaining failures seem all to be XPASSes on AArch64. I guess it's time to just remove aarch64 from the XFAIL in bindings.sh then

Sounds great! Two more things from my side:

  1. Please resolve the conflict so CI can run.

  2. You removed the changed CI trigger path again (clang/tests/bindings... or sth in libclang-python-tests.yml, can't check rn since I'm on mobile). I only noticed after merging yesterday, please add that one back in so the CI id triggered also when the tests in their new location are changed

rorth added 5 commits June 26, 2025 15:42
As discussed in PR llvm#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 llvm#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`.
- Handle `LLVM_USE_SANITIZER` in `lit.local.cfg`.
- Fix SPARC handling, restrict to Linux/sparc*.
- Explain failure modes, log unhandled ones.
Reintroduce check-clang-python.
- Replace llvm-config --libdir by %libdir substitution.
@rorth
Copy link
Collaborator Author

rorth commented Jun 26, 2025

the remaining failures seem all to be XPASSes on AArch64. I guess it's time to just remove aarch64 from the XFAIL in bindings.sh then

Sounds great! Two more things from my side:

1. Please resolve the conflict so CI can run.

Done now.

2. You removed the changed CI trigger path again (clang/tests/bindings... or sth in libclang-python-tests.yml, can't check rn since I'm on mobile). I only noticed after merging yesterday, please add that one back in so the CI id triggered also when the tests in their new location are changed

Right, I accidentally reverted all changes to libclang-python-tests.yml instead of just the new run: statement. Fixed now.

Copy link
Contributor

@DeinAlptraum DeinAlptraum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!
LGTM, just wait for the libclang CI to pass

@rorth rorth merged commit 90c9cc2 into llvm:main Jun 26, 2025
10 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jun 26, 2025

LLVM Buildbot has detected a new failure on builder llvm-clang-x86_64-sie-ubuntu-fast running on sie-linux-worker while building .github,clang at step 6 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/144/builds/28623

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-all) failure: test (failure)
...
PASS: Clang Tools :: clang-tidy/checkers/bugprone/pointer-arithmetic-on-polymorphic-object-decl-only.cpp (21682 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/return-const-ref-from-parameter.cpp (21683 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/signed-char-misuse-fsigned-char.cpp (21684 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/sizeof-expression-pointer-arithmetics-no-division.c (21685 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/not-null-terminated-result-memcpy-safe-cxx.cpp (21686 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/narrowing-conversions-ignoreconversionfromtypes-option.cpp (21687 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/signal-handler-minimal.c (21688 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/signed-char-misuse-with-option.cpp (21689 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/not-null-terminated-result-wcslen.cpp (21690 of 85173)
XPASS: Clang :: bindings/python/bindings.sh (21691 of 85173)
******************** TEST 'Clang :: bindings/python/bindings.sh' FAILED ********************
Exit Code: 0

Command Output (stderr):
--
env PYTHONPATH=/home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/llvm-project/clang/test/bindings/python/../../../bindings/python    CLANG_LIBRARY_PATH=/home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/build/lib    "/usr/bin/python3.10" -m unittest discover -s /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/llvm-project/clang/test/bindings/python/tests # RUN: at line 31
+ env PYTHONPATH=/home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/llvm-project/clang/test/bindings/python/../../../bindings/python CLANG_LIBRARY_PATH=/home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/build/lib /usr/bin/python3.10 -m unittest discover -s /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/llvm-project/clang/test/bindings/python/tests
.....................................................................................................................................................................
----------------------------------------------------------------------
Ran 165 tests in 2.664s

OK

--

********************
PASS: Clang Tools :: clang-tidy/checkers/bugprone/undelegated-constructor-cxx98.cpp (21692 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/lambda-function-name.cpp (21693 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/signed-char-misuse-funsigned-char.cpp (21694 of 85173)
UNSUPPORTED: Clang Tools :: clang-tidy/checkers/bugprone/unsafe-functions.c (21695 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/signal-handler.c (21696 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/posix-return.cpp (21697 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/infinite-loop-noreturn.mm (21698 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/no-escape.m (21699 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/not-null-terminated-result-wmemcpy-safe-cxx.cpp (21700 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/reserved-identifier-invert.cpp (21701 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/narrowing-conversions-narrowinginteger-option.cpp (21702 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/narrowing-conversions-intemplates-option.cpp (21703 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/not-null-terminated-result-strlen.c (21704 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/not-null-terminated-result-memcpy-safe.c (21705 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/suspicious-include.cpp (21706 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/sizeof-container.cpp (21707 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/multi-level-implicit-pointer-conversion.c (21708 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/unused-raii-crash.mm (21709 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/shared-ptr-array-mismatch.cpp (21710 of 85173)
PASS: Clang :: utils/update_cc_test_checks/check-globals.test (21711 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/suspicious-memory-comparison-32bits.cpp (21712 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/narrowing-conversions-equivalentbitwidth-option.cpp (21713 of 85173)
PASS: Clang Tools :: clang-tidy/checkers/bugprone/infinite-loop.mm (21714 of 85173)

@rorth
Copy link
Collaborator Author

rorth commented Jun 26, 2025

The llvm-clang-x86_64-sie-ubuntu-fast builder is an other cross setup:

      LLVM host triple: x86_64-unknown-linux-gnu
      LLVM default target triple: x86_64-scei-ps4

I strongly suspect we need REQUIRES: native rather than XFAIl: !native.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:as-a-library libclang and C++ API clang Clang issues not falling into any other category github:workflow
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants