Add missing headers to the project
Subsystems (3)
PATCH 1/1 Add missing headers to the project
The project's headers are not self-contained, as they use some types without explicitly including the corresponding headers with definitions. That makes the overall headers structure fragile: reordering/updating the headers may break the compilation. IDEs also suffer and generate dozens of errors when jumping to some random header file, distracting programmers from more serious code issues.
Some building issue caused by missing headers has arised just recently and it required some local cleanup in a31e7b79 (mptcp: cleanup include section). In this patch I tried to improve the situation for the whole project by adding missing includes for virtually all the project headers, except uapi ones.
--- bridge/br_common.h | 4 ++++ include/cg_map.h | 2 ++ include/color.h | 2 ++ include/json_print.h | 2 ++ include/json_writer.h | 1 + include/ll_map.h | 3 +++ include/mnl_utils.h | 3 +++ include/rtm_map.h | 2 ++ include/xt-internal.h | 2 ++ ip/ila_common.h | 1 + ip/ip_common.h | 7 +++++++ ip/nh_common.h | 6 ++++++ ip/xfrm.h | 3 +++ misc/lnstat.h | 1 + misc/ssfilter.h | 1 + tc/tc_common.h | 5 +++++ tc/tc_red.h | 2 ++ tc/tc_util.h | 3 +++ tipc/bearer.h | 1 + tipc/link.h | 3 +++ tipc/media.h | 3 +++ tipc/misc.h | 2 ++ tipc/msg.h | 2 ++ tipc/nametable.h | 3 +++ tipc/node.h | 3 +++ tipc/peer.h | 3 +++ tipc/socket.h | 3 +++ 27 files changed, 73 insertions(+)
bridge/br_common.h +4 -0
@@ -1,5 +1,9 @@/* SPDX-License-Identifier: GPL-2.0 */ +#include <stdbool.h> +#include <linux/netlink.h> +#include <linux/rtnetlink.h> +#define MDB_RTA (r) \ ((struct rtattr *)(((char *)(r)) + RTA_ALIGN(sizeof(struct br_mdb_entry))))
include/cg_map.h +2 -0
@@ -1,6 +1,8 @@#ifndef __CG_MAP_H__#define __CG_MAP_H__ +#include <linux/types.h> +const char *cg_id_to_path (__u64 id);#endif /* __CG_MAP_H__ */
include/color.h +2 -0
@@ -3,6 +3,8 @@#define __COLOR_H__ 1#include <stdbool.h> +#include <stdio.h> +#include <linux/types.h> enum color_attr {COLOR_IFNAME ,
include/json_print.h +2 -0
@@ -12,6 +12,8 @@#ifndef _JSON_PRINT_H_#define _JSON_PRINT_H_ +#include <stdbool.h> +#include <sys/time.h> #include "json_writer.h" #include "color.h"
include/json_writer.h +1 -0
@@ -13,6 +13,7 @@#include <stdbool.h> #include <stdint.h> +#include <stdio.h> /* Opaque class structure */ typedef struct json_writer json_writer_t ;
include/ll_map.h +3 -0
@@ -2,6 +2,9 @@#ifndef __LL_MAP_H__#define __LL_MAP_H__ 1 +#include <linux/netlink.h> +#include "libnetlink.h" +int ll_remember_index (struct nlmsghdr *n,void *arg);void ll_init_map (struct rtnl_handle *rth);
include/mnl_utils.h +3 -0
@@ -2,6 +2,9 @@ #ifndef __MNL_UTILS_H__ #define __MNL_UTILS_H__ 1 +#include <stdint.h> +#include <libmnl/libmnl.h> + struct mnlu_gen_socket { struct mnl_socket *nl; char *buf;
include/rtm_map.h +2 -0
@@ -2,6 +2,8 @@#ifndef __RTM_MAP_H__#define __RTM_MAP_H__ 1 +#include <linux/types.h> +char *rtnl_rtntype_n2a (int id,char *buf,int len);int rtnl_rtntype_a2n (int *id,char *arg);
include/xt-internal.h +2 -0
@@ -6,6 +6,8 @@# define XT_LIB_DIR "/lib/xtables"#endif +#include <stddef.h> +/* protocol family dependent informations */ struct afinfo {/* protocol family */
ip/ila_common.h +1 -0
@@ -3,6 +3,7 @@#define _ILA_COMMON_H_#include <linux/ila.h> +#include <linux/types.h> #include <string.h> static inline char *ila_csum_mode2name (__u8 csum_mode)
ip/ip_common.h +7 -0
@@ -3,8 +3,15 @@#define _IP_COMMON_H_#include <stdbool.h> +#include <stdio.h> + +#include <linux/netlink.h> +#include <linux/rtnetlink.h> +#include <linux/types.h> #include "json_print.h" +#include "libnetlink.h" +#include "utils.h" extern int use_iec;
ip/nh_common.h +6 -0
@@ -2,7 +2,13 @@#ifndef __NH_COMMON_H__#define __NH_COMMON_H__ 1 +#include <stdbool.h> +#include <stdio.h> #include <list.h> +#include <netinet/in.h> +#include <linux/netlink.h> +#include <linux/rtnetlink.h> +#include <linux/types.h> #define NH_CACHE_SIZE 1024
ip/xfrm.h +3 -0
@@ -24,9 +24,12 @@#ifndef __XFRM_H__#define __XFRM_H__ 1 +#include <stdbool.h> #include <stdio.h> #include <sys/socket.h> #include <linux/in.h> +#include <linux/netlink.h> +#include <linux/rtnetlink.h> #include <linux/xfrm.h> #include <linux/ipsec.h>
misc/lnstat.h +1 -0
@@ -3,6 +3,7 @@#define _LNSTAT_H#include <limits.h> +#include <stdio.h> #include <sys/select.h> #define PROC_NET_STAT "/proc/net/stat"
misc/ssfilter.h +1 -0
@@ -1,4 +1,5 @@#include <stdbool.h> +#include <stdio.h> enum {SSF_DCOND ,
tc/tc_common.h +5 -0
@@ -1,5 +1,10 @@/* SPDX-License-Identifier: GPL-2.0 */ +#include <stdbool.h> +#include <stdio.h> +#include <linux/netlink.h> +#include <linux/rtnetlink.h> +#define TCA_BUF_MAX (64*1024)extern struct rtnl_handle rth;
tc/tc_red.h +2 -0
@@ -2,6 +2,8 @@#ifndef _TC_RED_H_#define _TC_RED_H_ 1 +#include <linux/types.h> +int tc_red_eval_P (unsigned qmin,unsigned qmax,double prob);int tc_red_eval_ewma (unsigned qmin,unsigned burst,unsigned avpkt); int tc_red_eval_idle_damping(int wlog, unsigned avpkt, unsigned bandwidth,
tc/tc_util.h +3 -0
@@ -6,10 +6,13 @@#include <limits.h> #include <linux/if.h> #include <stdbool.h> +#include <stdio.h> #include <linux/pkt_sched.h> #include <linux/pkt_cls.h> #include <linux/gen_stats.h> +#include <linux/netlink.h> +#include <linux/rtnetlink.h> #include "tc_core.h" #include "json_print.h"
tipc/bearer.h +1 -0
@@ -12,6 +12,7 @@#ifndef _TIPC_BEARER_H#define _TIPC_BEARER_H +#include <linux/netlink.h> #include "cmdl.h" extern int help_flag;
tipc/link.h +3 -0
@@ -12,6 +12,9 @@#ifndef _TIPC_LINK_H#define _TIPC_LINK_H +#include <linux/netlink.h> +#include "cmdl.h" +extern int help_flag; int cmd_link(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl,
tipc/media.h +3 -0
@@ -12,6 +12,9 @@#ifndef _TIPC_MEDIA_H#define _TIPC_MEDIA_H +#include <linux/netlink.h> +#include "cmdl.h" +extern int help_flag; int cmd_media(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl,
tipc/misc.h +2 -0
@@ -14,6 +14,8 @@#include <stdint.h> +#include <linux/tipc.h> +uint32_t str2addr (char *str);int str2nodeid (char *str,uint8_t *id);void nodeid2str (uint8_t *id,char *str);
tipc/msg.h +2 -0
@@ -12,6 +12,8 @@#ifndef _TIPC_MSG_H#define _TIPC_MSG_H +#include <libmnl/libmnl.h> +struct nlmsghdr *msg_init (int cmd);int msg_doit (struct nlmsghdr *nlh,mnl_cb_t callback,void *data);int msg_dumpit (struct nlmsghdr *nlh,mnl_cb_t callback,void *data);
tipc/nametable.h +3 -0
@@ -12,6 +12,9 @@#ifndef _TIPC_NAMETABLE_H#define _TIPC_NAMETABLE_H +#include <linux/netlink.h> +#include "cmdl.h" +extern int help_flag;void cmd_nametable_help (struct cmdl *cmdl);
tipc/node.h +3 -0
@@ -12,6 +12,9 @@#ifndef _TIPC_NODE_H#define _TIPC_NODE_H +#include <linux/netlink.h> +#include "cmdl.h" +extern int help_flag; int cmd_node(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl,
tipc/peer.h +3 -0
@@ -12,6 +12,9 @@#ifndef _TIPC_PEER_H#define _TIPC_PEER_H +#include <linux/netlink.h> +#include "cmdl.h" +extern int help_flag; int cmd_peer(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl,
tipc/socket.h +3 -0
@@ -12,6 +12,9 @@#ifndef _TIPC_SOCKET_H#define _TIPC_SOCKET_H +#include <linux/netlink.h> +#include "cmdl.h" +extern int help_flag;void cmd_socket_help (struct cmdl *cmdl);
Signature · 1 line
2.25.1
Quoted 6 lines · On Sat, 20 Nov 2021 17:00:45 +0300 Maxim Petrov <[email protected]> wrote:
The project's headers are not self-contained, as they use some types without explicitly including the corresponding headers with definitions. That makes the overall headers structure fragile: reordering/updating the headers may break the compilation. IDEs also suffer and generate dozens of errors when jumping to some random header file, distracting programmers from more serious code issues.
Quoted 6 lines
Some building issue caused by missing headers has arised just recently and it required some local cleanup in
a31e7b79(mptcp: cleanup include section). In this patch I tried to improve the situation for the whole project by adding missing includes for virtually all the project headers, except uapi ones.Signed-off-by Maxim Petrov <[email protected]>
Would be better to do this with an existing tool like IncludeWhatYouUse Is this what you did?
From 343062efbb3895f9afbff015d49df497b180f3d9 Mon Sep 17 00:00:00 2001
From: Stephen Hemminger <[email protected]>
Date: Sat, 20 Nov 2021 09:21:22 -0800
Subject: [PATCH] lib: update headers using iwyu
This is a semi-automated patch generated by using the iwyu
tool: https://github.com/include-what-you-use/include-what-you-use
The result was manually corrected to remove unnecessary references to <asm/int-ll64.h>
--- lib/ax25_ntop.c | 1 + lib/bpf_glue.c | 3 ++- lib/bpf_legacy.c | 21 +++++++++++++++------ lib/cg_map.c | 7 ++++--- lib/color.c | 3 ++- lib/inet_proto.c | 5 +---- lib/json_print.c | 12 ++++++++++-- lib/json_print_math.c | 2 +- lib/json_writer.c | 6 +++--- lib/libgenl.c | 7 ++++++- lib/libnetlink.c | 15 +++++++++++---- lib/ll_addr.c | 9 --------- lib/ll_map.c | 10 ++++++---- lib/ll_proto.c | 13 ++----------- lib/ll_types.c | 12 ------------ lib/mnl_utils.c | 9 ++++++++- lib/mpls_ntop.c | 4 +++- lib/mpls_pton.c | 4 +++- lib/names.c | 5 ++--- lib/netrom_ntop.c | 1 + lib/rose_ntop.c | 12 ------------ lib/rt_names.c | 11 +++-------- lib/utils_math.c | 2 +- 23 files changed, 85 insertions(+), 89 deletions(-)
lib/ax25_ntop.c +1 -0
@@ -3,6 +3,7 @@#include <errno.h> #include <sys/socket.h> #include <netax25/ax25.h> +#include <stddef.h> #include "utils.h"
lib/bpf_glue.c +2 -1
@@ -4,9 +4,10 @@* Authors: Hangbin Liu <[email protected]> * */ -#include <limits.h> +#include <stddef.h> #include "bpf_util.h" +#include "linux/bpf.h" #ifdef HAVE_LIBBPF #include <bpf/bpf.h> #endif
lib/bpf_legacy.c +15 -6
@@ -22,29 +22,38 @@#include <stdarg.h> #include <limits.h> #include <assert.h> +#include <bsd/string.h> +#include <elf.h> +#include <netinet/in.h> +#include <sys/socket.h> +#include <sys/statfs.h> +#include <syscall.h> #ifdef HAVE_ELF #include <libelf.h> #include <gelf.h> #endif -#include <sys/types.h> #include <sys/stat.h> #include <sys/un.h> -#include <sys/vfs.h> #include <sys/mount.h> -#include <sys/syscall.h> #include <sys/sendfile.h> #include <sys/resource.h> -#include <arpa/inet.h> -#include "utils.h" #include "json_print.h" -#include "bpf_util.h" #include "bpf_elf.h" #include "bpf_scm.h" +#include "linux/bpf.h" +#include "linux/bpf_common.h" +#include "linux/btf.h" +#include "linux/elf-em.h" +#include "linux/filter.h" +#include "linux/if_alg.h" +#include "linux/limits.h" +#include "linux/magic.h" +#include "linux/rtnetlink.h" struct bpf_prog_meta {const char *type;
lib/cg_map.c +4 -3
@@ -9,18 +9,19 @@* Authors: Dmitry Yakunin <[email protected]> */ +#include "cg_map.h" +#include <stdlib.h> #include <string.h> #include <stdio.h> #include <stdbool.h> -#include <linux/types.h> -#include <linux/limits.h> #include <ftw.h> -#include "cg_map.h" #include "list.h" #include "utils.h" +struct stat ; +struct cg_cache {struct hlist_node id_hash;__u64 id;
lib/color.c +2 -1
@@ -5,10 +5,11 @@#include <string.h> #include <unistd.h> #include <sys/socket.h> -#include <sys/types.h> +#include <stdbool.h> #include <linux/if.h> #include "color.h" +#include "utils.h" static void set_color_palette (void );
lib/inet_proto.c +1 -4
@@ -12,12 +12,9 @@#include <stdio.h> #include <stdlib.h> -#include <unistd.h> -#include <fcntl.h> -#include <sys/socket.h> -#include <netinet/in.h> #include <netdb.h> #include <string.h> +#include <bsd/string.h> #include "rt_names.h" #include "utils.h"
lib/json_print.c +10 -2
@@ -9,11 +9,19 @@* Authors: Julien Fortin, <[email protected]> */ -#include <stdarg.h> +#include "json_print.h" +#include <stdio.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdlib.h> +#include <sys/time.h> #include "utils.h" -#include "json_print.h" +#include "color.h" +#include "json_writer.h" + +struct timeval ;static json_writer_t *_jw;
lib/json_print_math.c +1 -1
@@ -1,11 +1,11 @@// SPDX-License-Identifier: GPL-2.0+ -#include <stdarg.h> #include <stdio.h> #include <math.h> #include "utils.h" #include "json_print.h" +#include "color.h" char *sprint_size (__u32 sz,char *buf) {
lib/json_writer.c +3 -3
@@ -8,15 +8,15 @@* Authors: Stephen Hemminger <[email protected]> */ +#include "json_writer.h" +#include <stdio.h> #include <stdbool.h> #include <stdarg.h> #include <assert.h> -#include <malloc.h> #include <inttypes.h> #include <stdint.h> - -#include "json_writer.h" +#include <stdlib.h> struct json_writer {FILE *out;/* output file */
lib/libgenl.c +6 -1
@@ -6,9 +6,14 @@#include <errno.h> #include <stdio.h> #include <stdlib.h> -#include <unistd.h> +#include <string.h> #include <linux/genetlink.h> + +#include "libnetlink.h" +#include "linux/netlink.h" + +struct rtattr ;#include "libgenl.h" static int genl_parse_getfamily (struct nlmsghdr *nlh)
lib/libnetlink.c +11 -4
@@ -14,21 +14,28 @@#include <stdlib.h> #include <stdbool.h> #include <unistd.h> -#include <fcntl.h> -#include <net/if_arp.h> #include <sys/socket.h> -#include <netinet/in.h> #include <string.h> #include <errno.h> #include <time.h> -#include <sys/uio.h> +#include <bits/types/struct_iovec.h> +#include <bsd/sys/cdefs.h> +#include <stdint.h> +#include <sys/param.h> #include <linux/fib_rules.h> #include <linux/if_addrlabel.h> #include <linux/if_bridge.h> #include <linux/nexthop.h> #include "libnetlink.h" +#include "utils.h" +#include "linux/if_addr.h" +#include "linux/if_link.h" +#include "linux/neighbour.h" +#include "linux/netconf.h" +#include "linux/netlink.h" +#include "linux/rtnetlink.h" #ifndef __aligned#define __aligned (x) __attribute__((aligned(x)))
lib/ll_addr.c +0 -9
@@ -10,19 +10,10 @@*/ #include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <fcntl.h> -#include <sys/ioctl.h> #include <sys/socket.h> -#include <netinet/in.h> #include <arpa/inet.h> #include <string.h> - -#include <linux/netdevice.h> #include <linux/if_arp.h> -#include <linux/sockios.h> -#include "rt_names.h" #include "utils.h"
lib/ll_map.c +6 -4
@@ -10,19 +10,21 @@* */ +#include "ll_map.h" +#include <stdio.h> #include <stdlib.h> -#include <unistd.h> -#include <fcntl.h> #include <sys/socket.h> -#include <netinet/in.h> #include <string.h> #include <net/if.h> +#include <stdbool.h> #include "libnetlink.h" -#include "ll_map.h" #include "list.h" #include "utils.h" +#include "linux/if_link.h" +#include "linux/netlink.h" +#include "linux/rtnetlink.h" struct ll_cache {struct hlist_node idx_hash;
lib/ll_proto.c +2 -11
@@ -10,21 +10,12 @@*/ #include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <fcntl.h> -#include <sys/ioctl.h> -#include <sys/socket.h> #include <netinet/in.h> -#include <arpa/inet.h> -#include <string.h> - -#include <linux/netdevice.h> -#include <linux/if_arp.h> -#include <linux/sockios.h> +#include <strings.h> #include "utils.h" #include "rt_names.h" +#include "linux/if_ether.h" #define __PF (f,n) { ETH_P_##f, #n },
lib/ll_types.c +0 -12
@@ -10,19 +10,7 @@*/ #include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <fcntl.h> -#include <sys/ioctl.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <string.h> - -#include <linux/netdevice.h> #include <linux/if_arp.h> -#include <linux/sockios.h> -#include "rt_names.h" #include "utils.h"
lib/mnl_utils.c +8 -1
@@ -7,11 +7,18 @@#include <string.h> #include <time.h> #include <libmnl/libmnl.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> #include <linux/genetlink.h> -#include "libnetlink.h" #include "mnl_utils.h" + +#include "libnetlink.h" #include "utils.h" +#include "linux/netlink.h" + +struct mnl_socket ;struct mnl_socket *mnlu_socket_open (int bus) {
lib/mpls_ntop.c +3 -1
@@ -2,8 +2,10 @@#include <errno.h> #include <string.h> -#include <sys/types.h> #include <netinet/in.h> +#include <stdint.h> +#include <stdio.h> +#include <sys/socket.h> #include <linux/mpls.h> #include "utils.h"
lib/mpls_pton.c +3 -1
@@ -2,8 +2,10 @@#include <errno.h> #include <string.h> -#include <sys/types.h> #include <netinet/in.h> +#include <stdio.h> +#include <stdlib.h> +#include <sys/socket.h> #include <linux/mpls.h> #include "utils.h"
lib/names.c +2 -3
@@ -8,14 +8,13 @@* */ +#include "names.h" +#include <stdio.h> #include <string.h> #include <stdlib.h> #include <errno.h> -#include "names.h" -#include "utils.h" -#define MAX_ENTRIES 256#define NAME_MAX_LEN 512
lib/netrom_ntop.c +1 -0
@@ -2,6 +2,7 @@#include <sys/socket.h> #include <errno.h> +#include <stddef.h> #include <linux/ax25.h> #include "utils.h"
lib/rose_ntop.c +0 -12
@@ -1,22 +1,10 @@/* SPDX-License-Identifier: GPL-2.0+ */ #include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <fcntl.h> -#include <sys/ioctl.h> #include <sys/socket.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <string.h> #include <errno.h> - -#include <linux/netdevice.h> -#include <linux/if_arp.h> -#include <linux/sockios.h> #include <linux/rose.h> -#include "rt_names.h" #include "utils.h" static const char *rose_ntop1(const rose_address *src, char *dst,
lib/rt_names.c +3 -8
@@ -11,19 +11,14 @@#include <stdio.h> #include <stdlib.h> -#include <unistd.h> -#include <fcntl.h> #include <string.h> -#include <sys/time.h> -#include <sys/socket.h> #include <dirent.h> -#include <limits.h> - -#include <asm/types.h> #include <linux/rtnetlink.h> +#include "linux/limits.h" +#include "linux/netlink.h" +#include "rt_names.h" -#include "utils.h" #define NAME_MAX_LEN 512
lib/utils_math.c +1 -1
@@ -4,7 +4,7 @@#include <stdlib.h> #include <string.h> #include <math.h> -#include <asm/types.h> +#include <strings.h> #include "utils.h"
Signature · 1 line
2.30.2
Hello Stephen!
Quoted 2 lines · Stephen Hemminger
Would be better to do this with an existing tool like IncludeWhatYouUse Is this what you did?
This is my fault, I didn't even think about searching for any existing tools for this task. :)
I just wrote a stupid loop over the header files like this:
```bash for hdr in $(find . -name *.h) do printf '#include "%s"' $hdr > headertest.c gcc headertest.c -c -I ./include -I ./include/uapi done ```
and then manually patched the files until there are no compilation errors.
It seems like I reinvented the wheel and did it not so good. I targeted only header files and fixed only compilation errors, possibly keeping some transitive includes. IWYU looks much better as it targets usage, not just compilation errors. If you can run this tool and fix the whole project automagically, then it is better to ignore my patch. Anyway, thank you for the information, I will definitely try IWYU soon.
Max