[libc-commits] [PATCH] D80265: [libc] Make clang-tidy use host compiler's resource dir.

Paula Toth via Phabricator via libc-commits libc-commits at lists.llvm.org
Thu May 21 16:49:05 PDT 2020


PaulkaToast updated this revision to Diff 265643.
PaulkaToast marked 2 inline comments as done.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D80265/new/

https://reviews.llvm.org/D80265

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


Index: libc/cmake/modules/LLVMLibCObjectRules.cmake
===================================================================
--- libc/cmake/modules/LLVMLibCObjectRules.cmake
+++ libc/cmake/modules/LLVMLibCObjectRules.cmake
@@ -206,8 +206,30 @@
   )
 
   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 @@
               # 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
Index: libc/cmake/modules/LLVMLibCHeaderRules.cmake
===================================================================
--- libc/cmake/modules/LLVMLibCHeaderRules.cmake
+++ libc/cmake/modules/LLVMLibCHeaderRules.cmake
@@ -91,7 +91,7 @@
             ${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
   )
 
Index: libc/CMakeLists.txt
===================================================================
--- libc/CMakeLists.txt
+++ libc/CMakeLists.txt
@@ -19,6 +19,27 @@
 
 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 1)
+  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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80265.265643.patch
Type: text/x-patch
Size: 4269 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20200521/fb58268f/attachment-0001.bin>


More information about the libc-commits mailing list