15
15
*/
16
16
package ghidra .app .plugin .core .decompile ;
17
17
18
+ import java .util .Optional ;
19
+
18
20
import org .junit .*;
19
21
20
22
import ghidra .app .decompiler .*;
21
23
import ghidra .program .model .address .Address ;
24
+ import ghidra .program .model .address .AddressSpace ;
25
+ import ghidra .program .model .listing .CodeUnit ;
22
26
import ghidra .program .model .listing .Function ;
23
27
import ghidra .program .model .listing .Program ;
24
28
import ghidra .test .AbstractGhidraHeadedIntegrationTest ;
28
32
public class DecompilerTest extends AbstractGhidraHeadedIntegrationTest {
29
33
private Program prog ;
30
34
private DecompInterface decompiler ;
35
+ private long returnBytesOffset = 0x0 ;
31
36
32
37
@ Before
33
38
public void setUp () throws Exception {
34
39
35
40
ToyProgramBuilder builder = new ToyProgramBuilder ("notepad_decompiler" , true );
36
41
builder .createMemory ("test" , "0x0" , 2 );
37
- builder .addBytesReturn (0x0 );
42
+ builder .addBytesReturn (returnBytesOffset );
38
43
builder .createFunction ("0x0" );
39
44
prog = builder .getProgram ();
40
45
@@ -58,4 +63,74 @@ public void testDecompileInterfaceReturnsAFunction() throws Exception {
58
63
String decompilation = decompResults .getDecompiledFunction ().getC ();
59
64
Assert .assertNotNull (decompilation );
60
65
}
66
+
67
+ @ Test
68
+ public void testAlignedCommentIndentation () throws Exception {
69
+ int indent = 20 ;
70
+ DecompileOptions options = new DecompileOptions ();
71
+ options .setCommentIndent (indent );
72
+ options .setCommentIndentAlign (true );
73
+ options .setPRECommentIncluded (true );
74
+ decompiler .setOptions (options );
75
+
76
+ AddressSpace space = prog .getAddressFactory ().getDefaultAddressSpace ();
77
+
78
+ // add a comment to the program listing
79
+ Address returnBytesAddr = space .getAddress (returnBytesOffset );
80
+ int transaction = prog .startTransaction ("add comment for indentation test" );
81
+ String comment = "aligned-comment-indentation-test" ;
82
+ prog .getListing ().getCodeUnitAt (returnBytesAddr ).setComment (CodeUnit .PRE_COMMENT , comment );
83
+ prog .endTransaction (transaction , true );
84
+
85
+ Address addr = space .getAddress (0x0 );
86
+ Function func = prog .getListing ().getFunctionAt (addr );
87
+ DecompileResults decompResults = decompiler .decompileFunction (func ,
88
+ DecompileOptions .SUGGESTED_DECOMPILE_TIMEOUT_SECS , TaskMonitor .DUMMY );
89
+ String decompilation = decompResults .getDecompiledFunction ().getC ();
90
+ Assert .assertNotNull (decompilation );
91
+
92
+ Optional <String > commentLineCheck = decompilation .lines ().filter (line -> line .contains (comment )).findFirst ();
93
+ Optional <String > returnLineCheck = decompilation .lines ().filter (line -> line .endsWith ("return;" )).findFirst ();
94
+ Assert .assertTrue (commentLineCheck .isPresent ());
95
+ Assert .assertTrue (returnLineCheck .isPresent ());
96
+
97
+ String commentLine = commentLineCheck .get ();
98
+ String returnLine = returnLineCheck .get ();
99
+
100
+ Assert .assertFalse (commentLine .startsWith (" " .repeat (indent )));
101
+
102
+ int commentIndentation = commentLine .indexOf (commentLine .stripLeading ());
103
+ int returnIndentation = returnLine .indexOf (returnLine .stripLeading ());
104
+ Assert .assertEquals (commentIndentation , returnIndentation );
105
+ }
106
+
107
+ @ Test
108
+ public void testFixedCommentIndentation () throws Exception {
109
+ int indent = 20 ;
110
+ DecompileOptions options = new DecompileOptions ();
111
+ options .setCommentIndent (indent );
112
+ options .setCommentIndentAlign (false );
113
+ options .setPRECommentIncluded (true );
114
+ decompiler .setOptions (options );
115
+
116
+ AddressSpace space = prog .getAddressFactory ().getDefaultAddressSpace ();
117
+
118
+ // add a comment to the program listing
119
+ Address returnBytesAddr = space .getAddress (returnBytesOffset );
120
+ int transaction = prog .startTransaction ("add comment for indentation test" );
121
+ String comment = "fixed-comment-indentation-test" ;
122
+ prog .getListing ().getCodeUnitAt (returnBytesAddr ).setComment (CodeUnit .PRE_COMMENT , comment );
123
+ prog .endTransaction (transaction , true );
124
+
125
+ Address addr = space .getAddress (0x0 );
126
+ Function func = prog .getListing ().getFunctionAt (addr );
127
+ DecompileResults decompResults = decompiler .decompileFunction (func ,
128
+ DecompileOptions .SUGGESTED_DECOMPILE_TIMEOUT_SECS , TaskMonitor .DUMMY );
129
+ String decompilation = decompResults .getDecompiledFunction ().getC ();
130
+ Assert .assertNotNull (decompilation );
131
+
132
+ Optional <String > commentLine = decompilation .lines ().filter (line -> line .contains (comment )).findFirst ();
133
+ Assert .assertTrue (commentLine .isPresent ());
134
+ Assert .assertTrue (commentLine .get ().startsWith (" " .repeat (indent )));
135
+ }
61
136
}
0 commit comments