Skip to main content
summaryrefslogtreecommitdiffstats
blob: fcef59a28287a8b594cee9cfcf152cda555bdbda (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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.core.search;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.ICLogConstants;
import org.eclipse.cdt.core.dom.CDOM;
import org.eclipse.cdt.core.dom.IASTServiceProvider;
import org.eclipse.cdt.core.dom.ast.ASTTypeUtil;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.IEnumeration;
import org.eclipse.cdt.core.dom.ast.IEnumerator;
import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.dom.ast.IFunctionType;
import org.eclipse.cdt.core.dom.ast.IMacroBinding;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.cdt.core.dom.ast.c.CASTVisitor;
import org.eclipse.cdt.core.dom.ast.c.ICExternalBinding;
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPField;
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.ICPPNamespaceAlias;
import org.eclipse.cdt.core.parser.ParseError;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.core.search.ICSearchConstants.LimitTo;
import org.eclipse.cdt.core.search.ICSearchConstants.SearchFor;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTQualifiedName;
import org.eclipse.cdt.internal.core.search.matching.CSearchPattern;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.content.IContentType;

/**
 * Utility class to have commonly used algorithms in one place for searching with the DOM. 
 * 
 * @author dsteffle
 */
public class DOMSearchUtil {
    private static final IASTName[] BLANK_NAME_ARRAY = new IASTName[0];
    private static final IASTName[] EMPTY_NAME_LIST = BLANK_NAME_ARRAY;
    private static final Set EMPTY_MATCHES = new HashSet(0);

    /**
     * This is a convenience method that uses the SearchEngine to find declarations, references, or both that correspond
     * to the IASTName searchName found in the index.
     * 
     * @param scope is used to limit the scope that SearchEngine searches the index against
     * @param searchName is the IASTName whose delcarations/references are sought after
     * @param limitTo used to specify whether to get declarations, references, or both, one of: 
     * ( CSearchPattern.DECLARATION | CSearchPattern.REFERENCES | CSearchPattern.ALL_OCCURRENCES ) 
     * @return
     */
    public static Set getMatchesFromSearchEngine(ICSearchScope scope, IASTName searchName, LimitTo limitTo) {
        SearchEngine engine = new SearchEngine();
        BasicSearchResultCollector results = new BasicSearchResultCollector();
            
        ICSearchPattern pattern = createPattern(searchName, limitTo, true);
            
        try {
        	engine.setWaitingPolicy(ICSearchConstants.FORCE_IMMEDIATE_SEARCH);
            engine.search(CCorePlugin.getWorkspace(), pattern, scope, results, false);
        } catch (InterruptedException e) {
            return EMPTY_MATCHES;
        }
            
        return results.getSearchResults();
    }
    
    private static CSearchPattern createPattern( IASTName searchName, LimitTo limitTo, boolean caseSensitive) {
		IBinding binding = searchName.resolveBinding();
		if (binding == null)
			return null;
		
        // build the SearchFor/pattern based on the IBinding
        SearchFor searchFor = createSearchFor(binding);
        
        return CSearchPattern.createPattern(DOMSearchUtil.getSearchPattern(searchName), searchFor, limitTo, ICSearchConstants.EXACT_MATCH, caseSensitive);
    }
    
    private static SearchFor createSearchFor( IBinding binding ) {
        SearchFor searchFor = null;
    	if (binding instanceof ICPPMethod) {
    		searchFor = ICSearchConstants.METHOD;
    	} else if (binding instanceof IFunction) {
            searchFor = ICSearchConstants.FUNCTION;
        } else if (binding instanceof ICPPNamespace || binding instanceof ICPPNamespaceAlias) {
            searchFor = ICSearchConstants.NAMESPACE;
        } else if (binding instanceof ICPPField) {
            searchFor = ICSearchConstants.FIELD;
        } else if (binding instanceof IEnumerator) {
            searchFor = ICSearchConstants.ENUMTOR;
        } else if (binding instanceof IMacroBinding) {
            searchFor = ICSearchConstants.MACRO;
        } else if (binding instanceof ITypedef) {
            searchFor = ICSearchConstants.TYPEDEF;
        } else if (binding instanceof IVariable) {
            searchFor = ICSearchConstants.VAR;
        } else if (binding instanceof ICPPClassType) {
            searchFor = ICSearchConstants.CLASS;
        } else if (binding instanceof ICompositeType) {
            try {
                switch(((ICompositeType)binding).getKey()) {
                case ICompositeType.k_struct:
                    searchFor = ICSearchConstants.CLASS_STRUCT;
                    break;
                case ICompositeType.k_union:
                    searchFor = ICSearchConstants.UNION;
                    break;
                }
            } catch (DOMException e) {
                searchFor = ICSearchConstants.UNKNOWN_SEARCH_FOR;
            }
        } else if (binding instanceof IEnumeration) {
            searchFor = ICSearchConstants.ENUM;
        } else {
            searchFor = ICSearchConstants.UNKNOWN_SEARCH_FOR;
        }
        return searchFor;
    }
    
    /**
     * This is used to get an array of selected names in an IASTTranslationUnit based on the offset 
     * and length into that IASTTranslationUnit.
     * 
     * ex: IASTTranslationUnit contains: int foo;
     * then getSelectedNamesFrom(file, 4, 3) will return the IASTName corresponding to foo 
     * 
     * @param tu 
     * @param offset
     * @param length
     * @param lang
     * @return
     */
    public static IASTName[] getSelectedNamesFrom(IASTTranslationUnit tu, int offset, int length, ParserLanguage lang) {
        IASTNode node = null;
        try{
            node = tu.selectNodeForLocation(tu.getFilePath(), offset, length);
        } 
        catch (ParseError er){}
        catch ( VirtualMachineError vmErr){
            if (vmErr instanceof OutOfMemoryError){
                org.eclipse.cdt.internal.core.model.Util.log(null, "Open Declarations Out Of Memory error: " + vmErr.getMessage() + " on File: " + tu.getContainingFilename(), ICLogConstants.CDT); //$NON-NLS-1$ //$NON-NLS-2$
            }
        }
        catch (Exception ex){}
        
        finally{
            if (node == null){
                return EMPTY_NAME_LIST;
            }
        }
    
        if (node instanceof IASTName) {
            IASTName[] results = new IASTName[1];
            results[0] = (IASTName)node;
            return results;
        }
        
        ASTVisitor collector = null;
        if (lang == ParserLanguage.CPP) {
            collector = new CPPNameCollector();
        } else {
            collector = new CNameCollector();
        }
        
        node.accept( collector );
        
        List names = null;
        if (collector instanceof CPPNameCollector) {
            names = ((CPPNameCollector)collector).nameList;
        } else {
            names = ((CNameCollector)collector).nameList;
        }
        
        IASTName[] results = new IASTName[names.size()];
        for(int i=0; i<names.size(); i++) {
            if (names.get(i) instanceof IASTName)
                results[i] = (IASTName)names.get(i);
        }
        
        return results;
    }

    
    /**
     * This is used to get an array of selected names in an IFile based on the offset and length 
     * into that IFile.
     * 
     * NOTE:  Invoking this method causes a parse, if an IASTTranslationUnit is already obtained then
     * invoke getSelectedNamesFrom(IASTTranslationUnit, int, int, ParserLanguage) instead.
     * 
     * ex: IFile contains: int foo;
     * then getSelectedNamesFrom(file, 4, 3) will return the IASTName corresponding to foo 
     * 
     * @param file the IFile whose selection 
     * @param offset
     * @param length
     * @return
     */
    public static IASTName[] getSelectedNamesFrom(IFile file, int offset, int length) {
        IASTNode node = null;
        IASTTranslationUnit tu = null;
        try {
            tu = CDOM.getInstance().getASTService().getTranslationUnit(
                    file,
                    CDOM.getInstance().getCodeReaderFactory(
                            CDOM.PARSE_WORKING_COPY_WHENEVER_POSSIBLE));
        } catch (IASTServiceProvider.UnsupportedDialectException e) {
            return EMPTY_NAME_LIST;
        }
        try{
            node = tu.selectNodeForLocation(file.getRawLocation().toOSString(), offset, length);
        } 
        catch (ParseError er){}
        catch ( VirtualMachineError vmErr){
            if (vmErr instanceof OutOfMemoryError){
                org.eclipse.cdt.internal.core.model.Util.log(null, "Open Declarations Out Of Memory error: " + vmErr.getMessage() + " on File: " + file.getName(), ICLogConstants.CDT); //$NON-NLS-1$ //$NON-NLS-2$
            }
        }
        catch (Exception ex){}
        
        finally{
            if (node == null){
                return EMPTY_NAME_LIST;
            }
        }
    
        if (node instanceof IASTName) {
            IASTName[] results = new IASTName[1];
            results[0] = (IASTName)node;
            return results;
        }
        
        ASTVisitor collector = null;
        if (getLanguageFromFile(file) == ParserLanguage.CPP) {
            collector = new CPPNameCollector();
        } else {
            collector = new CNameCollector();
        }
        
        node.accept( collector );
        
        List names = null;
        if (collector instanceof CPPNameCollector) {
            names = ((CPPNameCollector)collector).nameList;
        } else {
            names = ((CNameCollector)collector).nameList;
        }
        
        IASTName[] results = new IASTName[names.size()];
        for(int i=0; i<names.size(); i++) {
            if (names.get(i) instanceof IASTName)
                results[i] = (IASTName)names.get(i);
        }
        
        return results;
    }
    
    /**
    * This retrieves the ParserLanguage from an IFile.
    *
    * @param file
    * @return
    */
    public static ParserLanguage getLanguageFromFile(IFile file) {
        IProject project = file.getProject();
        IContentType contentType = CCorePlugin.getContentType(project, file.getFullPath().lastSegment());
        if (contentType != null) {
        	String lid = contentType.getId();
        	if (CCorePlugin.CONTENT_TYPE_CXXSOURCE.equals(lid)) {
        		return ParserLanguage.CPP;
        	}
        }        
        return ParserLanguage.C;
    }

    /**
     * This is used to get the names from the TU that the IASTName searchName belongs to.
     * 
     * @param searchName the IASTName whose references/delcarations are to be retrieved
     * @param limitTo used to specify whether to get declarations, references, or both, one of: 
     * ( CSearchPattern.DECLARATION | CSearchPattern.REFERENCES | CSearchPattern.ALL_OCCURRENCES ) 
     * @return IASTName[] declarations, references, or both depending on limitTo that correspond to the IASTName searchName searched for
     */
    public static IASTName[] getNamesFromDOM(IASTName searchName, LimitTo limitTo) {
		IASTName[] names = null;
		IASTTranslationUnit tu = searchName.getTranslationUnit();
		
		if (tu == null) {
			return BLANK_NAME_ARRAY;
		}
		
		IBinding binding = searchName.resolveBinding();
		names = getNames(tu, binding, limitTo);
		
		if (names == null || names.length == 0) { // try alternate strategies		
			try {
				// fix for 86829, 95224
				if ((binding instanceof ICPPConstructor || (binding instanceof ICPPMethod && ((ICPPMethod)binding).isDestructor())) 
						&& binding.getScope() instanceof ICPPClassScope) {
					binding =  ((ICPPClassScope)binding.getScope()).getClassType();
					names = getNames(tu, binding, limitTo);
				}
			} catch (DOMException e) {}
		}
		
		return names;
    }
	
	private static IASTName[] getNames(IASTTranslationUnit tu, IBinding binding, LimitTo limitTo) {
        IASTName[] names = null;
		if (limitTo == ICSearchConstants.DECLARATIONS) {
            names = tu.getDeclarations(binding);
        } else if (limitTo == ICSearchConstants.REFERENCES) {
            names = tu.getReferences(binding);
        } else if (limitTo == ICSearchConstants.DEFINITIONS) {
            names = tu.getDefinitions(binding);
        } else if (limitTo == ICSearchConstants.ALL_OCCURRENCES){
            names = tu.getDeclarations(binding);
            names = (IASTName[])ArrayUtil.addAll(IASTName.class, names, tu.getReferences(binding));
        } else {  // assume ALL
            names = tu.getDeclarations(binding);
            names = (IASTName[])ArrayUtil.addAll(IASTName.class, names, tu.getReferences(binding));
        }
		
		return names;
	}

    /**
     * The CPPNameCollector used to get IASTNames from an IASTNode.
     * 
     * @author dsteffle
     */
    static public class CPPNameCollector extends CPPASTVisitor {
        {
            shouldVisitNames = true;
        }
        public List nameList = new ArrayList();
        public int visit( IASTName name ){
            nameList.add( name );
            return PROCESS_CONTINUE;
        }
        public IASTName getName( int idx ){
            if( idx < 0 || idx >= nameList.size() )
                return null;
            return (IASTName) nameList.get( idx );
        }
        public int size() { return nameList.size(); } 
    }

    /**
     * The CNameCollector used to get IASTNames from an IASTNode.
     * 
     * @author dsteffle
     */
    static public class CNameCollector extends CASTVisitor {
        {
            shouldVisitNames = true;
        }
        public List nameList = new ArrayList();
        public int visit( IASTName name ){
            nameList.add( name );
            return PROCESS_CONTINUE;
        }
        public IASTName getName( int idx ){
            if( idx < 0 || idx >= nameList.size() )
                return null;
            return (IASTName) nameList.get( idx );
        }
        public int size() { return nameList.size(); } 
    }
    
	/**
	 * Returns the ParserLanguage corresponding to the IPath and IProject.  Returns ParserLanguage.CPP if the file type is a header.
	 * 
	 * @param path
	 * @param project
	 * @return
	 */
    public static ParserLanguage getLanguage( IPath path, IProject project )
    {  
    	//FIXME: ALAIN, for headers should we assume CPP ??
    	// The problem is that it really depends on how the header was included.
    	String id = null;
    	IContentType contentType = CCorePlugin.getContentType(project, path.lastSegment());
    	if (contentType != null) {
    		id = contentType.getId();
    	}
    	if (id != null) {
    		if (CCorePlugin.CONTENT_TYPE_CXXHEADER.equals(id)) {
    			return ParserLanguage.CPP;
    		} else if (CCorePlugin.CONTENT_TYPE_CXXSOURCE.equals(id)) {
    			return ParserLanguage.CPP;
    		} else if (CCorePlugin.CONTENT_TYPE_CHEADER.equals(id)) {
    			return ParserLanguage.CPP; 				// <============== is that right ? should not this be C ?
    		} else if (CCorePlugin.CONTENT_TYPE_CSOURCE.equals(id)) {
    			return ParserLanguage.C;
    		} else if (CCorePlugin.CONTENT_TYPE_ASMSOURCE.equals(id)) {
    			// ???
    			// What do we do here ?
    		}
    	}
		return ParserLanguage.CPP;
    }
	
	/**
	 * Generates a search pattern String based on the IASTName passed as a parameter.
	 * 
	 * Used to generate a string to present to the user as well as a string used by
	 * the SearchEngine to parse for qualified names and parameters.
	 * 
	 * @param name
	 * @return
	 */
	public static String getSearchPattern(IASTName name) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("::"); //$NON-NLS-1$
        
        String[] namespaces = null;
        
        IASTNode parent = name.getParent();
        while(!(parent instanceof IASTTranslationUnit) && parent != null) {
            if (parent instanceof ICPPASTNamespaceDefinition) {
                namespaces = (String[])ArrayUtil.append(String.class, namespaces, ((ICPPASTNamespaceDefinition)parent).getName().toString());
            }
            parent = parent.getParent();
        }
        
        if (namespaces != null && namespaces.length > 0) {
            for( int i=namespaces.length-1; i>=0; i-- ) {
                if (namespaces[i] != null) {
                    buffer.append(namespaces[i]);
                    buffer.append("::"); //$NON-NLS-1$
                }
            }
        }
        
		if (name instanceof CPPASTName && name.getParent() instanceof CPPASTQualifiedName) {
			IASTName[] names = ((CPPASTQualifiedName)name.getParent()).getNames();
			for(int i=0; i<names.length; i++) {
				if (i != 0) buffer.append("::"); //$NON-NLS-1$
				buffer.append(names[i].toString());
			}
		} else {
			buffer.append(name.toString());
		}
		
	 	if( name.resolveBinding() instanceof IFunction ){
			try {
				IBinding binding = name.resolveBinding();
				IFunctionType type = ((IFunction)binding).getType();
				
				buffer.append("("); //$NON-NLS-1$
				if (binding instanceof ICExternalBinding) {
					buffer.append("..."); //$NON-NLS-1$
				} else {
					IType[] parms = type.getParameterTypes();
					for( int i = 0; i < parms.length; i++ ){
						if( i != 0 )
							buffer.append(", "); //$NON-NLS-1$
						buffer.append(ASTTypeUtil.getType(parms[i]));
					}
				}
				buffer.append(")"); //$NON-NLS-1$
			} catch (DOMException e) {
				buffer = new StringBuffer();
				buffer.append(name.toString());
			}
	 	}

		return buffer.toString();
	}
}

Back to the top