Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3b1dcc323fccd7b9afd52cdd7dbb2294b62862db (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
/*******************************************************************************
 * Copyright (c) 2006, 2008 Wind River Systems, Inc. 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:
 *    Markus Schorn - initial API and implementation
 *******************************************************************************/ 

package org.eclipse.cdt.internal.ui.callhierarchy;

import java.util.ArrayList;

import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTName;

/**
 * Visitor to find references in a given range of the source code.
 * @since 4.0
 */
class ReferenceVisitor extends ASTVisitor {
	private ArrayList<IASTName> fReferences= new ArrayList<IASTName>();
	private int fOffset;
	private int fEndOffset;
	private String fFileName;
	
	ReferenceVisitor(String fileName, int offset, int length) {
		shouldVisitNames= true;
		shouldVisitDeclarations= true;
		
		fFileName= fileName;
		fOffset= offset;
		fEndOffset= offset+length;
	}
	
	public IASTName[] getReferences() {
		return fReferences.toArray(new IASTName[fReferences.size()]);
	}

	@Override
	public int visit(IASTName name) {
		if (name.isReference()) {
			IASTFileLocation loc= name.getFileLocation();
			if (!loc.getFileName().equals(fFileName)) {
				return PROCESS_SKIP;
			}
			int offset= loc.getNodeOffset();
			if (fOffset <= offset && offset+loc.getNodeLength() <= fEndOffset) {
				fReferences.add(name);
			}
		}
		return super.visit(name);
	}

	@Override
	public int visit(IASTDeclaration declaration) {
		IASTFileLocation loc= declaration.getFileLocation();
		if (loc != null) {
			if (!loc.getFileName().equals(fFileName)) {
				return PROCESS_SKIP;
			}
			int offset= loc.getNodeOffset();
			if (offset + loc.getNodeLength() <= fOffset || fEndOffset <= offset) {
				return PROCESS_SKIP;
			}
		}
		return PROCESS_CONTINUE;
	}
}

Back to the top