blob: fd674c099e1f88bebbcd1ccc0f1489833e9f24f0 [file] [log] [blame]
Primiano Tucci4f9b6d72017-12-05 20:59:16 +00001#!/usr/bin/env python
2# Copyright (C) 2017 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import os
17import re
18import sys
19
Primiano Tucci40e98722018-02-16 11:50:17 +000020def fix_guards(fpath, checkonly):
21 with open(fpath, 'rb') as f:
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000022 lines = [l.strip('\n') for l in f.readlines()]
23 res = []
24 guard = re.sub(r'[^a-zA-Z0-9_-]', '_', fpath.upper()) + '_'
25 replacements = 0
26
27 endif_line_idx = -1
28 for line_idx in xrange(len(lines) - 1, -1, -1):
29 if lines[line_idx].startswith('#endif'):
30 endif_line_idx = line_idx
31 break
Primiano Tucci40e98722018-02-16 11:50:17 +000032 assert endif_line_idx > 0, fpath
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000033
34 line_idx = 0
35 for line in lines:
36 if replacements == 0 and line.startswith('#ifndef '):
37 line = '#ifndef ' + guard
38 replacements = 1
39 elif replacements == 1 and line.startswith('#define '):
40 line = '#define ' + guard
41 replacements = 2
42 elif line_idx == endif_line_idx and replacements == 2:
43 assert(line.startswith('#endif'))
Primiano Tucci40e98722018-02-16 11:50:17 +000044 line = '#endif // ' + guard
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000045 res.append(line)
46 line_idx += 1
47 if res == lines:
48 return 0
Primiano Tucci40e98722018-02-16 11:50:17 +000049 if checkonly:
50 print >>sys.stderr, 'Wrong #include guards in %s' % fpath
51 return 1
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000052 with open(fpath, 'w') as f:
Primiano Tucci40e98722018-02-16 11:50:17 +000053 f.write('\n'.join(res) + '\n')
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000054 return 1
55
56def main():
Primiano Tucci40e98722018-02-16 11:50:17 +000057 checkonly = '--check-only' in sys.argv
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000058 num_files_changed = 0
Lalit Maganti503f6c42018-02-16 17:36:13 +000059 for topdir in ('src', 'include', 'test'):
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000060 for root, dirs, files in os.walk(topdir):
61 for name in files:
62 if not name.endswith('.h'):
63 continue
64 fpath = os.path.join(root, name)
Primiano Tucci40e98722018-02-16 11:50:17 +000065 num_files_changed += fix_guards(fpath, checkonly)
66 if checkonly:
67 return 0 if num_files_changed == 0 else 1
68 else:
69 print '%d files changed' % num_files_changed
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000070
71if __name__ == '__main__':
72 sys.exit(main())