Search in sources :

Example 1 with TranslationUnitDeclaration

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

the class TypedefTest method testMultiple.

@Test
void testMultiple() throws Exception {
    List<TranslationUnitDeclaration> result = TestUtils.analyze("cpp", topLevel, true);
    List<ValueDeclaration> variables = TestUtils.subnodesOfType(result, ValueDeclaration.class);
    // simple type
    ValueDeclaration i1 = TestUtils.findByUniqueName(variables, "i1");
    ValueDeclaration i2 = TestUtils.findByUniqueName(variables, "i2");
    assertEquals(i1.getType(), i2.getType());
    // array
    ValueDeclaration a1 = TestUtils.findByUniqueName(variables, "a1");
    ValueDeclaration a2 = TestUtils.findByUniqueName(variables, "a2");
    assertEquals(a1.getType(), a2.getType());
    // pointer
    ValueDeclaration intPtr1 = TestUtils.findByUniqueName(variables, "intPtr1");
    ValueDeclaration intPtr2 = TestUtils.findByUniqueName(variables, "intPtr2");
    assertEquals(intPtr1.getType(), intPtr2.getType());
    // function pointer
    ValueDeclaration fPtr1 = TestUtils.findByUniqueName(variables, "intFptr1");
    ValueDeclaration fPtr2 = TestUtils.findByUniqueName(variables, "intFptr2");
    assertEquals(fPtr1.getType(), fPtr2.getType());
}
Also used : ValueDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.ValueDeclaration) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) Test(org.junit.jupiter.api.Test) BaseTest(de.fraunhofer.aisec.cpg.BaseTest)

Example 2 with TranslationUnitDeclaration

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

the class TypedefTest method testMemberTypeDef.

@Test
void testMemberTypeDef() throws Exception {
    List<TranslationUnitDeclaration> result = TestUtils.analyze("cpp", topLevel, true);
    List<ValueDeclaration> variables = TestUtils.subnodesOfType(result, ValueDeclaration.class);
    List<RecordDeclaration> records = TestUtils.subnodesOfType(result, RecordDeclaration.class);
    RecordDeclaration addConst = TestUtils.findByUniqueName(records, "add_const");
    ValueDeclaration typeMember1 = TestUtils.findByUniqueName(addConst.getFields(), "typeMember1");
    ValueDeclaration typeMember2 = TestUtils.findByUniqueName(addConst.getFields(), "typeMember2");
    assertEquals(typeMember1.getType(), typeMember2.getType());
    ValueDeclaration typeMemberOutside = TestUtils.findByUniqueName(variables, "typeMemberOutside");
    assertNotEquals(typeMemberOutside.getType(), typeMember2.getType());
    ValueDeclaration cptr1 = TestUtils.findByUniqueName(variables, "cptr1");
    ValueDeclaration cptr2 = TestUtils.findByUniqueName(variables, "cptr2");
    assertEquals(cptr1.getType(), cptr2.getType());
    assertNotEquals(typeMemberOutside.getType(), cptr2.getType());
}
Also used : RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) ValueDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.ValueDeclaration) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) Test(org.junit.jupiter.api.Test) BaseTest(de.fraunhofer.aisec.cpg.BaseTest)

Example 3 with TranslationUnitDeclaration

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

the class TypedefTest method testWithModifier.

@Test
void testWithModifier() throws Exception {
    List<TranslationUnitDeclaration> result = TestUtils.analyze("cpp", topLevel, true);
    List<ValueDeclaration> variables = TestUtils.subnodesOfType(result, ValueDeclaration.class);
    // pointer
    ValueDeclaration l1ptr = TestUtils.findByUniqueName(variables, "l1ptr");
    ValueDeclaration l2ptr = TestUtils.findByUniqueName(variables, "l2ptr");
    ValueDeclaration l3ptr = TestUtils.findByUniqueName(variables, "l3ptr");
    ValueDeclaration l4ptr = TestUtils.findByUniqueName(variables, "l4ptr");
    assertEquals(l1ptr.getType(), l2ptr.getType());
    assertEquals(l1ptr.getType(), l3ptr.getType());
    assertEquals(l1ptr.getType(), l4ptr.getType());
    // arrays
    ValueDeclaration l1arr = TestUtils.findByUniqueName(variables, "l1arr");
    ValueDeclaration l2arr = TestUtils.findByUniqueName(variables, "l2arr");
    ValueDeclaration l3arr = TestUtils.findByUniqueName(variables, "l3arr");
    ValueDeclaration l4arr = TestUtils.findByUniqueName(variables, "l4arr");
    assertTrue(TypeManager.getInstance().checkArrayAndPointer(l1arr.getType(), l2arr.getType()));
    assertTrue(TypeManager.getInstance().checkArrayAndPointer(l1arr.getType(), l3arr.getType()));
    assertTrue(TypeManager.getInstance().checkArrayAndPointer(l1arr.getType(), l4arr.getType()));
}
Also used : ValueDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.ValueDeclaration) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) Test(org.junit.jupiter.api.Test) BaseTest(de.fraunhofer.aisec.cpg.BaseTest)

Example 4 with TranslationUnitDeclaration

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

the class VisitorTest method setup.

@BeforeAll
public static void setup() throws TranslationException, InterruptedException, ExecutionException, TimeoutException {
    File file = new File("src/test/resources/compiling/RecordDeclaration.java");
    TranslationConfiguration config = TranslationConfiguration.builder().sourceLocations(file).defaultPasses().defaultLanguages().build();
    TranslationResult result = TranslationManager.builder().config(config).build().analyze().get(20, TimeUnit.SECONDS);
    TranslationUnitDeclaration tu = result.getTranslationUnits().get(0);
    namespace = (NamespaceDeclaration) tu.getDeclarations().get(0);
}
Also used : TranslationResult(de.fraunhofer.aisec.cpg.TranslationResult) File(java.io.File) TranslationConfiguration(de.fraunhofer.aisec.cpg.TranslationConfiguration) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 5 with TranslationUnitDeclaration

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

the class ConstructorsTest method testCPPFullDefault.

@Test
void testCPPFullDefault() throws Exception {
    List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(Path.of(topLevel.toString(), "defaultarg", "constructorDefault.cpp").toFile()), topLevel, true);
    List<ConstructorDeclaration> constructors = TestUtils.subnodesOfType(result, ConstructorDeclaration.class);
    List<VariableDeclaration> variables = TestUtils.subnodesOfType(result, VariableDeclaration.class);
    ConstructorDeclaration twoDefaultArg = TestUtils.findByUniquePredicate(constructors, c -> c.getDefaultParameters().size() == 2 && c.getName().equals("D"));
    Literal<?> literal0 = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(0));
    Literal<?> literal1 = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(1));
    VariableDeclaration d1 = TestUtils.findByUniqueName(variables, "d1");
    assertTrue(d1.getInitializer() instanceof ConstructExpression);
    ConstructExpression d1Initializer = (ConstructExpression) d1.getInitializer();
    assertEquals(twoDefaultArg, d1Initializer.getConstructor());
    assertEquals(0, d1Initializer.getArguments().size());
    assertTrue(twoDefaultArg.getNextEOG().contains(literal0));
    assertTrue(twoDefaultArg.getNextEOG().contains(literal1));
    assertTrue(literal0.getNextEOG().contains(literal1));
    for (Node node : twoDefaultArg.getNextEOG()) {
        if (!(node.equals(literal0) || node.equals(literal1))) {
            assertTrue(literal1.getNextEOG().contains(node));
        }
    }
    VariableDeclaration d2 = TestUtils.findByUniqueName(variables, "d2");
    assertTrue(d2.getInitializer() instanceof ConstructExpression);
    ConstructExpression d2Initializer = (ConstructExpression) d2.getInitializer();
    assertEquals(twoDefaultArg, d2Initializer.getConstructor());
    assertEquals(1, d2Initializer.getArguments().size());
    assertEquals(2, ((Literal) d2Initializer.getArguments().get(0)).getValue());
    VariableDeclaration d3 = TestUtils.findByUniqueName(variables, "d3");
    assertTrue(d3.getInitializer() instanceof ConstructExpression);
    ConstructExpression d3Initializer = (ConstructExpression) d3.getInitializer();
    assertEquals(twoDefaultArg, d3Initializer.getConstructor());
    assertEquals(2, d3Initializer.getArguments().size());
    assertEquals(3, ((Literal) d3Initializer.getArguments().get(0)).getValue());
    assertEquals(4, ((Literal) d3Initializer.getArguments().get(1)).getValue());
}
Also used : ConstructorDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration) Node(de.fraunhofer.aisec.cpg.graph.Node) VariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) Test(org.junit.jupiter.api.Test) BaseTest(de.fraunhofer.aisec.cpg.BaseTest)

Aggregations

TranslationUnitDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration)74 Test (org.junit.jupiter.api.Test)67 BaseTest (de.fraunhofer.aisec.cpg.BaseTest)57 File (java.io.File)41 FunctionDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration)31 VariableDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration)29 MethodDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration)18 RecordDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration)17 CompoundStatement (de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement)15 Path (java.nio.file.Path)15 CaseStatement (de.fraunhofer.aisec.cpg.graph.statements.CaseStatement)12 DefaultStatement (de.fraunhofer.aisec.cpg.graph.statements.DefaultStatement)12 SwitchStatement (de.fraunhofer.aisec.cpg.graph.statements.SwitchStatement)12 DeclarationStatement (de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement)11 IfStatement (de.fraunhofer.aisec.cpg.graph.statements.IfStatement)11 TryStatement (de.fraunhofer.aisec.cpg.graph.statements.TryStatement)11 NodeComparator (de.fraunhofer.aisec.cpg.helpers.NodeComparator)11 TestUtils (de.fraunhofer.aisec.cpg.TestUtils)10 ForEachStatement (de.fraunhofer.aisec.cpg.graph.statements.ForEachStatement)10 ReturnStatement (de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement)10