(roots, G=None)
| 68 | |
| 69 | |
| 70 | def build_call_graph_c(roots, G=None): |
| 71 | if G is None: |
| 72 | G = nx.DiGraph() |
| 73 | |
| 74 | new_func = {} |
| 75 | func_to_file = {} |
| 76 | for root in roots: |
| 77 | # print('------ ' + root.attrib['filename'] + ' ------') |
| 78 | |
| 79 | for func_node in root.findall('./srcml:function', namespaces=ns): |
| 80 | |
| 81 | caller_name, start_line, end_line = handle_function(func_node) |
| 82 | if not caller_name: |
| 83 | continue |
| 84 | |
| 85 | if start_line and end_line: |
| 86 | num_lines = end_line - start_line + 1 |
| 87 | else: |
| 88 | # default num_lines is 1 |
| 89 | num_lines = 1 |
| 90 | |
| 91 | if caller_name not in G: |
| 92 | # Case 1: hasn't been defined and hasn't been called |
| 93 | new_func[caller_name] = num_lines |
| 94 | G.add_node(caller_name, num_lines=num_lines, defined=True) |
| 95 | elif not G.node[caller_name]['defined']: |
| 96 | # Case 2: has been called but hasn't been defined |
| 97 | new_func[caller_name] = num_lines |
| 98 | G.node[caller_name]['defined'] = True |
| 99 | G.node[caller_name]['num_lines'] = num_lines |
| 100 | else: |
| 101 | # Case 3: has been called and has been defined |
| 102 | # it is modified in the latest commit |
| 103 | # pass because it's not a new function |
| 104 | # so no need to add it to new_func and to |
| 105 | # update G.node[caller_name]['num_lines'] |
| 106 | pass |
| 107 | |
| 108 | func_to_file[caller_name] = root.attrib['filename'] |
| 109 | |
| 110 | # handle all function calls |
| 111 | for call_node in func_node.xpath('.//srcml:call', namespaces=ns): |
| 112 | |
| 113 | try: |
| 114 | callee_name = handle_call(call_node) |
| 115 | except NotFunctionCallError: |
| 116 | continue |
| 117 | except: |
| 118 | print("Callee name not found! (in func %s)" % caller_name) |
| 119 | continue |
| 120 | |
| 121 | if callee_name not in G: |
| 122 | G.add_node(callee_name, num_lines=1, defined=False) |
| 123 | G.add_edge(caller_name, callee_name) |
| 124 | |
| 125 | return G, new_func, func_to_file |
| 126 | |
| 127 |
no test coverage detected