Skip to content

[RFC][libc++] Reworks clang-tidy selection. #81362

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 2 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions libcxx/test/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@ set(LIBCXX_TEST_TOOLS_PATH ${CMAKE_CURRENT_BINARY_DIR} PARENT_SCOPE)

# TODO: Remove LIBCXX_ENABLE_CLANG_TIDY
if(LIBCXX_ENABLE_CLANG_TIDY)
if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
message(STATUS "Clang-tidy can only be used when building libc++ with "
"a clang compiler.")
return()
endif()
add_subdirectory(clang_tidy_checks)
endif()
8 changes: 4 additions & 4 deletions libcxx/test/tools/clang_tidy_checks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
set(LLVM_DIR_SAVE ${LLVM_DIR})
set(Clang_DIR_SAVE ${Clang_DIR})

find_package(Clang 18)
if (NOT Clang_FOUND)
find_package(Clang 17)
endif()
# Since the Clang C++ ABI is not stable the Clang libraries and clang-tidy
# versions must match. Otherwise there likely will be ODR-violations. This had
# led to crashes and incorrect output of the clang-tidy based checks.
find_package(Clang ${CMAKE_CXX_COMPILER_VERSION})

set(SOURCES
abi_tag_on_virtual.cpp
Expand Down
31 changes: 0 additions & 31 deletions libcxx/utils/libcxx/test/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,6 @@
_isMSVC = lambda cfg: "_MSC_VER" in compilerMacros(cfg)
_msvcVersion = lambda cfg: (int(compilerMacros(cfg)["_MSC_VER"]) // 100, int(compilerMacros(cfg)["_MSC_VER"]) % 100)


def _getSuitableClangTidy(cfg):
try:
# If we didn't build the libcxx-tidy plugin via CMake, we can't run the clang-tidy tests.
if runScriptExitCode(cfg, ["stat %{test-tools-dir}/clang_tidy_checks/libcxx-tidy.plugin"]) != 0:
return None

# TODO MODULES require ToT due module specific fixes.
if runScriptExitCode(cfg, ['clang-tidy-18 --version']) == 0:
return 'clang-tidy-18'

# TODO This should be the last stable release.
# LLVM RELEASE bump to latest stable version
if runScriptExitCode(cfg, ["clang-tidy-16 --version"]) == 0:
return "clang-tidy-16"

# LLVM RELEASE bump version
if int(re.search("[0-9]+", commandOutput(cfg, ["clang-tidy --version"])).group()) >= 16:
return "clang-tidy"

except ConfigurationRuntimeError:
return None


def _getAndroidDeviceApi(cfg):
return int(
programOutput(
Expand Down Expand Up @@ -297,13 +273,6 @@ def _getAndroidDeviceApi(cfg):
name="executor-has-no-bash",
when=lambda cfg: runScriptExitCode(cfg, ["%{exec} bash -c 'bash --version'"]) != 0,
),
Feature(
name="has-clang-tidy",
when=lambda cfg: _getSuitableClangTidy(cfg) is not None,
actions=[
AddSubstitution("%{clang-tidy}", lambda cfg: _getSuitableClangTidy(cfg))
],
),
# Whether module support for the platform is available.
Feature(
name="has-no-cxx-module-support",
Expand Down
43 changes: 42 additions & 1 deletion libcxx/utils/libcxx/test/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,37 @@ def getSizeOptimizationFlag(cfg):
)


def testClangTidy(cfg, version, executable):
try:
if version in commandOutput(cfg, [f"{executable} --version"]):
return executable
except ConfigurationRuntimeError:
return None


def getSuitableClangTidy(cfg):
# If we didn't build the libcxx-tidy plugin via CMake, we can't run the clang-tidy tests.
if (
runScriptExitCode(
cfg, ["stat %{test-tools-dir}/clang_tidy_checks/libcxx-tidy.plugin"]
)
!= 0
):
return None

version = "{__clang_major__}.{__clang_minor__}.{__clang_patchlevel__}".format(
**compilerMacros(cfg)
)
exe = testClangTidy(
cfg, version, "clang-tidy-{__clang_major__}".format(**compilerMacros(cfg))
)

if not exe:
exe = testClangTidy(cfg, version, "clang-tidy")

return exe


# fmt: off
DEFAULT_PARAMETERS = [
Parameter(
Expand Down Expand Up @@ -366,6 +397,16 @@ def getSizeOptimizationFlag(cfg):
default=f"{shlex.quote(sys.executable)} {shlex.quote(str(Path(__file__).resolve().parent.parent.parent / 'run.py'))}",
help="Custom executor to use instead of the configured default.",
actions=lambda executor: [AddSubstitution("%{executor}", executor)],
)
),
Parameter(
name='clang-tidy-executable',
type=str,
default=lambda cfg: getSuitableClangTidy(cfg),
help="Selects the clang-tidy executable to use.",
actions=lambda exe: [] if exe is None else [
AddFeature('has-clang-tidy'),
AddSubstitution('%{clang-tidy}', exe),
]
),
]
# fmt: on