11import ast
22import logging
33from typing import List , Tuple
4+ import importlib
5+ import inspect
46from .vmlinux_class_handler import process_vmlinux_class
57
68logger = logging .getLogger (__name__ )
@@ -55,7 +57,7 @@ def detect_import_statement(tree: ast.AST) -> List[Tuple[str, str]]:
5557 import_name = alias .name
5658 # Use alias if provided, otherwise use the original name
5759 as_name = alias .asname if alias .asname else alias .name
58- vmlinux_imports .append (("vmlinux" , import_name ))
60+ vmlinux_imports .append (("vmlinux" , node ))
5961 logger .info (f"Found vmlinux import: { import_name } " )
6062
6163 # Handle "import vmlinux" statements (not typical but should be rejected)
@@ -77,20 +79,40 @@ def vmlinux_proc(tree: ast.AST, module):
7779 logger .info ("No vmlinux imports found" )
7880 return
7981
80- vmlinux_types = set ()
81- for module_name , imported_item in import_statements :
82- vmlinux_types .add (imported_item )
83- logger .info (f"Registered vmlinux type: { imported_item } " )
84-
85- for node in ast .walk (tree ):
86- if isinstance (node , ast .ClassDef ):
87- # Check if this class uses vmlinux types
88- logger .info (f"Processing ClassDef with vmlinux types: { node .name } " )
89- process_vmlinux_class (node , module , vmlinux_types )
82+ # Import vmlinux module directly
83+ try :
84+ vmlinux_mod = importlib .import_module ("vmlinux" )
85+ except ImportError :
86+ logger .warning ("Could not import vmlinux module" )
87+ return
9088
91- elif isinstance (node , ast .Assign ):
92- logger .info (f"Processing Assign with vmlinux types" )
93- process_vmlinux_assign (node , module , vmlinux_types )
89+ source_file = inspect .getsourcefile (vmlinux_mod )
90+ if source_file is None :
91+ logger .warning ("Cannot find source for vmlinux module" )
92+ return
9493
95- def process_vmlinux_assign (node , module , vmlinux_types ):
94+ with open (source_file , "r" ) as f :
95+ mod_ast = ast .parse (f .read (), filename = source_file )
96+
97+ for import_mod , import_node in import_statements :
98+ for alias in import_node .names :
99+ imported_name = alias .name
100+ found = False
101+ for mod_node in mod_ast .body :
102+ if isinstance (mod_node , ast .ClassDef ) and mod_node .name == imported_name :
103+ process_vmlinux_class (mod_node , module )
104+ found = True
105+ break
106+ if isinstance (mod_node , ast .Assign ):
107+ for target in mod_node .targets :
108+ if isinstance (target , ast .Name ) and target .id == imported_name :
109+ process_vmlinux_assign (mod_node , module )
110+ found = True
111+ break
112+ if found :
113+ break
114+ if not found :
115+ logger .info (f"{ imported_name } not found as ClassDef or Assign in vmlinux" )
116+
117+ def process_vmlinux_assign (node , module ):
96118 raise NotImplementedError ("Assignment handling has not been implemented yet" )
0 commit comments