1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-30 21:55:31 +02:00

Bug 317875: extended utility classes

This commit is contained in:
Alena Laskavaia 2010-07-01 13:39:34 +00:00
parent 081788d5df
commit 69016edc87
2 changed files with 135 additions and 1 deletions

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2009,2010 Alena Laskavaia
* Copyright (c) 2009, 2010 Alena Laskavaia, Tomasz Wesolowksi
* 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
@ -7,16 +7,33 @@
*
* Contributors:
* Alena Laskavaia - initial API and implementation
* Tomasz Wesolowski - extension
*******************************************************************************/
package org.eclipse.cdt.codan.core.cxx;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeSelector;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroExpansion;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IBasicType.Kind;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.INodeFactory;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.rewrite.DeclarationGenerator;
/**
* Useful functions for doing code analysis on c/c++ AST
@ -63,4 +80,64 @@ public final class CxxAstUtils {
fileLocation.getNodeLength());
return macro != null;
}
public IASTFunctionDefinition getEnclosingFunction(IASTNode node) {
while (node != null && !(node instanceof IASTFunctionDefinition)) {
node = node.getParent();
}
return (IASTFunctionDefinition) node;
}
public IASTCompositeTypeSpecifier getEnclosingCompositeTypeSpecifier(IASTNode node) {
while (node != null && !(node instanceof IASTCompositeTypeSpecifier)) {
node = node.getParent();
}
return (IASTCompositeTypeSpecifier) node;
}
public IASTStatement getEnclosingStatement(IASTNode node) {
while (node != null && !(node instanceof IASTStatement)) {
node = node.getParent();
}
return (IASTStatement) node;
}
/**
* @param astName a name for the declaration
* @param factory the factory
* @return
*/
public IASTDeclaration createDeclaration(IASTName astName, INodeFactory factory) {
IASTSimpleDeclaration declaration = factory.newSimpleDeclaration(null);
IASTDeclarator declarator = factory.newDeclarator(astName.copy());
IASTDeclSpecifier declspec = factory.newSimpleDeclSpecifier();
((IASTSimpleDeclSpecifier)declspec).setType(Kind.eVoid);
if (astName.getParent() instanceof IASTIdExpression
&& astName.getParent().getParent() instanceof IASTBinaryExpression
&& ((IASTBinaryExpression) astName.getParent().getParent())
.getOperator() == IASTBinaryExpression.op_assign
&& astName.getParent().getParent().getParent() instanceof IASTExpressionStatement) {
IASTNode binaryExpr = astName.getParent().getParent();
IASTExpression tgt = null;
for (IASTNode node : binaryExpr.getChildren()) {
if (node != astName.getParent()) {
// use this expression as type source
tgt = (IASTExpression) node;
break;
}
}
if (tgt != null) {
DeclarationGenerator generator = DeclarationGenerator.create(factory);
IType type = tgt.getExpressionType();
declarator = generator.createDeclaratorFromType(type, astName.toCharArray());
declspec = generator.createDeclSpecFromType(type);
}
}
declaration.setDeclSpecifier(declspec);
declaration.addDeclarator(declarator);
return declaration;
}
}

View file

@ -0,0 +1,57 @@
/*******************************************************************************
* Copyright (c) 2010 Tomasz Wesolowski
* 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:
* Tomasz Wesolowski - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.codan.ui;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.link.LinkedModeModel;
import org.eclipse.jface.text.link.LinkedModeUI;
import org.eclipse.jface.text.link.LinkedPosition;
import org.eclipse.jface.text.link.LinkedPositionGroup;
/**
*
* Collection of utils methods for location/text manipulations
*
* @noinstantiate This class is not intended to be instantiated by clients.
* @since 1.1
*
*/
public final class JFaceTextUtils {
/**
* Enters the linked mode and creates a singular LinkedPosition around a
* given IASTFileLocation in a given Document.
*
* @param location
* the location to wrap
* @param document
* the document to use
*/
public static void markLocationForInsert(IASTFileLocation location,
ITextViewer viewer) {
IDocument document = viewer.getDocument();
LinkedPosition pos = new LinkedPosition(document,
location.getNodeOffset(), location.getNodeLength());
LinkedModeModel model = new LinkedModeModel();
LinkedPositionGroup group = new LinkedPositionGroup();
try {
group.addPosition(pos);
model.addGroup(group);
model.forceInstall();
} catch (BadLocationException e) {
return;
}
LinkedModeUI ui = new LinkedModeUI(model, new ITextViewer[] { viewer });
ui.enter();
}
}