-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcross_file_analysis_ast.py
More file actions
executable file
·1234 lines (1018 loc) · 51.2 KB
/
cross_file_analysis_ast.py
File metadata and controls
executable file
·1234 lines (1018 loc) · 51.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
"""
Enhanced cross-file dependency analysis with AST-based caller identification.
Author: Vaibhav-api-code
Co-Author: Claude Code (https://claude.ai/code)
Created: 2025-07-20
Updated: 2025-07-23
License: Mozilla Public License 2.0 (MPL-2.0)
"""
import subprocess
import json
import re
import sys
import argparse
from pathlib import Path
import shutil
import os
from collections import Counter, defaultdict
from typing import List, Dict, Optional, Tuple, Set
import ast
# Try to import javalang for Java AST support
# Import standard argument parser
try:
from enhanced_standard_arg_parser import create_analyze_parser
HAS_STANDARD_PARSER = True
except ImportError:
HAS_STANDARD_PARSER = False
create_analyze_parser = None
# Import preflight checks
try:
from preflight_checks import run_preflight_checks, PreflightChecker
except ImportError:
def run_preflight_checks(checks, exit_on_fail=True):
pass
class PreflightChecker:
@staticmethod
def check_file_readable(path):
return True, ""
@staticmethod
def check_directory_accessible(path):
return True, ""
@staticmethod
def check_ripgrep_installed():
return True, ""
@staticmethod
def check_regex_pattern(pattern):
return True, ""
try:
import javalang
JAVALANG_AVAILABLE = True
except ImportError:
JAVALANG_AVAILABLE = False
# Import common utilities if available
try:
from common_utils import check_ripgrep, detect_language, add_common_args
except ImportError:
# Fallback if common_utils not available
def check_ripgrep():
if not shutil.which('rg'):
print("Error: ripgrep (rg) is not installed or not in your PATH.", file=sys.stderr)
print("Please install it from: https://github.com/BurntSushi/ripgrep#installation", file=sys.stderr)
sys.exit(1)
# Import AST context finder
try:
from ast_context_finder import ASTContextFinder
HAS_AST_CONTEXT = True
except ImportError:
HAS_AST_CONTEXT = False
class CallerIdentifier:
"""Identifies the enclosing function/method for a given line using AST."""
@staticmethod
def find_enclosing_function_python(filepath: str, line_number: int) -> Optional[str]:
"""
Find the enclosing function for a line in Python code.
Args:
filepath: Path to the Python file
line_number: Line number (1-indexed)
Returns:
Function name or None
"""
try:
# Check file size before reading (prevent memory issues)
file_path_obj = Path(filepath)
if file_path_obj.stat().st_size > 5 * 1024 * 1024: # 5MB limit
print(f"Warning: File '{filepath}' is very large ({file_path_obj.stat().st_size // (1024*1024)}MB), skipping AST parsing", file=sys.stderr)
return CallerIdentifier.find_enclosing_function_regex(filepath, line_number)
with open(filepath, 'r', encoding='utf-8') as f:
code = f.read()
# Basic sanity check on file content
if len(code.strip()) == 0:
return None
tree = ast.parse(code)
class FunctionFinder(ast.NodeVisitor):
def __init__(self, target_line):
self.target_line = target_line
self.best_match = None
self.best_span = float('inf')
def visit_FunctionDef(self, node):
# Check if our target line is within this function
if hasattr(node, 'lineno'):
start_line = node.lineno
end_line = getattr(node, 'end_lineno', None)
# If end_lineno not available, estimate from the AST
if end_line is None:
# Find the last line by looking at all child nodes
end_line = start_line
for child in ast.walk(node):
if hasattr(child, 'lineno') and child.lineno > end_line:
end_line = child.lineno
# Check if target line is within this function
if start_line <= self.target_line <= end_line:
span = end_line - start_line
# Prefer the innermost (smallest span) function
if span < self.best_span:
self.best_span = span
self.best_match = node.name
# Continue visiting children for nested functions
self.generic_visit(node)
def visit_AsyncFunctionDef(self, node):
self.visit_FunctionDef(node)
def get_result(self):
return self.best_match
finder = FunctionFinder(line_number)
finder.visit(tree)
return finder.get_result()
except Exception as e:
# Fall back to None if AST parsing fails
return None
@staticmethod
def find_enclosing_function_java(filepath: str, line_number: int) -> Optional[str]:
"""
Find the enclosing method for a line in Java code with nested class support.
This enhanced version correctly handles:
- Regular methods in classes
- Methods in static nested classes
- Methods in inner classes
- Methods in anonymous classes
- Multiple levels of nesting
Args:
filepath: Path to the Java file
line_number: Line number (1-indexed)
Returns:
Method name with context (e.g., "OuterClass.InnerClass.methodName") or None
"""
if not JAVALANG_AVAILABLE:
return None
try:
# Check file size before reading (prevent memory issues)
file_path_obj = Path(filepath)
if file_path_obj.stat().st_size > 5 * 1024 * 1024: # 5MB limit
print(f"Warning: File '{filepath}' is very large ({file_path_obj.stat().st_size // (1024*1024)}MB), skipping AST parsing", file=sys.stderr)
return CallerIdentifier.find_enclosing_function_regex(filepath, line_number)
with open(filepath, 'r', encoding='utf-8') as f:
code = f.read()
# Basic sanity check on file content
if len(code.strip()) == 0:
return None
tree = javalang.parse.parse(code)
# Build a mapping of line numbers to AST nodes
line_to_node = {}
# Custom walker to build line mapping and track nesting
class JavaASTWalker:
def __init__(self):
self.class_stack = [] # Track nested classes
self.method_candidates = [] # (line, end_line, full_name, node)
def walk(self, node, path=None):
if path is None:
path = []
# Handle class declarations (including nested)
if isinstance(node, (javalang.tree.ClassDeclaration,
javalang.tree.InterfaceDeclaration,
javalang.tree.EnumDeclaration,
javalang.tree.AnnotationDeclaration)):
self.class_stack.append(node.name)
current_class_path = ".".join(self.class_stack)
# Process children
for child_name, child in node:
if child:
if isinstance(child, list):
for item in child:
self.walk(item, path + [child_name])
else:
self.walk(child, path + [child_name])
self.class_stack.pop()
# Handle anonymous classes and lambda expressions
elif isinstance(node, javalang.tree.ClassCreator):
# Anonymous class - use a generic name
anonymous_name = f"Anonymous${len(self.class_stack)}"
self.class_stack.append(anonymous_name)
# Process the body of anonymous class
if hasattr(node, 'body') and node.body:
for item in node.body:
self.walk(item, path + ['body'])
self.class_stack.pop()
# Handle lambda expressions (Java 8+)
elif isinstance(node, javalang.tree.LambdaExpression):
# Lambda expressions can contain method calls
lambda_name = f"Lambda${len(self.class_stack)}"
self.class_stack.append(lambda_name)
if hasattr(node, 'body') and node.body:
self.walk(node.body, path + ['lambda_body'])
self.class_stack.pop()
# Handle method declarations
elif isinstance(node, javalang.tree.MethodDeclaration):
if hasattr(node, 'position') and node.position:
start_line = node.position.line
# Estimate method end line by looking at the method body
end_line = self._estimate_method_end_line(node, start_line, code)
# Build full method name with class context
class_context = ".".join(self.class_stack) if self.class_stack else ""
full_method_name = f"{class_context}.{node.name}" if class_context else node.name
self.method_candidates.append((start_line, end_line, full_method_name, node))
# Still process method body for nested classes/lambdas
for child_name, child in node:
if child and child_name == 'body':
if isinstance(child, list):
for item in child:
self.walk(item, path + [child_name])
else:
self.walk(child, path + [child_name])
# Handle constructor declarations
elif isinstance(node, javalang.tree.ConstructorDeclaration):
if hasattr(node, 'position') and node.position:
start_line = node.position.line
end_line = self._estimate_method_end_line(node, start_line, code)
class_context = ".".join(self.class_stack) if self.class_stack else ""
constructor_name = f"{class_context}.<init>" if class_context else "<init>"
self.method_candidates.append((start_line, end_line, constructor_name, node))
# Process constructor body for nested constructs
if hasattr(node, 'body') and node.body:
for stmt in node.body:
self.walk(stmt, path + ['constructor_body'])
# Handle static/instance initializers
elif isinstance(node, javalang.tree.BlockStatement):
# Static blocks and instance initializers
parent_path = path[-1] if path else ""
if parent_path in ['static_initializers', 'instance_initializers']:
class_context = ".".join(self.class_stack) if self.class_stack else ""
initializer_name = f"{class_context}.<{parent_path}>" if class_context else f"<{parent_path}>"
if hasattr(node, 'position') and node.position:
start_line = node.position.line
end_line = self._estimate_block_end_line(node, start_line, code)
self.method_candidates.append((start_line, end_line, initializer_name, node))
# Continue processing block statements
for child_name, child in node:
if child:
if isinstance(child, list):
for item in child:
self.walk(item, path + [child_name])
else:
self.walk(child, path + [child_name])
# Continue walking for other node types
else:
for child_name, child in node:
if child:
if isinstance(child, list):
for item in child:
self.walk(item, path + [child_name])
else:
self.walk(child, path + [child_name])
def _estimate_method_end_line(self, method_node, start_line: int, source_code: str) -> int:
"""
Estimate the end line of a method by counting braces.
Enhanced version that handles nested braces and complex syntax.
"""
lines = source_code.split('\n')
if start_line > len(lines):
return start_line
return self._estimate_block_end_line_enhanced(start_line, lines)
def _estimate_block_end_line(self, block_node, start_line: int, source_code: str) -> int:
"""
Estimate the end line of a block statement.
"""
lines = source_code.split('\n')
if start_line > len(lines):
return start_line
return self._estimate_block_end_line_enhanced(start_line, lines)
def _estimate_block_end_line_enhanced(self, start_line: int, lines: List[str]) -> int:
"""
Enhanced brace counting that handles complex Java syntax including:
- Nested braces in lambdas, anonymous classes, array initializers
- String literals with escape sequences
- Block comments spanning multiple lines
- Generic type parameters with angle brackets
"""
brace_count = 0
found_opening_brace = False
in_block_comment = False
for i in range(start_line - 1, len(lines)):
line = lines[i]
# Handle multi-line block comments
if in_block_comment:
end_comment = line.find('*/')
if end_comment != -1:
in_block_comment = False
line = line[end_comment + 2:] # Continue processing rest of line
else:
continue # Skip entire line if still in comment
# Process character by character
in_string = False
in_char = False
escape_next = False
j = 0
while j < len(line):
char = line[j]
if escape_next:
escape_next = False
j += 1
continue
if char == '\\':
escape_next = True
elif char == '"' and not in_char:
in_string = not in_string
elif char == "'" and not in_string:
in_char = not in_char
elif not in_string and not in_char:
if char == '{':
brace_count += 1
found_opening_brace = True
elif char == '}':
brace_count -= 1
if found_opening_brace and brace_count == 0:
return i + 1 # Found the end
elif line[j:j+2] == '//':
break # Rest of line is comment
elif line[j:j+2] == '/*':
# Start of block comment
end_comment = line.find('*/', j + 2)
if end_comment != -1:
j = end_comment + 1 # Skip past end of comment
else:
in_block_comment = True
break # Comment continues to next line
j += 1
# If we couldn't find the end, estimate based on typical method length
# but cap it to prevent runaway estimates
return min(start_line + 100, len(lines))
# Walk the AST and build method candidates
walker = JavaASTWalker()
walker.walk(tree)
# Find the innermost method containing our target line
best_match = None
best_span = float('inf')
for start_line, end_line, method_name, node in walker.method_candidates:
if start_line <= line_number <= end_line:
span = end_line - start_line
if span < best_span:
best_span = span
best_match = method_name
return best_match
except Exception as e:
# If AST parsing fails, fall back to the regex method
return CallerIdentifier.find_enclosing_function_regex(filepath, line_number)
@staticmethod
def find_enclosing_function(filepath: str, line_number: int) -> Optional[str]:
"""
Find the enclosing function/method for a line.
Args:
filepath: Path to the file
line_number: Line number (1-indexed)
Returns:
Function/method name or None
"""
if filepath.endswith('.py'):
return CallerIdentifier.find_enclosing_function_python(filepath, line_number)
elif filepath.endswith('.java') and JAVALANG_AVAILABLE:
return CallerIdentifier.find_enclosing_function_java(filepath, line_number)
else:
# Fallback to regex-based extraction
return CallerIdentifier.find_enclosing_function_regex(filepath, line_number)
@staticmethod
def find_enclosing_function_regex(filepath: str, line_number: int) -> Optional[str]:
"""
Fallback regex-based function finder.
Args:
filepath: Path to the file
line_number: Line number (1-indexed)
Returns:
Function name or None
"""
try:
with open(filepath, 'r', encoding='utf-8') as f:
lines = f.readlines()
# Search backwards from the target line for a function definition
for i in range(line_number - 1, -1, -1):
line = lines[i]
# Python function
match = re.match(r'^\s*def\s+(\w+)\s*\(', line)
if match:
return match.group(1)
# Java/C++ method
match = re.match(r'^\s*(?:public|private|protected|static|\s)+\w+\s+(\w+)\s*\(', line)
if match:
return match.group(1)
# JavaScript function
match = re.match(r'^\s*(?:function\s+)?(\w+)\s*(?:=\s*function\s*)?\(', line)
if match:
return match.group(1)
return None
except Exception:
return None
def find_all_callers(target: str, scope: str = ".", language: str = None,
whole_word: bool = True, ignore_case: bool = False) -> str:
"""
Finds all files and locations that reference the target using ripgrep's JSON output.
Args:
target: The method or class name to search for.
scope: The directory to search within.
language: The language to filter for (e.g., 'java', 'python').
whole_word: Whether to match whole words only.
ignore_case: Whether to ignore case in matching.
Returns:
The raw JSON string output from ripgrep.
"""
check_ripgrep()
# Input validation and sanitization
if not target or not target.strip():
print("Error: Target cannot be empty", file=sys.stderr)
return ""
# Sanitize target to prevent command injection
if any(char in target for char in ['`', '$', ';', '|', '&', '>', '<', '(', ')', '\n', '\r']):
print(f"Error: Invalid characters in target '{target}'", file=sys.stderr)
return ""
# Validate scope path
scope_path = Path(scope)
if not scope_path.exists():
print(f"Error: Scope path '{scope}' does not exist", file=sys.stderr)
return ""
# Sanitize language parameter
if language and not re.match(r'^[a-zA-Z0-9_-]+$', language):
print(f"Error: Invalid language identifier '{language}'", file=sys.stderr)
return ""
# Use ripgrep with JSON output for robust parsing
cmd = ['rg', '--json']
if whole_word:
cmd.append('-w')
if ignore_case:
cmd.append('-i')
if language:
cmd.extend(['-t', language])
# Add the search pattern
cmd.append(target)
# Use absolute path for scope to prevent path traversal
cmd.append(str(scope_path.resolve()))
try:
# Enhanced subprocess security
result = subprocess.run(
cmd,
capture_output=True,
text=True,
timeout=120,
cwd=scope_path.resolve(), # Set working directory explicitly
shell=False, # Never use shell=True
env={'PATH': os.environ.get('PATH', '')}, # Minimal environment
check=False # Don't raise on non-zero exit
)
if result.returncode != 0 and result.stderr:
print(f"Warning: ripgrep returned error: {result.stderr.strip()}", file=sys.stderr)
return result.stdout
except subprocess.TimeoutExpired:
print(f"Search timed out for target: {target}", file=sys.stderr)
return ""
except Exception as e:
print(f"Search failed: {e}", file=sys.stderr)
return ""
def is_likely_definition(content: str, filepath: str) -> bool:
"""
Determines if a line is likely a definition rather than a usage.
Args:
content: The line content.
filepath: The file path (to determine language).
Returns:
True if likely a definition, False otherwise.
"""
content_lower = content.lower().strip()
# Language-specific definition patterns
if filepath.endswith(('.py', '.pyw')):
# Python function/class definitions
return bool(re.match(r'^\s*(def|class)\s+', content))
elif filepath.endswith(('.java', '.scala', '.kt')):
# Java/JVM language method/class definitions
# Look for access modifiers, return types, etc.
patterns = [
r'^\s*(public|private|protected|static|final|abstract|synchronized|native|strictfp)*\s*class\s+',
r'^\s*(public|private|protected|static|final|abstract|synchronized|native)*\s*\w+\s+\w+\s*\(',
r'^\s*@\w+\s*$', # Annotations often precede definitions
]
return any(re.search(pattern, content) for pattern in patterns)
elif filepath.endswith(('.js', '.jsx', '.ts', '.tsx')):
# JavaScript/TypeScript definitions
patterns = [
r'^\s*function\s+\w+\s*\(',
r'^\s*(?:export\s+)?(?:async\s+)?function\s+',
r'^\s*class\s+\w+',
r'^\s*(?:const|let|var)\s+\w+\s*=\s*(?:async\s+)?(?:function|\([^)]*\)\s*=>)',
]
return any(re.search(pattern, content) for pattern in patterns)
elif filepath.endswith(('.c', '.cpp', '.cc', '.cxx', '.h', '.hpp')):
# C/C++ definitions
# Look for function signatures (return type followed by name and parentheses)
return bool(re.search(r'^\s*(?:\w+\s+)*\w+\s+\w+\s*\([^)]*\)\s*\{?', content))
elif filepath.endswith('.go'):
# Go definitions
return bool(re.match(r'^\s*func\s+', content))
elif filepath.endswith('.rs'):
# Rust definitions
return bool(re.match(r'^\s*(fn|struct|enum|trait|impl)\s+', content))
elif filepath.endswith('.rb'):
# Ruby definitions
return bool(re.match(r'^\s*(def|class|module)\s+', content))
elif filepath.endswith('.php'):
# PHP definitions
return bool(re.match(r'^\s*(function|class)\s+', content))
# Generic heuristic for unknown languages
# Look for common definition keywords
generic_patterns = [
r'^\s*(function|def|class|struct|interface|enum)\s+',
r'^\s*(public|private|protected)\s+.*\(',
]
return any(re.search(pattern, content_lower) for pattern in generic_patterns)
def parse_caller_results(output: str) -> List[Dict]:
"""
Parses ripgrep's JSON output to extract structured caller information.
Enhanced with memory safety and security checks.
Args:
output: The raw JSON string from ripgrep.
Returns:
A list of dictionaries, where each dictionary represents a caller.
"""
callers = []
line_count = 0
max_lines = 10000 # Prevent memory issues with huge outputs
for line in output.strip().split('\n'):
line_count += 1
if line_count > max_lines:
print(f"Warning: Output truncated after {max_lines} lines", file=sys.stderr)
break
if not line:
continue
try:
data = json.loads(line)
# We only care about the 'match' events
if data['type'] == 'match':
match_data = data['data']
# Security: Validate file path
file_path = match_data['path']['text']
if not file_path or '..' in file_path:
continue # Skip suspicious paths
# Extract context lines if available
context_before = []
context_after = []
# ripgrep JSON format includes submatches for exact positions
submatches = match_data.get('submatches', [])
match_positions = [(sm['start'], sm['end']) for sm in submatches] if submatches else []
# Determine if this is a definition or usage
content = match_data['lines']['text'].strip()
# Limit content length to prevent memory issues
if len(content) > 1000:
content = content[:1000] + "..."
is_definition = is_likely_definition(content, file_path)
callers.append({
'file': file_path,
'line_number': match_data['line_number'],
'content': content,
'match_positions': match_positions,
'context_before': context_before,
'context_after': context_after,
'is_definition': is_definition
})
# Limit total results to prevent memory issues
if len(callers) > 5000:
print(f"Warning: Results truncated after {len(callers)} matches", file=sys.stderr)
break
except (json.JSONDecodeError, KeyError, ValueError) as e:
# Log parsing errors but continue
if line_count <= 10: # Only log first few errors
print(f"Warning: Failed to parse line {line_count}: {e}", file=sys.stderr)
continue
except Exception as e:
print(f"Unexpected error parsing line {line_count}: {e}", file=sys.stderr)
continue
return callers
def highlight_match_enhanced(content: str, match_positions: List[Tuple[int, int]]) -> str:
"""
Enhanced highlighting that colors only the exact match positions.
Args:
content: The line content.
match_positions: List of (start, end) positions for matches.
Returns:
Content with highlighted matches.
"""
if not match_positions:
return content
# Sort positions by start index
positions = sorted(match_positions, key=lambda x: x[0])
# Build highlighted string
result = []
last_end = 0
for start, end in positions:
# Adjust positions - ripgrep uses byte offsets, we need character offsets
# For simplicity, we'll use the positions as-is
start = max(0, start)
end = min(len(content), end)
# Add text before match
if start > last_end:
result.append(content[last_end:start])
# Add highlighted match
if start < len(content) and end <= len(content):
result.append(f"\033[93m{content[start:end]}\033[0m") # Yellow highlight
last_end = end
# Add remaining text
if last_end < len(content):
result.append(content[last_end:])
return ''.join(result)
def analyze_method_usage(calls: List[Dict], target: str) -> Dict:
"""
Analyzes a list of method calls to calculate frequency and usage patterns.
Args:
calls: A list of call-site dictionaries from parse_caller_results.
target: The target method/class name for context.
Returns:
A dictionary containing the analysis.
"""
# Separate definitions from usages
definitions = [c for c in calls if c.get('is_definition', False)]
usages = [c for c in calls if not c.get('is_definition', False)]
analysis = {
'target': target,
'total_references': len(calls),
'total_definitions': len(definitions),
'total_usages': len(usages),
'unique_files': len(set(c['file'] for c in calls)),
'by_file': Counter(c['file'] for c in usages), # Count only usages per file
'definitions': definitions,
'calling_contexts': Counter(),
'usage_patterns': defaultdict(list),
'enclosing_functions': Counter() # NEW: Track which functions call our target
}
# Analyze calling contexts and identify enclosing functions
for call in usages:
content = call['content']
content_lower = content.lower()
# Identify the enclosing function using AST
enclosing_func = CallerIdentifier.find_enclosing_function(
call['file'],
call['line_number']
)
if enclosing_func:
analysis['enclosing_functions'][enclosing_func] += 1
# Detect calling context
if re.search(r'\bif\s*\(|else\s+if\s*\(|\belse\b', content):
analysis['calling_contexts']['conditional'] += 1
analysis['usage_patterns']['conditional'].append(call)
elif re.search(r'\bfor\s*\(|\bwhile\s*\(|\bdo\s*{', content):
analysis['calling_contexts']['loop'] += 1
analysis['usage_patterns']['loop'].append(call)
elif re.search(r'\breturn\s+', content):
analysis['calling_contexts']['return_statement'] += 1
analysis['usage_patterns']['return'].append(call)
elif re.search(r'\bcatch\s*\(|\btry\s*{|\bthrow\s+', content):
analysis['calling_contexts']['exception_handling'] += 1
analysis['usage_patterns']['exception'].append(call)
elif re.search(r'=\s*' + re.escape(target), content):
analysis['calling_contexts']['assignment'] += 1
analysis['usage_patterns']['assignment'].append(call)
elif re.search(r'\bnew\s+' + re.escape(target), content):
analysis['calling_contexts']['instantiation'] += 1
analysis['usage_patterns']['instantiation'].append(call)
else:
analysis['calling_contexts']['other'] += 1
analysis['usage_patterns']['other'].append(call)
# Detect if this is likely a utility method (used across many files)
if analysis['unique_files'] > 5 and analysis['total_usages'] > 10:
analysis['likely_utility'] = True
else:
analysis['likely_utility'] = False
return analysis
def build_recursive_call_tree_enhanced(target: str, scope: str, language: str,
whole_word: bool, ignore_case: bool,
max_depth: int, current_depth: int = 0,
seen_targets: Optional[Set[str]] = None) -> Dict:
"""
Enhanced recursive call tree building using AST-based caller identification.
Args:
target: The method/class to analyze.
scope: Directory to search.
language: Programming language filter.
whole_word: Whether to match whole words only.
ignore_case: Whether to ignore case.
max_depth: Maximum recursion depth.
current_depth: Current recursion depth.
seen_targets: Set of already analyzed targets to prevent cycles.
Returns:
Dictionary representing the call tree.
"""
if seen_targets is None:
seen_targets = set()
# Prevent infinite recursion
if current_depth >= max_depth or target in seen_targets:
return {'target': target, 'depth': current_depth, 'truncated': True}
seen_targets.add(target)
# Find all callers of this target
search_results = find_all_callers(target, scope, language, whole_word, ignore_case)
callers = parse_caller_results(search_results)
# Separate definitions from usages
definitions = [c for c in callers if c.get('is_definition', False)]
usages = [c for c in callers if not c.get('is_definition', False)]
# Build node
node = {
'target': target,
'depth': current_depth,
'total_references': len(callers),
'definitions': len(definitions),
'usages': len(usages),
'callers': []
}
# Extract unique caller functions using AST
caller_functions = Counter()
for usage in usages:
enclosing_func = CallerIdentifier.find_enclosing_function(
usage['file'],
usage['line_number']
)
if enclosing_func and enclosing_func != target:
caller_functions[enclosing_func] += 1
# Recursively analyze top caller functions
for caller_func, count in caller_functions.most_common(5): # Top 5 callers
if caller_func not in seen_targets:
child_node = build_recursive_call_tree_enhanced(
caller_func, scope, language, whole_word, ignore_case,
max_depth, current_depth + 1, seen_targets
)
child_node['call_count'] = count # Add how many times it calls the target
node['callers'].append(child_node)
return node
def format_call_tree_enhanced(tree: Dict, indent: str = "") -> str:
"""
Enhanced call tree formatting with call counts.
Args:
tree: The call tree dictionary.
indent: Current indentation level.
Returns:
Formatted string representation.
"""
output = []
# Format current node
truncated = " [TRUNCATED]" if tree.get('truncated', False) else ""
refs = f"({tree.get('usages', 0)} usages, {tree.get('definitions', 0)} defs)"
call_count = f" ×{tree.get('call_count', '')}" if tree.get('call_count') else ""
output.append(f"{indent}└─ {tree['target']}{call_count} {refs}{truncated}")
# Format children
for i, child in enumerate(tree.get('callers', [])):
is_last = i == len(tree['callers']) - 1
child_indent = indent + (" " if is_last else "│ ")
output.extend(format_call_tree_enhanced(child, child_indent).split('\n'))
return '\n'.join(output).rstrip()
def format_analysis_report(analysis: Dict, show_samples: bool = True,
max_samples: int = 3, show_ast_context: bool = False) -> str:
"""
Formats the usage analysis into a readable report with enhanced function tracking.
"""
# Initialize AST context finder if needed
context_finder = None
if show_ast_context and HAS_AST_CONTEXT:
context_finder = ASTContextFinder()
output = []
# Header
output.append("=" * 80)
output.append(f"CROSS-FILE USAGE ANALYSIS: '{analysis['target']}'")
output.append("=" * 80)
# Summary statistics
output.append(f"\n📊 SUMMARY STATISTICS")
output.append("-" * 40)
output.append(f"Total References: {analysis['total_references']}")
output.append(f" Definitions: {analysis['total_definitions']}")
output.append(f" Usages: {analysis['total_usages']}")
output.append(f"Unique Files: {analysis['unique_files']}")
output.append(f"Average Usages per File: {analysis['total_usages'] / max(1, analysis['unique_files']):.1f}")
if analysis['likely_utility']:
output.append("\n⚡ This appears to be a utility method (used across many files)")
# Show definitions if any
if analysis['definitions']:
output.append(f"\n📌 DEFINITIONS FOUND")
output.append("-" * 40)
for defn in analysis['definitions'][:3]: # Show up to 3 definitions
# Get AST context if available
context_str = ""
if context_finder:
context = context_finder._format_context_parts(
context_finder.get_context_for_line(defn['file'], defn['line_number'])
)
if context:
context_str = f" [{context}]"
output.append(f" {Path(defn['file']).name}:{defn['line_number']}{context_str}")
highlighted = highlight_match_enhanced(defn['content'], defn.get('match_positions', []))
output.append(f" {highlighted}")
if len(analysis['definitions']) > 3:
output.append(f" ... and {len(analysis['definitions']) - 3} more")
# NEW: Show enclosing functions
if analysis.get('enclosing_functions'):
output.append(f"\n🔗 CALLING FUNCTIONS")
output.append("-" * 40)
for func_name, count in analysis['enclosing_functions'].most_common(10):
output.append(f" {func_name}() - {count} call{'s' if count > 1 else ''}")
if len(analysis['enclosing_functions']) > 10:
output.append(f" ... and {len(analysis['enclosing_functions']) - 10} more")
# Top files by usage
output.append(f"\n📁 TOP FILES BY USAGE (excluding definitions)")
output.append("-" * 40)
for file_path, count in analysis['by_file'].most_common(10):
file_name = Path(file_path).name
output.append(f" {count:3d}x {file_name:30} ({file_path})")
if len(analysis['by_file']) > 10:
output.append(f" ... and {len(analysis['by_file']) - 10} more files")
# Calling contexts