[libc-commits] [libc] b2a485e - [libc] Fix accidental inclusion of system libc headers.

Paula Toth via libc-commits libc-commits at lists.llvm.org
Thu May 21 01:42:58 PDT 2020


Author: Paula Toth
Date: 2020-05-21T01:21:37-07:00
New Revision: b2a485e37edf6415701c472d93afa7fa26dbcccb

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

LOG: [libc] Fix accidental inclusion of system libc headers.

Summary:
I found that because `--system-headers` flag was not included when running clang-tidy, errors produced from compiler provided headers were being suppressed. After passing this flag I realized that by including headers like stdint.h we were indirectly including headers from the system libc. To prevent this we pass `-ffreestanding`.
We don't want to pass `--system-headers` for all checks just the `llvmlibc-restrict-system-libc-headers` therefore we do a separate invocation of clang-tidy for this check.

Reviewers: abrachet, sivachandra

Reviewed By: sivachandra

Subscribers: mgorny, aheejin, tschuett, ecnelises, libc-commits

Tags: #libc-project

Differential Revision: https://reviews.llvm.org/D80178

Added: 
    

Modified: 
    libc/cmake/modules/LLVMLibCObjectRules.cmake
    libc/src/.clang-tidy

Removed: 
    


################################################################################
diff  --git a/libc/cmake/modules/LLVMLibCObjectRules.cmake b/libc/cmake/modules/LLVMLibCObjectRules.cmake
index e4bf15723129..2b7b53778de1 100644
--- a/libc/cmake/modules/LLVMLibCObjectRules.cmake
+++ b/libc/cmake/modules/LLVMLibCObjectRules.cmake
@@ -144,7 +144,7 @@ function(add_entrypoint_object target_name)
     ${objects_target_name}
     BEFORE
     PRIVATE
-      -fpie ${LLVM_CXX_STD_default}
+      -fpie ${LLVM_CXX_STD_default} -ffreestanding
   )
   target_include_directories(
     ${objects_target_name}
@@ -217,10 +217,18 @@ function(add_entrypoint_object target_name)
       #     X warnings generated.
       # Until this is fixed upstream, we use -fno-caret-diagnostics to surpress
       # these.
-      COMMAND $<TARGET_FILE:clang-tidy> "--extra-arg=-fno-caret-diagnostics" --quiet
+      COMMAND $<TARGET_FILE:clang-tidy>
+              "--extra-arg=-fno-caret-diagnostics" --quiet
               # 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}
       # 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

diff  --git a/libc/src/.clang-tidy b/libc/src/.clang-tidy
index 6d6043a11a3a..29f78fdfbcba 100644
--- a/libc/src/.clang-tidy
+++ b/libc/src/.clang-tidy
@@ -3,4 +3,4 @@ HeaderFilterRegex: '.*'
 WarningsAsErrors: 'llvmlibc-*'
 CheckOptions:
   - key:             llvmlibc-restrict-system-libc-headers.Includes
-    value:           '-*, linux/*, asm/unistd.h'
+    value:           '-*, linux/*, asm/*.h, asm-generic/*.h'


        


More information about the libc-commits mailing list