From: Alexey Dobriyan <adobriyan@gmail.com>
To: linux-kernel@vger.kernel.org
Subject: #pragma once
Date: Thu, 26 Apr 2018 02:09:43 +0300 [thread overview]
Message-ID: <20180425230943.GA10728@avx2> (raw)
Following up with negative LOC trend...
16171 files changed, 16197 insertions(+), 87671 deletions(-)
Not all files can be blindly converted as some headers check for master
header include guard (spinlock.h et al).
include/uapi/ can be skipped probably out of fear of finding a C compiler
without "#pragma once" support.
Script modifies files in place and gives up if something goes wrong even
slightly.
Use "#pragma once" in you code today!
#!/usr/bin/python2
# Change include guard to "#pragma once" directive in place.
import os
import re
import sys
re_ifndef = re.compile('#ifndef ([A-Za-z_][A-Za-z0-9_]*)\n')
re_define = re.compile('#define ([A-Za-z_][A-Za-z0-9_]*)\n')
re_endif = re.compile('#endif([ \t]*/\*[^/]*\*/[ \t]*)?\n')
def read_file(filename):
with open(filename) as f:
buf = f.read()
return buf
def write_file(filename, buf, chop_list):
# [start, end)
s1, e1 = chop_list[0]
s2, e2 = chop_list[1]
tmp = '%s.pragma-once' % filename
with open(tmp, 'w') as f:
f.write(buf[0:s1])
if s1 > 0 and buf[s1 - 1] != '\n':
f.write('\n')
f.write('#pragma once\n')
f.write(buf[e1:s2])
if buf[s2 - 1] != '\n':
f.write('\n')
os.rename(tmp, filename)
def ws(c):
return c == '\n' or c == ' ' or c == '\t'
def pragma_once(filename):
c = read_file(filename)
chop_list = []
i = 0
j = len(c)
while i < j:
if ws(c[i]):
i = i + 1
elif c[i] == '/' and c[i + 1] == '/':
i = c.index('\n', i + 2) + 1
elif c[i] == '/' and c[i + 1] == '*':
i = c.index('*/', i + 2) + 2
else:
break
ii = i;
#ifndef
match = re_ifndef.match(c, ii)
if match is None:
return
guard = match.group(1)
ii = match.end()
#define
match = re_define.match(c, ii)
if match is None:
return
if guard != match.group(1):
return
ii = match.end()
while i > 0 and ws(c[i - 1]):
i = i - 1
while ws(c[ii]):
ii = ii + 1
chop_list.append((i, ii))
#endif
ii = c.rindex("\n#endif", i) + 1
match = re_endif.match(c, ii)
if match is None:
return
jj = match.end()
if jj != len(c):
return
while ws(c[ii - 1]):
ii = ii - 1
chop_list.append((ii, len(c)))
write_file(filename, c, chop_list)
for filename in sys.argv[1:]:
try:
pragma_once(filename)
except:
pass
reply other threads:[~2018-04-25 23:09 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20180425230943.GA10728@avx2 \
--to=adobriyan@gmail.com \
--cc=linux-kernel@vger.kernel.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 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.