[go: nahoru, domu]

blob: bef74b32c079baff464ab9a971ce09bcaec3ab85 [file] [log] [blame]
Lei Zhangac975fb2021-04-21 03:51:061#!/usr/bin/env python3
Avi Drissmandfd880852022-09-15 20:11:092# Copyright 2021 The Chromium Authors
Lei Zhangac975fb2021-04-21 03:51:063# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Applies cpplint build/header_guard recommendations.
6
7Reads cpplint build/header_guard recommendations from stdin and applies them.
8
9Run cpplint for a single header:
10cpplint.py --filter=-,+build/header_guard foo.h 2>&1 | grep build/header_guard
11
12Run cpplint for all headers in dir foo in parallel:
13find foo -name '*.h' | \
14 xargs parallel cpplint.py --filter=-,+build/header_guard -- 2>&1 | \
15 grep build/header_guard
16"""
17
18import sys
19
20IFNDEF_MSG = ' #ifndef header guard has wrong style, please use'
21ENDIF_MSG_START = ' #endif line should be "'
22ENDIF_MSG_END = '" [build/header_guard] [5]'
23NO_GUARD_MSG = ' No #ifndef header guard found, suggested CPP variable is'
24
25
26def process_cpplint_recommendations(cpplint_data):
Sumaid Syed7263132b2021-07-23 04:57:1327 root = sys.argv[1] if len(sys.argv) > 1 else ''
28 root = "_".join(root.upper().strip(r'[/]+').split('/'))+"_"
Lei Zhangac975fb2021-04-21 03:51:0629 for entry in cpplint_data:
30 entry = entry.split(':')
shaochenguange46cbd42023-11-27 00:32:1131 # The length of the entry may be less than 3,
32 # e.g the last line of text 'Total errors found: xx'.
33 if len(entry) < 3:
34 continue
Lei Zhangac975fb2021-04-21 03:51:0635 header = entry[0]
36 line = entry[1]
37 index = int(line) - 1
38 msg = entry[2].rstrip()
39 if msg == IFNDEF_MSG:
40 assert len(entry) == 4
41
42 with open(header, 'rb') as f:
43 content = f.readlines()
44
45 if not content[index + 1].startswith(b'#define '):
46 raise Exception('Missing #define: %s:%d' % (header, index + 2))
47
48 guard = entry[3].split(' ')[1]
Sumaid Syed7263132b2021-07-23 04:57:1349 guard = guard.replace(root, '') if len(root) > 1 else guard
Lei Zhangac975fb2021-04-21 03:51:0650 content[index] = ('#ifndef %s\n' % guard).encode('utf-8')
51 # Since cpplint does not print messages for the #define line, just
52 # blindly overwrite the #define that was here.
53 content[index + 1] = ('#define %s\n' % guard).encode('utf-8')
54 elif msg.startswith(ENDIF_MSG_START):
55 assert len(entry) == 3
56 assert msg.endswith(ENDIF_MSG_END)
57
58 with open(header, 'rb') as f:
59 content = f.readlines()
60 endif = msg[len(ENDIF_MSG_START):-len(ENDIF_MSG_END)]
Sumaid Syed7263132b2021-07-23 04:57:1361 endif = endif.replace(root, '') if len(root) > 1 else endif
Lei Zhangac975fb2021-04-21 03:51:0662 content[index] = ('%s\n' % endif).encode('utf-8')
63 elif msg == NO_GUARD_MSG:
64 assert index == -1
65 continue
66 else:
67 raise Exception('Unknown cpplint message: %s for %s:%s' %
68 (msg, header, line))
69
70 with open(header, 'wb') as f:
71 f.writelines(content)
72
73
74if __name__ == '__main__':
75 process_cpplint_recommendations(sys.stdin)