Skip to content

Conversation

cjdb
Copy link
Contributor

@cjdb cjdb commented Aug 20, 2025

The default standard library for Clang on Linux is libstdc++. As such, testing libc++ with a clang-tidy that's been built with libc++ results in a host of linker errors, because the libc++ clang-tidy plugin is built with the compiler default.

This commit alleviates the problem by allowing the developer to choose which implementation the plugin should be built with.

The default standard library for Clang on Linux is libstdc++. As such,
testing libc++ with a clang-tidy that's been built with libc++ results
in a host of linker errors, because the libc++ clang-tidy plugin is
built with the compiler default.

This commit alleviates the problem by allowing the developer to choose
which implementation the plugin should be built with.
@cjdb cjdb requested a review from a team as a code owner August 20, 2025 16:48
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Aug 20, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 20, 2025

@llvm/pr-subscribers-libcxx

Author: Christopher Di Bella (cjdb)

Changes

The default standard library for Clang on Linux is libstdc++. As such, testing libc++ with a clang-tidy that's been built with libc++ results in a host of linker errors, because the libc++ clang-tidy plugin is built with the compiler default.

This commit alleviates the problem by allowing the developer to choose which implementation the plugin should be built with.


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

2 Files Affected:

  • (modified) libcxx/CMakeLists.txt (+3)
  • (modified) libcxx/test/tools/clang_tidy_checks/CMakeLists.txt (+7)
diff --git a/libcxx/CMakeLists.txt b/libcxx/CMakeLists.txt
index 85514cc7547a9..8c3b9c289f91a 100644
--- a/libcxx/CMakeLists.txt
+++ b/libcxx/CMakeLists.txt
@@ -186,6 +186,9 @@ cmake_dependent_option(LIBCXX_INSTALL_STATIC_LIBRARY
 cmake_dependent_option(LIBCXX_INSTALL_SHARED_LIBRARY
   "Install the shared libc++ library." ON
   "LIBCXX_ENABLE_SHARED;LIBCXX_INSTALL_LIBRARY" OFF)
+set(LIBCXX_BUILD_CLANG_TIDY_PLUGIN_WITH_STDLIB "" CACHE STRING
+  "Builds the clang-tidy plugin with the specified standard library implementation. Must either be blank (use compiler default), 'libc++', or 'libstdc++'.
+   This should only be set when the clang-tidy binary is built with a different standard library implementation to the compiler's default choice.")
 
 option(LIBCXX_ABI_UNSTABLE "Use the unstable ABI of libc++. This is equivalent to specifying LIBCXX_ABI_VERSION=n, where n is the not-yet-stable version." OFF)
 if (LIBCXX_ABI_UNSTABLE)
diff --git a/libcxx/test/tools/clang_tidy_checks/CMakeLists.txt b/libcxx/test/tools/clang_tidy_checks/CMakeLists.txt
index 521a60c0fc498..1f796fb4005b0 100644
--- a/libcxx/test/tools/clang_tidy_checks/CMakeLists.txt
+++ b/libcxx/test/tools/clang_tidy_checks/CMakeLists.txt
@@ -103,6 +103,13 @@ set(SOURCES
    )
 
 add_library(cxx-tidy MODULE ${SOURCES})
+if(LIBCXX_BUILD_CLANG_TIDY_PLUGIN_WITH_STDLIB)
+  if(LIBCXX_BUILD_CLANG_TIDY_PLUGIN_WITH_STDLIB STREQUAL "libc++" OR LIBCXX_BUILD_CLANG_TIDY_PLUGIN_WITH_STDLIB STREQUAL "libstdc++")
+    target_compile_options(cxx-tidy PUBLIC -stdlib=${LIBCXX_BUILD_CLANG_TIDY_PLUGIN_WITH_STDLIB})
+  else()
+    message(ERROR "${LIBCXX_BUILD_CLANG_TIDY_PLUGIN_WITH_STDLIB} must either be empty, 'libc++', or 'libstdc++'.")
+  endif()
+endif()
 target_link_libraries(cxx-tidy clangTidy)
 
 set_target_properties(cxx-tidy PROPERTIES

@cjdb
Copy link
Contributor Author

cjdb commented Aug 20, 2025

Hmm, I'm wondering if this solution is adequate, given that I run into the issue when running libcxx/utils/ci/run-buildbot.

Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

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

We already build the clang tidy plugin using the system compiler and we don't force the usage of -stdlib=libc++ -- so I don't think this patch should be necessary. Instead, I think we should have a look at the errors you were running into and try to figure out what's going on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants