Search in sources :

Example 1 with ParamVariableDeclaration

use of de.fraunhofer.aisec.cpg.graph.declarations.ParamVariableDeclaration in project cpg by Fraunhofer-AISEC.

the class VariableResolverCppTest method testParamVarNameAccessed.

@Test
void testParamVarNameAccessed() {
    ParamVariableDeclaration declaration = TestUtils.getSubnodeOfTypeWithName(outerFunction2, ParamVariableDeclaration.class, "varName");
    VRUtil.assertUsageOf(callParamMap.get("func2_param_varName"), declaration);
}
Also used : ParamVariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.ParamVariableDeclaration) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 2 with ParamVariableDeclaration

use of de.fraunhofer.aisec.cpg.graph.declarations.ParamVariableDeclaration in project cpg by Fraunhofer-AISEC.

the class DeclarationHandler method handleMethodDeclaration.

public de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration handleMethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration methodDecl) {
    ResolvedMethodDeclaration resolvedMethod = methodDecl.resolve();
    var record = lang.getScopeManager().getCurrentRecord();
    de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration functionDeclaration = newMethodDeclaration(resolvedMethod.getName(), methodDecl.toString(), methodDecl.isStatic(), record);
    // create the receiver
    var receiver = newVariableDeclaration("this", record != null ? TypeParser.createFrom(record.getName(), false) : UnknownType.getUnknownType(), "this", false);
    functionDeclaration.setReceiver(receiver);
    lang.getScopeManager().enterScope(functionDeclaration);
    functionDeclaration.addThrowTypes(methodDecl.getThrownExceptions().stream().map(type -> TypeParser.createFrom(type.asString(), true)).collect(Collectors.toList()));
    for (Parameter parameter : methodDecl.getParameters()) {
        Type resolvedType = TypeManager.getInstance().getTypeParameter(functionDeclaration.getRecordDeclaration(), parameter.getType().toString());
        if (resolvedType == null) {
            resolvedType = this.lang.getTypeAsGoodAsPossible(parameter, parameter.resolve());
        }
        ParamVariableDeclaration param = newMethodParameterIn(parameter.getNameAsString(), resolvedType, parameter.isVarArgs(), parameter.toString());
        functionDeclaration.addParameter(param);
        lang.setCodeAndRegion(param, parameter);
        lang.processAnnotations(param, parameter);
        lang.getScopeManager().addDeclaration(param);
    }
    functionDeclaration.setType(this.lang.getReturnTypeAsGoodAsPossible(methodDecl, resolvedMethod));
    // check, if method has body (i.e. its not abstract or something)
    Optional<BlockStmt> o = methodDecl.getBody();
    if (o.isEmpty()) {
        lang.getScopeManager().leaveScope(functionDeclaration);
        return functionDeclaration;
    }
    BlockStmt body = o.get();
    addImplicitReturn(body);
    functionDeclaration.setBody(this.lang.getStatementHandler().handle(body));
    lang.processAnnotations(functionDeclaration, methodDecl);
    lang.getScopeManager().leaveScope(functionDeclaration);
    return functionDeclaration;
}
Also used : UnknownType(de.fraunhofer.aisec.cpg.graph.types.UnknownType) Type(de.fraunhofer.aisec.cpg.graph.types.Type) ParameterizedType(de.fraunhofer.aisec.cpg.graph.types.ParameterizedType) ParamVariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.ParamVariableDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Parameter(com.github.javaparser.ast.body.Parameter) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)

Example 3 with ParamVariableDeclaration

use of de.fraunhofer.aisec.cpg.graph.declarations.ParamVariableDeclaration in project cpg by Fraunhofer-AISEC.

the class DeclarationHandler method handleConstructorDeclaration.

public de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration handleConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration constructorDecl) {
    ResolvedConstructorDeclaration resolvedConstructor = constructorDecl.resolve();
    de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration declaration = newConstructorDeclaration(resolvedConstructor.getName(), constructorDecl.toString(), lang.getScopeManager().getCurrentRecord());
    lang.getScopeManager().addDeclaration(declaration);
    lang.getScopeManager().enterScope(declaration);
    declaration.addThrowTypes(constructorDecl.getThrownExceptions().stream().map(type -> TypeParser.createFrom(type.asString(), true)).collect(Collectors.toList()));
    for (Parameter parameter : constructorDecl.getParameters()) {
        ParamVariableDeclaration param = newMethodParameterIn(parameter.getNameAsString(), this.lang.getTypeAsGoodAsPossible(parameter, parameter.resolve()), parameter.isVarArgs(), parameter.toString());
        declaration.addParameter(param);
        lang.setCodeAndRegion(param, parameter);
        lang.getScopeManager().addDeclaration(param);
    }
    Type type = TypeParser.createFrom(lang.getScopeManager().firstScopeOrNull(RecordScope.class::isInstance).getAstNode().getName(), true);
    declaration.setType(type);
    // check, if constructor has body (i.e. its not abstract or something)
    BlockStmt body = constructorDecl.getBody();
    addImplicitReturn(body);
    declaration.setBody(this.lang.getStatementHandler().handle(body));
    lang.processAnnotations(declaration, constructorDecl);
    lang.getScopeManager().leaveScope(declaration);
    return declaration;
}
Also used : UnknownType(de.fraunhofer.aisec.cpg.graph.types.UnknownType) Type(de.fraunhofer.aisec.cpg.graph.types.Type) ParameterizedType(de.fraunhofer.aisec.cpg.graph.types.ParameterizedType) ParamVariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.ParamVariableDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Parameter(com.github.javaparser.ast.body.Parameter) ResolvedConstructorDeclaration(com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration)

Example 4 with ParamVariableDeclaration

use of de.fraunhofer.aisec.cpg.graph.declarations.ParamVariableDeclaration in project cpg by Fraunhofer-AISEC.

the class Util method createInferredParameters.

public static List<ParamVariableDeclaration> createInferredParameters(List<Type> signature) {
    List<ParamVariableDeclaration> params = new ArrayList<>();
    for (int i = 0; i < signature.size(); i++) {
        Type targetType = signature.get(i);
        String paramName = generateParamName(i, targetType);
        ParamVariableDeclaration param = newMethodParameterIn(paramName, targetType, false, "");
        param.setInferred(true);
        param.setArgumentIndex(i);
        params.add(param);
    }
    return params;
}
Also used : 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) ParamVariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.ParamVariableDeclaration) ArrayList(java.util.ArrayList)

Aggregations

ParamVariableDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.ParamVariableDeclaration)4 Type (de.fraunhofer.aisec.cpg.graph.types.Type)3 Parameter (com.github.javaparser.ast.body.Parameter)2 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)2 ParameterizedType (de.fraunhofer.aisec.cpg.graph.types.ParameterizedType)2 UnknownType (de.fraunhofer.aisec.cpg.graph.types.UnknownType)2 ResolvedConstructorDeclaration (com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration)1 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)1 BaseTest (de.fraunhofer.aisec.cpg.BaseTest)1 FunctionPointerType (de.fraunhofer.aisec.cpg.graph.types.FunctionPointerType)1 PointerType (de.fraunhofer.aisec.cpg.graph.types.PointerType)1 ReferenceType (de.fraunhofer.aisec.cpg.graph.types.ReferenceType)1 ArrayList (java.util.ArrayList)1 Test (org.junit.jupiter.api.Test)1