From: Rasmus Villemoes <linux@rasmusvillemoes.dk>
To: Michal Marek <mmarek@suse.cz>
Cc: linux-kbuild@vger.kernel.org
Subject: include guard weirdness
Date: Fri, 11 Jul 2014 13:51:51 +0200 [thread overview]
Message-ID: <87a98gnmeg.fsf@rasmusvillemoes.dk> (raw)
Hi,
gcc/cpp claims to be smart enough to recognize the include guard idiom,
and most of the time this works very well: the second and subsequent
#includes do not even cause the file to be opened (thus saving at least
open+fstat+read+close). Except in a few cases, which are so common that
it might be worth investigating.
For example, linux/mm.h often shows up exactly twice in strace output;
another very common header such as linux/types.h always occurs only
once.
Steps to reproduce:
(1) Pick some file which includes linux/mm.h through multiple paths
(those are not hard to find); I use kernel/fork.c as example.
strace -f -o /tmp/strace.txt gcc -Wp,-MD,kernel/.fork.o.d -nostdinc -isystem /usr/lib/gcc/x86_64-linux-gnu/4.7/include -I./arch/x86/include -Iarch/x86/include/generated -Iinclude -I./arch/x86/include/uapi -Iarch/x86/include/generated/uapi -I./include/uapi -Iinclude/generated/uapi -include ./include/linux/kconfig.h -D__KERNEL__ -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -Wno-format-security -m64 -mno-mmx -mno-sse -mno-80387 -mno-fp-ret-in-387 -mtune=generic -mno-red-zone -mcmodel=kernel -funit-at-a-time -maccumulate-outgoing-args -DCONFIG_AS_CFI=1 -DCONFIG_AS_CFI_SIGNAL_FRAME=1 -DCONFIG_AS_CFI_SECTIONS=1 -DCONFIG_AS_FXSAVEQ=1 -DCONFIG_AS_CRC32=1 -DCONFIG_AS_AVX=1 -DCONFIG_AS_AVX2=1 -pipe -Wno-sign-compare -fno-asynchronous-unwind-tables -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -fno-delete-null-pointer-checks -O2 -Wframe-larger-than=2048 -fno-stack-protector -Wno-unused-but-set-variable -fno-omit-frame-pointer -fno-optimize-sibling-calls -Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow -fconserve-stack -Werror=implicit-int -Werror=strict-prototypes -DCC_HAVE_ASM_GOTO -D"KBUILD_STR(s)=#s" -D"KBUILD_BASENAME=KBUILD_STR(fork)" -D"KBUILD_MODNAME=KBUILD_STR(fork)" -c -o kernel/fork.o kernel/fork.c
(2) Run
perl -ne 'print if s/^.*open\("([^"]*\.h)".*\) = [0-9]/$1/;' /tmp/strace.txt | sort | uniq -c --repeated
to find all the header files which were succesfully opened more than once.
For me, this gives
2 ./arch/x86/include/asm/fixmap.h
2 ./arch/x86/include/asm/posix_types.h
2 ./arch/x86/include/asm/smp.h
2 ./arch/x86/include/asm/spinlock_types.h
4 ./arch/x86/include/uapi/asm/errno.h
4 ./arch/x86/include/uapi/asm/param.h
2 ./arch/x86/include/uapi/asm/socket.h
2 ./arch/x86/include/uapi/asm/sockios.h
2 include/linux/bitops.h
2 include/linux/cgroup_subsys.h
2 include/linux/mm.h
2 include/linux/mmzone.h
3 include/linux/static_key.h
13 include/linux/tracepoint.h
11 include/trace/define_trace.h
10 include/trace/events/task.h
Some of these are expected (no include guards), but mm.h, mmzone.h and
tracepoint.h certainly have include guards.
The same happens when I try a few other random examples (fs/dcache.c,
drivers/dma/intel_mid_dma.c); mm.h is always read twice.
I'm using
$ gcc --version
gcc (Debian 4.7.2-5) 4.7.2
Rasmus
next reply other threads:[~2014-07-11 11:51 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-07-11 11:51 Rasmus Villemoes [this message]
2014-07-11 12:20 ` include guard weirdness Michal Marek
2014-07-12 11:11 ` Rasmus Villemoes
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87a98gnmeg.fsf@rasmusvillemoes.dk \
--to=linux@rasmusvillemoes.dk \
--cc=linux-kbuild@vger.kernel.org \
--cc=mmarek@suse.cz \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.