use of de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration 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());
}
use of de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration in project cpg by Fraunhofer-AISEC.
the class ConstructorsTest method testCPPImplicitCast.
@Test
void testCPPImplicitCast() throws Exception {
List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(Path.of(topLevel.toString(), "implicitcastarg", "constructorImplicit.cpp").toFile()), topLevel, true);
List<ConstructorDeclaration> constructors = TestUtils.subnodesOfType(result, ConstructorDeclaration.class);
List<VariableDeclaration> variables = TestUtils.subnodesOfType(result, VariableDeclaration.class);
ConstructorDeclaration implicitConstructor = TestUtils.findByUniquePredicate(constructors, c -> c.getName().equals("I"));
Literal<?> literal10 = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(10));
VariableDeclaration i1 = TestUtils.findByUniqueName(variables, "i1");
assertTrue(i1.getInitializer() instanceof ConstructExpression);
ConstructExpression i1Constructor = (ConstructExpression) i1.getInitializer();
assertFalse(i1Constructor.isImplicit());
assertEquals(implicitConstructor, i1Constructor.getConstructor());
assertEquals(1, i1Constructor.getArguments().size());
assertTrue(i1Constructor.getArguments().get(0) instanceof CastExpression);
CastExpression i1ConstructorArgument = (CastExpression) i1Constructor.getArguments().get(0);
assertEquals("int", i1ConstructorArgument.getCastType().getName());
assertEquals("1.0", i1ConstructorArgument.getExpression().getCode());
assertEquals("double", i1ConstructorArgument.getExpression().getType().getName());
ConstructorDeclaration implicitConstructorWithDefault = TestUtils.findByUniquePredicate(constructors, c -> c.getName().equals("H"));
VariableDeclaration h1 = TestUtils.findByUniqueName(variables, "h1");
assertTrue(h1.getInitializer() instanceof ConstructExpression);
ConstructExpression h1Constructor = (ConstructExpression) h1.getInitializer();
assertFalse(h1Constructor.isImplicit());
assertEquals(implicitConstructorWithDefault, h1Constructor.getConstructor());
assertEquals(1, h1Constructor.getArguments().size());
assertTrue(h1Constructor.getArguments().get(0) instanceof CastExpression);
CastExpression h1ConstructorArgument1 = (CastExpression) h1Constructor.getArguments().get(0);
assertEquals("int", h1ConstructorArgument1.getCastType().getName());
assertEquals("2.0", h1ConstructorArgument1.getExpression().getCode());
assertEquals("double", h1ConstructorArgument1.getExpression().getType().getName());
assertTrue(implicitConstructorWithDefault.getNextEOG().contains(literal10));
for (Node node : implicitConstructorWithDefault.getNextEOG()) {
if (!node.equals(literal10)) {
assertTrue(literal10.getNextEOG().contains(node));
}
}
}
use of de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration in project cpg by Fraunhofer-AISEC.
the class CXXLanguageFrontendTest method testRecordDeclaration.
@Test
void testRecordDeclaration() throws Exception {
File file = new File("src/test/resources/recordstmt.cpp");
TranslationUnitDeclaration declaration = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
RecordDeclaration recordDeclaration = declaration.getDeclarationAs(0, RecordDeclaration.class);
assertNotNull(recordDeclaration);
assertEquals("SomeClass", recordDeclaration.getName());
assertEquals("class", recordDeclaration.getKind());
assertEquals(3, recordDeclaration.getFields().size());
var field = recordDeclaration.getField("field");
assertNotNull(field);
var constant = recordDeclaration.getField("CONSTANT");
assertNotNull(constant);
assertEquals(TypeParser.createFrom("void*", true), field.getType());
assertEquals(3, recordDeclaration.getMethods().size());
MethodDeclaration method = recordDeclaration.getMethods().get(0);
assertEquals("method", method.getName());
assertEquals(0, method.getParameters().size());
assertEquals(TypeParser.createFrom("void*", true), method.getType());
assertFalse(method.hasBody());
MethodDeclaration definition = (MethodDeclaration) method.getDefinition();
assertNotNull(definition);
assertEquals("method", definition.getName());
assertEquals(0, definition.getParameters().size());
assertTrue(definition.isDefinition());
MethodDeclaration methodWithParam = recordDeclaration.getMethods().get(1);
assertEquals("method", methodWithParam.getName());
assertEquals(1, methodWithParam.getParameters().size());
assertEquals(TypeParser.createFrom("int", true), methodWithParam.getParameters().get(0).getType());
assertEquals(TypeParser.createFrom("void*", true), methodWithParam.getType());
assertFalse(methodWithParam.hasBody());
definition = (MethodDeclaration) methodWithParam.getDefinition();
assertNotNull(definition);
assertEquals("method", definition.getName());
assertEquals(1, definition.getParameters().size());
assertTrue(definition.isDefinition());
MethodDeclaration inlineMethod = recordDeclaration.getMethods().get(2);
assertEquals("inlineMethod", inlineMethod.getName());
assertEquals(TypeParser.createFrom("void*", true), inlineMethod.getType());
assertTrue(inlineMethod.hasBody());
ConstructorDeclaration inlineConstructor = recordDeclaration.getConstructors().get(0);
assertEquals(recordDeclaration.getName(), inlineConstructor.getName());
assertEquals(TypeParser.createFrom("SomeClass", true), inlineConstructor.getType());
assertTrue(inlineConstructor.hasBody());
ConstructorDeclaration constructorDefinition = declaration.getDeclarationAs(3, ConstructorDeclaration.class);
assertNotNull(constructorDefinition);
assertEquals(1, constructorDefinition.getParameters().size());
assertEquals(TypeParser.createFrom("int", true), constructorDefinition.getParameters().get(0).getType());
assertEquals(TypeParser.createFrom("SomeClass", true), constructorDefinition.getType());
assertTrue(constructorDefinition.hasBody());
ConstructorDeclaration constructorDeclaration = recordDeclaration.getConstructors().get(1);
assertNotNull(constructorDeclaration);
assertFalse(constructorDeclaration.isDefinition());
assertEquals(constructorDefinition, constructorDeclaration.getDefinition());
var main = declaration.getDeclarationsByName("main", FunctionDeclaration.class).iterator().next();
assertNotNull(main);
var methodCallWithConstant = main.getBodyStatementAs(2, CallExpression.class);
assertNotNull(methodCallWithConstant);
var arg = methodCallWithConstant.getArguments().get(0);
assertSame(constant, ((DeclaredReferenceExpression) arg).getRefersTo());
}
use of de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration in project cpg by Fraunhofer-AISEC.
the class ConstructorsTest method testCPP.
@Test
void testCPP() throws Exception {
List<TranslationUnitDeclaration> result = TestUtils.analyze("cpp", topLevel, true);
List<ConstructorDeclaration> constructors = TestUtils.subnodesOfType(result, ConstructorDeclaration.class);
ConstructorDeclaration noArg = TestUtils.findByUniquePredicate(constructors, c -> c.getParameters().size() == 0 && c.getName().equals("A"));
ConstructorDeclaration singleArg = TestUtils.findByUniquePredicate(constructors, c -> c.getParameters().size() == 1 && c.getName().equals("A"));
ConstructorDeclaration twoArgs = TestUtils.findByUniquePredicate(constructors, c -> c.getParameters().size() == 2 && c.getName().equals("A"));
List<VariableDeclaration> variables = TestUtils.subnodesOfType(result, VariableDeclaration.class);
VariableDeclaration a1 = TestUtils.findByUniqueName(variables, "a1");
assertTrue(a1.getInitializer() instanceof ConstructExpression);
ConstructExpression a1Initializer = (ConstructExpression) a1.getInitializer();
assertEquals(noArg, a1Initializer.getConstructor());
VariableDeclaration a2 = TestUtils.findByUniqueName(variables, "a2");
assertTrue(a2.getInitializer() instanceof ConstructExpression);
ConstructExpression a2Initializer = (ConstructExpression) a2.getInitializer();
assertEquals(singleArg, a2Initializer.getConstructor());
VariableDeclaration a3 = TestUtils.findByUniqueName(variables, "a3");
assertTrue(a3.getInitializer() instanceof ConstructExpression);
ConstructExpression a3Initializer = (ConstructExpression) a3.getInitializer();
assertEquals(twoArgs, a3Initializer.getConstructor());
VariableDeclaration a4 = TestUtils.findByUniqueName(variables, "a4");
assertTrue(a4.getInitializer() instanceof ConstructExpression);
ConstructExpression a4Initializer = (ConstructExpression) a4.getInitializer();
assertEquals(noArg, a4Initializer.getConstructor());
VariableDeclaration a5 = TestUtils.findByUniqueName(variables, "a5");
assertTrue(a5.getInitializer() instanceof ConstructExpression);
ConstructExpression a5Initializer = (ConstructExpression) a5.getInitializer();
assertEquals(singleArg, a5Initializer.getConstructor());
VariableDeclaration a6 = TestUtils.findByUniqueName(variables, "a6");
assertTrue(a6.getInitializer() instanceof ConstructExpression);
ConstructExpression a6Initializer = (ConstructExpression) a6.getInitializer();
assertEquals(twoArgs, a6Initializer.getConstructor());
VariableDeclaration a7 = TestUtils.findByUniqueName(variables, "a7");
assertTrue(a7.getInitializer() instanceof NewExpression);
assertTrue(((NewExpression) a7.getInitializer()).getInitializer() instanceof ConstructExpression);
ConstructExpression a7Initializer = (ConstructExpression) ((NewExpression) a7.getInitializer()).getInitializer();
assertEquals(noArg, a7Initializer.getConstructor());
VariableDeclaration a8 = TestUtils.findByUniqueName(variables, "a8");
assertTrue(a8.getInitializer() instanceof NewExpression);
assertTrue(((NewExpression) a8.getInitializer()).getInitializer() instanceof ConstructExpression);
ConstructExpression a8Initializer = (ConstructExpression) ((NewExpression) a8.getInitializer()).getInitializer();
assertEquals(noArg, a8Initializer.getConstructor());
VariableDeclaration a9 = TestUtils.findByUniqueName(variables, "a9");
assertTrue(a9.getInitializer() instanceof NewExpression);
assertTrue(((NewExpression) a9.getInitializer()).getInitializer() instanceof ConstructExpression);
ConstructExpression a9Initializer = (ConstructExpression) ((NewExpression) a9.getInitializer()).getInitializer();
assertEquals(singleArg, a9Initializer.getConstructor());
VariableDeclaration a10 = TestUtils.findByUniqueName(variables, "a10");
assertTrue(a10.getInitializer() instanceof NewExpression);
assertTrue(((NewExpression) a10.getInitializer()).getInitializer() instanceof ConstructExpression);
ConstructExpression a10Initializer = (ConstructExpression) ((NewExpression) a10.getInitializer()).getInitializer();
assertEquals(twoArgs, a10Initializer.getConstructor());
}
use of de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration in project cpg by Fraunhofer-AISEC.
the class ConstructorsTest method testJava.
@Test
void testJava() throws Exception {
List<TranslationUnitDeclaration> result = TestUtils.analyze("java", topLevel, true);
List<ConstructorDeclaration> constructors = TestUtils.subnodesOfType(result, ConstructorDeclaration.class);
ConstructorDeclaration noArg = TestUtils.findByUniquePredicate(constructors, c -> c.getParameters().size() == 0);
ConstructorDeclaration singleArg = TestUtils.findByUniquePredicate(constructors, c -> c.getParameters().size() == 1);
ConstructorDeclaration twoArgs = TestUtils.findByUniquePredicate(constructors, c -> c.getParameters().size() == 2);
List<VariableDeclaration> variables = TestUtils.subnodesOfType(result, VariableDeclaration.class);
VariableDeclaration a1 = TestUtils.findByUniqueName(variables, "a1");
assertTrue(a1.getInitializer() instanceof NewExpression);
assertTrue(((NewExpression) a1.getInitializer()).getInitializer() instanceof ConstructExpression);
ConstructExpression a1Initializer = (ConstructExpression) ((NewExpression) a1.getInitializer()).getInitializer();
assertEquals(noArg, a1Initializer.getConstructor());
VariableDeclaration a2 = TestUtils.findByUniqueName(variables, "a2");
assertTrue(a2.getInitializer() instanceof NewExpression);
assertTrue(((NewExpression) a2.getInitializer()).getInitializer() instanceof ConstructExpression);
ConstructExpression a2Initializer = (ConstructExpression) ((NewExpression) a2.getInitializer()).getInitializer();
assertEquals(singleArg, a2Initializer.getConstructor());
VariableDeclaration a3 = TestUtils.findByUniqueName(variables, "a3");
assertTrue(a3.getInitializer() instanceof NewExpression);
assertTrue(((NewExpression) a3.getInitializer()).getInitializer() instanceof ConstructExpression);
ConstructExpression a3Initializer = (ConstructExpression) ((NewExpression) a3.getInitializer()).getInitializer();
assertEquals(twoArgs, a3Initializer.getConstructor());
VariableDeclaration a4 = TestUtils.findByUniqueName(variables, "a4");
assertTrue(a4.getInitializer() instanceof UninitializedValue);
}
Aggregations