Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 49315c0d2729bf3497842c5b3df0a700cc8345a6 (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
/*******************************************************************************
 * Copyright (c) 2008, 2012 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
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;

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.core.dom.ast.IASTPreprocessorIncludeStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexMacro;
import org.eclipse.cdt.core.index.IIndexName;
import org.eclipse.cdt.core.index.IndexFilter;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMMacroReferenceName;
import org.eclipse.core.runtime.CoreException;

/**
 * Recognizes nodes that are the result of an macro expansion and replaces them
 * with a suitable macro call.
 *
 * @author Emanuel Graf IFS
 */
public class MacroExpansionHandler {
	private final Scribe scribe;
	private int lastMacroExpOffset;
	private IASTTranslationUnit ast;
	private Map<String, List<IIndexName>> macroExpansion = new TreeMap<String, List<IIndexName>>();

	public MacroExpansionHandler(Scribe scribe) {
		this.scribe = scribe;
	}

	protected boolean checkisMacroExpansionNode(IASTNode node) {
		return checkisMacroExpansionNode(node, true);
	}

	protected boolean isStatementWithMixedLocation(IASTStatement node) {
		IASTNodeLocation[] nodeLocations = getNodeLocations(node);
		if (nodeLocations != null && nodeLocations.length > 1) {
			for (IASTNodeLocation loc : nodeLocations) {
				if (loc instanceof IASTMacroExpansionLocation) {
					return true;
				}
			}
		}
		return false;
	}

	protected boolean macroExpansionAlreadyPrinted(IASTNode node) {
		IASTNodeLocation[] locs = node.getNodeLocations();
		if (locs.length ==1) {
			if (locs[0] instanceof IASTMacroExpansionLocation) {
				IASTMacroExpansionLocation macroNode = (IASTMacroExpansionLocation) locs[0];
				if (macroNode.asFileLocation().getNodeOffset() == lastMacroExpOffset) {
					return true;
				}
			}
		}
		return false;
	}

	protected boolean checkisMacroExpansionNode(IASTNode node, boolean write) {
		IASTTranslationUnit unit = node.getTranslationUnit();
		if (ast == null || !ast.equals(unit)) {
			initEmptyMacros(unit);
		}
		IASTNodeLocation[] locs = getNodeLocations(node);
		if (locs != null && locs.length == 1) {
			if (locs[0] instanceof IASTMacroExpansionLocation) {
				IASTMacroExpansionLocation macroNode = (IASTMacroExpansionLocation) locs[0];

				if (macroNode.asFileLocation().getNodeOffset() == lastMacroExpOffset) {
					return true;
				}
				if (write) {
					lastMacroExpOffset = macroNode.asFileLocation().getNodeOffset();
					node = node.getOriginalNode();
					scribe.print(node.getRawSignature());
				}
				return true;

			}
		}
		handleEmptyMacroExpansion(node);
		return false;
	}

	private IASTNodeLocation[] getNodeLocations(IASTNode node) {
		return node.getOriginalNode().getNodeLocations();
	}

	private void handleEmptyMacroExpansion(IASTNode node) {
		if (node.getTranslationUnit() == null)
			return;
		String file = node.getContainingFilename();
		List<IIndexName> exps = macroExpansion.get(file);
		if (exps != null && !exps.isEmpty()) {
			IASTFileLocation fileLocation = getFileLocation(node);
			if (fileLocation != null) {
				int nOff = fileLocation.getNodeOffset();
				for (IIndexName iIndexName : exps) {
					if (iIndexName instanceof PDOMMacroReferenceName) {
						PDOMMacroReferenceName mName = (PDOMMacroReferenceName) iIndexName;
						int eOff = mName.getFileLocation().getNodeOffset();
						int eLength = mName.getFileLocation().getNodeLength();
						if (eOff < nOff && Math.abs((eOff + eLength - nOff)) < 3) {
							scribe.print(mName.toString() + " "); //$NON-NLS-1$
						}
					}
				}
			}
		}
	}

	private IASTFileLocation getFileLocation(IASTNode node) {
		return node.getOriginalNode().getFileLocation();
	}

	private void initEmptyMacros(IASTTranslationUnit unit) {
		if (unit != null) {
			ast = unit;
			IIndex index = ast.getIndex();
			if (index != null) {
				macroExpansion = new TreeMap<String, List<IIndexName>>();
				IASTPreprocessorMacroDefinition[] md = ast.getMacroDefinitions();

				TreeSet<String>paths = new TreeSet<String>();
				for (IASTPreprocessorIncludeStatement is :ast.getIncludeDirectives()) {
					if (!is.isSystemInclude()) {
						paths.add(is.getContainingFilename());
					}
				}
				paths.add(ast.getContainingFilename());

				for (IASTPreprocessorMacroDefinition iastPreprocessorMacroDefinition : md) {
					if (iastPreprocessorMacroDefinition.getExpansion().length() == 0) {
						try {
							IIndexMacro[] macroBinding = index.findMacros(
									iastPreprocessorMacroDefinition.getName().toCharArray(),
									IndexFilter.ALL, null);
							if (macroBinding.length > 0) {
								IIndexName[] refs = index.findReferences(macroBinding[0]);
								for (IIndexName iIndexName : refs) {
									String filename = iIndexName.getFileLocation().getFileName();
									List<IIndexName> fileList = macroExpansion.get(filename);
									if (paths.contains(filename)) {
										if (fileList == null) {
											fileList = new ArrayList<IIndexName>();
											macroExpansion.put(filename, fileList);
										}
										fileList.add(iIndexName);
									}
								}
							}
						} catch (CoreException e) {
							e.printStackTrace();
						}
					}
				}
			} else {
				macroExpansion = Collections.emptyMap();
			}
		}
	}

	public void reset() {
		lastMacroExpOffset = -1;
	}
}

Back to the top