Last active
May 22, 2016 04:11
-
-
Save AndrewWalker/daa2af23f34fe9a6acc2de579ec45535 to your computer and use it in GitHub Desktop.
stackoverflow-answer-37210684
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from clang.cindex import * | |
def is_function_call(funcdecl, c): | |
""" Determine where a call-expression cursor refers to a particular function declaration | |
""" | |
defn = c.get_definition() | |
return (defn is not None) and (defn == funcdecl) | |
def fully_qualified(c): | |
""" Retrieve a fully qualified function name (with namespaces) | |
""" | |
res = c.spelling | |
c = c.semantic_parent | |
while c.kind != CursorKind.TRANSLATION_UNIT: | |
res = c.spelling + '::' + res | |
c = c.semantic_parent | |
return res | |
def find_funcs_and_calls(tu): | |
""" Retrieve lists of function declarations and call expressions in a translation unit | |
""" | |
filename = tu.cursor.spelling | |
calls = [] | |
funcs = [] | |
for c in tu.cursor.walk_preorder(): | |
if c.location.file is None: | |
pass | |
elif c.location.file.name != filename: | |
pass | |
elif c.kind == CursorKind.CALL_EXPR: | |
calls.append(c) | |
elif c.kind == CursorKind.FUNCTION_DECL: | |
funcs.append(c) | |
return funcs, calls | |
idx = Index.create() | |
args = '-x c++ --std=c++11'.split() | |
tu = idx.parse('tmp.cpp', args=args) | |
funcs, calls = find_funcs_and_calls(tu) | |
for f in funcs: | |
print(fully_qualified(f), f.location) | |
for c in calls: | |
if is_function_call(f, c): | |
print('-', c) | |
print() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// tmp.cpp | |
#include <iostream> | |
using namespace std; | |
namespace impl { | |
int addition(int x, int y) { | |
return x + y; | |
} | |
void f() { | |
addition(2, 3); | |
} | |
} | |
int addition (int a, int b) { | |
int r; | |
r=a+b; | |
return r; | |
} | |
int main () { | |
int z, q; | |
z = addition (5,3); | |
q = addition (5,5); | |
cout << "The first result is " << z; | |
cout << "The second result is " << q; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment