Search in sources :

Example 1 with DeclarationStatement

use of de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement in project cpg by Fraunhofer-AISEC.

the class CXXLanguageFrontendTest method testDeclarationStatement.

@Test
void testDeclarationStatement() throws Exception {
    File file = new File("src/test/resources/declstmt.cpp");
    TranslationUnitDeclaration declaration = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
    FunctionDeclaration function = declaration.getDeclarationAs(0, FunctionDeclaration.class);
    List<Statement> statements = getStatementsOfFunction(function);
    statements.forEach(node -> {
        LOGGER.debug("{}", node);
        assertTrue(node instanceof DeclarationStatement || statements.indexOf(node) == statements.size() - 1 && node instanceof ReturnStatement);
    });
    VariableDeclaration declFromMultiplicateExpression = ((DeclarationStatement) statements.get(0)).getSingleDeclarationAs(VariableDeclaration.class);
    assertEquals(TypeParser.createFrom("SSL_CTX*", true), declFromMultiplicateExpression.getType());
    assertEquals("ptr", declFromMultiplicateExpression.getName());
    VariableDeclaration withInitializer = ((DeclarationStatement) statements.get(1)).getSingleDeclarationAs(VariableDeclaration.class);
    Expression initializer = withInitializer.getInitializer();
    assertNotNull(initializer);
    assertTrue(initializer instanceof Literal);
    assertEquals(1, ((Literal) initializer).getValue());
    List<Declaration> twoDeclarations = statements.get(2).getDeclarations();
    assertEquals(2, twoDeclarations.size());
    VariableDeclaration b = (VariableDeclaration) twoDeclarations.get(0);
    assertNotNull(b);
    assertEquals("b", b.getName());
    assertEquals(TypeParser.createFrom("int*", false), b.getType());
    VariableDeclaration c = (VariableDeclaration) twoDeclarations.get(1);
    assertNotNull(c);
    assertEquals("c", c.getName());
    assertEquals(TypeParser.createFrom("int", false), c.getType());
    VariableDeclaration withoutInitializer = ((DeclarationStatement) statements.get(3)).getSingleDeclarationAs(VariableDeclaration.class);
    initializer = withoutInitializer.getInitializer();
    assertEquals(TypeParser.createFrom("int*", true), withoutInitializer.getType());
    assertEquals("d", withoutInitializer.getName());
    assertNull(initializer);
    VariableDeclaration qualifiedType = ((DeclarationStatement) statements.get(4)).getSingleDeclarationAs(VariableDeclaration.class);
    assertEquals(TypeParser.createFrom("std.string", true), qualifiedType.getType());
    assertEquals("text", qualifiedType.getName());
    assertTrue(qualifiedType.getInitializer() instanceof Literal);
    assertEquals("some text", ((Literal) qualifiedType.getInitializer()).getValue());
    VariableDeclaration pointerWithAssign = ((DeclarationStatement) statements.get(5)).getSingleDeclarationAs(VariableDeclaration.class);
    assertEquals(TypeParser.createFrom("void*", true), pointerWithAssign.getType());
    assertEquals("ptr2", pointerWithAssign.getName());
    assertEquals("NULL", pointerWithAssign.getInitializer().getName());
    List<Declaration> classWithVariable = statements.get(6).getDeclarations();
    assertEquals(2, classWithVariable.size());
    RecordDeclaration classA = (RecordDeclaration) classWithVariable.get(0);
    assertNotNull(classA);
    assertEquals("A", classA.getName());
    VariableDeclaration myA = (VariableDeclaration) classWithVariable.get(1);
    assertNotNull(myA);
    assertEquals("myA", myA.getName());
    assertEquals(classA, ((ObjectType) myA.getType()).getRecordDeclaration());
}
Also used : TryStatement(de.fraunhofer.aisec.cpg.graph.statements.TryStatement) Statement(de.fraunhofer.aisec.cpg.graph.statements.Statement) SwitchStatement(de.fraunhofer.aisec.cpg.graph.statements.SwitchStatement) CaseStatement(de.fraunhofer.aisec.cpg.graph.statements.CaseStatement) ForEachStatement(de.fraunhofer.aisec.cpg.graph.statements.ForEachStatement) ReturnStatement(de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement) IfStatement(de.fraunhofer.aisec.cpg.graph.statements.IfStatement) DeclarationStatement(de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement) CompoundStatement(de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement) DefaultStatement(de.fraunhofer.aisec.cpg.graph.statements.DefaultStatement) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) ReturnStatement(de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement) VariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration) FieldDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration) ConstructorDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration) FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) MethodDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration) VariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration) Declaration(de.fraunhofer.aisec.cpg.graph.declarations.Declaration) RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) NamespaceDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.NamespaceDeclaration) File(java.io.File) DeclarationStatement(de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 2 with DeclarationStatement

use of de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement 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 DeclarationStatement

use of de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement in project cpg by Fraunhofer-AISEC.

the class CXXLanguageFrontendTest method testAssignmentExpression.

@Test
void testAssignmentExpression() throws Exception {
    File file = new File("src/test/resources/assignmentexpression.cpp");
    TranslationUnitDeclaration declaration = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
    // just take a look at the second function
    FunctionDeclaration functionDeclaration = declaration.getDeclarationAs(1, FunctionDeclaration.class);
    List<Statement> statements = getStatementsOfFunction(functionDeclaration);
    Statement declareA = statements.get(0);
    Declaration a = ((DeclarationStatement) declareA).getSingleDeclaration();
    Statement assignA = statements.get(1);
    assertTrue(assignA instanceof BinaryOperator);
    Expression lhs = ((BinaryOperator) assignA).getLhs();
    Expression rhs = ((BinaryOperator) assignA).getRhs();
    assertEquals("a", ((BinaryOperator) assignA).getLhs().getName());
    assertEquals(2, ((Literal) ((BinaryOperator) assignA).getRhs()).getValue());
    assertRefersTo(((BinaryOperator) assignA).getLhs(), a);
    Statement declareB = statements.get(2);
    assertTrue(declareB instanceof DeclarationStatement);
    Declaration b = ((DeclarationStatement) declareB).getSingleDeclaration();
    // a = b
    Statement assignB = statements.get(3);
    assertTrue(assignB instanceof BinaryOperator);
    lhs = ((BinaryOperator) assignB).getLhs();
    rhs = ((BinaryOperator) assignB).getRhs();
    assertEquals("a", lhs.getName());
    assertTrue(rhs instanceof DeclaredReferenceExpression);
    assertEquals("b", rhs.getName());
    assertRefersTo(rhs, b);
    Statement assignBWithFunction = statements.get(4);
    assertTrue(assignBWithFunction instanceof BinaryOperator);
    assertEquals("a", ((BinaryOperator) assignBWithFunction).getLhs().getName());
    assertTrue(((BinaryOperator) assignBWithFunction).getRhs() instanceof CallExpression);
    CallExpression call = (CallExpression) ((BinaryOperator) assignBWithFunction).getRhs();
    assertEquals("someFunction", call.getName());
    assertRefersTo(call.getArguments().get(0), b);
}
Also used : FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) TryStatement(de.fraunhofer.aisec.cpg.graph.statements.TryStatement) Statement(de.fraunhofer.aisec.cpg.graph.statements.Statement) SwitchStatement(de.fraunhofer.aisec.cpg.graph.statements.SwitchStatement) CaseStatement(de.fraunhofer.aisec.cpg.graph.statements.CaseStatement) ForEachStatement(de.fraunhofer.aisec.cpg.graph.statements.ForEachStatement) ReturnStatement(de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement) IfStatement(de.fraunhofer.aisec.cpg.graph.statements.IfStatement) DeclarationStatement(de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement) CompoundStatement(de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement) DefaultStatement(de.fraunhofer.aisec.cpg.graph.statements.DefaultStatement) FieldDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration) ConstructorDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration) FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) MethodDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration) VariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration) Declaration(de.fraunhofer.aisec.cpg.graph.declarations.Declaration) RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) NamespaceDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.NamespaceDeclaration) File(java.io.File) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) DeclarationStatement(de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 4 with DeclarationStatement

use of de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement in project cpg by Fraunhofer-AISEC.

the class CXXLanguageFrontendTest method testBinaryOperator.

@Test
void testBinaryOperator() throws Exception {
    File file = new File("src/test/resources/binaryoperator.cpp");
    TranslationUnitDeclaration declaration = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
    List<Statement> statements = getStatementsOfFunction(declaration.getDeclarationAs(0, FunctionDeclaration.class));
    // first two statements are just declarations
    // a = b * 2
    BinaryOperator operator = (BinaryOperator) statements.get(2);
    assertEquals("a", operator.getLhs().getName());
    assertTrue(operator.getRhs() instanceof BinaryOperator);
    BinaryOperator rhs = (BinaryOperator) operator.getRhs();
    assertTrue(rhs.getLhs() instanceof DeclaredReferenceExpression);
    assertEquals("b", rhs.getLhs().getName());
    assertTrue(rhs.getRhs() instanceof Literal);
    assertEquals(2, ((Literal) rhs.getRhs()).getValue());
    // a = 1 * 1
    operator = (BinaryOperator) statements.get(3);
    assertEquals("a", operator.getLhs().getName());
    assertTrue(operator.getRhs() instanceof BinaryOperator);
    rhs = (BinaryOperator) operator.getRhs();
    assertTrue(rhs.getLhs() instanceof Literal);
    assertEquals(1, ((Literal) rhs.getLhs()).getValue());
    assertTrue(rhs.getRhs() instanceof Literal);
    assertEquals(1, ((Literal) rhs.getRhs()).getValue());
    // std::string* notMultiplication
    // this is not a multiplication, but a variable declaration with a pointer type, but
    // syntactically no different than the previous ones
    DeclarationStatement stmt = (DeclarationStatement) statements.get(4);
    VariableDeclaration decl = (VariableDeclaration) stmt.getSingleDeclaration();
    assertEquals(TypeParser.createFrom("std.string*", true), decl.getType());
    assertEquals("notMultiplication", decl.getName());
    assertTrue(decl.getInitializer() instanceof BinaryOperator);
    operator = (BinaryOperator) decl.getInitializer();
    assertTrue(operator.getLhs() instanceof Literal);
    assertEquals(0, ((Literal) operator.getLhs()).getValue());
    assertTrue(operator.getRhs() instanceof Literal);
    assertEquals(0, ((Literal) operator.getRhs()).getValue());
}
Also used : FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) TryStatement(de.fraunhofer.aisec.cpg.graph.statements.TryStatement) Statement(de.fraunhofer.aisec.cpg.graph.statements.Statement) SwitchStatement(de.fraunhofer.aisec.cpg.graph.statements.SwitchStatement) CaseStatement(de.fraunhofer.aisec.cpg.graph.statements.CaseStatement) ForEachStatement(de.fraunhofer.aisec.cpg.graph.statements.ForEachStatement) ReturnStatement(de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement) IfStatement(de.fraunhofer.aisec.cpg.graph.statements.IfStatement) DeclarationStatement(de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement) CompoundStatement(de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement) DefaultStatement(de.fraunhofer.aisec.cpg.graph.statements.DefaultStatement) VariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration) File(java.io.File) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) DeclarationStatement(de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 5 with DeclarationStatement

use of de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement in project cpg by Fraunhofer-AISEC.

the class CXXLanguageFrontendTest method testForEach.

/*@Test
  void testFF() throws Exception {
    File file = new File("src/test/resources/hqxvlc.c");
    TranslationUnitDeclaration tu =
        TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);

    assertNotNull(tu);

    var nodes = SubgraphWalker.flattenAST(tu);
    var types = new HashMap<String, Integer>();
    var codes = new HashMap<String, Set<Integer>>();
    for (var n : nodes) {
      var count = types.computeIfAbsent(n.getClass().getSimpleName(), key -> 0);
      types.put(n.getClass().getSimpleName(), count + 1);

      var code = codes.computeIfAbsent(n.getClass().getSimpleName(), key -> new HashSet<>());
      code.add(n.hashCode());
    }

    for (var t : types.keySet()) {
      System.out.println(t + ": " + types.get(t) + " | unique hash codes: " + codes.get(t).size());
    }
  }*/
@Test
void testForEach() throws Exception {
    File file = new File("src/test/resources/components/foreachstmt.cpp");
    TranslationUnitDeclaration tu = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
    @NonNull Set<FunctionDeclaration> main = tu.getDeclarationsByName("main", FunctionDeclaration.class);
    assertFalse(main.isEmpty());
    FunctionDeclaration decl = main.iterator().next();
    VariableDeclaration ls = decl.getVariableDeclarationByName("ls").orElse(null);
    assertNotNull(ls);
    assertEquals(TypeParser.createFrom("std::vector<int>", true), ls.getType());
    assertEquals("ls", ls.getName());
    ForEachStatement forEachStatement = decl.getBodyStatementAs(1, ForEachStatement.class);
    assertNotNull(forEachStatement);
    // should loop over ls
    assertEquals(ls, ((DeclaredReferenceExpression) forEachStatement.getIterable()).getRefersTo());
    // should declare auto i (so far no concrete type inferrable)
    Statement stmt = forEachStatement.getVariable();
    assertNotNull(stmt);
    assertTrue(stmt instanceof DeclarationStatement);
    assertTrue(((DeclarationStatement) stmt).isSingleDeclaration());
    VariableDeclaration i = (VariableDeclaration) ((DeclarationStatement) stmt).getSingleDeclaration();
    assertNotNull(i);
    assertEquals("i", i.getName());
    assertEquals(UnknownType.getUnknownType(), i.getType());
}
Also used : FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) TryStatement(de.fraunhofer.aisec.cpg.graph.statements.TryStatement) Statement(de.fraunhofer.aisec.cpg.graph.statements.Statement) SwitchStatement(de.fraunhofer.aisec.cpg.graph.statements.SwitchStatement) CaseStatement(de.fraunhofer.aisec.cpg.graph.statements.CaseStatement) ForEachStatement(de.fraunhofer.aisec.cpg.graph.statements.ForEachStatement) ReturnStatement(de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement) IfStatement(de.fraunhofer.aisec.cpg.graph.statements.IfStatement) DeclarationStatement(de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement) CompoundStatement(de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement) DefaultStatement(de.fraunhofer.aisec.cpg.graph.statements.DefaultStatement) NonNull(org.checkerframework.checker.nullness.qual.NonNull) VariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration) File(java.io.File) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) ForEachStatement(de.fraunhofer.aisec.cpg.graph.statements.ForEachStatement) DeclarationStatement(de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Aggregations

DeclarationStatement (de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement)8 VariableDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration)7 File (java.io.File)7 BaseTest (de.fraunhofer.aisec.cpg.BaseTest)6 FunctionDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration)6 TranslationUnitDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration)6 CaseStatement (de.fraunhofer.aisec.cpg.graph.statements.CaseStatement)6 CompoundStatement (de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement)6 DefaultStatement (de.fraunhofer.aisec.cpg.graph.statements.DefaultStatement)6 ForEachStatement (de.fraunhofer.aisec.cpg.graph.statements.ForEachStatement)6 IfStatement (de.fraunhofer.aisec.cpg.graph.statements.IfStatement)6 ReturnStatement (de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement)6 Statement (de.fraunhofer.aisec.cpg.graph.statements.Statement)6 SwitchStatement (de.fraunhofer.aisec.cpg.graph.statements.SwitchStatement)6 TryStatement (de.fraunhofer.aisec.cpg.graph.statements.TryStatement)6 Test (org.junit.jupiter.api.Test)6 ConstructorDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration)2 Declaration (de.fraunhofer.aisec.cpg.graph.declarations.Declaration)2 FieldDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration)2 MethodDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration)2