1
1
import os
2
+ from pdb import set_trace
2
3
import platform
3
4
from clang .cindex import Config
4
5
from pathlib import Path
@@ -19,16 +20,27 @@ class ClangAnalyzer:
19
20
"""Analyzes C code using Clang's Python bindings."""
20
21
21
22
def __init__ (self , compilation_database_path : Optional [Path ] = None ):
23
+ # # Let's turn off Address sanitization for parsing code
24
+ # # Initialize libclang at module level
25
+ # try:
26
+ if platform .system () == "Darwin" :
27
+ possible_paths = [
28
+ "/opt/homebrew/opt/llvm/lib/libclang.dylib" , # Apple Silicon
29
+ "/usr/local/opt/llvm/lib/libclang.dylib" , # Intel Mac
30
+ "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libclang.dylib" ,
31
+ ]
22
32
23
- # Initialize libclang at module level
24
- try :
25
- Config .set_library_file (self .__find_libclang ())
26
- except Exception as e :
27
- logger .error (f"Failed to initialize libclang: { e } " )
28
- raise Exception ("Failed to initialize libclang" )
33
+ # We could not find libclang. Raise an error and provide instructions.
34
+ if len (possible_paths ) == 0 :
35
+ raise RuntimeError ("Install LLVM 18 using: brew install llvm@18" )
36
+
37
+ # Check each possible path and return the first one that exists
38
+ for path in possible_paths :
39
+ if os .path .exists (path ):
40
+ logger .info (f"Found libclang at: { path } " )
41
+ # Configure Clang before creating the Index
42
+ Config .set_library_file (path )
29
43
30
- logger .info ("Successfully initialized libclang" )
31
- # Configure Clang before creating the Index
32
44
self .index = Index .create ()
33
45
self .compilation_database = None
34
46
# TODO: Implement compilation database for C/C++ projects so that we can get compile arguments for each file
@@ -59,8 +71,8 @@ def __find_libclang(self) -> str:
59
71
from pathlib import Path
60
72
61
73
lib_paths = [Path ("/usr/lib" ), Path ("/usr/lib64" )]
62
- possible_paths = [str (p ) for base in lib_paths if base .exists () for p in base .rglob ("libclang*.so*" )]
63
-
74
+ possible_paths = [str (p ) for base in lib_paths if base .exists () for p in base .rglob ("libclang*.so.17 *" )]
75
+ print ( possible_paths )
64
76
install_instructions = "Install libclang development package using your system's package manager"
65
77
else :
66
78
raise RuntimeError (f"Unsupported operating system: { system } " )
@@ -98,7 +110,7 @@ def analyze_file(self, file_path: Path) -> CTranslationUnit:
98
110
return translation_unit
99
111
100
112
def _process_translation_unit (self , cursor , translation_unit : CTranslationUnit ):
101
- """Processes all declarations in a translation unit."""
113
+ """Should process all declarations in a translation unit."""
102
114
103
115
for child in cursor .get_children ():
104
116
if child .location .file and str (child .location .file ) != translation_unit .file_path :
0 commit comments