Search in sources :

Example 1 with PointerType

use of de.fraunhofer.aisec.cpg.graph.types.PointerType in project cpg by Fraunhofer-AISEC.

the class ClassTemplateTest method testReferenceInTemplates.

@Test
void testReferenceInTemplates() throws Exception {
    // Test array.cpp: checks usage of referencetype of parameterized type (T[])
    TypeManager.reset();
    List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(Path.of(topLevel.toString(), "array.cpp").toFile()), topLevel, true);
    ClassTemplateDeclaration template = TestUtils.findByUniqueName(TestUtils.subnodesOfType(result, ClassTemplateDeclaration.class), "template<typename T, int N=10> class Array");
    RecordDeclaration array = TestUtils.findByUniqueName(TestUtils.subnodesOfType(result, RecordDeclaration.class), "Array");
    ParamVariableDeclaration N = TestUtils.findByUniqueName(TestUtils.subnodesOfType(result, ParamVariableDeclaration.class), "N");
    TypeParamDeclaration T = TestUtils.findByUniqueName(TestUtils.subnodesOfType(result, TypeParamDeclaration.class), "typename T");
    Literal<?> literal10 = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(10));
    FieldDeclaration thisField = TestUtils.findByUniqueName(TestUtils.subnodesOfType(result, FieldDeclaration.class), "this");
    FieldDeclaration m_Array = TestUtils.findByUniqueName(TestUtils.subnodesOfType(result, FieldDeclaration.class), "m_Array");
    assertEquals(2, template.getParameters().size());
    assertEquals(T, template.getParameters().get(0));
    assertEquals(N, template.getParameters().get(1));
    assertEquals(literal10, N.getDefault());
    assertEquals(2, array.getFields().size());
    assertEquals(thisField, array.getFields().get(0));
    assertEquals(m_Array, array.getFields().get(1));
    ObjectType arrayType = (ObjectType) thisField.getType();
    assertEquals(1, arrayType.getGenerics().size());
    assertEquals("T", arrayType.getGenerics().get(0).getName());
    ParameterizedType typeT = (ParameterizedType) arrayType.getGenerics().get(0);
    assertEquals(typeT, T.getType());
    assertTrue(m_Array.getType() instanceof PointerType);
    PointerType tArray = (PointerType) m_Array.getType();
    assertEquals(typeT, tArray.getElementType());
    ConstructExpression constructExpression = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, ConstructExpression.class), c -> c.getCode().equals("()"));
    assertEquals(template, constructExpression.getTemplateInstantiation());
    assertEquals(array, constructExpression.getInstantiates());
    assertEquals("int", constructExpression.getTemplateParameters().get(0).getName());
    assertEquals(literal10, constructExpression.getTemplateParameters().get(1));
    assertEquals("Array", constructExpression.getType().getName());
    ObjectType instantiatedType = (ObjectType) constructExpression.getType();
    assertEquals(1, instantiatedType.getGenerics().size());
    assertEquals("int", instantiatedType.getGenerics().get(0).getName());
}
Also used : PointerType(de.fraunhofer.aisec.cpg.graph.types.PointerType) ParameterizedType(de.fraunhofer.aisec.cpg.graph.types.ParameterizedType) ObjectType(de.fraunhofer.aisec.cpg.graph.types.ObjectType) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 2 with PointerType

use of de.fraunhofer.aisec.cpg.graph.types.PointerType in project cpg by Fraunhofer-AISEC.

the class ExpressionHandler method handleVariableDeclarationExpr.

// Not sure how to handle this exactly yet
private DeclarationStatement handleVariableDeclarationExpr(Expression expr) {
    VariableDeclarationExpr variableDeclarationExpr = expr.asVariableDeclarationExpr();
    DeclarationStatement declarationStatement = NodeBuilder.newDeclarationStatement(variableDeclarationExpr.toString());
    for (VariableDeclarator variable : variableDeclarationExpr.getVariables()) {
        ResolvedValueDeclaration resolved = variable.resolve();
        Type declarationType = this.lang.getTypeAsGoodAsPossible(variable, resolved);
        declarationType.setAdditionalTypeKeywords(variableDeclarationExpr.getModifiers().stream().map(m -> m.getKeyword().asString()).collect(Collectors.joining(" ")));
        VariableDeclaration declaration = NodeBuilder.newVariableDeclaration(resolved.getName(), declarationType, variable.toString(), false, lang, variable);
        if (declarationType instanceof PointerType && ((PointerType) declarationType).isArray()) {
            declaration.setIsArray(true);
        }
        Optional<Expression> oInitializer = variable.getInitializer();
        if (oInitializer.isPresent()) {
            de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression initializer = (de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression) handle(oInitializer.get());
            if (initializer instanceof ArrayCreationExpression) {
                declaration.setIsArray(true);
            }
            declaration.setInitializer(initializer);
        } else {
            de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression uninitialzedInitializer = new UninitializedValue();
            declaration.setInitializer(uninitialzedInitializer);
        }
        lang.setCodeAndRegion(declaration, variable);
        declarationStatement.addToPropertyEdgeDeclaration(declaration);
        lang.processAnnotations(declaration, variableDeclarationExpr);
        lang.getScopeManager().addDeclaration(declaration);
    }
    return declarationStatement;
}
Also used : PointerType(de.fraunhofer.aisec.cpg.graph.types.PointerType) de.fraunhofer.aisec.cpg.graph.statements.expressions(de.fraunhofer.aisec.cpg.graph.statements.expressions) de.fraunhofer.aisec.cpg.graph(de.fraunhofer.aisec.cpg.graph) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) UnknownType(de.fraunhofer.aisec.cpg.graph.types.UnknownType) PointerType(de.fraunhofer.aisec.cpg.graph.types.PointerType) Type(de.fraunhofer.aisec.cpg.graph.types.Type) Expression(com.github.javaparser.ast.expr.Expression) VariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration) ResolvedValueDeclaration(com.github.javaparser.resolution.declarations.ResolvedValueDeclaration) DeclarationStatement(de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement)

Example 3 with PointerType

use of de.fraunhofer.aisec.cpg.graph.types.PointerType in project cpg by Fraunhofer-AISEC.

the class Util method generateParamName.

private static String generateParamName(int i, @NonNull Type targetType) {
    Deque<String> hierarchy = new ArrayDeque<>();
    Type currLevel = targetType;
    while (currLevel != null) {
        if (currLevel instanceof FunctionPointerType) {
            hierarchy.push("Fptr");
            currLevel = null;
        } else if (currLevel instanceof PointerType) {
            hierarchy.push("Ptr");
            currLevel = ((PointerType) currLevel).getElementType();
        } else if (currLevel instanceof ReferenceType) {
            hierarchy.push("Ref");
            currLevel = ((ReferenceType) currLevel).getElementType();
        } else {
            hierarchy.push(currLevel.getTypeName());
            currLevel = null;
        }
    }
    StringBuilder paramName = new StringBuilder();
    while (!hierarchy.isEmpty()) {
        String part = hierarchy.pop();
        if (part.isEmpty()) {
            continue;
        }
        if (paramName.length() > 0) {
            paramName.append(part.substring(0, 1).toUpperCase());
            if (part.length() >= 2) {
                paramName.append(part.substring(1));
            }
        } else {
            paramName.append(part.toLowerCase());
        }
    }
    paramName.append(i);
    return paramName.toString();
}
Also used : FunctionPointerType(de.fraunhofer.aisec.cpg.graph.types.FunctionPointerType) FunctionPointerType(de.fraunhofer.aisec.cpg.graph.types.FunctionPointerType) PointerType(de.fraunhofer.aisec.cpg.graph.types.PointerType) Type(de.fraunhofer.aisec.cpg.graph.types.Type) ReferenceType(de.fraunhofer.aisec.cpg.graph.types.ReferenceType) FunctionPointerType(de.fraunhofer.aisec.cpg.graph.types.FunctionPointerType) PointerType(de.fraunhofer.aisec.cpg.graph.types.PointerType) ArrayDeque(java.util.ArrayDeque) ReferenceType(de.fraunhofer.aisec.cpg.graph.types.ReferenceType)

Aggregations

PointerType (de.fraunhofer.aisec.cpg.graph.types.PointerType)3 Type (de.fraunhofer.aisec.cpg.graph.types.Type)2 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 Expression (com.github.javaparser.ast.expr.Expression)1 ResolvedValueDeclaration (com.github.javaparser.resolution.declarations.ResolvedValueDeclaration)1 BaseTest (de.fraunhofer.aisec.cpg.BaseTest)1 de.fraunhofer.aisec.cpg.graph (de.fraunhofer.aisec.cpg.graph)1 VariableDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration)1 DeclarationStatement (de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement)1 de.fraunhofer.aisec.cpg.graph.statements.expressions (de.fraunhofer.aisec.cpg.graph.statements.expressions)1 FunctionPointerType (de.fraunhofer.aisec.cpg.graph.types.FunctionPointerType)1 ObjectType (de.fraunhofer.aisec.cpg.graph.types.ObjectType)1 ParameterizedType (de.fraunhofer.aisec.cpg.graph.types.ParameterizedType)1 ReferenceType (de.fraunhofer.aisec.cpg.graph.types.ReferenceType)1 UnknownType (de.fraunhofer.aisec.cpg.graph.types.UnknownType)1 ArrayDeque (java.util.ArrayDeque)1 Test (org.junit.jupiter.api.Test)1