Improve check_binding_retval.py: fix regex and brace parsing issues

Co-authored-by: blueloveTH <28104173+blueloveTH@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2025-12-03 08:23:10 +00:00
parent 5d246dff53
commit 7a455d3777

View File

@ -72,15 +72,29 @@ class BindingChecker:
def extract_functions(self, content: str) -> Dict[str, Dict]:
"""Extract all bool-returning functions from C code."""
# Pattern to match bool-returning functions
pattern = r'(?:static\s+)?bool\s+(\w+)\s*\(([^)]*)\)\s*\{([^{}]*(?:\{[^{}]*\}[^{}]*)*)\}'
# Pattern to match function declarations (start of bool functions)
pattern = r'(?:static\s+)?bool\s+(\w+)\s*\(([^)]*)\)\s*\{'
functions = {}
for match in re.finditer(pattern, content, re.MULTILINE | re.DOTALL):
for match in re.finditer(pattern, content):
func_name = match.group(1)
func_params = match.group(2)
func_body = match.group(3)
full_func = match.group(0)
start_pos = match.end() # Position after the opening brace
# Find matching closing brace using brace counting
brace_count = 1
pos = start_pos
while pos < len(content) and brace_count > 0:
if content[pos] == '{':
brace_count += 1
elif content[pos] == '}':
brace_count -= 1
pos += 1
if brace_count == 0:
# Successfully found matching brace
func_body = content[start_pos:pos-1] # Exclude closing brace
full_func = content[match.start():pos]
functions[func_name] = {
'params': func_params,
@ -101,7 +115,7 @@ class BindingChecker:
r'py_bind\s*\([^,]+,\s*"[^"]*",\s*(\w+)\)',
r'py_bindmagic\s*\([^,]+,\s*\w+,\s*(\w+)\)',
r'py_bindmethod\s*\([^,]+,\s*"[^"]+",\s*(\w+)\)',
r'py_bindproperty\s*\([^,]+,\s*"[^"]+",\s*(\w+)',
r'py_bindproperty\s*\([^,]+,\s*"[^"]+",\s*(\w+)(?:,|\))',
]
for pattern in patterns:
@ -131,8 +145,10 @@ class BindingChecker:
return True
# Check for functions that set py_retval internally
# Use word boundaries to avoid matching substrings in comments or other identifiers
for func in RETVAL_SETTING_FUNCTIONS:
if func + '(' in code_without_comments:
pattern = r'\b' + re.escape(func) + r'\s*\('
if re.search(pattern, code_without_comments):
return True
return False