1#!/usr/bin/env python2.7 2 3# Copyright 2016 gRPC authors. 4# 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 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 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. 16 17import argparse 18import os 19import os.path 20import re 21import sys 22import subprocess 23 24 25def build_valid_guard(fpath): 26 prefix = 'GRPC_' if not fpath.startswith('include/') else '' 27 return prefix + '_'.join( 28 fpath.replace('++', 'XX').replace('.', '_').upper().split('/')[1:]) 29 30 31def load(fpath): 32 with open(fpath, 'r') as f: 33 return f.read() 34 35 36def save(fpath, contents): 37 with open(fpath, 'w') as f: 38 f.write(contents) 39 40 41class GuardValidator(object): 42 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 /\* (?: *\\\n *)?([A-Z][A-Z_1-9]*) (?:\\\n *)?\*/$') 48 self.endif_cpp_re = re.compile(r'#endif // ([A-Z][A-Z_1-9]*)') 49 self.failed = False 50 51 def fail(self, fpath, regexp, fcontents, match_txt, correct, fix): 52 cpp_header = 'grpc++' in fpath or 'grpcpp' 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 66 67 print( 68 '{}: Wrong preprocessor guards (RE {}):' 69 '\n\tFound {}, expected {}').format(fpath, regexp.pattern, 70 match_txt, correct) 71 if fix: 72 print 'Fixing {}...\n'.format(fpath) 73 fixed_fcontents = re.sub(match_txt, correct, fcontents) 74 if fixed_fcontents: 75 self.failed = False 76 return fixed_fcontents 77 else: 78 print 79 return fcontents 80 81 def check(self, fpath, fix): 82 cpp_header = 'grpc++' in fpath or 'grpcpp' in fpath 83 valid_guard = build_valid_guard(fpath) 84 85 fcontents = load(fpath) 86 87 match = self.ifndef_re.search(fcontents) 88 if not match: 89 print 'something drastically wrong with: %s' % fpath 90 return False # failed 91 if match.lastindex is None: 92 # No ifndef. Request manual addition with hints 93 self.fail(fpath, match.re, match.string, '', '', False) 94 return False # failed 95 96 # Does the guard end with a '_H'? 97 running_guard = match.group(1) 98 if not running_guard.endswith('_H'): 99 fcontents = self.fail(fpath, match.re, match.string, match.group(1), 100 valid_guard, fix) 101 if fix: save(fpath, fcontents) 102 103 # Is it the expected one based on the file path? 104 if running_guard != valid_guard: 105 fcontents = self.fail(fpath, match.re, match.string, match.group(1), 106 valid_guard, fix) 107 if fix: save(fpath, fcontents) 108 109 # Is there a #define? Is it the same as the #ifndef one? 110 match = self.define_re.search(fcontents) 111 if match.lastindex is None: 112 # No define. Request manual addition with hints 113 self.fail(fpath, match.re, match.string, '', '', False) 114 return False # failed 115 116 # Is the #define guard the same as the #ifndef guard? 117 if match.group(1) != running_guard: 118 fcontents = self.fail(fpath, match.re, match.string, match.group(1), 119 valid_guard, fix) 120 if fix: save(fpath, fcontents) 121 122 # Is there a properly commented #endif? 123 endif_re = self.endif_cpp_re if cpp_header else self.endif_c_re 124 flines = fcontents.rstrip().splitlines() 125 match = endif_re.search('\n'.join(flines[-3:])) 126 if not match: 127 # No endif. Check if we have the last line as just '#endif' and if so 128 # replace it with a properly commented one. 129 if flines[-1] == '#endif': 130 flines[-1] = ('#endif' + 131 (' // {}\n'.format(valid_guard) if cpp_header 132 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? 141 fcontents = self.fail(fpath, endif_re, fcontents, match.group(1), 142 valid_guard, fix) 143 if fix: save(fpath, fcontents) 144 145 return not self.failed # Did the check succeed? (ie, not failed) 146 147 148# find our home 149ROOT = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '../..')) 150os.chdir(ROOT) 151 152# parse command line 153argp = argparse.ArgumentParser(description='include guard checker') 154argp.add_argument('-f', '--fix', default=False, action='store_true') 155argp.add_argument('--precommit', default=False, action='store_true') 156args = argp.parse_args() 157 158KNOWN_BAD = set([ 159 'src/core/ext/filters/client_channel/health/health.pb.h', 160 'src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.h', 161 'src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/duration.pb.h', 162 'src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/timestamp.pb.h', 163 'src/core/tsi/alts/handshaker/altscontext.pb.h', 164 'src/core/tsi/alts/handshaker/handshaker.pb.h', 165 'src/core/tsi/alts/handshaker/transport_security_common.pb.h', 166 'include/grpc++/ext/reflection.grpc.pb.h', 167 'include/grpc++/ext/reflection.pb.h', 168]) 169 170grep_filter = r"grep -E '^(include|src/core)/.*\.h$'" 171if args.precommit: 172 git_command = 'git diff --name-only HEAD' 173else: 174 git_command = 'git ls-tree -r --name-only -r HEAD' 175 176FILE_LIST_COMMAND = ' | '.join((git_command, grep_filter)) 177 178# scan files 179ok = True 180filename_list = [] 181try: 182 filename_list = subprocess.check_output(FILE_LIST_COMMAND, 183 shell=True).splitlines() 184 # Filter out non-existent files (ie, file removed or renamed) 185 filename_list = (f for f in filename_list if os.path.isfile(f)) 186except subprocess.CalledProcessError: 187 sys.exit(0) 188 189validator = GuardValidator() 190 191for filename in filename_list: 192 if filename in KNOWN_BAD: continue 193 # Skip check for upb generated code. 194 if filename.endswith('.upb.h') or filename.endswith('.upb.c'): 195 continue 196 ok = ok and validator.check(filename, args.fix) 197 198sys.exit(0 if ok else 1) 199