[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

GitOrigin-RevId: 2a4c30985ded329d64357f5535d2ff2a3f955c81
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4995663..ce78d0a 100644
--- a/CMakeLists.txt
+++ b/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 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/cmake/modules/LLVMLibCHeaderRules.cmake b/cmake/modules/LLVMLibCHeaderRules.cmake
index da61f27..072203c 100644
--- a/cmake/modules/LLVMLibCHeaderRules.cmake
+++ b/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
   )
 
diff --git a/cmake/modules/LLVMLibCObjectRules.cmake b/cmake/modules/LLVMLibCObjectRules.cmake
index 2b7b537..898831c 100644
--- a/cmake/modules/LLVMLibCObjectRules.cmake
+++ b/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