import os import re def extract_variable_name(var_expr): # Remove strings and character literals to avoid interference var_expr_no_strings = re.sub(r'"[^"]*"|\'[^\']*\'', '', var_expr) # Replace function calls with their names var_expr_no_strings = re.sub(r'\b(\w+)\s*\([^()]*\)', r'\1', var_expr_no_strings) # Extract variable-like tokens, including member access and pointers tokens = re.findall(r'\b(?:\w+)(?:->\w+|\.\w+)+\b', var_expr_no_strings) # If no tokens with member access, include standalone variables if not tokens: tokens = re.findall(r'\b\w+\b', var_expr_no_strings) # Prioritize tokens with '->' or '.' if tokens: # Choose the longest token variable_name = max(tokens, key=len) else: variable_name = slugify(var_expr) return variable_name def slugify(var_expr): # Remove any quotes var_expr = var_expr.replace('"', '').replace("'", '') # Replace non-alphanumeric characters with underscores var_expr = re.sub(r'\W+', '_', var_expr) # Remove leading/trailing underscores var_expr = var_expr.strip('_') # Optionally limit the length var_expr = var_expr[:50] # limit to 50 characters return var_expr def process_file(file_path): with open(file_path, 'r') as f: content = f.read() original_content = content # Regular expression to match Log macros log_macro_pattern = re.compile(r'(Log\w*\s*\()([^\n]*?\))', re.DOTALL) def replace_log(match): log_call = match.group(1) args_content = match.group(2) # Split arguments, handling nested structures args = split_arguments(args_content.strip(')')) if len(args) < 2: # Not enough arguments to process return match.group(0) format_string = args[0].strip() variables = args[1:] # Remove quotes from format string if (format_string.startswith('"') and format_string.endswith('"')) or \ (format_string.startswith("'") and format_string.endswith("'")): quote_char = format_string[0] format_string_content = format_string[1:-1] else: # Format string is not properly quoted return match.group(0) # Find all '{}' placeholders in format string placeholder_pattern = re.compile(r'(?