Lines 48-54
Link Here
|
48 |
*/ |
48 |
*/ |
49 |
public class CPPPopulateASTViewAction extends CPPASTVisitor implements IPopulateDOMASTAction { |
49 |
public class CPPPopulateASTViewAction extends CPPASTVisitor implements IPopulateDOMASTAction { |
50 |
private static final int INITIAL_PROBLEM_SIZE = 4; |
50 |
private static final int INITIAL_PROBLEM_SIZE = 4; |
51 |
private static final int INITIAL_INCLUDE_STATEMENT_SIZE = 8; |
|
|
52 |
{ |
51 |
{ |
53 |
shouldVisitNames = true; |
52 |
shouldVisitNames = true; |
54 |
shouldVisitDeclarations = true; |
53 |
shouldVisitDeclarations = true; |
Lines 73-86
Link Here
|
73 |
this.monitor = monitor; |
72 |
this.monitor = monitor; |
74 |
} |
73 |
} |
75 |
|
74 |
|
76 |
private int addRoot(IASTNode node) { |
75 |
private class DOMASTNodeLeafContinue extends DOMASTNodeLeaf { |
77 |
if (monitor != null && monitor.isCanceled()) return PROCESS_ABORT; |
76 |
public DOMASTNodeLeafContinue(IASTNode node) { |
78 |
if (node == null) return PROCESS_CONTINUE; |
77 |
super(node); |
|
|
78 |
} |
79 |
} |
80 |
|
81 |
/** |
82 |
* return null if the algorithm should stop (monitor was cancelled) |
83 |
* return DOMASTNodeLeafContinue if the algorithm should continue but no valid DOMASTNodeLeaf was added (i.e. node was null |
84 |
* return the DOMASTNodeLeaf added to the DOM AST View's model otherwise |
85 |
* |
86 |
* @param node |
87 |
* @return |
88 |
*/ |
89 |
private DOMASTNodeLeaf addRoot(IASTNode node) { |
90 |
if (monitor != null && monitor.isCanceled()) return null; |
91 |
if (node == null) return new DOMASTNodeLeafContinue(null); |
79 |
|
92 |
|
80 |
// only do length check for ASTNode (getNodeLocations on PreprocessorStatements is very expensive) |
93 |
// only do length check for ASTNode (getNodeLocations on PreprocessorStatements is very expensive) |
81 |
if (!(node instanceof ICPPASTLinkageSpecification) && |
94 |
if (!(node instanceof ICPPASTLinkageSpecification) && |
82 |
node instanceof ASTNode && ((ASTNode)node).getLength() <= 0) |
95 |
node instanceof ASTNode && ((ASTNode)node).getLength() <= 0) |
83 |
return PROCESS_CONTINUE; |
96 |
return new DOMASTNodeLeafContinue(null); |
84 |
|
97 |
|
85 |
DOMASTNodeParent parent = null; |
98 |
DOMASTNodeParent parent = null; |
86 |
|
99 |
|
Lines 99-110
Link Here
|
99 |
if (parent == null) |
112 |
if (parent == null) |
100 |
parent = root; |
113 |
parent = root; |
101 |
|
114 |
|
102 |
createNode(parent, node); |
115 |
return createNode(parent, node); |
103 |
|
|
|
104 |
return PROCESS_CONTINUE; |
105 |
} |
116 |
} |
106 |
|
117 |
|
107 |
private void createNode(DOMASTNodeParent parent, IASTNode node) { |
118 |
private DOMASTNodeLeaf createNode(DOMASTNodeParent parent, IASTNode node) { |
108 |
DOMASTNodeParent tree = new DOMASTNodeParent(node); |
119 |
DOMASTNodeParent tree = new DOMASTNodeParent(node); |
109 |
parent.addChild(tree); |
120 |
parent.addChild(tree); |
110 |
|
121 |
|
Lines 121-140
Link Here
|
121 |
tree.setFiltersFlag(DOMASTNodeLeaf.FLAG_PREPROCESSOR); |
132 |
tree.setFiltersFlag(DOMASTNodeLeaf.FLAG_PREPROCESSOR); |
122 |
if (node instanceof IASTPreprocessorIncludeStatement) |
133 |
if (node instanceof IASTPreprocessorIncludeStatement) |
123 |
tree.setFiltersFlag(DOMASTNodeLeaf.FLAG_INCLUDE_STATEMENTS); |
134 |
tree.setFiltersFlag(DOMASTNodeLeaf.FLAG_INCLUDE_STATEMENTS); |
|
|
135 |
|
136 |
return tree; |
124 |
} |
137 |
} |
125 |
|
138 |
|
126 |
/* (non-Javadoc) |
139 |
/* (non-Javadoc) |
127 |
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction#processDeclaration(org.eclipse.cdt.core.dom.ast.IASTDeclaration) |
140 |
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction#processDeclaration(org.eclipse.cdt.core.dom.ast.IASTDeclaration) |
128 |
*/ |
141 |
*/ |
129 |
public int visit(IASTDeclaration declaration) { |
142 |
public int visit(IASTDeclaration declaration) { |
130 |
return addRoot(declaration); |
143 |
DOMASTNodeLeaf temp = addRoot(declaration); |
|
|
144 |
if (temp == null) |
145 |
return PROCESS_ABORT; |
146 |
else if (temp instanceof DOMASTNodeLeafContinue) |
147 |
return PROCESS_CONTINUE; |
148 |
else |
149 |
return PROCESS_CONTINUE; |
131 |
} |
150 |
} |
132 |
|
151 |
|
133 |
/* (non-Javadoc) |
152 |
/* (non-Javadoc) |
134 |
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction#processDeclarator(org.eclipse.cdt.core.dom.ast.IASTDeclarator) |
153 |
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction#processDeclarator(org.eclipse.cdt.core.dom.ast.IASTDeclarator) |
135 |
*/ |
154 |
*/ |
136 |
public int visit(IASTDeclarator declarator) { |
155 |
public int visit(IASTDeclarator declarator) { |
137 |
int ret = addRoot(declarator); |
156 |
DOMASTNodeLeaf temp = addRoot(declarator); |
138 |
|
157 |
|
139 |
IASTPointerOperator[] ops = declarator.getPointerOperators(); |
158 |
IASTPointerOperator[] ops = declarator.getPointerOperators(); |
140 |
for(int i=0; i<ops.length; i++) |
159 |
for(int i=0; i<ops.length; i++) |
Lines 160-217
Link Here
|
160 |
} |
179 |
} |
161 |
} |
180 |
} |
162 |
|
181 |
|
163 |
return ret; |
182 |
if (temp == null) |
|
|
183 |
return PROCESS_ABORT; |
184 |
else if (temp instanceof DOMASTNodeLeafContinue) |
185 |
return PROCESS_CONTINUE; |
186 |
else |
187 |
return PROCESS_CONTINUE; |
164 |
} |
188 |
} |
165 |
|
189 |
|
166 |
/* (non-Javadoc) |
190 |
/* (non-Javadoc) |
167 |
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction#processBaseSpecifier(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier) |
191 |
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction#processBaseSpecifier(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier) |
168 |
*/ |
192 |
*/ |
169 |
public int visit(ICPPASTBaseSpecifier specifier) { |
193 |
public int visit(ICPPASTBaseSpecifier specifier) { |
170 |
return addRoot(specifier); |
194 |
DOMASTNodeLeaf temp = addRoot(specifier); |
|
|
195 |
if (temp == null) |
196 |
return PROCESS_ABORT; |
197 |
else if (temp instanceof DOMASTNodeLeafContinue) |
198 |
return PROCESS_CONTINUE; |
199 |
else |
200 |
return PROCESS_CONTINUE; |
171 |
} |
201 |
} |
172 |
|
202 |
|
173 |
/* (non-Javadoc) |
203 |
/* (non-Javadoc) |
174 |
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction#processDeclSpecifier(org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier) |
204 |
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction#processDeclSpecifier(org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier) |
175 |
*/ |
205 |
*/ |
176 |
public int visit(IASTDeclSpecifier declSpec) { |
206 |
public int visit(IASTDeclSpecifier declSpec) { |
177 |
return addRoot(declSpec); |
207 |
DOMASTNodeLeaf temp = addRoot(declSpec); |
|
|
208 |
if (temp == null) |
209 |
return PROCESS_ABORT; |
210 |
else if (temp instanceof DOMASTNodeLeafContinue) |
211 |
return PROCESS_CONTINUE; |
212 |
else |
213 |
return PROCESS_CONTINUE; |
178 |
} |
214 |
} |
179 |
|
215 |
|
180 |
/* (non-Javadoc) |
216 |
/* (non-Javadoc) |
181 |
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction#processEnumerator(org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator) |
217 |
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction#processEnumerator(org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator) |
182 |
*/ |
218 |
*/ |
183 |
public int visit(IASTEnumerator enumerator) { |
219 |
public int visit(IASTEnumerator enumerator) { |
184 |
return addRoot(enumerator); |
220 |
DOMASTNodeLeaf temp = addRoot(enumerator); |
|
|
221 |
if (temp == null) |
222 |
return PROCESS_ABORT; |
223 |
else if (temp instanceof DOMASTNodeLeafContinue) |
224 |
return PROCESS_CONTINUE; |
225 |
else |
226 |
return PROCESS_CONTINUE; |
185 |
} |
227 |
} |
186 |
|
228 |
|
187 |
/* (non-Javadoc) |
229 |
/* (non-Javadoc) |
188 |
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction#processExpression(org.eclipse.cdt.core.dom.ast.IASTExpression) |
230 |
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction#processExpression(org.eclipse.cdt.core.dom.ast.IASTExpression) |
189 |
*/ |
231 |
*/ |
190 |
public int visit(IASTExpression expression) { |
232 |
public int visit(IASTExpression expression) { |
191 |
return addRoot(expression); |
233 |
DOMASTNodeLeaf temp = addRoot(expression); |
|
|
234 |
if (temp == null) |
235 |
return PROCESS_ABORT; |
236 |
else if (temp instanceof DOMASTNodeLeafContinue) |
237 |
return PROCESS_CONTINUE; |
238 |
else |
239 |
return PROCESS_CONTINUE; |
192 |
} |
240 |
} |
193 |
|
241 |
|
194 |
/* (non-Javadoc) |
242 |
/* (non-Javadoc) |
195 |
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction#processInitializer(org.eclipse.cdt.core.dom.ast.IASTInitializer) |
243 |
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction#processInitializer(org.eclipse.cdt.core.dom.ast.IASTInitializer) |
196 |
*/ |
244 |
*/ |
197 |
public int visit(IASTInitializer initializer) { |
245 |
public int visit(IASTInitializer initializer) { |
198 |
return addRoot(initializer); |
246 |
DOMASTNodeLeaf temp = addRoot(initializer); |
|
|
247 |
if (temp == null) |
248 |
return PROCESS_ABORT; |
249 |
else if (temp instanceof DOMASTNodeLeafContinue) |
250 |
return PROCESS_CONTINUE; |
251 |
else |
252 |
return PROCESS_CONTINUE; |
199 |
} |
253 |
} |
200 |
|
254 |
|
201 |
/* (non-Javadoc) |
255 |
/* (non-Javadoc) |
202 |
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction#processName(org.eclipse.cdt.core.dom.ast.IASTName) |
256 |
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction#processName(org.eclipse.cdt.core.dom.ast.IASTName) |
203 |
*/ |
257 |
*/ |
204 |
public int visit(IASTName name) { |
258 |
public int visit(IASTName name) { |
|
|
259 |
DOMASTNodeLeaf temp = null; |
205 |
if (name.toString() != null) |
260 |
if (name.toString() != null) |
206 |
return addRoot(name); |
261 |
temp = addRoot(name); |
207 |
return PROCESS_CONTINUE; |
262 |
else |
|
|
263 |
return PROCESS_CONTINUE; |
264 |
|
265 |
if (temp == null) |
266 |
return PROCESS_ABORT; |
267 |
else if (temp instanceof DOMASTNodeLeafContinue) |
268 |
return PROCESS_CONTINUE; |
269 |
else |
270 |
return PROCESS_CONTINUE; |
208 |
} |
271 |
} |
209 |
|
272 |
|
210 |
/* (non-Javadoc) |
273 |
/* (non-Javadoc) |
211 |
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction#processNamespace(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition) |
274 |
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction#processNamespace(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition) |
212 |
*/ |
275 |
*/ |
213 |
public int visit(ICPPASTNamespaceDefinition namespace) { |
276 |
public int visit(ICPPASTNamespaceDefinition namespace) { |
214 |
return addRoot(namespace); |
277 |
DOMASTNodeLeaf temp = addRoot(namespace); |
|
|
278 |
if (temp == null) |
279 |
return PROCESS_ABORT; |
280 |
else if (temp instanceof DOMASTNodeLeafContinue) |
281 |
return PROCESS_CONTINUE; |
282 |
else |
283 |
return PROCESS_CONTINUE; |
215 |
} |
284 |
} |
216 |
|
285 |
|
217 |
/* (non-Javadoc) |
286 |
/* (non-Javadoc) |
Lines 219-255
Link Here
|
219 |
*/ |
288 |
*/ |
220 |
public int visit( |
289 |
public int visit( |
221 |
IASTParameterDeclaration parameterDeclaration) { |
290 |
IASTParameterDeclaration parameterDeclaration) { |
222 |
return addRoot(parameterDeclaration); |
291 |
DOMASTNodeLeaf temp = addRoot(parameterDeclaration); |
|
|
292 |
if (temp == null) |
293 |
return PROCESS_ABORT; |
294 |
else if (temp instanceof DOMASTNodeLeafContinue) |
295 |
return PROCESS_CONTINUE; |
296 |
else |
297 |
return PROCESS_CONTINUE; |
223 |
} |
298 |
} |
224 |
|
299 |
|
225 |
/* (non-Javadoc) |
300 |
/* (non-Javadoc) |
226 |
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction#processStatement(org.eclipse.cdt.core.dom.ast.IASTStatement) |
301 |
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction#processStatement(org.eclipse.cdt.core.dom.ast.IASTStatement) |
227 |
*/ |
302 |
*/ |
228 |
public int visit(IASTStatement statement) { |
303 |
public int visit(IASTStatement statement) { |
229 |
return addRoot(statement); |
304 |
DOMASTNodeLeaf temp = addRoot(statement); |
|
|
305 |
if (temp == null) |
306 |
return PROCESS_ABORT; |
307 |
else if (temp instanceof DOMASTNodeLeafContinue) |
308 |
return PROCESS_CONTINUE; |
309 |
else |
310 |
return PROCESS_CONTINUE; |
230 |
} |
311 |
} |
231 |
|
312 |
|
232 |
/* (non-Javadoc) |
313 |
/* (non-Javadoc) |
233 |
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction#processTypeId(org.eclipse.cdt.core.dom.ast.IASTTypeId) |
314 |
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction#processTypeId(org.eclipse.cdt.core.dom.ast.IASTTypeId) |
234 |
*/ |
315 |
*/ |
235 |
public int visit(IASTTypeId typeId) { |
316 |
public int visit(IASTTypeId typeId) { |
236 |
return addRoot(typeId); |
317 |
DOMASTNodeLeaf temp = addRoot(typeId); |
|
|
318 |
if (temp == null) |
319 |
return PROCESS_ABORT; |
320 |
else if (temp instanceof DOMASTNodeLeafContinue) |
321 |
return PROCESS_CONTINUE; |
322 |
else |
323 |
return PROCESS_CONTINUE; |
237 |
} |
324 |
} |
238 |
|
325 |
|
239 |
private void mergeNode(ASTNode node) { |
326 |
private DOMASTNodeLeaf mergeNode(ASTNode node) { |
240 |
addRoot(node); |
327 |
DOMASTNodeLeaf leaf = addRoot(node); |
241 |
|
328 |
|
242 |
if (node instanceof IASTPreprocessorMacroDefinition) |
329 |
if (node instanceof IASTPreprocessorMacroDefinition) |
243 |
addRoot(((IASTPreprocessorMacroDefinition)node).getName()); |
330 |
addRoot(((IASTPreprocessorMacroDefinition)node).getName()); |
|
|
331 |
|
332 |
return leaf; |
244 |
} |
333 |
} |
245 |
|
334 |
|
246 |
public void mergePreprocessorStatements(IASTPreprocessorStatement[] statements) { |
335 |
public DOMASTNodeLeaf[] mergePreprocessorStatements(IASTPreprocessorStatement[] statements) { |
|
|
336 |
DOMASTNodeLeaf[] leaves = new DOMASTNodeLeaf[statements.length]; |
247 |
for(int i=0; i<statements.length; i++) { |
337 |
for(int i=0; i<statements.length; i++) { |
248 |
if (monitor != null && monitor.isCanceled()) return; |
338 |
if (monitor != null && monitor.isCanceled()) return leaves; |
249 |
|
339 |
|
250 |
if (statements[i] instanceof ASTNode) |
340 |
if (statements[i] instanceof ASTNode) |
251 |
mergeNode((ASTNode)statements[i]); |
341 |
leaves[i] = mergeNode((ASTNode)statements[i]); |
252 |
} |
342 |
} |
|
|
343 |
|
344 |
return leaves; |
253 |
} |
345 |
} |
254 |
|
346 |
|
255 |
public void mergePreprocessorProblems(IASTProblem[] problems) { |
347 |
public void mergePreprocessorProblems(IASTProblem[] problems) { |
Lines 265-309
Link Here
|
265 |
return root; |
357 |
return root; |
266 |
} |
358 |
} |
267 |
|
359 |
|
268 |
public void groupIncludes(IASTPreprocessorStatement[] statements) { |
360 |
public void groupIncludes(DOMASTNodeLeaf[] treeIncludes) { |
269 |
// get all of the includes from the preprocessor statements (need the object since .equals isn't implemented) |
|
|
270 |
IASTPreprocessorIncludeStatement[] includes = new IASTPreprocessorIncludeStatement[INITIAL_INCLUDE_STATEMENT_SIZE]; |
271 |
int index = 0; |
272 |
for(int i=0; i<statements.length; i++) { |
273 |
if (monitor != null && monitor.isCanceled()) return; |
274 |
if (statements[i] instanceof IASTPreprocessorIncludeStatement) { |
275 |
if (index == includes.length) { |
276 |
includes = (IASTPreprocessorIncludeStatement[])ArrayUtil.append(IASTPreprocessorIncludeStatement.class, includes, statements[i]); |
277 |
index++; |
278 |
} else { |
279 |
includes[index++] = (IASTPreprocessorIncludeStatement)statements[i]; |
280 |
} |
281 |
} |
282 |
} |
283 |
|
284 |
// get the tree model elements corresponding to the includes |
285 |
DOMASTNodeParent[] treeIncludes = new DOMASTNodeParent[index]; |
286 |
for (int i=0; i<treeIncludes.length; i++) { |
287 |
if (monitor != null && monitor.isCanceled()) return; |
288 |
treeIncludes[i] = root.findTreeObject(includes[i], false); |
289 |
} |
290 |
|
291 |
// loop through the includes and make sure that all of the nodes |
361 |
// loop through the includes and make sure that all of the nodes |
292 |
// that are children of the TU are in the proper include (based on offset) |
362 |
// that are children of the TU are in the proper include (based on offset) |
293 |
DOMASTNodeLeaf child = null; |
363 |
DOMASTNodeLeaf child = null; |
294 |
outerLoop: for (int i=treeIncludes.length-1; i>=0; i--) { |
364 |
outerLoop: for (int i=treeIncludes.length-1; i>=0; i--) { |
295 |
if (treeIncludes[i] == null) continue; |
365 |
if (treeIncludes[i] == null) continue; |
296 |
|
366 |
|
|
|
367 |
IASTNode node = null; |
297 |
for(int j=root.getChildren(false).length-1; j>=0; j--) { |
368 |
for(int j=root.getChildren(false).length-1; j>=0; j--) { |
298 |
if (monitor != null && monitor.isCanceled()) return; |
369 |
// if (monitor != null && monitor.isCanceled()) return; // this causes a deadlock when checked here |
299 |
child = root.getChildren(false)[j]; |
370 |
child = root.getChildren(false)[j]; |
300 |
|
371 |
|
|
|
372 |
node = treeIncludes[i].getNode(); |
301 |
if (child != null && treeIncludes[i] != child && |
373 |
if (child != null && treeIncludes[i] != child && |
302 |
includes[i] instanceof ASTInclusionStatement && |
374 |
node instanceof ASTInclusionStatement && |
303 |
((ASTNode)child.getNode()).getOffset() >= ((ASTInclusionStatement)includes[i]).startOffset && |
375 |
((ASTNode)child.getNode()).getOffset() >= ((ASTInclusionStatement)node).startOffset && |
304 |
((ASTNode)child.getNode()).getOffset() <= ((ASTInclusionStatement)includes[i]).endOffset) { |
376 |
((ASTNode)child.getNode()).getOffset() <= ((ASTInclusionStatement)node).endOffset) { |
305 |
root.removeChild(child); |
377 |
root.removeChild(child); |
306 |
treeIncludes[i].addChild(child); |
378 |
((DOMASTNodeParent)treeIncludes[i]).addChild(child); |
307 |
} |
379 |
} |
308 |
} |
380 |
} |
309 |
} |
381 |
} |