use of de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration 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());
}
use of de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration in project cpg by Fraunhofer-AISEC.
the class VisitorTest method testAllEogNodeVisitor.
/**
* Visits all nodes along EOG.
*/
@Test
void testAllEogNodeVisitor() {
List<Node> nodeList = new ArrayList<>();
RecordDeclaration recordDeclaration = namespace.getDeclarationAs(0, RecordDeclaration.class);
MethodDeclaration method = recordDeclaration.getMethods().stream().filter(m -> m.getName().equals("method")).collect(Collectors.toList()).get(0);
/* TODO A better way to get the "first" statement in a method body is needed.
This is currently the only (fragile, ugly and unsafe) way to get to the first "real" statement in a method body.
getNextEOG() and getNextCFG() return empty lists.
*/
Statement firstStmt = ((CompoundStatement) method.getBody()).getStatements().get(0);
firstStmt.accept(Strategy::EOG_FORWARD, new IVisitor<Node>() {
public void visit(Node n) {
System.out.println(n);
nodeList.add(n);
}
});
assertEquals(22, nodeList.size());
}
use of de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration in project cpg by Fraunhofer-AISEC.
the class VisitorTest method testReturnStmtVisitor.
/**
* Visits only ReturnStatement nodes.
*/
@Test
void testReturnStmtVisitor() {
List<ReturnStatement> returnStmts = new ArrayList<>();
RecordDeclaration recordDeclaration = namespace.getDeclarationAs(0, RecordDeclaration.class);
recordDeclaration.accept(Strategy::AST_FORWARD, new IVisitor<Node>() {
public void visit(ReturnStatement r) {
returnStmts.add(r);
}
});
assertEquals(2, returnStmts.size());
}
use of de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration in project cpg by Fraunhofer-AISEC.
the class SuperCallTest method testNoExcessFields.
@Test
void testNoExcessFields() throws Exception {
List<TranslationUnitDeclaration> result = TestUtils.analyze("java", topLevel, true);
List<RecordDeclaration> records = TestUtils.subnodesOfType(result, RecordDeclaration.class);
RecordDeclaration superClass = TestUtils.findByUniqueName(records, "SuperClass");
assertEquals(2, superClass.getFields().size());
assertEquals(Set.of("this", "field"), superClass.getFields().stream().map(Node::getName).collect(Collectors.toSet()));
RecordDeclaration subClass = TestUtils.findByUniqueName(records, "SubClass");
assertEquals(2, subClass.getFields().size());
assertEquals(Set.of("this", "field"), subClass.getFields().stream().map(Node::getName).collect(Collectors.toSet()));
RecordDeclaration inner = TestUtils.findByUniqueName(records, "SubClass.Inner");
assertEquals(1, inner.getFields().size());
assertEquals(Set.of("this"), inner.getFields().stream().map(Node::getName).collect(Collectors.toSet()));
}
use of de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration in project cpg by Fraunhofer-AISEC.
the class SuperCallTest method testInterfaceCall.
@Test
void testInterfaceCall() throws Exception {
List<TranslationUnitDeclaration> result = TestUtils.analyze("java", topLevel, true);
List<RecordDeclaration> records = TestUtils.subnodesOfType(result, RecordDeclaration.class);
RecordDeclaration interface1 = TestUtils.findByUniqueName(records, "Interface1");
List<MethodDeclaration> interface1Methods = TestUtils.subnodesOfType(interface1, MethodDeclaration.class);
MethodDeclaration interface1Target = TestUtils.findByUniqueName(interface1Methods, "target");
RecordDeclaration interface2 = TestUtils.findByUniqueName(records, "Interface2");
List<MethodDeclaration> interface2Methods = TestUtils.subnodesOfType(interface2, MethodDeclaration.class);
MethodDeclaration interface2Target = TestUtils.findByUniqueName(interface2Methods, "target");
RecordDeclaration subClass = TestUtils.findByUniqueName(records, "SubClass");
List<MethodDeclaration> methods = TestUtils.subnodesOfType(subClass, MethodDeclaration.class);
MethodDeclaration target = TestUtils.findByUniqueName(methods, "target");
List<CallExpression> calls = TestUtils.subnodesOfType(target, CallExpression.class);
CallExpression interface1Call = TestUtils.findByUniquePredicate(calls, c -> "Interface1.super.target();".equals(c.getCode()));
CallExpression interface2Call = TestUtils.findByUniquePredicate(calls, c -> "Interface2.super.target();".equals(c.getCode()));
assertEquals(List.of(interface1Target), interface1Call.getInvokes());
assertEquals(List.of(interface2Target), interface2Call.getInvokes());
}
Aggregations