Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 36e223cfade77da79c0af3b4197b440fa484bc09 (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
/*******************************************************************************
 *  Copyright (c) 2007, 2009 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
 *     Markus Schorn (Wind River Systems)
 *******************************************************************************/
package org.eclipse.cdt.internal.pdom.tests;

import java.util.Arrays;
import java.util.Comparator;
import java.util.regex.Pattern;

import junit.framework.Test;

import org.eclipse.cdt.core.dom.IName;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPField;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IndexFilter;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.internal.core.CCoreInternals;
import org.eclipse.cdt.internal.core.pdom.PDOM;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMNode;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;

/**
 * Test the correctness of C/C++ searches
 * 
 * @author Vivian Kong
 * 
 */
public class PDOMSearchTest extends PDOMTestBase {
	final Comparator<IBinding> BINDING_COMPARATOR = new Comparator<IBinding>() {
		public int compare(IBinding o1, IBinding o2) {
			return o1.getName().compareTo(o2.getName());
		}};

	protected ICProject project;	
	protected PDOM pdom;
	protected IProgressMonitor NULL_MONITOR = new NullProgressMonitor();
	protected IndexFilter INDEX_FILTER = IndexFilter.ALL_DECLARED;

	public static Test suite() {
		return suite(PDOMSearchTest.class);
	}
	
	@Override
	protected void setUp() throws Exception {
		if (pdom == null) {
			ICProject project = createProject("searchTests", true);
			pdom = (PDOM)CCoreInternals.getPDOMManager().getPDOM(project);
		}
		pdom.acquireReadLock();
	}
	
	@Override
	protected void tearDown() throws Exception {
		pdom.releaseReadLock();
	}
	/**
	 * Test the members inside namespaces
	 */
	public void testNamespaces() throws Exception {

		/* Members in the namespace */
		IBinding[] namespaces = pdom.findBindings(Pattern.compile("namespace1"), false, INDEX_FILTER, NULL_MONITOR);
		assertEquals(1, namespaces.length);
		assertTrue(namespaces[0] instanceof ICPPNamespace);
		ICPPNamespace namespace1 = (ICPPNamespace) namespaces[0];

		/* Consistent search results */

		// Searching for "namespace1::namespace2"
		Pattern[] patterns = { Pattern.compile("namespace1"), Pattern.compile("namespace2") };
		namespaces = pdom.findBindings(patterns, true, INDEX_FILTER, NULL_MONITOR);
		assertEquals(1, namespaces.length);
		assertTrue(namespaces[0] instanceof ICPPNamespace);
		ICPPNamespace namespace2 = (ICPPNamespace) namespaces[0];

		// Searching for "namespace2"
		namespaces = pdom.findBindings(Pattern.compile("namespace2"), false, INDEX_FILTER, NULL_MONITOR);
		assertEquals(1, namespaces.length);
		assertTrue(namespaces[0] instanceof ICPPNamespace);
		assertEquals(namespace2, namespaces[0]);
			
		/* Namespace references */
		IName[] refs = pdom.findNames(namespace1,IIndex.FIND_REFERENCES);
		assertEquals(3, refs.length);
		IASTFileLocation loc = refs[0].getFileLocation();
		assertEquals(offset("main.cpp","namespace1::Class1"), loc.getNodeOffset()); //character offset	
		loc = refs[1].getFileLocation();
		assertEquals(offset("Class1.cpp","namespace1::Class1::~Class1()"), loc.getNodeOffset()); //character offset	
		loc = refs[2].getFileLocation();
		assertEquals(offset("Class1.cpp","namespace1::Class1::Class1()"), loc.getNodeOffset()); //character offset	
		
		/* Namespace declaration */
		IName[] decls = pdom.findNames(namespace1, IIndex.FIND_DECLARATIONS);
		assertEquals(0, decls.length);

		/* Namespace definition */
		IName[] defs = pdom.findNames(namespace1, IIndex.FIND_DEFINITIONS);
		assertEquals(1, defs.length);
		loc = defs[0].getFileLocation();
		assertEquals(offset("Class1.h","namespace namespace1") + 10, loc.getNodeOffset()); //character offset	

	}

	public void testClasses() throws Exception {
		// Bugzilla 160913
		// classes and nested classes

		/* Search for "Class1" */
		IBinding[] class1s = pdom.findBindings(Pattern.compile("Class1"), false, INDEX_FILTER, NULL_MONITOR);
		assertEquals(4, class1s.length);
		assertTrue(class1s[0] instanceof ICPPClassType);
		assertTrue(class1s[1] instanceof ICPPClassType);
		assertTrue(class1s[2] instanceof ICPPClassType);
		assertTrue(class1s[3] instanceof ICPPMethod);

		/** result #1 * */
		ICPPClassType class1 = (ICPPClassType) class1s[0];
		assertEquals("Class1", getBindingQualifiedName(pdom.getLinkageImpls()[0].adaptBinding(class1)));
		IBinding[] methods = class1.getDeclaredMethods();
		assertEquals(0, methods.length);

		/** result #2 * */
		ICPPClassType class2 = (ICPPClassType) class1s[1];
		assertEquals("namespace1::Class1", getBindingQualifiedName(pdom.getLinkageImpls()[0].adaptBinding(class2)));

		/* Members in this class */

		// methods
		methods = class2.getDeclaredMethods();
		assertEquals(2, methods.length);
		if (methods[0].getName().equals("~Class1")) {
			IBinding h= methods[1]; methods[1]= methods[0]; methods[0]=h;
		}
		assertEquals("Class1", methods[0].getName());
		assertEquals("~Class1", methods[1].getName());

		// nested class
		IBinding[] nested = class2.getNestedClasses();
		assertEquals(1, nested.length);
		assertEquals("Class2", nested[0].getName());

		// fields
		IBinding[] fields = class2.getFields();
		assertEquals(2, fields.length);
		Arrays.sort(fields, BINDING_COMPARATOR);
		assertEquals("class1x", fields[0].getName());
		assertEquals("class1y", fields[1].getName());

		/** result #3 * */
		ICPPMethod method3 = (ICPPMethod) class1s[3];
		assertEquals("namespace1::Class1::Class1", getBindingQualifiedName(pdom.getLinkageImpls()[0].adaptBinding(method3)));
		assertEquals(method3, methods[0]);

		/** result #4 * */
		ICPPClassType class4 = (ICPPClassType) class1s[2];
		assertEquals("namespace1::namespace2::Class1", getBindingQualifiedName(pdom.getLinkageImpls()[0].adaptBinding(class4)));
		methods = class4.getDeclaredMethods();
		assertEquals(0, methods.length);

		/* Search for "Class2" */
		IBinding[] class2s = pdom.findBindings(Pattern.compile("Class2"), false, INDEX_FILTER, NULL_MONITOR);
		assertEquals(4, class2s.length);
		assertTrue(class2s[0] instanceof ICPPClassType);
		assertTrue(class2s[1] instanceof ICPPClassType);
		assertTrue(class2s[2] instanceof ICPPClassType);
		assertTrue(class2s[3] instanceof ICPPMethod);

		/** result #1 * */
		ICPPClassType cls1 = (ICPPClassType) class2s[0];
		assertEquals("Class2", getBindingQualifiedName(pdom.getLinkageImpls()[0].adaptBinding(cls1)));
		methods = cls1.getDeclaredMethods();
		assertEquals(3, methods.length);
		Arrays.sort(methods, BINDING_COMPARATOR);
		assertEquals("Class2", methods[0].getName());
		assertEquals("~Class2", methods[2].getName());
		assertEquals("foo", methods[1].getName());

		/** result #2 * */
		ICPPMethod meth2 = (ICPPMethod) class2s[3];
		assertEquals("Class2::Class2", getBindingQualifiedName(pdom.getLinkageImpls()[0].adaptBinding(meth2)));
		assertEquals(meth2, methods[0]);

		/** result #3 * */
		ICPPClassType cls3 = (ICPPClassType) class2s[1];
		assertEquals("namespace1::Class1::Class2", getBindingQualifiedName(pdom.getLinkageImpls()[0].adaptBinding(cls3)));

		/** result #3 * */
		ICPPClassType cls4 = (ICPPClassType) class2s[2];
		assertEquals("namespace1::Class2", getBindingQualifiedName(pdom.getLinkageImpls()[0].adaptBinding(cls4)));
		
		/* Nested class references - namespace1::Class1::Class2 */
		IName[] refs = pdom.findNames(cls3, IIndex.FIND_REFERENCES);
		assertEquals(0, refs.length);
		
		/* Nested class declaration */
		IName[] decls = pdom.findNames(cls3, IIndex.FIND_DECLARATIONS);
		assertEquals(0, decls.length);

		/* Nested class definition */
		IName[] defs = pdom.findNames(cls3, IIndex.FIND_DEFINITIONS);
		assertEquals(1, defs.length);
		IASTFileLocation loc = defs[0].getFileLocation();
		assertEquals(offset("Class1.h","class Class2 { //namespace1::Class1::Class2") + 6, loc.getNodeOffset()); //character offset	
	}

	public void testFunction() throws Exception {

		IBinding[] functions = pdom.findBindings(Pattern.compile("foo2"), false, INDEX_FILTER, NULL_MONITOR);
		assertEquals(1, functions.length);
		assertTrue(functions[0] instanceof ICPPFunction);
		assertEquals("foo2", getBindingQualifiedName(pdom.getLinkageImpls()[0].adaptBinding(functions[0])));
		
		functions = pdom.findBindings(Pattern.compile("main"), false, INDEX_FILTER, NULL_MONITOR);
		assertEquals(1, functions.length);
		assertTrue(functions[0] instanceof ICPPFunction);
		assertEquals("main", getBindingQualifiedName(pdom.getLinkageImpls()[0].adaptBinding(functions[0])));

	}

	public void testMethods() throws Exception {

		IBinding[] methods = pdom.findBindings(Pattern.compile("~Class2"), false, INDEX_FILTER, NULL_MONITOR);
		assertEquals(1, methods.length);
		assertTrue(methods[0] instanceof ICPPMethod);
		assertEquals("Class2::~Class2", getBindingQualifiedName(pdom.getLinkageImpls()[0].adaptBinding(methods[0])));

	}

	public void testFields() throws Exception {

		IBinding[] fields = pdom.findBindings(Pattern.compile("class1x"), false, INDEX_FILTER, NULL_MONITOR);
		assertEquals(1, fields.length);
		assertTrue(fields[0] instanceof ICPPField);
		assertEquals("namespace1::Class1::class1x", getBindingQualifiedName(pdom.getLinkageImpls()[0].adaptBinding(fields[0])));

		fields = pdom.findBindings(Pattern.compile("class1y"), false, INDEX_FILTER, NULL_MONITOR);
		assertEquals(1, fields.length);
		assertTrue(fields[0] instanceof ICPPField);
		assertEquals("namespace1::Class1::class1y", getBindingQualifiedName(pdom.getLinkageImpls()[0].adaptBinding(fields[0])));

	}

	public void testVariables() throws Exception {

		IBinding[] variables = pdom.findBindings(Pattern.compile("var"), false, INDEX_FILTER, NULL_MONITOR);
		assertEquals(1, variables.length);
		assertTrue(variables[0] instanceof ICPPVariable);
		assertEquals("var", getBindingQualifiedName(pdom.getLinkageImpls()[0].adaptBinding(variables[0])));
		
		/* Variable references */
		IName[] refs = pdom.findNames(variables[0], IIndex.FIND_REFERENCES);
		assertEquals(1, refs.length);
		IASTFileLocation loc = refs[0].getFileLocation();
		assertEquals(offset("main.cpp","var = 0;"), loc.getNodeOffset()); //character offset	
		
		/* Variable declaration */
		IName[] decls = pdom.findNames(variables[0], IIndex.FIND_DECLARATIONS_DEFINITIONS);
		assertEquals(1, decls.length);
		loc = decls[0].getFileLocation();
		assertEquals(offset("main.cpp","int var;") + 4, loc.getNodeOffset()); //character offset	

		/* Variable definition */
		IName[] defs = pdom.findNames(variables[0], IIndex.FIND_DEFINITIONS);
		assertEquals(1, defs.length);
		loc = defs[0].getFileLocation();
		assertEquals(offset("main.cpp","int var;") + 4, loc.getNodeOffset()); //character offset	

	}

	/**
	 * Get the fully qualified name for a given PDOMBinding
	 * 
	 * @param pdomBinding
	 * @return binding's fully qualified name
	 * @throws CoreException
	 */
	private String getBindingQualifiedName(PDOMBinding pdomBinding) throws CoreException {
		StringBuffer buf = new StringBuffer(pdomBinding.getName());
		PDOMNode parent = pdomBinding.getParentNode();
		while (parent != null) {
			if (parent instanceof PDOMBinding) {
				buf.insert(0, ((PDOMBinding) parent).getName() + "::");
			}
			parent = parent.getParentNode();
		}
		return buf.toString();
	}
}

Back to the top