Skip to content

Commit 303c02d

Browse files
committed
chore: format code
Signed-off-by: pengyiqiang <pengyiqiang@xiaomi.com>
1 parent 7a47b96 commit 303c02d

3 files changed

Lines changed: 28 additions & 30 deletions

File tree

ghidra_common.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -301,42 +301,42 @@ def should_skip_function(func, program):
301301
def clean_decompiled_code(code):
302302
"""
303303
Clean up decompiled code by removing unnecessary comments and blank lines.
304-
304+
305305
Removes:
306306
- Function signature comments like /* FuncName(args) */
307307
- Excessive blank lines (keep max 1 between statements, none inside blocks)
308-
308+
309309
Args:
310310
code: Raw decompiled C code
311-
311+
312312
Returns:
313313
Cleaned up code
314314
"""
315315
if not code:
316316
return code
317-
318-
lines = code.split('\n')
317+
318+
lines = code.split("\n")
319319
cleaned_lines = []
320320
prev_blank = False
321321
inside_function = False
322322
brace_depth = 0
323-
323+
324324
for line in lines:
325325
stripped = line.strip()
326-
326+
327327
# Skip function signature comments: /* FuncName(...) */ or /* FuncName */
328328
# These appear at the start of functions and are redundant
329-
if stripped.startswith('/*') and stripped.endswith('*/'):
329+
if stripped.startswith("/*") and stripped.endswith("*/"):
330330
# Check if it looks like a function signature comment
331331
inner = stripped[2:-2].strip()
332332
# Skip if it contains parentheses (function signature) or is just a name
333-
if '(' in inner or (inner and ' ' not in inner and len(inner) < 100):
333+
if "(" in inner or (inner and " " not in inner and len(inner) < 100):
334334
continue
335-
335+
336336
# Track brace depth to know if we're inside a function body
337-
brace_depth += stripped.count('{') - stripped.count('}')
337+
brace_depth += stripped.count("{") - stripped.count("}")
338338
inside_function = brace_depth > 0
339-
339+
340340
# Handle blank lines
341341
if not stripped:
342342
# Inside function body: skip all blank lines for compact code
@@ -348,14 +348,14 @@ def clean_decompiled_code(code):
348348
prev_blank = True
349349
else:
350350
prev_blank = False
351-
351+
352352
cleaned_lines.append(line)
353-
353+
354354
# Remove trailing blank lines
355355
while cleaned_lines and not cleaned_lines[-1].strip():
356356
cleaned_lines.pop()
357-
358-
return '\n'.join(cleaned_lines)
357+
358+
return "\n".join(cleaned_lines)
359359

360360

361361
def get_decompiled_function_basic(decomp_ifc, func, monitor):

similarity_analyzer/test_similarity_analyzer.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,7 @@ def test_full_workflow(self):
270270
# Create similar files
271271
for name in ["DrawerRGB565.cpp", "DrawerRGB888.cpp"]:
272272
path = Path(tmpdir) / name
273-
path.write_text(
274-
f"""/**
273+
path.write_text(f"""/**
275274
* Auto-generated file
276275
*/
277276
class {Path(name).stem} {{
@@ -282,8 +281,7 @@ class {Path(name).stem} {{
282281
}}
283282
}}
284283
}};
285-
"""
286-
)
284+
""")
287285

288286
# Create a different file
289287
(Path(tmpdir) / "Other.cpp").write_text("completely different content here")

tests/test_libsurgeon.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,13 +1494,13 @@ def test_remove_blank_lines_inside_function(self):
14941494
}"""
14951495
cleaned = clean_decompiled_code(code)
14961496
# Should have no blank lines inside the function
1497-
lines = cleaned.split('\n')
1497+
lines = cleaned.split("\n")
14981498
inside_braces = False
14991499
blank_inside = 0
15001500
for line in lines:
1501-
if '{' in line:
1501+
if "{" in line:
15021502
inside_braces = True
1503-
if '}' in line:
1503+
if "}" in line:
15041504
inside_braces = False
15051505
if inside_braces and not line.strip():
15061506
blank_inside += 1
@@ -1579,23 +1579,23 @@ def test_real_ghidra_output(self):
15791579
15801580
undefined4 in_register_0000003c;
15811581
1582-
1582+
15831583
15841584
pvVar1 = operator_new__(CONCAT44(in_register_0000003c,param_1));
15851585
15861586
return pvVar1;
15871587
15881588
}"""
15891589
cleaned = clean_decompiled_code(code)
1590-
1590+
15911591
# Should remove the signature comment
15921592
assert "/* CMemStore::Alloc" not in cleaned
1593-
1593+
15941594
# Should remove blank lines inside function
1595-
lines = cleaned.split('\n')
1595+
lines = cleaned.split("\n")
15961596
# Count lines - should be much fewer
1597-
assert len(lines) < len(code.split('\n'))
1598-
1597+
assert len(lines) < len(code.split("\n"))
1598+
15991599
# Function should still be valid
16001600
assert "void * CMemStore::Alloc" in cleaned
16011601
assert "return pvVar1;" in cleaned
@@ -1618,4 +1618,4 @@ def test_nested_braces(self):
16181618
}"""
16191619
cleaned = clean_decompiled_code(code)
16201620
# Should remove all internal blank lines
1621-
assert "\n\n" not in cleaned.split('{', 1)[1].rsplit('}', 1)[0]
1621+
assert "\n\n" not in cleaned.split("{", 1)[1].rsplit("}", 1)[0]

0 commit comments

Comments
 (0)