From 69016edc878f8bcdc26d7d0eec486ef3e8103b3e Mon Sep 17 00:00:00 2001 From: Alena Laskavaia Date: Thu, 1 Jul 2010 13:39:34 +0000 Subject: [PATCH] Bug 317875: extended utility classes --- .../cdt/codan/core/cxx/CxxAstUtils.java | 79 ++++++++++++++++++- .../eclipse/cdt/codan/ui/JFaceTextUtils.java | 57 +++++++++++++ 2 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/JFaceTextUtils.java diff --git a/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/CxxAstUtils.java b/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/CxxAstUtils.java index e3a5c5163f1..53bc34e738d 100644 --- a/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/CxxAstUtils.java +++ b/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/CxxAstUtils.java @@ -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; + } } diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/JFaceTextUtils.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/JFaceTextUtils.java new file mode 100644 index 00000000000..79923062218 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/JFaceTextUtils.java @@ -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(); + } +} \ No newline at end of file