[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
Tue May 19 19:49:22 PDT 2020


PaulkaToast created this revision.
PaulkaToast added a reviewer: sivachandra.
PaulkaToast added a project: libc-project.
Herald added subscribers: libc-commits, ecnelises, tschuett, mgorny.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D80265

Files:
  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
@@ -207,7 +207,7 @@
 
   if(LLVM_LIBC_ENABLE_LINTING)
     set(lint_timestamp "${CMAKE_CURRENT_BINARY_DIR}/.${target_name}.__lint_timestamp__")
-
+    get_compiler_resource_dir()
     add_custom_command(
       OUTPUT ${lint_timestamp}
       # --quiet is used to surpress warning statistics from clang-tidy like:
@@ -218,8 +218,11 @@
       # Until this is fixed upstream, we use -fno-caret-diagnostics to surpress
       # these.
       COMMAND $<TARGET_FILE:clang-tidy> --system-headers
-              "--extra-arg=-fno-caret-diagnostics" --quiet
-              # Path to directory containing compile_commands.json
+              "--extra-arg=-fno-caret-diagnostics"
+              # 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}
       # We have two options for running commands, add_custom_command and
Index: libc/cmake/modules/LLVMLibCHeaderRules.cmake
===================================================================
--- libc/cmake/modules/LLVMLibCHeaderRules.cmake
+++ libc/cmake/modules/LLVMLibCHeaderRules.cmake
@@ -104,3 +104,37 @@
     DEPENDS ${out_file} ${fq_deps_list}
   )
 endfunction(add_gen_header)
+
+# This function is use to retrieve the host compiler's resource dir.
+function(get_compiler_resource_dir)
+  if(COMPILER_RESOURCE_DIR)
+    return()
+  endif()
+
+  # Use --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
+  )
+  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")
+    return()
+  endif()
+
+  # Fallback if --print-resource-dir is not supported by the compiler.
+  # TODO, see if there is a better way to do this.
+  find_path(stddef_file_loc stddef.h)
+  set(COMPILER_RESOURCE_DIR
+    "${stddef_file_loc}/../" CACHE PATH "path to compiler resource dir"
+  )
+  message(STATUS "Set COMPILER_RESOURCE_DIR to
+                  ${COMPILER_RESOURCE_DIR} using location of stddef.h")
+
+endfunction(get_compiler_resource_dir)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80265.265112.patch
Type: text/x-patch
Size: 2787 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20200520/5c334959/attachment.bin>


More information about the libc-commits mailing list