100 m = PRAGMA_RE.search(text)
105 if len(PRAGMA_RE.findall(text)) > 1:
106 raise ValueError(
'more than one #pragma once directive')
108 head = text[:m.start()]
109 tail = text[m.end():]
110 opening =
'#ifndef %s\n#define %s\n' % (guard, guard)
114 closing =
'\n#endif // %s\n' % guard
115 return head + opening + tail.rstrip(
'\n') + closing
117 body, trailer = tail[:idx], tail[idx:]
118 return head + opening + body.rstrip(
'\n') +
'\n\n#endif // %s\n\n' % guard + trailer
122 """Guard a header that has neither #pragma once nor an #ifndef/#define pair.
124 The opening goes after the leading licence block comment (so the guard wraps the
125 actual content, not the whole file including its header comment); the #endif goes
126 just above the trailing modeline block, mirroring convert().
129 stripped = text.lstrip()
130 if stripped.startswith(
'/*'):
131 end = text.find(
'*/')
133 start = text.index(
'\n', end) + 1
if '\n' in text[end:]
else len(text)
135 opening =
'\n#ifndef %s\n#define %s\n' % (guard, guard)
136 head, rest = text[:start], text[start:]
140 return head + opening + rest.rstrip(
'\n') +
'\n\n#endif // %s\n' % guard
141 body, trailer = rest[:idx], rest[idx:]
142 return head + opening + body.rstrip(
'\n') +
'\n\n#endif // %s\n\n' % guard + trailer
146 check =
'--check' in sys.argv
147 add_missing =
'--add-missing' in sys.argv
149 if '--verify' in sys.argv:
150 offenders = [p
for p
in sorted(
headers())
151 if PRAGMA_RE.search(open(p, encoding=
'utf-8').read())]
153 print(
'%s: #pragma once is forbidden, use an include guard' %
rel(p), file=sys.stderr)
154 return 1
if offenders
else 0
156 changed = skipped = 0
159 text = open(p, encoding=
'utf-8').read()
160 if '#pragma once' not in text:
161 if not add_missing
or rel(p)
in UNGUARDED_BY_DESIGN:
163 if GUARD_RE.search(text):
168 print(
'%s -> %s (was UNGUARDED)' % (
rel(p), g))
174 print(
'COLLISION: %s and %s both map to %s' % (
rel(p),
rel(seen[g]), g), file=sys.stderr)
179 except ValueError
as e:
180 print(
'SKIP %s: %s' % (
rel(p), e), file=sys.stderr)
187 print(
'%s -> %s' % (
rel(p), g))
189 open(p,
'w', encoding=
'utf-8').write(out)
190 print(
'%s %d headers (%d skipped)' % (
'would convert' if check
else 'converted', changed, skipped))