[libc] Make clang-tidy use host compiler's resource dir.

Summary: When building llvm-libc with linting enabled, clang-tidy would use the resource dir of the monorepo rather then the host compiler's resource dir. This presented issues when including headers from the host compiler e.g. for sanitizers. Therefore this patch explicitly tells clang-tidy to use the host compiler's resource dir.

Reviewers: sivachandra

Reviewed By: sivachandra

Subscribers: mgorny, tschuett, ecnelises, libc-commits

Tags: #libc-project

Differential Revision: https://reviews.llvm.org/D80265
This commit is contained in:
Paula Toth 2020-05-21 17:39:03 -07:00
parent 54c2c2add7
commit 2a4c30985d
3 changed files with 48 additions and 9 deletions

View File

@ -19,6 +19,27 @@ string(TOLOWER ${LIBC_TARGET_OS} LIBC_TARGET_OS)
set(LIBC_TARGET_MACHINE ${CMAKE_SYSTEM_PROCESSOR})
# Check --print-resource-dir to find the compiler resource dir if this flag
# is supported by the compiler.
execute_process(
OUTPUT_STRIP_TRAILING_WHITESPACE
COMMAND ${CMAKE_CXX_COMPILER} --print-resource-dir
RESULT_VARIABLE COMMAND_RETURN_CODE
OUTPUT_VARIABLE COMPILER_RESOURCE_DIR
)
# Retrieve the host compiler's resource dir.
if(COMMAND_RETURN_CODE EQUAL 0)
set(COMPILER_RESOURCE_DIR
"${COMPILER_RESOURCE_DIR}" CACHE PATH "path to compiler resource dir"
)
message(STATUS "Set COMPILER_RESOURCE_DIR to
${COMPILER_RESOURCE_DIR} using --print-resource-dir")
else()
set(COMPILER_RESOURCE_DIR OFF)
message(STATUS "COMPILER_RESOURCE_DIR not set
--print-resource-dir not supported by host compiler")
endif()
option(LLVM_LIBC_ENABLE_LINTING "Enables linting of libc source files" ON)
if(LLVM_LIBC_ENABLE_LINTING)
if("clang-tools-extra" IN_LIST LLVM_ENABLE_PROJECTS

View File

@ -91,7 +91,7 @@ function(add_gen_header target_name)
${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/api.td
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${in_file} ${fq_data_files} ${td_includes}
DEPENDS ${in_file} ${fq_data_files} ${td_includes}
${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/api.td libc-hdrgen
)

View File

@ -206,8 +206,30 @@ function(add_entrypoint_object target_name)
)
if(LLVM_LIBC_ENABLE_LINTING)
set(lint_timestamp "${CMAKE_CURRENT_BINARY_DIR}/.${target_name}.__lint_timestamp__")
# We only want a second invocation of clang-tidy to run
# restrict-system-libc-headers if the compiler-resource-dir was set in
# order to prevent false-positives due to a mismatch between the host
# compiler and the compiled clang-tidy.
if(COMPILER_RESOURCE_DIR)
# We run restrict-system-libc-headers with --system-headers to prevent
# transitive inclusion through compler provided headers.
set(restrict_system_headers_check_invocation
COMMAND $<TARGET_FILE:clang-tidy> --system-headers
--checks="-*,llvmlibc-restrict-system-libc-headers"
# We explicitly set the resource dir here to match the
# resource dir of the host compiler.
"--extra-arg=-resource-dir=${COMPILER_RESOURCE_DIR}"
--quiet
-p ${PROJECT_BINARY_DIR}
${ADD_ENTRYPOINT_OBJ_SRCS}
)
else()
set(restrict_system_headers_check_invocation
COMMAND ${CMAKE_COMMAND} -E echo "Header file check skipped")
endif()
set(lint_timestamp "${CMAKE_CURRENT_BINARY_DIR}/.${target_name}.__lint_timestamp__")
add_custom_command(
OUTPUT ${lint_timestamp}
# --quiet is used to surpress warning statistics from clang-tidy like:
@ -222,13 +244,9 @@ function(add_entrypoint_object target_name)
# Path to directory containing compile_commands.json
-p ${PROJECT_BINARY_DIR}
${ADD_ENTRYPOINT_OBJ_SRCS}
# We run restrict-system-libc-headers with --system-headers to prevent
# transitive inclusion through compler provided headers.
COMMAND $<TARGET_FILE:clang-tidy> --system-headers
--checks="-*,llvmlibc-restrict-system-libc-headers"
--quiet
-p ${PROJECT_BINARY_DIR}
${ADD_ENTRYPOINT_OBJ_SRCS}
# See above: this might be a second invocation of clang-tidy depending on
# the conditions above.
${restrict_system_headers_check_invocation}
# We have two options for running commands, add_custom_command and
# add_custom_target. We don't want to run the linter unless source files
# have changed. add_custom_target explicitly runs everytime therefore we