David Garcia Quintas | 7d53836 | 2016-03-15 14:51:29 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python2.7 |
| 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | # Copyright 2016 gRPC authors. |
David Garcia Quintas | 7d53836 | 2016-03-15 14:51:29 -0700 | [diff] [blame] | 4 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
David Garcia Quintas | 7d53836 | 2016-03-15 14:51:29 -0700 | [diff] [blame] | 8 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
David Garcia Quintas | 7d53836 | 2016-03-15 14:51:29 -0700 | [diff] [blame] | 10 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
David Garcia Quintas | 7d53836 | 2016-03-15 14:51:29 -0700 | [diff] [blame] | 16 | |
| 17 | import argparse |
| 18 | import os |
David Garcia Quintas | c74f62b | 2016-04-28 00:56:31 -0700 | [diff] [blame] | 19 | import os.path |
David Garcia Quintas | 7d53836 | 2016-03-15 14:51:29 -0700 | [diff] [blame] | 20 | import re |
| 21 | import sys |
| 22 | import subprocess |
| 23 | |
| 24 | |
| 25 | def build_valid_guard(fpath): |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 26 | prefix = 'GRPC_' if not fpath.startswith('include/') else '' |
| 27 | return prefix + '_'.join( |
| 28 | fpath.replace('++', 'XX').replace('.', '_').upper().split('/')[1:]) |
David Garcia Quintas | 7d53836 | 2016-03-15 14:51:29 -0700 | [diff] [blame] | 29 | |
| 30 | |
| 31 | def load(fpath): |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 32 | with open(fpath, 'r') as f: |
| 33 | return f.read() |
David Garcia Quintas | 7d53836 | 2016-03-15 14:51:29 -0700 | [diff] [blame] | 34 | |
| 35 | |
| 36 | def save(fpath, contents): |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 37 | with open(fpath, 'w') as f: |
| 38 | f.write(contents) |
David Garcia Quintas | 7d53836 | 2016-03-15 14:51:29 -0700 | [diff] [blame] | 39 | |
| 40 | |
| 41 | class GuardValidator(object): |
David Garcia Quintas | 7d53836 | 2016-03-15 14:51:29 -0700 | [diff] [blame] | 42 | |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 43 | def __init__(self): |
| 44 | self.ifndef_re = re.compile(r'#ifndef ([A-Z][A-Z_1-9]*)') |
| 45 | self.define_re = re.compile(r'#define ([A-Z][A-Z_1-9]*)') |
| 46 | self.endif_c_re = re.compile( |
| 47 | r'#endif /\* ([A-Z][A-Z_1-9]*) (?:\\ *\n *)?\*/') |
| 48 | self.endif_cpp_re = re.compile(r'#endif // ([A-Z][A-Z_1-9]*)') |
David Garcia Quintas | 7d53836 | 2016-03-15 14:51:29 -0700 | [diff] [blame] | 49 | self.failed = False |
David Garcia Quintas | 7d53836 | 2016-03-15 14:51:29 -0700 | [diff] [blame] | 50 | |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 51 | def fail(self, fpath, regexp, fcontents, match_txt, correct, fix): |
| 52 | cpp_header = 'grpc++' in fpath |
| 53 | self.failed = True |
| 54 | invalid_guards_msg_template = ( |
| 55 | '{0}: Missing preprocessor guards (RE {1}). ' |
| 56 | 'Please wrap your code around the following guards:\n' |
| 57 | '#ifndef {2}\n' |
| 58 | '#define {2}\n' |
| 59 | '...\n' |
| 60 | '... epic code ...\n' |
| 61 | '...\n') + ('#endif // {2}' if cpp_header else '#endif /* {2} */') |
| 62 | if not match_txt: |
| 63 | print invalid_guards_msg_template.format(fpath, regexp.pattern, |
| 64 | build_valid_guard(fpath)) |
| 65 | return fcontents |
David Garcia Quintas | 7d53836 | 2016-03-15 14:51:29 -0700 | [diff] [blame] | 66 | |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 67 | print('{}: Wrong preprocessor guards (RE {}):' |
| 68 | '\n\tFound {}, expected {}').format(fpath, regexp.pattern, |
| 69 | match_txt, correct) |
David Garcia Quintas | 3c5def5 | 2016-03-17 22:26:22 -0700 | [diff] [blame] | 70 | if fix: |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 71 | print 'Fixing {}...\n'.format(fpath) |
| 72 | fixed_fcontents = re.sub(match_txt, correct, fcontents) |
| 73 | if fixed_fcontents: |
| 74 | self.failed = False |
| 75 | return fixed_fcontents |
| 76 | else: |
| 77 | print |
| 78 | return fcontents |
David Garcia Quintas | 7d53836 | 2016-03-15 14:51:29 -0700 | [diff] [blame] | 79 | |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 80 | def check(self, fpath, fix): |
| 81 | cpp_header = 'grpc++' in fpath |
| 82 | valid_guard = build_valid_guard(fpath) |
| 83 | |
| 84 | fcontents = load(fpath) |
| 85 | |
| 86 | match = self.ifndef_re.search(fcontents) |
| 87 | if not match: |
| 88 | print 'something drastically wrong with: %s' % fpath |
| 89 | return False # failed |
| 90 | if match.lastindex is None: |
| 91 | # No ifndef. Request manual addition with hints |
| 92 | self.fail(fpath, match.re, match.string, '', '', False) |
| 93 | return False # failed |
| 94 | |
| 95 | # Does the guard end with a '_H'? |
| 96 | running_guard = match.group(1) |
| 97 | if not running_guard.endswith('_H'): |
Mehrdad Afshari | 87cd994 | 2018-01-02 14:40:00 -0800 | [diff] [blame] | 98 | fcontents = self.fail(fpath, match.re, match.string, match.group(1), |
| 99 | valid_guard, fix) |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 100 | if fix: save(fpath, fcontents) |
| 101 | |
| 102 | # Is it the expected one based on the file path? |
| 103 | if running_guard != valid_guard: |
Mehrdad Afshari | 87cd994 | 2018-01-02 14:40:00 -0800 | [diff] [blame] | 104 | fcontents = self.fail(fpath, match.re, match.string, match.group(1), |
| 105 | valid_guard, fix) |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 106 | if fix: save(fpath, fcontents) |
| 107 | |
| 108 | # Is there a #define? Is it the same as the #ifndef one? |
| 109 | match = self.define_re.search(fcontents) |
| 110 | if match.lastindex is None: |
| 111 | # No define. Request manual addition with hints |
| 112 | self.fail(fpath, match.re, match.string, '', '', False) |
| 113 | return False # failed |
| 114 | |
| 115 | # Is the #define guard the same as the #ifndef guard? |
| 116 | if match.group(1) != running_guard: |
Mehrdad Afshari | 87cd994 | 2018-01-02 14:40:00 -0800 | [diff] [blame] | 117 | fcontents = self.fail(fpath, match.re, match.string, match.group(1), |
| 118 | valid_guard, fix) |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 119 | if fix: save(fpath, fcontents) |
| 120 | |
| 121 | # Is there a properly commented #endif? |
| 122 | endif_re = self.endif_cpp_re if cpp_header else self.endif_c_re |
| 123 | flines = fcontents.rstrip().splitlines() |
| 124 | match = endif_re.search('\n'.join(flines[-2:])) |
| 125 | if not match: |
| 126 | # No endif. Check if we have the last line as just '#endif' and if so |
| 127 | # replace it with a properly commented one. |
| 128 | if flines[-1] == '#endif': |
| 129 | flines[-1] = ( |
| 130 | '#endif' + |
| 131 | (' // {}\n'.format(valid_guard) |
| 132 | if cpp_header else ' /* {} */\n'.format(valid_guard))) |
| 133 | if fix: |
| 134 | fcontents = '\n'.join(flines) |
| 135 | save(fpath, fcontents) |
| 136 | else: |
| 137 | # something else is wrong, bail out |
| 138 | self.fail(fpath, endif_re, flines[-1], '', '', False) |
| 139 | elif match.group(1) != running_guard: |
| 140 | # Is the #endif guard the same as the #ifndef and #define guards? |
Mehrdad Afshari | 87cd994 | 2018-01-02 14:40:00 -0800 | [diff] [blame] | 141 | fcontents = self.fail(fpath, endif_re, fcontents, match.group(1), |
| 142 | valid_guard, fix) |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 143 | if fix: save(fpath, fcontents) |
| 144 | |
| 145 | return not self.failed # Did the check succeed? (ie, not failed) |
| 146 | |
David Garcia Quintas | 7d53836 | 2016-03-15 14:51:29 -0700 | [diff] [blame] | 147 | |
| 148 | # find our home |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 149 | ROOT = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '../..')) |
David Garcia Quintas | 7d53836 | 2016-03-15 14:51:29 -0700 | [diff] [blame] | 150 | os.chdir(ROOT) |
| 151 | |
| 152 | # parse command line |
| 153 | argp = argparse.ArgumentParser(description='include guard checker') |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 154 | argp.add_argument('-f', '--fix', default=False, action='store_true') |
| 155 | argp.add_argument('--precommit', default=False, action='store_true') |
David Garcia Quintas | 7d53836 | 2016-03-15 14:51:29 -0700 | [diff] [blame] | 156 | args = argp.parse_args() |
| 157 | |
| 158 | KNOWN_BAD = set([ |
Craig Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 159 | 'src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.h', |
Yuchen Zeng | 7ae31a8 | 2016-06-06 14:21:11 -0700 | [diff] [blame] | 160 | 'include/grpc++/ext/reflection.grpc.pb.h', |
| 161 | 'include/grpc++/ext/reflection.pb.h', |
David Garcia Quintas | 7d53836 | 2016-03-15 14:51:29 -0700 | [diff] [blame] | 162 | ]) |
| 163 | |
David Garcia Quintas | 7d53836 | 2016-03-15 14:51:29 -0700 | [diff] [blame] | 164 | grep_filter = r"grep -E '^(include|src/core)/.*\.h$'" |
| 165 | if args.precommit: |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 166 | git_command = 'git diff --name-only HEAD' |
David Garcia Quintas | 7d53836 | 2016-03-15 14:51:29 -0700 | [diff] [blame] | 167 | else: |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 168 | git_command = 'git ls-tree -r --name-only -r HEAD' |
David Garcia Quintas | 7d53836 | 2016-03-15 14:51:29 -0700 | [diff] [blame] | 169 | |
| 170 | FILE_LIST_COMMAND = ' | '.join((git_command, grep_filter)) |
| 171 | |
| 172 | # scan files |
| 173 | ok = True |
| 174 | filename_list = [] |
| 175 | try: |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 176 | filename_list = subprocess.check_output( |
| 177 | FILE_LIST_COMMAND, shell=True).splitlines() |
| 178 | # Filter out non-existent files (ie, file removed or renamed) |
| 179 | filename_list = (f for f in filename_list if os.path.isfile(f)) |
David Garcia Quintas | 7d53836 | 2016-03-15 14:51:29 -0700 | [diff] [blame] | 180 | except subprocess.CalledProcessError: |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 181 | sys.exit(0) |
David Garcia Quintas | 7d53836 | 2016-03-15 14:51:29 -0700 | [diff] [blame] | 182 | |
| 183 | validator = GuardValidator() |
| 184 | |
| 185 | for filename in filename_list: |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 186 | if filename in KNOWN_BAD: continue |
| 187 | ok = ok and validator.check(filename, args.fix) |
David Garcia Quintas | 7d53836 | 2016-03-15 14:51:29 -0700 | [diff] [blame] | 188 | |
| 189 | sys.exit(0 if ok else 1) |