22 """Given filename, synthesize what should go in an include statement to get that file"""
23 if filename.startswith(
"include/"):
24 return '<%s>' % filename[
len(
"include/"):]
25 return '"%s"' % filename
29 """Set the file-level IWYU pragma in filename"""
31 saw_first_define =
False
32 for line
in open(filename).
read().splitlines():
33 if line.startswith(
'// IWYU pragma: '):
36 if not saw_first_define
and line.startswith(
'#define '):
37 saw_first_define =
True
39 for pragma
in pragmas:
40 lines.append(
'// IWYU pragma: %s' % pragma)
42 open(filename,
'w').
write(
'\n'.join(lines) +
'\n')
46 """In file pub, mark the include for cg with IWYU pragma: export"""
48 for line
in open(pub).
read().splitlines():
49 if line.startswith(
'#include %s' %
to_inc(cg)):
50 lines.append(
'#include %s // IWYU pragma: export' %
to_inc(cg))
53 open(pub,
'w').
write(
'\n'.join(lines) +
'\n')
57 (
r'sync',
'grpc/support/sync.h',
False),
58 (
r'atm',
'grpc/support/atm.h',
False),
59 (
r'grpc_types',
'grpc/grpc.h',
True),
60 (
r'gpr_types',
'grpc/grpc.h',
True),
61 (
r'compression_types',
'grpc/compression.h',
True),
62 (
r'connectivity_state',
'grpc/grpc.h',
True),
66 (
r'status_code_enum',
'grpcpp/support/status.h',
False),
71 """Fix one include tree"""
73 reverse_map = collections.defaultdict(list)
75 cg_reverse_map = collections.defaultdict(list)
76 for root, dirs, files
in os.walk(tree):
77 root_map = cg_reverse_map
if '/impl/codegen' in root
else reverse_map
78 for filename
in files:
79 root_map[filename].append(root)
81 for filename, paths
in cg_reverse_map.items():
82 print(
"****", filename)
84 if not filename.endswith(
'.h'):
89 for root, target, friend
in cg_roots:
90 print(root, target, friend)
91 if filename.startswith(root):
92 pragmas = [
'private, include <%s>' % target]
94 pragmas.append(
'friend "src/.*"')
97 if filename.startswith(root +
'.'):
98 set_exports(
'include/' + target, path +
'/' + filename)
99 if filename.startswith(root +
'_'):
101 path +
'/' + filename)
103 if not pragmas
and len(paths) == 1:
106 if filename
in reverse_map:
107 proper = reverse_map[filename]
111 cg = path +
'/' + filename
112 pub = proper[0] +
'/' + filename
116 pragmas = [
'private, include %s' %
to_inc(pub)]
123 pragmas = [
'private']
128 fix_tree(
'include/grpc', CG_ROOTS_GRPC)
129 fix_tree(
'include/grpcpp', CG_ROOTS_GRPCPP)