Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ba7a270190a9b6f74f72cf19147926aafc6addba (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
/*******************************************************************************
 * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik  
 * Rapperswil, University of applied sciences 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: 
 * Institute for Software - initial API and implementation 
 ******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.util;

import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTMacroExpansionLocation;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;

/**
 * @author Emanuel Graf IFS
 *
 */
public class OffsetHelper {
	
	public static int getOffsetIncludingComment(IASTNode node) {
		int nodeStart = Integer.MAX_VALUE;
		IASTNodeLocation[] nodeLocations = node.getNodeLocations();
		if (nodeLocations.length != 1) {
			int offset;
			for (IASTNodeLocation location : nodeLocations) {
				if (location instanceof IASTMacroExpansionLocation) {
					IASTMacroExpansionLocation macroLoc = (IASTMacroExpansionLocation) location;
					offset = macroLoc.asFileLocation().getNodeOffset();
				}else {
					offset = location.asFileLocation().getNodeOffset();
				}
				if(offset < nodeStart) nodeStart = offset;
			}
		} else {
			nodeStart = node.getFileLocation().getNodeOffset();
		}
		
		return nodeStart;
	}
	
	public static int getEndOffsetIncludingComments(IASTNode node) {
		int fileOffset = 0;
		int length = 0;
		
		IASTNodeLocation[] nodeLocations = node.getNodeLocations();
		if (nodeLocations.length != 1) {
			for (IASTNodeLocation location : nodeLocations) {
				if (location instanceof IASTMacroExpansionLocation) {
					IASTMacroExpansionLocation macroLoc = (IASTMacroExpansionLocation) location;
					fileOffset = macroLoc.asFileLocation().getNodeOffset();
					length = macroLoc.asFileLocation().getNodeLength();
				}else {
					fileOffset = location.asFileLocation().getNodeOffset();
					length = location.asFileLocation().getNodeLength();
				}
			}
		} else {
			IASTFileLocation loc = node.getFileLocation();
			
			fileOffset = loc.getNodeOffset();
			length = loc.getNodeLength();
		}
		return fileOffset + length;
		
	}

	public static int getEndOffsetWithoutComments(IASTNode node) {
		return node.getFileLocation().getNodeOffset() + node.getFileLocation().getNodeLength();
	}
	
	public static int getLengthIncludingComment(IASTNode node) {
		return OffsetHelper.getEndOffsetIncludingComments(node) - OffsetHelper.getOffsetIncludingComment(node);
	}

	public static int getNodeOffset(ASTNode node) {
		return node.getOffset();
	}

	public static int getNodeEndPoint(ASTNode node) {
		return node.getOffset() + node.getLength();
	}

	public static int getStartingLineNumber(IASTNode node) {
		return node.getFileLocation().getStartingLineNumber();
	}

	public static int getEndingLineNumber(IASTNode node) {
		return node.getFileLocation().getEndingLineNumber();
	}	
	
}

Back to the top