use of de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration 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.VariableDeclaration 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.VariableDeclaration in project cpg by Fraunhofer-AISEC.
the class CXXLiteralTest method testLargeNegativeNumber.
@Test
void testLargeNegativeNumber() throws Exception {
File file = new File("src/test/resources/largenegativenumber.cpp");
TranslationUnitDeclaration tu = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
Set<FunctionDeclaration> main = tu.getDeclarationsByName("main", FunctionDeclaration.class);
assertFalse(main.isEmpty());
FunctionDeclaration funcDecl = main.iterator().next();
VariableDeclaration a = funcDecl.getVariableDeclarationByName("a").orElse(null);
assertNotNull(a);
assertEquals(1, ((Literal) Objects.requireNonNull(a.getInitializerAs(UnaryOperator.class)).getInput()).getValue());
// there are no negative literals, so the construct "-2147483648" is
// a unary expression and the literal "2147483648". Since "2147483648" is too large to fit
// in an integer, it should be automatically converted to a long. The resulting value
// -2147483648 however is small enough to fit into an int, so it is ok for the variable a to
// have an int type
VariableDeclaration b = funcDecl.getVariableDeclarationByName("b").orElse(null);
assertNotNull(b);
assertEquals(2147483648L, ((Literal) Objects.requireNonNull(b.getInitializerAs(UnaryOperator.class)).getInput()).getValue());
VariableDeclaration c = funcDecl.getVariableDeclarationByName("c").orElse(null);
assertNotNull(c);
assertEquals(2147483649L, ((Literal) Objects.requireNonNull(c.getInitializerAs(UnaryOperator.class)).getInput()).getValue());
VariableDeclaration d = funcDecl.getVariableDeclarationByName("d").orElse(null);
assertNotNull(d);
assertEquals(new BigInteger("9223372036854775808"), ((Literal) Objects.requireNonNull(d.getInitializerAs(UnaryOperator.class)).getInput()).getValue());
}
use of de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration in project cpg by Fraunhofer-AISEC.
the class DFGTest method testControlSensitiveDFGPassSwitch.
@Test
void testControlSensitiveDFGPassSwitch() throws Exception {
Path topLevel = Path.of("src", "test", "resources", "dfg");
List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(topLevel.resolve("ControlFlowSensitiveDFGSwitch.java").toFile()), topLevel, true);
VariableDeclaration a = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, VariableDeclaration.class), v -> v.getName().equals("a")).get(0);
VariableDeclaration b = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, VariableDeclaration.class), v -> v.getName().equals("b")).get(0);
DeclaredReferenceExpression ab = (DeclaredReferenceExpression) b.getPrevEOG().get(0);
DeclaredReferenceExpression a10 = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, DeclaredReferenceExpression.class), dre -> TestUtils.compareLineFromLocationIfExists(dre, true, 8)).get(0);
DeclaredReferenceExpression a11 = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, DeclaredReferenceExpression.class), dre -> TestUtils.compareLineFromLocationIfExists(dre, true, 11)).get(0);
DeclaredReferenceExpression a12 = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, DeclaredReferenceExpression.class), dre -> TestUtils.compareLineFromLocationIfExists(dre, true, 14)).get(0);
Literal<?> literal0 = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(0)).get(0);
Literal<?> literal10 = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(10)).get(0);
Literal<?> literal11 = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(11)).get(0);
Literal<?> literal12 = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(12)).get(0);
assertEquals(3, literal10.getNextDFG().size());
assertTrue(literal10.getNextDFG().contains(a10));
assertEquals(3, literal11.getNextDFG().size());
assertTrue(literal11.getNextDFG().contains(a11));
assertEquals(4, literal12.getNextDFG().size());
assertTrue(literal12.getNextDFG().contains(a12));
assertEquals(4, a.getPrevDFG().size());
assertTrue(a.getPrevDFG().contains(literal0));
assertTrue(a.getPrevDFG().contains(a10));
assertTrue(a.getPrevDFG().contains(a11));
assertTrue(a.getPrevDFG().contains(a12));
assertTrue(ab.getPrevDFG().contains(literal0));
assertTrue(ab.getPrevDFG().contains(literal10));
assertTrue(ab.getPrevDFG().contains(literal11));
assertTrue(ab.getPrevDFG().contains(literal12));
assertEquals(1, ab.getNextDFG().size());
assertTrue(ab.getNextDFG().contains(b));
// Fallthrough test
CallExpression println = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, CallExpression.class), c -> c.getName().equals("println")).get(0);
DeclaredReferenceExpression aPrintln = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, DeclaredReferenceExpression.class), e -> e.getNextEOG().contains(println)).get(0);
assertEquals(2, aPrintln.getPrevDFG().size());
assertTrue(aPrintln.getPrevDFG().contains(literal0));
assertTrue(aPrintln.getPrevDFG().contains(literal12));
}
use of de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration in project cpg by Fraunhofer-AISEC.
the class VariableResolverCppTest method testMemberAccessedOverInstance.
@Test
void testMemberAccessedOverInstance() {
VariableDeclaration declaration = TestUtils.getSubnodeOfTypeWithName(outerFunction2, VariableDeclaration.class, "scopeVariables");
VRUtil.assertUsageOfMemberAndBase(callParamMap.get("func2_instance_varName"), declaration, outerVarName);
}
Aggregations