* #pragma once
@ 2018-04-25 23:09 Alexey Dobriyan
0 siblings, 0 replies; only message in thread
From: Alexey Dobriyan @ 2018-04-25 23:09 UTC (permalink / raw)
To: linux-kernel
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
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2018-04-25 23:09 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-04-25 23:09 #pragma once Alexey Dobriyan
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.