11import os
2+ from pdb import set_trace
23import platform
34from clang .cindex import Config
45from pathlib import Path
@@ -19,16 +20,27 @@ class ClangAnalyzer:
1920 """Analyzes C code using Clang's Python bindings."""
2021
2122 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+ ]
2232
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 )
2943
30- logger .info ("Successfully initialized libclang" )
31- # Configure Clang before creating the Index
3244 self .index = Index .create ()
3345 self .compilation_database = None
3446 # 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:
5971 from pathlib import Path
6072
6173 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 )
6476 install_instructions = "Install libclang development package using your system's package manager"
6577 else :
6678 raise RuntimeError (f"Unsupported operating system: { system } " )
@@ -98,7 +110,7 @@ def analyze_file(self, file_path: Path) -> CTranslationUnit:
98110 return translation_unit
99111
100112 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."""
102114
103115 for child in cursor .get_children ():
104116 if child .location .file and str (child .location .file ) != translation_unit .file_path :
0 commit comments