From: Josh Triplett <josh@joshtriplett.org>
To: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
Andrew Morton <akpm@linux-foundation.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Michal Marek <mmarek@suse.cz>, Sam Ravnborg <sam@ravnborg.org>,
Rashika Kheria <rashika.kheria@gmail.com>
Subject: #pragma once?
Date: Mon, 6 Jan 2014 12:47:07 -0800 [thread overview]
Message-ID: <20140106204706.GA16924@leaf> (raw)
[CCing build-system folks and others likely to know about potential
issues.]
Does anyone have any objection to the use of "#pragma once" instead of
the usual #ifndef-#define-...-#endif include guard? GCC, LLVM/clang,
and the latest Sparse all support either method just fine. (I added
support to Sparse myself.) Both have equivalent performance. "#pragma
once" is simpler, and avoids the possibility of a typo in the defined
guard symbol.
That's not a theoretical concern: I've found quite a few headers in the
kernel with typoed (and thus non-functional) include guards. Rashika
Kheria (CCed) ran into one such header when attempting to add some
includes to fix some warnings (resulting in the broken header being
included twice), and that prompted me to look for other headers with
broken guards. I've included a Python script at the end of this mail
that finds many such issues, though it does have a few false positives
on files that don't have include guards at all.
I'm not suggesting a mass conversion from include guards to "#pragma
once", though that *would* be fairly easy to script and review if
desired. I'd just like to verify that "#pragma once" seems acceptable
before, for instance, converting the headers that already have broken
include guards, adding recommendations in Documentation to standardize
on the pragma for new headers, and possibly adding checks in checkpatch
or similar if I can come up with one that has minimal false positives.
Python script to check header guards (run with $(find -name '*.h') as
arguments, and grep for "mismatch" in the output if you don't care about
files without guards):
#!/usr/bin/python
import re
import sys
start = re.compile("[ \t]*#[ \t]*(ifndef|define)[ \t]+([a-zA-Z0-9_]*)")
def main(args):
for header in args[1:]:
symbol = None
for line in open(header):
m = start.match(line)
if not m:
if symbol:
print "{}: does not appear to use ifndef-based include guard".format(header)
break
continue
if m.group(1) == "ifndef":
if symbol is not None:
print "{}: does not appear to use ifndef-based include guard".format(header)
break
symbol = m.group(2)
elif m.group(1) == "define":
if symbol is None:
print "{}: does not appear to use ifndef-based include guard".format(header)
break
if symbol != m.group(2):
print "{}: include guard symbol mismatch: #ifndef {} with #define {}".format(header, symbol, m.group(2))
break
else:
if symbol:
print "{}: ifndef {} with no subsequent define".format(header, symbol)
if __name__ == "__main__":
sys.exit(main(sys.argv))
next reply other threads:[~2014-01-06 20:47 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-01-06 20:47 Josh Triplett [this message]
2014-01-06 21:00 ` #pragma once? Andrew Morton
2014-01-06 21:09 ` Josh Triplett
2014-01-06 21:33 ` Theodore Ts'o
2014-01-06 21:50 ` Dave Jones
2014-01-07 5:55 ` Sam Ravnborg
2014-01-07 9:48 ` Geert Uytterhoeven
2014-01-07 10:09 ` Michal Marek
2014-01-07 10:38 ` Josh Triplett
2014-01-07 10:50 ` Josh Triplett
2014-01-12 16:14 ` Patrick Palka
2014-01-13 5:53 ` Josh Triplett
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=20140106204706.GA16924@leaf \
--to=josh@joshtriplett.org \
--cc=akpm@linux-foundation.org \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mmarek@suse.cz \
--cc=rashika.kheria@gmail.com \
--cc=sam@ravnborg.org \
--cc=torvalds@linux-foundation.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox