Skip to content

Instantly share code, notes, and snippets.

@daniel-j-h
Last active January 13, 2016 16:44
Show Gist options
  • Select an option

  • Save daniel-j-h/6f7b58236bf0517167f1 to your computer and use it in GitHub Desktop.

Select an option

Save daniel-j-h/6f7b58236bf0517167f1 to your computer and use it in GitHub Desktop.
print function symbols S and function call expressions C to derive S \ C
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import argparse as A, sys as S
from clang.cindex import Index as I, CursorKind as K, CompilationDatabase as D
if __name__ == '__main__':
parser = A.ArgumentParser(description='Customizable Naming Convention Checker')
parser.add_argument('files', help='path to files', metavar='file', nargs='+')
parser.add_argument('--dbdir', dest='dbdir',
help='build path is used to read a `compile_commands.json` compile command database')
mode = parser.add_mutually_exclusive_group(required=True)
mode.add_argument('--fnSymbols', dest='symbols', action="store_true", help='Print function symbols')
mode.add_argument('--fnCalls', dest='calls', action="store_true", help='Print function calls')
args = parser.parse_args()
compiledb = None
if args.dbdir:
compiledb = D.fromDirectory(args.dbdir)
local = lambda node: node.location.file and not node.location.file.name.startswith('/')
sym = lambda node: node.kind in [K.FUNCTION_DECL, K.CXX_METHOD]
call = lambda node: node.kind == K.CALL_EXPR
pred = sym if args.symbols else call
for f in args.files:
commands = None
if compiledb:
commands = compiledb.getCompileCommands(f)
index = I.create()
unit = index.parse(f, args=commands)
cursor = unit.cursor
notify = [node for node in cursor.walk_preorder() if pred(node) and local(node)]
for node in notify:
S.stderr.write('{}\n'.format(node.spelling))
S.exit(1 if notify else 0)
find src include -type f -name '*.hpp' -o -name '*.cpp' \
  | xargs printFunctions.py --dbdir build --fnCalls 2> fnCalls.txt
  
find src include -type f -name '*.hpp' -o -name '*.cpp' \
  | xargs printFunctions.py --dbdir build --fnSymbols 2> fnSymbols.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment