61 m = PRAGMA_RE.search(text)
66 if len(PRAGMA_RE.findall(text)) > 1:
67 raise ValueError(
'more than one #pragma once directive')
69 head = text[:m.start()]
71 opening =
'#ifndef %s\n#define %s\n' % (guard, guard)
73 idx = tail.rfind(MODELINE)
75 closing =
'\n#endif // %s\n' % guard
76 return head + opening + tail.rstrip(
'\n') + closing
78 body, trailer = tail[:idx], tail[idx:]
79 return head + opening + body.rstrip(
'\n') +
'\n\n#endif // %s\n\n' % guard + trailer
83 """Guard a header that has neither #pragma once nor an #ifndef/#define pair.
85 The opening goes after the leading licence block comment (so the guard wraps the
86 actual content, not the whole file including its header comment); the #endif goes
87 just above the trailing modeline block, mirroring convert().
90 stripped = text.lstrip()
91 if stripped.startswith(
'/*'):
94 start = text.index(
'\n', end) + 1
if '\n' in text[end:]
else len(text)
96 opening =
'\n#ifndef %s\n#define %s\n' % (guard, guard)
97 head, rest = text[:start], text[start:]
99 idx = rest.rfind(MODELINE)
101 return head + opening + rest.rstrip(
'\n') +
'\n\n#endif // %s\n' % guard
102 body, trailer = rest[:idx], rest[idx:]
103 return head + opening + body.rstrip(
'\n') +
'\n\n#endif // %s\n\n' % guard + trailer
107 check =
'--check' in sys.argv
108 add_missing =
'--add-missing' in sys.argv
110 if '--verify' in sys.argv:
111 offenders = [p
for p
in sorted(
headers())
112 if PRAGMA_RE.search(open(p, encoding=
'utf-8').read())]
114 print(
'%s: #pragma once is forbidden, use an include guard' % p, file=sys.stderr)
115 return 1
if offenders
else 0
117 changed = skipped = 0
120 text = open(p, encoding=
'utf-8').read()
121 if '#pragma once' not in text:
122 if not add_missing
or p.replace(os.sep,
'/')
in XMACRO_HEADERS:
124 if GUARD_RE.search(text):
129 print(
'%s -> %s (was UNGUARDED)' % (p, g))
135 print(
'COLLISION: %s and %s both map to %s' % (p, seen[g], g), file=sys.stderr)
140 except ValueError
as e:
141 print(
'SKIP %s: %s' % (p, e), file=sys.stderr)
148 print(
'%s -> %s' % (p, g))
150 open(p,
'w', encoding=
'utf-8').write(out)
151 print(
'%s %d headers (%d skipped)' % (
'would convert' if check
else 'converted', changed, skipped))