From 30635b53e0fde649b3821d6d79cfd3f01adfe0fd Mon Sep 17 00:00:00 2001 From: Evgeny Date: Fri, 13 Feb 2026 02:44:49 +0300 Subject: [PATCH] Simplify toc output format to C-like syntax MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Format: [start-end] declaration; - Normalize multi-line function signatures into single line - Remove 'File:' headers, use blank lines between files - Function declarations shown as prototypes (with semicolon) - Struct/enum declarations preserved with typedef when present - Whitespace normalized (multiple spaces → single space) --- c_util | 103 +++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 90 insertions(+), 13 deletions(-) diff --git a/c_util b/c_util index 086a4475..d8c571d1 100755 --- a/c_util +++ b/c_util @@ -527,10 +527,70 @@ def parse_function(lines_info, start_idx): 'pre': pre_lines, 'start': start_idx, 'end': closing, + 'decl_end': decl_end, # Store the line with opening brace 'line_count': closing - start_idx + 1 } +def normalize_whitespace(text): + """Normalize whitespace: replace multiple spaces/newlines with single space.""" + # Replace all whitespace (spaces, tabs, newlines) with single space + text = re.sub(r'\s+', ' ', text) + # Strip leading/trailing whitespace + return text.strip() + + +def get_function_signature(lines, func): + """Get normalized function signature (like prototype).""" + # Get all lines from start to the line with opening brace + start = func['start'] + end = func.get('decl_end', func['start']) # We need to track decl_end in parse_function + + # Collect signature lines + sig_lines = [] + for i in range(start, min(end + 1, len(lines))): + line = lines[i] + # Remove comments + if '//' in line: + line = line[:line.index('//')] + sig_lines.append(line) + + # Join and normalize + sig = ' '.join(sig_lines) + sig = normalize_whitespace(sig) + + # Remove the opening brace if present + if '{' in sig: + sig = sig[:sig.index('{')].strip() + + # Ensure it ends with semicolon + if not sig.endswith(';'): + sig += ';' + + return sig + + +def get_declaration_text(lines, decl): + """Get normalized declaration text for struct/enum.""" + start = decl['start'] + end = decl['end'] + + # Collect all lines + decl_lines = [] + for i in range(start, min(end + 1, len(lines))): + line = lines[i] + # Remove comments + if '//' in line: + line = line[:line.index('//')] + decl_lines.append(line) + + # Join and normalize + text = ' '.join(decl_lines) + text = normalize_whitespace(text) + + return text + + def main(): if len(sys.argv) < 2: print("Usage: c_util toc | func | struct | enum ") @@ -553,26 +613,43 @@ def main(): project_enums[f] = enums if cmd == 'toc': + first_file = True for f in files: if f not in contents: continue - print(f"File: {f}") + # Get all declarations for this file + declarations = [] + + # Add functions + for func in project_functions.get(f, []): + sig = get_function_signature(contents[f], func) + declarations.append((func['start'], func['end'], sig)) + + # Add structs + for s in project_structs.get(f, []): + text = get_declaration_text(contents[f], s) + declarations.append((s['start'], s['end'], text)) + + # Add enums + for e in project_enums.get(f, []): + text = get_declaration_text(contents[f], e) + declarations.append((e['start'], e['end'], text)) + + if not declarations: + continue - funcs = project_functions.get(f, []) - if funcs: - for func in funcs: - print(f"Function: {func['ret']} {func['name']}({func['args']}) - {func['line_count']} lines") + # Sort by line number + declarations.sort(key=lambda x: x[0]) - strs = project_structs.get(f, []) - if strs: - for s in strs: - print(f"Struct: {s['name']} - {s['line_count']} lines") + # Print empty line between files (except before first) + if not first_file: + print() + first_file = False - enums = project_enums.get(f, []) - if enums: - for e in enums: - print(f"Enum: {e['name']} - {e['line_count']} lines") + # Print in new format + for start, end, text in declarations: + print(f"[{start + 1}-{end + 1}] {text}") elif cmd == 'func': if len(sys.argv) < 4: