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

Paula Toth via libc-commits libc-commits at lists.llvm.org
Thu May 21 18:41:33 PDT 2020


Author: Paula Toth
Date: 2020-05-21T18:40:59-07:00
New Revision: 2a4c30985ded329d64357f5535d2ff2a3f955c81

URL: https://github.com/llvm/llvm-project/commit/2a4c30985ded329d64357f5535d2ff2a3f955c81
DIFF: https://github.com/llvm/llvm-project/commit/2a4c30985ded329d64357f5535d2ff2a3f955c81.diff

LOG: [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

Added: 
    

Modified: 
    libc/CMakeLists.txt
    libc/cmake/modules/LLVMLibCHeaderRules.cmake
    libc/cmake/modules/LLVMLibCObjectRules.cmake

Removed: 
    


################################################################################
diff  --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt
index 499566370407..ce78d0a5aebf 100644
--- a/libc/CMakeLists.txt
+++ b/libc/CMakeLists.txt
@@ -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

diff  --git a/libc/cmake/modules/LLVMLibCHeaderRules.cmake b/libc/cmake/modules/LLVMLibCHeaderRules.cmake
index da61f27b9590..072203c737fa 100644
--- a/libc/cmake/modules/LLVMLibCHeaderRules.cmake
+++ b/libc/cmake/modules/LLVMLibCHeaderRules.cmake
@@ -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
   )
 

diff  --git a/libc/cmake/modules/LLVMLibCObjectRules.cmake b/libc/cmake/modules/LLVMLibCObjectRules.cmake
index 2b7b53778de1..898831ca6818 100644
--- a/libc/cmake/modules/LLVMLibCObjectRules.cmake
+++ b/libc/cmake/modules/LLVMLibCObjectRules.cmake
@@ -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


        


More information about the libc-commits mailing list