Search in sources :

Example 11 with IASTTranslationUnit

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

the class ASTComparison method typeSensitiveNodeEquals.

private static <T extends IASTNode> boolean typeSensitiveNodeEquals(T expected, T actual, EnumSet<ComparisonArg> args) {
    if (!expected.getClass().equals(actual.getClass())) {
        return false;
    }
    if (expected instanceof IASTDeclaration) {
        /* Terminal checks */
        if (expected instanceof ICPPASTFunctionDefinition) {
            ICPPASTFunctionDefinition et = as(expected);
            ICPPASTFunctionDefinition at = as(actual);
            return et.isDefaulted() == at.isDefaulted() && et.isDeleted() == at.isDeleted();
        } else if (expected instanceof ICPPASTTemplateDeclaration) {
            ICPPASTTemplateDeclaration et = as(expected);
            ICPPASTTemplateDeclaration at = as(actual);
            return et.isExported() == at.isExported();
        } else if (expected instanceof ICPPASTNamespaceDefinition) {
            ICPPASTNamespaceDefinition et = as(expected);
            ICPPASTNamespaceDefinition at = as(actual);
            return et.isInline() == at.isInline();
        } else if (expected instanceof ICPPASTVisibilityLabel) {
            ICPPASTVisibilityLabel et = as(expected);
            ICPPASTVisibilityLabel at = as(actual);
            return et.getVisibility() == at.getVisibility();
        } else if (expected instanceof ICPPASTLinkageSpecification) {
            ICPPASTLinkageSpecification et = as(expected);
            ICPPASTLinkageSpecification at = as(actual);
            return et.getLiteral().equals(at.getLiteral());
        } else if (expected instanceof ICPPASTUsingDeclaration) {
            ICPPASTUsingDeclaration et = as(expected);
            ICPPASTUsingDeclaration at = as(actual);
            return et.isTypename() == at.isTypename();
        } else if (expected instanceof IASTSimpleDeclaration || expected instanceof ICPPASTAliasDeclaration || expected instanceof ICPPASTUsingDirective) {
            /* All tokens are stored in the child-nodes */
            return defaultHandler(expected, actual, args);
        }
    /* Continue comparing raw-signature */
    } else if (expected instanceof ICPPASTTemplateParameter) {
        /* Template Parameter */
        ICPPASTTemplateParameter e = as(expected);
        ICPPASTTemplateParameter a = as(actual);
        if (e.isParameterPack() != a.isParameterPack())
            return false;
        if (expected instanceof ICPPASTSimpleTypeTemplateParameter) {
            ICPPASTSimpleTypeTemplateParameter et = as(expected);
            ICPPASTSimpleTypeTemplateParameter at = as(actual);
            return et.getParameterType() == at.getParameterType();
        } else if (expected instanceof ICPPASTParameterDeclaration || expected instanceof ICPPASTTemplatedTypeTemplateParameter) {
            /* All tokens are stored in the child-nodes */
            return defaultHandler(expected, actual, args);
        }
    } else if (expected instanceof ICPPASTDeclarator) {
        /* Terminal checks */
        if (expected instanceof ICPPASTFunctionDeclarator) {
            ICPPASTFunctionDeclarator e = as(expected);
            ICPPASTFunctionDeclarator a = as(actual);
            return e.isConst() == a.isConst() && e.isFinal() == a.isFinal() && e.isPureVirtual() == a.isPureVirtual() && e.isVolatile() == a.isVolatile();
        } else {
            /* All tokens are stored in the child-nodes */
            return defaultHandler(expected, actual, args);
        }
    /* Continue comparing raw-signature */
    } else if (expected instanceof ICPPASTDeclSpecifier) {
        /* DeclSpecifier */
        ICPPASTDeclSpecifier e = as(expected);
        ICPPASTDeclSpecifier a = as(actual);
        if (e.isConst() != a.isConst() || e.isVirtual() != a.isVirtual() || e.isVolatile() != a.isVolatile() || e.isConstexpr() != a.isConstexpr() || e.isExplicit() != a.isExplicit() || e.isFriend() != a.isFriend() || e.isRestrict() != a.isRestrict() || e.isThreadLocal() != a.isThreadLocal() || e.getStorageClass() != a.getStorageClass())
            return false;
        /* Terminal checks */
        if (expected instanceof ICPPASTNamedTypeSpecifier) {
            ICPPASTNamedTypeSpecifier et = as(expected);
            ICPPASTNamedTypeSpecifier at = as(actual);
            return et.isTypename() == at.isTypename();
        } else if (expected instanceof ICPPASTCompositeTypeSpecifier) {
            ICPPASTCompositeTypeSpecifier et = as(expected);
            ICPPASTCompositeTypeSpecifier at = as(actual);
            return et.isFinal() == at.isFinal() && et.isVirtual() == at.isVirtual() && et.getKey() == at.getKey();
        } else if (expected instanceof ICPPASTElaboratedTypeSpecifier) {
            ICPPASTElaboratedTypeSpecifier et = as(expected);
            ICPPASTElaboratedTypeSpecifier at = as(actual);
            return et.getKind() == at.getKind();
        } else if (expected instanceof ICPPASTEnumerationSpecifier) {
            ICPPASTEnumerationSpecifier et = as(expected);
            ICPPASTEnumerationSpecifier at = as(actual);
            return et.isOpaque() == at.isOpaque() && et.isScoped() == at.isScoped();
        } else {
            return defaultHandler(expected, actual, args);
        }
    /* Continue comparing raw-signature */
    } else if (expected instanceof IASTExpression) {
        if (expected instanceof ICPPASTUnaryExpression) {
            ICPPASTUnaryExpression et = as(expected);
            ICPPASTUnaryExpression at = as(actual);
            return et.getOperator() == at.getOperator();
        } else if (expected instanceof ICPPASTBinaryExpression) {
            ICPPASTBinaryExpression et = as(expected);
            ICPPASTBinaryExpression at = as(actual);
            return et.getOperator() == at.getOperator();
        } else if (expected instanceof ICPPASTLiteralExpression) {
            ICPPASTLiteralExpression et = as(expected);
            ICPPASTLiteralExpression at = as(actual);
            return et.getKind() == at.getKind() && equalsRaw(et, at, args);
        } else if (expected instanceof ICPPASTCastExpression) {
            ICPPASTCastExpression et = as(expected);
            ICPPASTCastExpression at = as(actual);
            return et.getOperator() == at.getOperator();
        } else if (expected instanceof ICPPASTNewExpression) {
            ICPPASTNewExpression et = as(expected);
            ICPPASTNewExpression at = as(actual);
            return et.isArrayAllocation() == at.isArrayAllocation() && et.isGlobal() == at.isGlobal() && et.isNewTypeId() == at.isNewTypeId();
        } else if (expected instanceof ICPPASTDeleteExpression) {
            ICPPASTDeleteExpression et = as(expected);
            ICPPASTDeleteExpression at = as(actual);
            return et.isVectored() == at.isVectored() && et.isGlobal() == at.isGlobal();
        } else if (expected instanceof ICPPASTFieldReference) {
            ICPPASTFieldReference et = as(expected);
            ICPPASTFieldReference at = as(actual);
            return et.isPointerDereference() == at.isPointerDereference() && et.isTemplate() == at.isTemplate();
        } else if (expected instanceof ICPPASTLambdaExpression) {
            ICPPASTLambdaExpression et = as(expected);
            ICPPASTLambdaExpression at = as(actual);
            return et.getCaptureDefault() == at.getCaptureDefault();
        } else if (expected instanceof ICPPASTFunctionCallExpression || expected instanceof ICPPASTSimpleTypeConstructorExpression || expected instanceof ICPPASTLambdaExpression || expected instanceof ICPPASTPackExpansionExpression || expected instanceof IASTIdExpression || expected instanceof ICPPASTArraySubscriptExpression || expected instanceof ICPPASTExpressionList || expected instanceof IASTConditionalExpression) {
            /* All tokens are stored in the child-nodes */
            return defaultHandler(expected, actual, args);
        }
    /* Continue comparing raw-signature */
    } else if (expected instanceof ICPPASTReferenceOperator) {
        /* ReferenceOperator */
        ICPPASTReferenceOperator et = as(expected);
        ICPPASTReferenceOperator at = as(actual);
        return et.isRValueReference() == at.isRValueReference();
    } else if (expected instanceof IASTPointer) {
        /* Pointers */
        IASTPointer et = as(expected);
        IASTPointer at = as(actual);
        return et.isConst() == at.isConst() && et.isRestrict() == at.isRestrict() && et.isVolatile() == at.isVolatile();
    } else if (expected instanceof IASTStatement) {
        return defaultHandler(expected, actual, args);
    } else if (expected instanceof ICPPASTPackExpandable) {
        /* ICPPASTPackExpandable */
        ICPPASTPackExpandable e = as(expected);
        ICPPASTPackExpandable a = as(actual);
        if (e.isPackExpansion() != a.isPackExpansion())
            return false;
        if (expected instanceof ICPPASTInitializerList) {
            ICPPASTInitializerList et = as(expected);
            ICPPASTInitializerList at = as(actual);
            return et.getSize() == at.getSize();
        } else if (expected instanceof ICPPASTBaseSpecifier) {
            ICPPASTBaseSpecifier et = as(expected);
            ICPPASTBaseSpecifier at = as(actual);
            return et.isVirtual() == at.isVirtual() && et.getVisibility() == at.getVisibility();
        } else if (expected instanceof ICPPASTCapture) {
            ICPPASTCapture et = as(expected);
            ICPPASTCapture at = as(actual);
            return et.capturesThisPointer() == at.capturesThisPointer() && et.isByReference() == at.isByReference();
        } else if (expected instanceof ICPPASTTypeId) {
            return true;
        } else {
            return defaultHandler(expected, actual, args);
        }
    } else if (expected instanceof ICPPASTName) {
        /* Names */
        ICPPASTName e = as(expected);
        ICPPASTName a = as(actual);
        if (e.isQualified() != a.isQualified())
            return false;
        if (expected instanceof ICPPASTTemplateId) {
            ICPPASTTemplateId et = as(expected);
            ICPPASTTemplateId at = as(actual);
            return et.isDeclaration() == at.isDeclaration();
        } else if (expected instanceof ICPPASTQualifiedName) {
            ICPPASTQualifiedName et = as(expected);
            ICPPASTQualifiedName at = as(actual);
            return et.isFullyQualified() == at.isFullyQualified();
        } else if (expected instanceof ICPPASTTemplateId) {
            /* Relevant information is contained in the children */
            return defaultHandler(expected, actual, args);
        }
    } else {
        /* OTHER */
        if (expected instanceof IASTTranslationUnit || expected instanceof IASTArrayModifier || expected instanceof IASTInitializer) {
            /* Relevant information is contained in the children */
            return defaultHandler(expected, actual, args);
        }
    /* Continue comparing raw-signature */
    }
    /* Default case */
    return equalsNormalizedRaw(expected, actual);
}
Also used : IASTExpression(org.eclipse.cdt.core.dom.ast.IASTExpression) ICPPASTLinkageSpecification(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLinkageSpecification) ICPPASTInitializerList(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTInitializerList) ICPPASTDeclSpecifier(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeclSpecifier) ICPPASTTemplateDeclaration(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration) IASTStatement(org.eclipse.cdt.core.dom.ast.IASTStatement) IASTTranslationUnit(org.eclipse.cdt.core.dom.ast.IASTTranslationUnit) ICPPASTExpressionList(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTExpressionList) ICPPASTParameterDeclaration(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTParameterDeclaration) IASTConditionalExpression(org.eclipse.cdt.core.dom.ast.IASTConditionalExpression) ICPPASTDeleteExpression(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeleteExpression) ICPPASTArraySubscriptExpression(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTArraySubscriptExpression) ICPPASTFunctionDeclarator(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator) IASTArrayModifier(org.eclipse.cdt.core.dom.ast.IASTArrayModifier) ICPPASTLiteralExpression(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLiteralExpression) ICPPASTUsingDirective(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUsingDirective) IASTIdExpression(org.eclipse.cdt.core.dom.ast.IASTIdExpression) ICPPASTTemplateId(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateId) ICPPASTNamedTypeSpecifier(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamedTypeSpecifier) ICPPASTNamespaceDefinition(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition) ICPPASTTemplateParameter(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateParameter) ICPPASTFieldReference(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFieldReference) ICPPASTVisibilityLabel(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTVisibilityLabel) IASTInitializer(org.eclipse.cdt.core.dom.ast.IASTInitializer) ICPPASTElaboratedTypeSpecifier(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTElaboratedTypeSpecifier) ICPPASTUnaryExpression(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUnaryExpression) ICPPASTName(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTName) ICPPASTQualifiedName(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName) IASTPointer(org.eclipse.cdt.core.dom.ast.IASTPointer) ICPPASTNewExpression(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression) ICPPASTDeclarator(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeclarator) ICPPASTCompositeTypeSpecifier(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier) ICPPASTFunctionCallExpression(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionCallExpression) ICPPASTFunctionDefinition(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDefinition) ICPPASTSimpleTypeTemplateParameter(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleTypeTemplateParameter) ICPPASTLambdaExpression(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLambdaExpression) ICPPASTSimpleTypeConstructorExpression(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleTypeConstructorExpression) ICPPASTReferenceOperator(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTReferenceOperator) ICPPASTBaseSpecifier(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier) ICPPASTEnumerationSpecifier(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTEnumerationSpecifier) IASTSimpleDeclaration(org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration) IASTDeclaration(org.eclipse.cdt.core.dom.ast.IASTDeclaration) ICPPASTAliasDeclaration(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTAliasDeclaration) ICPPASTPackExpandable(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTPackExpandable) ICPPASTUsingDeclaration(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUsingDeclaration) ICPPASTPackExpansionExpression(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTPackExpansionExpression) ICPPASTTypeId(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTypeId) ICPPASTCastExpression(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCastExpression) ICPPASTTemplatedTypeTemplateParameter(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplatedTypeTemplateParameter) ICPPASTCapture(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCapture) ICPPASTBinaryExpression(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTBinaryExpression)

Example 12 with IASTTranslationUnit

use of org.eclipse.cdt.core.dom.ast.IASTTranslationUnit in project arduino-eclipse-plugin by Sloeber.

the class PdePreprocessor method extendHeaderForFile.

private static String extendHeaderForFile(String header, IIndex index, ITranslationUnit tu) throws CoreException {
    String localHeader = header;
    // Locate All lines that are extern "C"
    HashMap<Integer, Integer> externCLines = new HashMap<>();
    IASTTranslationUnit astTuTest = tu.getAST(index, 0);
    IASTDeclaration[] topDeclaratons = astTuTest.getDeclarations();
    for (IASTDeclaration curTopDeclaration : topDeclaratons) {
        ICPPASTLinkageSpecification test = curTopDeclaration instanceof ICPPASTLinkageSpecification ? (ICPPASTLinkageSpecification) curTopDeclaration : null;
        if (test != null) {
            if (test.getLiteral().equals("\"C\"")) {
                Path curFile = new Path(curTopDeclaration.getContainingFilename());
                if (curFile.equals(tu.getFile().getLocation())) {
                    int startLine = test.getFileLocation().getStartingLineNumber();
                    int endLine = test.getFileLocation().getEndingLineNumber();
                    for (int curline = startLine; curline <= endLine; curline++) {
                        externCLines.put(new Integer(curline), null);
                    }
                }
            }
        }
    }
    // find the last line containing a include
    IInclude[] includes = tu.getIncludes();
    int lastHeaderLine = 0;
    for (IInclude include : includes) {
        int curHeaderLine = include.getSourceRange().getEndLine();
        lastHeaderLine = Math.max(lastHeaderLine, curHeaderLine);
    }
    // parse line by line until all includes have been parsed
    for (int curline = 1; curline <= lastHeaderLine; curline++) {
        ICElement curElement = tu.getElementAtLine(curline);
        if (curElement != null) {
            switch(curElement.getElementType()) {
                case ICElement.C_MACRO:
                    IMacro curMacro = (IMacro) curElement;
                    if (curMacro.isActive()) {
                        localHeader += curMacro.getSource() + NEWLINE;
                    }
                    break;
                case ICElement.C_VARIABLE:
                    IVariable curVardeclar = (IVariable) curElement;
                    if (curVardeclar.isActive()) {
                        String fullTypeName = curVardeclar.getTypeName();
                        // ignore double arrays
                        if (fullTypeName.indexOf('[') == fullTypeName.lastIndexOf('[')) {
                            String typeName = fullTypeName.replace('[', ' ').replace(']', ' ').trim();
                            String typeExtensions = fullTypeName.replace(typeName, "");
                            localHeader += "extern " + typeName + " " + curVardeclar.getElementName() + typeExtensions + ";" + NEWLINE;
                        }
                    }
                    break;
                case ICElement.C_INCLUDE:
                    IInclude curInclude = (IInclude) curElement;
                    int curHeaderLine = curInclude.getSourceRange().getStartLine();
                    if (curInclude.isActive()) {
                        if (externCLines.containsKey(new Integer(curHeaderLine))) {
                            localHeader += "extern \"C\" {" + NEWLINE;
                            localHeader += curInclude.getSource() + NEWLINE;
                            localHeader += "}" + NEWLINE;
                        } else {
                            localHeader += curInclude.getSource();
                            localHeader += NEWLINE;
                        }
                    }
                    break;
            }
        }
    }
    return localHeader;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) HashMap(java.util.HashMap) IASTDeclaration(org.eclipse.cdt.core.dom.ast.IASTDeclaration) IVariable(org.eclipse.cdt.core.model.IVariable) IInclude(org.eclipse.cdt.core.model.IInclude) ICPPASTLinkageSpecification(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLinkageSpecification) ICElement(org.eclipse.cdt.core.model.ICElement) IASTTranslationUnit(org.eclipse.cdt.core.dom.ast.IASTTranslationUnit) IMacro(org.eclipse.cdt.core.model.IMacro)

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