Search in sources :

Example 6 with IASTTranslationUnit

use of org.eclipse.cdt.core.dom.ast.IASTTranslationUnit in project ch.hsr.ifs.cdttesting by IFS-HSR.

the class ASTComparisonTest method assertDifferentAST.

private void assertDifferentAST() throws IOException, ParserException {
    CharSequence[] sections = getContents(2);
    IASTTranslationUnit first = parse(sections[0].toString(), ParserLanguage.CPP, true);
    IASTTranslationUnit second = parse(sections[1].toString(), ParserLanguage.CPP, true);
    ComparisonResult result = ASTComparison.equalsAST(first, second, EnumSet.of(ComparisonArg.COMPARE_COMMENTS, ComparisonArg.COMPARE_INCLUDE_DIRECTIVES));
    assertTrue(result.state != ComparisonState.EQUAL);
}
Also used : IASTTranslationUnit(org.eclipse.cdt.core.dom.ast.IASTTranslationUnit) ComparisonResult(ch.hsr.ifs.cdttesting.cdttest.comparison.ASTComparison.ComparisonResult)

Example 7 with IASTTranslationUnit

use of org.eclipse.cdt.core.dom.ast.IASTTranslationUnit in project linuxtools by eclipse.

the class CParser method parseCurrentFunction.

@Override
public String parseCurrentFunction(IEditorInput input, int offset) throws CoreException {
    String currentElementName;
    if (input instanceof IFileEditorInput) {
        // Get the working copy and connect to input.
        IWorkingCopyManager manager = CUIPlugin.getDefault().getWorkingCopyManager();
        manager.connect(input);
        // Retrieve the C/C++ Element in question.
        IWorkingCopy workingCopy = manager.getWorkingCopy(input);
        ICElement method = workingCopy.getElementAtOffset(offset);
        manager.disconnect(input);
        // no element selected
        if (method == null)
            return "";
        // Get the current element name, to test it.
        currentElementName = method.getElementName();
        // Element doesn't have a name. Can go no further.
        if (currentElementName == null) {
            // element doesn't have a name
            return "";
        }
        // Get the Element Type to test.
        int elementType = method.getElementType();
        switch(elementType) {
            case ICElement.C_FIELD:
            case ICElement.C_METHOD:
            case ICElement.C_FUNCTION:
                break;
            case ICElement.C_MODEL:
                return "";
            // So it's not a method, field, function, or model. Where are we?
            default:
                ICElement tmpMethodType;
                if (((tmpMethodType = method.getAncestor(ICElement.C_FUNCTION)) == null) && ((tmpMethodType = method.getAncestor(ICElement.C_METHOD)) == null) && ((tmpMethodType = method.getAncestor(ICElement.C_CLASS)) == null)) {
                    return "";
                } else {
                    // In a class, but not in a method. Return class name instead.
                    method = tmpMethodType;
                    currentElementName = method.getElementName();
                }
        }
        // Build all ancestor classes.
        // Append all ancestor class names to string
        ICElement tmpParent = method.getParent();
        while (tmpParent != null) {
            ICElement tmpParentClass = tmpParent.getAncestor(ICElement.C_CLASS);
            if (tmpParentClass != null) {
                String tmpParentClassName = tmpParentClass.getElementName();
                if (tmpParentClassName == null)
                    return currentElementName;
                currentElementName = tmpParentClassName + "." + currentElementName;
            } else
                return currentElementName;
            tmpParent = tmpParentClass.getParent();
        }
        return currentElementName;
    } else if (input instanceof IStorageEditorInput) {
        // Get the working copy and connect to input.
        // don't follow inclusions
        currentElementName = "";
        IStorageEditorInput sei = (IStorageEditorInput) input;
        // don't follow inclusions
        IncludeFileContentProvider contentProvider = IncludeFileContentProvider.getEmptyFilesProvider();
        // empty scanner info
        IScannerInfo scanInfo = new ScannerInfo();
        IStorage ancestorStorage = sei.getStorage();
        if (ancestorStorage == null)
            return "";
        InputStream stream = ancestorStorage.getContents();
        byte[] buffer = new byte[100];
        String data = "";
        int read = 0;
        try {
            do {
                read = stream.read(buffer);
                if (read > 0) {
                    String tmp = new String(buffer, 0, read);
                    data = data.concat(tmp);
                }
            } while (read == 100);
            stream.close();
        } catch (IOException e) {
        // do nothing
        }
        // $NON-NLS-1$
        FileContent content = FileContent.create("<text>", data.toCharArray());
        // determine the language
        ILanguage language = GPPLanguage.getDefault();
        try {
            IASTTranslationUnit ast;
            int options = 0;
            ast = language.getASTTranslationUnit(content, scanInfo, contentProvider, null, options, ParserUtil.getParserLogService());
            IASTNodeSelector n = ast.getNodeSelector(null);
            IASTNode node = n.findFirstContainedNode(offset, 100);
            while (node != null && !(node instanceof IASTTranslationUnit)) {
                if (node instanceof IASTFunctionDefinition) {
                    IASTFunctionDefinition fd = (IASTFunctionDefinition) node;
                    IASTFunctionDeclarator d = fd.getDeclarator();
                    currentElementName = new String(d.getName().getSimpleID());
                    break;
                }
                node = node.getParent();
            }
        } catch (CoreException exc) {
            currentElementName = "";
            CUIPlugin.log(exc);
        }
        return currentElementName;
    }
    return "";
}
Also used : IScannerInfo(org.eclipse.cdt.core.parser.IScannerInfo) ScannerInfo(org.eclipse.cdt.core.parser.ScannerInfo) IncludeFileContentProvider(org.eclipse.cdt.core.parser.IncludeFileContentProvider) IWorkingCopy(org.eclipse.cdt.core.model.IWorkingCopy) IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) IASTFunctionDeclarator(org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator) InputStream(java.io.InputStream) IASTNode(org.eclipse.cdt.core.dom.ast.IASTNode) IOException(java.io.IOException) ICElement(org.eclipse.cdt.core.model.ICElement) IStorage(org.eclipse.core.resources.IStorage) IASTFunctionDefinition(org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition) FileContent(org.eclipse.cdt.core.parser.FileContent) ILanguage(org.eclipse.cdt.core.model.ILanguage) IASTNodeSelector(org.eclipse.cdt.core.dom.ast.IASTNodeSelector) CoreException(org.eclipse.core.runtime.CoreException) IFileEditorInput(org.eclipse.ui.IFileEditorInput) IASTTranslationUnit(org.eclipse.cdt.core.dom.ast.IASTTranslationUnit) IScannerInfo(org.eclipse.cdt.core.parser.IScannerInfo) IWorkingCopyManager(org.eclipse.cdt.ui.IWorkingCopyManager)

Example 8 with IASTTranslationUnit

use of org.eclipse.cdt.core.dom.ast.IASTTranslationUnit in project linuxtools by eclipse.

the class AbstractValgrindMarkerResolution method getIASTNode.

/**
 * Returns the enclosed AST node in the given marker.
 * @param marker The {@link IMarker} containing the {@link IASTNode}
 * @param document - document which is used to calculate offset
 * @return the enclosed {@link IASTNode}
 */
protected IASTNode getIASTNode(IMarker marker, IDocument document) {
    int offset = this.getOffset(marker, document);
    int length = this.getLength(marker, document);
    IASTNode node = null;
    IASTTranslationUnit ast = getASTTranslationUnit(marker);
    IASTNodeSelector nodeSelector = ast.getNodeSelector(marker.getResource().getLocationURI().getPath());
    node = nodeSelector.findFirstContainedNode(offset, length);
    return node;
}
Also used : IASTNodeSelector(org.eclipse.cdt.core.dom.ast.IASTNodeSelector) IASTTranslationUnit(org.eclipse.cdt.core.dom.ast.IASTTranslationUnit) IASTNode(org.eclipse.cdt.core.dom.ast.IASTNode)

Example 9 with IASTTranslationUnit

use of org.eclipse.cdt.core.dom.ast.IASTTranslationUnit in project ch.hsr.ifs.cdttesting by IFS-HSR.

the class DummyRenameRefactoring method collectModifications.

@Override
protected void collectModifications(IProgressMonitor pm, ModificationCollector collector) throws CoreException, OperationCanceledException {
    IASTTranslationUnit ast = refactoringContext.getAST(tu, pm);
    IASTSimpleDeclaration funcDeclaration = (IASTSimpleDeclaration) ast.getDeclarations()[0];
    IASTFunctionDeclarator declarator = (IASTFunctionDeclarator) funcDeclaration.getDeclarators()[0];
    IASTFunctionDeclarator newDeclarator = declarator.copy();
    IASTName newName = CPPNodeFactory.getDefault().newName("b".toCharArray());
    newDeclarator.setName(newName);
    ASTRewrite rewrite = collector.rewriterForTranslationUnit(ast);
    rewrite.replace(declarator, newDeclarator, new TextEditGroup(""));
}
Also used : IASTFunctionDeclarator(org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator) IASTSimpleDeclaration(org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration) IASTName(org.eclipse.cdt.core.dom.ast.IASTName) IASTTranslationUnit(org.eclipse.cdt.core.dom.ast.IASTTranslationUnit) ASTRewrite(org.eclipse.cdt.core.dom.rewrite.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 10 with IASTTranslationUnit

use of org.eclipse.cdt.core.dom.ast.IASTTranslationUnit in project ch.hsr.ifs.cdttesting by IFS-HSR.

the class MyQuickFix method modifyAST.

@Override
public void modifyAST(IIndex index, IMarker marker) {
    try {
        @SuppressWarnings("unused") IASTTranslationUnit ast = getTranslationUnitViaEditor(marker).getAST(index, ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
        // a real test should use ModificationCollector/ASTRewrite or Refactoring here.
        // For testing purposes, we only replace some text directly.
        TextFileChange change = new TextFileChange("rename foo to bar text edit", (IFile) marker.getResource());
        change.setEdit(new ReplaceEdit(4, 3, "bar"));
        change.perform(new NullProgressMonitor());
    } catch (CoreException e) {
        throw new RuntimeException(e);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) IASTTranslationUnit(org.eclipse.cdt.core.dom.ast.IASTTranslationUnit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange)

Aggregations

IASTTranslationUnit (org.eclipse.cdt.core.dom.ast.IASTTranslationUnit)12 IASTFunctionDeclarator (org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator)3 IASTName (org.eclipse.cdt.core.dom.ast.IASTName)3 IASTNode (org.eclipse.cdt.core.dom.ast.IASTNode)3 IASTSimpleDeclaration (org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration)3 IASTDeclaration (org.eclipse.cdt.core.dom.ast.IASTDeclaration)2 IASTNodeSelector (org.eclipse.cdt.core.dom.ast.IASTNodeSelector)2 ICPPASTLinkageSpecification (org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLinkageSpecification)2 ICElement (org.eclipse.cdt.core.model.ICElement)2 CoreException (org.eclipse.core.runtime.CoreException)2 ComparisonResult (ch.hsr.ifs.cdttesting.cdttest.comparison.ASTComparison.ComparisonResult)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 IASTArrayModifier (org.eclipse.cdt.core.dom.ast.IASTArrayModifier)1 IASTConditionalExpression (org.eclipse.cdt.core.dom.ast.IASTConditionalExpression)1 IASTExpression (org.eclipse.cdt.core.dom.ast.IASTExpression)1 IASTFunctionDefinition (org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition)1 IASTIdExpression (org.eclipse.cdt.core.dom.ast.IASTIdExpression)1 IASTInitializer (org.eclipse.cdt.core.dom.ast.IASTInitializer)1