Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run clang-include-cleaner as a pass of clang-extract #39

Open
giulianobelinassi opened this issue Jun 9, 2024 · 1 comment
Open

Run clang-include-cleaner as a pass of clang-extract #39

giulianobelinassi opened this issue Jun 9, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@giulianobelinassi
Copy link
Collaborator

giulianobelinassi commented Jun 9, 2024

In the package clang-tools there is an application named clang-include-cleaner, which is supposed to remove unnecessary headers, as well as adding some system header the code uses. This may help us reduce the code output by removing unnecessary headers.

Example of how to run the program:

#!/bin/bash
clang-include-cleaner \
 --extra-arg="-Wp,-MMD,drivers/gpu/drm/vmwgfx/.vmwgfx_execbuf.o.d" \
 --extra-arg="-nostdinc" \
 --extra-arg="-isystem" \
 --extra-arg="/usr/lib64/gcc/x86_64-suse-linux/7/include" \
 --extra-arg="-Iarch/x86/include" \
 --extra-arg="-I./arch/x86/include/generated" \
 --extra-arg="-Iinclude" \
 --extra-arg="-I./include" \
 --extra-arg="-Iarch/x86/include/uapi" \
 --extra-arg="-I./arch/x86/include/generated/uapi" \
 --extra-arg="-Iinclude/uapi" \
 --extra-arg="-I./include/generated/uapi" \
 --extra-arg="-include" \
 --extra-arg="include/linux/compiler-version.h" \
 --extra-arg="-include" \
 --extra-arg="include/linux/kconfig.h" \
 --extra-arg="-include" \
 --extra-arg="include/linux/compiler_types.h" \
 --extra-arg="-D__KERNEL__" \
 --extra-arg="-Wall" \
 --extra-arg="-Wundef" \
 --extra-arg="-Werror=strict-prototypes" \
 --extra-arg="-Wno-trigraphs" \
 --extra-arg="-fno-strict-aliasing" \
 --extra-arg="-fno-common" \
 --extra-arg="-fshort-wchar" \
 --extra-arg="-fno-PIE" \
 --extra-arg="-Werror=implicit-function-declaration" \
 --extra-arg="-Werror=implicit-int" \
 --extra-arg="-Werror=return-type" \
 --extra-arg="-Wno-format-security" \
 --extra-arg="-std=gnu11" \
 --extra-arg="-mno-sse" \
 --extra-arg="-mno-mmx" \
 --extra-arg="-mno-sse2" \
 --extra-arg="-mno-3dnow" \
 --extra-arg="-mno-avx" \
 --extra-arg="-m64" \
 --extra-arg="-falign-jumps=1" \
 --extra-arg="-falign-loops=1" \
 --extra-arg="-mno-80387" \
 --extra-arg="-mno-fp-ret-in-387" \
 --extra-arg="-mskip-rax-setup" \
 --extra-arg="-mtune=generic" \
 --extra-arg="-mno-red-zone" \
 --extra-arg="-mcmodel=kernel" \
 --extra-arg="-Wno-sign-compare" \
 --extra-arg="-fno-asynchronous-unwind-tables" \
 --extra-arg="-mfunction-return=thunk-extern" \
 --extra-arg="-fno-jump-tables" \
 --extra-arg="-fno-delete-null-pointer-checks" \
 --extra-arg="-Wno-frame-address" \
 --extra-arg="-Wno-format-truncation" \
 --extra-arg="-Wno-format-overflow" \
 --extra-arg="-O2" \
 --extra-arg="-Wframe-larger-than=2048" \
 --extra-arg="-fstack-protector-strong" \
 --extra-arg="-Wimplicit-fallthrough=5" \
 --extra-arg="-Wno-main" \
 --extra-arg="-Wno-unused-but-set-variable" \
 --extra-arg="-Wno-unused-const-variable" \
 --extra-arg="-fno-stack-clash-protection" \
 --extra-arg="-g" \
 --extra-arg="-gdwarf-4" \
 --extra-arg="-pg" \
 --extra-arg="-mfentry" \
 --extra-arg="-DCC_USING_FENTRY" \
 --extra-arg="-Wdeclaration-after-statement" \
 --extra-arg="-Wvla" \
 --extra-arg="-Wno-pointer-sign" \
 --extra-arg="-Wno-array-bounds" \
 --extra-arg="-Wno-stringop-overflow" \
 --extra-arg="-Wno-restrict" \
 --extra-arg="-Wno-maybe-uninitialized" \
 --extra-arg="-fno-strict-overflow" \
 --extra-arg="-fno-stack-check" \
 --extra-arg="-Werror=date-time" \
 --extra-arg="-Werror=incompatible-pointer-types" \
 --extra-arg="-Werror=designated-init" \
 --extra-arg="-I" \
 --extra-arg=" drivers/gpu/drm/vmwgfx" \
 --extra-arg="-I" \
 --extra-arg="./drivers/gpu/drm/vmwgfx" \
 --extra-arg="-DMODULE" \
 --extra-arg="-DKBUILD_BASENAME="vmwgfx_execbuf"" \
 --extra-arg="-DKBUILD_MODNAME="vmwgfx"" \
 --extra-arg="-D__KBUILD_MODNAME=kmod_vmwgfx" \
 /tmp/output.c \
 --insert \
 --remove \
 --print

Here is the diff output when run on top of what clang-extract did:

diff --git a/tmp/output.c b/tmp/clean.c
index e6465da..c90c19e 100644
--- a/tmp/output.c
+++ b/tmp/clean.c
@@ -1,28 +1,45 @@
+#include "asm-generic/bitops/instrumented-non-atomic.h"
+#include "asm-generic/bug.h"
+#include "asm-generic/fcntl.h"
+#include "asm-generic/int-ll64.h"
+#include "asm-generic/param.h"
+#include "drm/drm_mode_config.h"
+#include "drm/ttm/ttm_resource.h"
+#include "linux/build_bug.h"
+#include "linux/compiler_types.h"
+#include "linux/container_of.h"
+#include "linux/err.h"
+#include "linux/errno.h"
+#include "linux/file.h"
+#include "linux/idr.h"
+#include "linux/kconfig.h"
+#include "linux/minmax.h"
+#include "linux/rbtree_types.h"
+#include "linux/scatterlist.h"
+#include "linux/spinlock_types.h"
+#include "linux/stddef.h"
+#include "linux/uaccess.h"
+#include "linux/vmalloc.h"
+#include "vdso/bits.h"
+#include <cerrno>
+#include <cstddef>
 #include <linux/sync_file.h>
 #include <linux/hashtable.h>
-#include <linux/suspend.h>
 #include <linux/sync_file.h>
 #include <linux/hashtable.h>
 #include <drm/drm_auth.h>
 #include <drm/drm_device.h>
 #include <drm/drm_file.h>
-#include <drm/drm_rect.h>
+#include <stdint.h>
+#include <string.h>
 #define _TTM_BO_DRIVER_H_
 #define _DRM_MM_H_
-#include <linux/bug.h>
-#include <linux/rbtree.h>
-#include <linux/limits.h>
 #include <linux/mm_types.h>
 #include <linux/list.h>
-#include <linux/spinlock.h>
 #include <linux/types.h>
 #define DRM_PRINT_H_
 #include <linux/compiler.h>
 #include <linux/printk.h>
-#include <linux/seq_file.h>
-#include <linux/device.h>
-#include <linux/debugfs.h>
-#include <drm/drm.h>
 /** clang-extract: from include/drm/drm_print.h:75:1  */
 struct drm_printer {
 	/* private: */
@@ -120,9 +137,7 @@ static inline bool drm_mm_node_allocated(const struct drm_mm_node *node)
 
 #define drm_mm_nodes(mm) (&(mm)->head_node.node_list)
 #include <drm/drm_vma_manager.h>
-#include <linux/workqueue.h>
 #include <linux/fs.h>
-#include <linux/spinlock.h>
 #include <linux/dma-resv.h>
 #include <drm/ttm/ttm_device.h>
 #define _TTM_BO_API_H_
@@ -133,7 +148,6 @@ static inline bool drm_mm_node_allocated(const struct drm_mm_node *node)
 #include <linux/wait.h>
 #include <linux/mutex.h>
 #include <linux/mm.h>
-#include <linux/bitmap.h>
 #include <linux/dma-resv.h>
 /** clang-extract: from include/drm/ttm/ttm_bo_api.h:54:1  */
 struct ttm_placement;
@@ -243,10 +257,8 @@ struct ttm_place;
 struct ttm_placement;
 
 #define _TTM_TT_H_
-#include <linux/pagemap.h>
 #include <linux/types.h>
 #include <drm/ttm/ttm_caching.h>
-#include <drm/ttm/ttm_kmap_iter.h>
 /** clang-extract: from include/drm/ttm/ttm_tt.h:36:1  */
 struct ttm_tt;
 
@@ -313,7 +325,6 @@ struct ttm_tt {
 	enum ttm_caching caching;
 };
 
-#include <linux/agp_backend.h>
 #define _TTM_EXECBUF_UTIL_H_
 #include <linux/list.h>
 /** clang-extract: from include/drm/ttm/ttm_execbuf_util.h:98:1  */
@@ -322,10 +333,8 @@ static int (*klpe_ttm_eu_reserve_buffers)(struct ww_acquire_ctx *, struct list_h
 /** clang-extract: from include/drm/ttm/ttm_execbuf_util.h:113:1  */
 static void (*klpe_ttm_eu_fence_buffer_objects)(struct ww_acquire_ctx *, struct list_head *, struct dma_fence *);
 
-#include <linux/dma-buf.h>
 #include <linux/kref.h>
 #include <linux/list.h>
-#include <linux/rcupdate.h>
 /** clang-extract: from drivers/gpu/drm/vmwgfx/ttm_object.h:65:8  */
 struct ttm_object_file;
 
@@ -333,7 +342,6 @@ struct ttm_object_file;
 struct ttm_object_device;
 
 #include <linux/dma-fence.h>
-#include <linux/dma-fence-array.h>
 #define VMW_FENCE_WAIT_TIMEOUT (5*HZ)
 /** clang-extract: from drivers/gpu/drm/vmwgfx/vmwgfx_fence.h:39:1  */
 struct vmw_private;
@@ -365,7 +373,6 @@ vmw_fence_obj_unreference(struct vmw_fence_obj **fence_p)
 static int (*klpe_vmw_fence_obj_wait)(struct vmw_fence_obj *, bool, bool, unsigned long);
 
 #include <linux/types.h>
-#include <linux/kernel.h>
 #include <linux/mm.h>
 #include <asm/page.h>
 /** clang-extract: from drivers/gpu/drm/vmwgfx/device_include/vm_basic_types.h:33:1  */
@giulianobelinassi giulianobelinassi added the enhancement New feature or request label Jun 9, 2024
@marcosps
Copy link
Collaborator

Interesting. This might help us to reduce the number of LoC to review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants