1#!/usr/bin/env python3 2 3# Copyright 2022 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 17# Check for includes of the form `#include "bar.h"` - i.e. not including the subdirectory. We require instead `#include "foo/bar.h"`. 18 19import argparse 20import os 21import re 22import sys 23 24# find our home 25ROOT = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '../..')) 26os.chdir(ROOT) 27 28# parse command line 29argp = argparse.ArgumentParser(description='include guard checker') 30argp.add_argument('-f', '--fix', default=False, action='store_true') 31args = argp.parse_args() 32 33# error count 34errors = 0 35 36CHECK_SUBDIRS = [ 37 'src/core', 38 'src/cpp', 39 'test/core', 40 'test/cpp', 41 'fuzztest', 42] 43 44for subdir in CHECK_SUBDIRS: 45 for root, dirs, files in os.walk(subdir): 46 for f in files: 47 if f.endswith('.h') or f.endswith('.cc'): 48 fpath = os.path.join(root, f) 49 output = open(fpath, 'r').readlines() 50 changed = False 51 for (i, line) in enumerate(output): 52 m = re.match(r'^#include "([^"]*)"(.*)', line) 53 if not m: 54 continue 55 include = m.group(1) 56 if '/' in include: 57 continue 58 expect_path = os.path.join(root, include) 59 trailing = m.group(2) 60 if not os.path.exists(expect_path): 61 continue 62 changed = True 63 errors += 1 64 output[i] = '#include "{0}"{1}\n'.format( 65 expect_path, trailing) 66 print("Found naked include '{0}' in {1}".format( 67 include, fpath)) 68 if changed and args.fix: 69 open(fpath, 'w').writelines(output) 70 71if errors > 0: 72 print('{} errors found.'.format(errors)) 73 sys.exit(1) 74