Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ecb9846b3f62599c7e8b856cb85083a530c527a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*******************************************************************************
 * Copyright (c) 2006, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.core.lrparser.tests.c99;

import java.util.Arrays;
import java.util.Comparator;

import junit.framework.TestCase;

import org.eclipse.cdt.core.dom.ast.IASTCompletionNode;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IEnumerator;
import org.eclipse.cdt.core.dom.ast.IField;
import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.cdt.core.dom.lrparser.BaseExtensibleLanguage;
import org.eclipse.cdt.core.dom.lrparser.c99.C99Language;


/**
 * Reuse the completion parse tests from the old parser for now.
 * 
 * This test suite is specific to C99.
 */
public class C99CompletionParseTest extends TestCase {

	public C99CompletionParseTest() { }
	public C99CompletionParseTest(String name) { super(name); }
	

	protected IASTCompletionNode parse(String code, int offset) throws Exception {
		return ParseHelper.getCompletionNode(code, getLanguage(), offset);
	}


	private static class BindingsComparator implements Comparator {
		public int compare(Object o1, Object o2) {
			IBinding b1 = (IBinding)o1;
			IBinding b2 = (IBinding)o2;
			return b1.getName().compareTo(b2.getName());
		}
	}
	
	private static BindingsComparator bindingsComparator  = new BindingsComparator();
	
	protected IBinding[] sortBindings(IBinding[] bindings) {
		Arrays.sort(bindings, bindingsComparator);
		return bindings;
	}
	
	protected IBinding[] getBindings(IASTName[] names) {
		return sortBindings(names[0].getCompletionContext().findBindings(names[0], true));
	}
	
	
	protected BaseExtensibleLanguage getLanguage() {
		return C99Language.getDefault();
	}
	
	
	// First steal tests from CompletionParseTest
	
	
	public void testCompletionStructField() throws Exception
	{
		StringBuffer sb = new StringBuffer();
		sb.append( "int aVar; " ); //$NON-NLS-1$
		sb.append( "struct D{ " ); //$NON-NLS-1$
		sb.append( "   int aField1; " ); //$NON-NLS-1$
		sb.append( "   int aField2; " ); //$NON-NLS-1$
		sb.append( "}; " ); //$NON-NLS-1$
		sb.append( "void foo(){" ); //$NON-NLS-1$
		sb.append( "   struct D d; " ); //$NON-NLS-1$
		sb.append( "   d.a " ); //$NON-NLS-1$
		sb.append( "}\n" ); //$NON-NLS-1$
		
		String code = sb.toString();
		int index = code.indexOf( "d.a" ); //$NON-NLS-1$
		
		IASTCompletionNode node = parse( code, index + 3 );				
		assertNotNull( node );
		
		String prefix = node.getPrefix();
		assertNotNull( prefix );
		assertEquals( prefix, "a" ); //$NON-NLS-1$
		
		IASTName[] names = node.getNames();
		assertEquals(1, names.length); 
		
		IBinding[] bindings = getBindings(names);
		
		assertEquals(2, bindings.length);
		assertEquals("aField1", ((IField)bindings[0]).getName());
		assertEquals("aField2", ((IField)bindings[1]).getName());
	}
	
	public void testCompletionStructFieldPointer() throws Exception
	{
		StringBuffer sb = new StringBuffer();
		sb.append("struct Cube {                       "); //$NON-NLS-1$
		sb.append("   int nLen;                        "); //$NON-NLS-1$
		sb.append("   int nWidth;                      "); //$NON-NLS-1$
		sb.append("   int nHeight;                     "); //$NON-NLS-1$
		sb.append("};                                  "); //$NON-NLS-1$
		sb.append("int volume( struct Cube * pCube ) { "); //$NON-NLS-1$
		sb.append("   pCube->SP                        "); //$NON-NLS-1$

		String code = sb.toString();
		IASTCompletionNode node = parse( code, code.indexOf("SP")); //$NON-NLS-1$
		
		IASTName[] names = node.getNames();
		assertEquals(1, names.length); 
		
		IBinding[] bindings = getBindings(names);
		
		assertEquals(3, bindings.length);
		assertEquals("nHeight", ((IField)bindings[0]).getName());
		assertEquals("nLen", ((IField)bindings[1]).getName());
		assertEquals("nWidth", ((IField)bindings[2]).getName());
	}
	
	
	public void testCompletionParametersAsLocalVariables() throws Exception{
		StringBuffer sb = new StringBuffer();
		sb.append( "int foo( int aParameter ){" ); //$NON-NLS-1$
		sb.append( "   int aLocal;" ); //$NON-NLS-1$
		sb.append( "   if( aLocal != 0 ){" );		 //$NON-NLS-1$
		sb.append( "      int aBlockLocal;" ); //$NON-NLS-1$
		sb.append( "      a \n" ); //$NON-NLS-1$
		
		String code = sb.toString();
		int index = code.indexOf( " a " ); //$NON-NLS-1$
		
		IASTCompletionNode node = parse( code, index + 2 );
		assertNotNull( node );
		
		assertEquals("a", node.getPrefix()); //$NON-NLS-1$
		
		IASTName[] names = node.getNames();
		assertEquals(1, names.length); 
		
		IBinding[] bindings = getBindings(names);
		
		assertEquals(3, bindings.length);
		assertEquals("aBlockLocal", ((IVariable)bindings[0]).getName());
		assertEquals("aLocal",      ((IVariable)bindings[1]).getName());
		assertEquals("aParameter",  ((IVariable)bindings[2]).getName());
	}
	
	
	public void testCompletionTypedef() throws Exception{
		StringBuffer sb = new StringBuffer();
		sb.append( "typedef int Int; "); //$NON-NLS-1$
		sb.append( "InSP" ); //$NON-NLS-1$
		
		String code = sb.toString();
		int index = code.indexOf( "SP" ); //$NON-NLS-1$
		
		IASTCompletionNode node = parse( code, index );
		assertNotNull(node);
		
		IASTName[] names = node.getNames();
		assertEquals(1, names.length); 
		
		assertEquals("In", node.getPrefix());
		
		IBinding[] bindings = getBindings(names);
		
		assertEquals(1, bindings.length);
		assertEquals("Int", ((ITypedef)bindings[0]).getName());
	}
	
	public void testCompletion() throws Exception
	{
		StringBuffer sb = new StringBuffer();
		sb.append("#define GL_T 0x2001\n"); //$NON-NLS-1$
		sb.append("#define GL_TRUE 0x1\n"); //$NON-NLS-1$
		sb.append("typedef unsigned char   GLboolean;\n"); //$NON-NLS-1$
		sb.append("static GLboolean should_rotate = GL_T"); //$NON-NLS-1$
		
		String code = sb.toString();
		
		int index = code.indexOf("= GL_T"); //$NON-NLS-1$
		
		IASTCompletionNode node = parse( code, index + 6);
		assertNotNull(node);
		
		assertEquals("GL_T", node.getPrefix()); //$NON-NLS-1$
		
		IASTName[] names = node.getNames();
		assertEquals(1, names.length); 
	}
	
	public void testCompletionInTypeDef() throws Exception{
		StringBuffer sb = new StringBuffer();
		sb.append( "struct A {  int name;  };  \n" ); //$NON-NLS-1$
		sb.append( "typedef struct A * PA;     \n" ); //$NON-NLS-1$
		sb.append( "int main() {               \n" ); //$NON-NLS-1$
		sb.append( "   PA a;                   \n" ); //$NON-NLS-1$
		sb.append( "   a->SP                   \n" ); //$NON-NLS-1$
		sb.append( "}                          \n" ); //$NON-NLS-1$
		
		String code = sb.toString();
		int index = code.indexOf("SP"); //$NON-NLS-1$
		
		IASTCompletionNode node = parse( code, index );
		assertNotNull( node );
		
		
		IASTName[] names = node.getNames();
		assertEquals(1, names.length); 
		
		IBinding[] bindings = getBindings(names);
		
		assertEquals(1, bindings.length);
		assertEquals("name", ((IField)bindings[0]).getName());
	}
	
	
	public void _testCompletionFunctionCall() throws Exception
	{
		StringBuffer sb = new StringBuffer();
		sb.append( "struct A {  	     \n" ); //$NON-NLS-1$ 
		sb.append( "   int f2;  		 \n" ); //$NON-NLS-1$
		sb.append( "   int f4;           \n" ); //$NON-NLS-1$
		sb.append( "};                   \n" ); //$NON-NLS-1$
		sb.append( "const A * foo(){}    \n" ); //$NON-NLS-1$
		sb.append( "void main( )         \n" ); //$NON-NLS-1$
		sb.append( "{                    \n" ); //$NON-NLS-1$
		sb.append( "   foo()->SP         \n" ); //$NON-NLS-1$
		
		String code = sb.toString();
		int index = code.indexOf( "SP" ); //$NON-NLS-1$
		
		IASTCompletionNode node = parse( code, index );
		assertNotNull( node );
		
		IASTName[] names = node.getNames();
		assertEquals(1, names.length); 
		
		IBinding[] bindings = getBindings(names);
		
		assertEquals(2, bindings.length);
		assertEquals("f2", ((IField)bindings[0]).getName());
		assertEquals("f4", ((IField)bindings[1]).getName());
	}
	
	
	public void _testCompletionSizeof() throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append( "int f() {\n" ); //$NON-NLS-1$
		sb.append( "short blah;\n" ); //$NON-NLS-1$
		sb.append( "int x = sizeof(bl" ); //$NON-NLS-1$
		
		String code = sb.toString();
		int index = code.indexOf( "of(bl" ); //$NON-NLS-1$
		
		IASTCompletionNode node = parse( code, index + 5);
		assertNotNull( node );
		
		IASTName[] names = node.getNames();
		assertEquals(1, names.length); 
		
		IBinding[] bindings = getBindings(names);
		
		assertEquals(1, bindings.length);
		assertEquals("blah", ((IVariable)bindings[0]).getName());
	}
	
	
	public void testCompletionForLoop() throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append( "int f() {\n" ); //$NON-NLS-1$
		sb.append( " int biSizeImage = 5;\n" ); //$NON-NLS-1$
		sb.append( "for (int i = 0; i < bi " ); //$NON-NLS-1$
		String code = sb.toString();
		
		int index = code.indexOf("< bi");
		
		IASTCompletionNode node = parse( code, index + 4);
		assertNotNull( node );
		
		IASTName[] names = node.getNames();
		assertEquals(1, names.length); 
		
		IBinding[] bindings = getBindings(names);
		
		assertEquals(1, bindings.length);
		assertEquals("biSizeImage", ((IVariable)bindings[0]).getName());
	}
	
	
	
	public void testCompletionStructPointer() throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append(" struct Temp { char * total; };" );
		sb.append(" int f(struct Temp * t) {" );
		sb.append(" t->t[5] = t->" );
		String code = sb.toString();
		
		int index = code.indexOf("= t->");
		
		IASTCompletionNode node = parse( code, index + 5);
		assertNotNull( node );
		
		IASTName[] names = node.getNames();
		assertEquals(1, names.length); 
		
		IBinding[] bindings = getBindings(names);
		
		assertEquals(1, bindings.length);
		assertEquals("total", ((IVariable)bindings[0]).getName());
	}
	
	
	public void testCompletionEnum() throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append( "typedef int DWORD;\n" ); //$NON-NLS-1$
		sb.append( "typedef char BYTE;\n"); //$NON-NLS-1$
		sb.append( "#define MAKEFOURCC(ch0, ch1, ch2, ch3)                  \\\n"); //$NON-NLS-1$
		sb.append( "((DWORD)(BYTE)(ch0) | ((DWORD)(BYTE)(ch1) << 8) |       \\\n"); //$NON-NLS-1$
		sb.append( "((DWORD)(BYTE)(ch2) << 16) | ((DWORD)(BYTE)(ch3) << 24 ))\n"); //$NON-NLS-1$
		sb.append( "enum e {\n"); //$NON-NLS-1$
		sb.append( "blah1 = 5,\n"); //$NON-NLS-1$
		sb.append( "blah2 = MAKEFOURCC('a', 'b', 'c', 'd'),\n"); //$NON-NLS-1$
		sb.append( "blah3\n"); //$NON-NLS-1$
		sb.append( "};\n"); //$NON-NLS-1$
		sb.append( "e mye = bl\n"); //$NON-NLS-1$
		String code = sb.toString();
		
		int index = code.indexOf("= bl");
		
		IASTCompletionNode node = parse( code, index + 4);
		assertNotNull( node );
		
		IASTName[] names = node.getNames();
		assertEquals(1, names.length); 
		
		IBinding[] bindings = getBindings(names);
		
		assertEquals(3, bindings.length);
		assertEquals("blah1", ((IEnumerator)bindings[0]).getName());
		assertEquals("blah2", ((IEnumerator)bindings[1]).getName());
		assertEquals("blah3", ((IEnumerator)bindings[2]).getName());
	}
	
	
	public void testCompletionStructArray() throws Exception { 
		StringBuffer sb = new StringBuffer();
		sb.append( "struct packet { int a; int b; };\n" ); //$NON-NLS-1$
		sb.append( "struct packet buffer[5];\n" ); //$NON-NLS-1$
		sb.append( "int main(int argc, char **argv) {\n" ); //$NON-NLS-1$
		sb.append( " buffer[2]." ); //$NON-NLS-1$
		String code = sb.toString();
		
		int index = code.indexOf("[2].");
		
		IASTCompletionNode node = parse( code, index + 4);
		assertNotNull( node );
		
		IASTName[] names = node.getNames();
		assertEquals(1, names.length); 
		
		IBinding[] bindings = getBindings(names);
		
		assertEquals(2, bindings.length);
		assertEquals("a", ((IField)bindings[0]).getName());
		assertEquals("b", ((IField)bindings[1]).getName());
	}
	
	
	public void testCompletionPreprocessorDirective() throws Exception {
		IASTCompletionNode node = parse("#", 1);
		assertNotNull( node );
		
		IASTName[] names = node.getNames();
		assertEquals(1, names.length); 
		
		assertEquals("#", node.getPrefix());
	}
	
	public void testCompletionPreprocessorMacro() throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append( "#define AMACRO 99 \n");
		sb.append( "int main() { \n");
		sb.append( "	int AVAR; \n");
		sb.append( "	int x = A \n");
		String code = sb.toString();
		
		int index = code.indexOf("= A");
		
		IASTCompletionNode node = parse( code, index + 3);
		assertNotNull( node );
		
		IASTName[] names = node.getNames();
		assertEquals(1, names.length); 
		assertEquals("A", node.getPrefix());
	}
	
	
	public void testCompletionInsidePreprocessorDirective() throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append( "#define MAC1 99 \n");
		sb.append( "#define MAC2 99 \n");
		sb.append( "#ifdef MA");
		String code = sb.toString();
		
		int index = code.length();
		
		IASTCompletionNode node = parse( code, index );
		assertNotNull( node );
		
		assertEquals("MA", node.getPrefix());
	}
}

Back to the top