2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Counts the number of #if or #ifdef lines containing at least one
7 preprocessor token that is a full match for the given pattern, in the
18 # Filename extensions we know will be handled by the C preprocessor.
19 # We ignore files not matching these.
28 def _IsTestFile(filename):
29 """Does a rudimentary check to try to skip test files; this could be
30 improved but is good enough for basic metrics generation.
32 return re.match('(test|mock|dummy)_.*|.*_[a-z]*test\.(h|cc|mm)', filename)
35 def CountIfdefs(token_pattern, directory, skip_tests=False):
36 """Returns the number of lines in files in |directory| and its
37 subdirectories that have an extension from |CPP_EXTENSIONS| and are
38 an #if or #ifdef line with a preprocessor token fully matching
39 the string |token_pattern|.
41 If |skip_tests| is true, a best effort is made to ignore test files.
43 token_line_re = re.compile(r'^#if(def)?.*\b(%s)\b.*$' % token_pattern)
45 for root, dirs, files in os.walk(directory):
46 for filename in files:
47 if os.path.splitext(filename)[1] in CPP_EXTENSIONS:
48 if not skip_tests or not _IsTestFile(filename):
49 with open(os.path.join(root, filename)) as f:
52 if token_line_re.match(line):
58 print "Usage: %s [--skip-tests] TOKEN_PATTERN DIRECTORY" % sys.argv[0]
62 option_parser = optparse.OptionParser()
63 option_parser.add_option('', '--skip-tests', action='store_true',
64 dest='skip_tests', default=False,
65 help='Skip test files.')
66 options, args = option_parser.parse_args()
72 print CountIfdefs(args[0], args[1], options.skip_tests)
76 if __name__ == '__main__':