use of de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge in project cpg by Fraunhofer-AISEC.
the class EOGTest method testBranchProperty.
/**
* Tests EOG branch edge property in if/else if/else construct
*
* @throws Exception
*/
@Test
void testBranchProperty() throws Exception {
Path topLevel = Path.of("src", "test", "resources", "eog");
List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(topLevel.resolve("EOG.java").toFile()), topLevel, true);
// Test If-Block
IfStatement firstIf = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, IfStatement.class), l -> l.getLocation().getRegion().getStartLine() == 6).get(0);
DeclaredReferenceExpression a = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, DeclaredReferenceExpression.class), l -> l.getLocation().getRegion().getStartLine() == 8 && l.getName().equals("a")).get(0);
DeclaredReferenceExpression b = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, DeclaredReferenceExpression.class), l -> l.getLocation().getRegion().getStartLine() == 7 && l.getName().equals("b")).get(0);
List<PropertyEdge<Node>> nextEOG = firstIf.getNextEOGEdges();
assertEquals(2, nextEOG.size());
for (PropertyEdge<Node> edge : nextEOG) {
assertEquals(firstIf, edge.getStart());
if (edge.getEnd().equals(b)) {
assertEquals(true, edge.getProperty(Properties.BRANCH));
assertEquals(0, edge.getProperty(Properties.INDEX));
} else {
assertEquals(a, edge.getEnd());
assertEquals(false, edge.getProperty(Properties.BRANCH));
assertEquals(1, edge.getProperty(Properties.INDEX));
}
}
IfStatement elseIf = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, IfStatement.class), l -> l.getLocation().getRegion().getStartLine() == 8).get(0);
assertEquals(elseIf, firstIf.getElseStatement());
DeclaredReferenceExpression b2 = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, DeclaredReferenceExpression.class), l -> l.getLocation().getRegion().getStartLine() == 9 && l.getName().equals("b")).get(0);
DeclaredReferenceExpression x = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, DeclaredReferenceExpression.class), l -> l.getLocation().getRegion().getStartLine() == 11 && l.getName().equals("x")).get(0);
nextEOG = elseIf.getNextEOGEdges();
assertEquals(2, nextEOG.size());
for (PropertyEdge<Node> edge : nextEOG) {
assertEquals(elseIf, edge.getStart());
if (edge.getEnd().equals(b2)) {
assertEquals(true, edge.getProperty(Properties.BRANCH));
assertEquals(0, edge.getProperty(Properties.INDEX));
} else {
assertEquals(x, edge.getEnd());
assertEquals(false, edge.getProperty(Properties.BRANCH));
assertEquals(1, edge.getProperty(Properties.INDEX));
}
}
}
use of de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge in project cpg by Fraunhofer-AISEC.
the class InitializerListExpression method typeChanged.
@Override
public void typeChanged(HasType src, HasType root, Type oldType) {
if (!TypeManager.isTypeSystemActive()) {
return;
}
if (!TypeManager.getInstance().isUnknown(this.type) && src.getPropagationType().equals(oldType)) {
return;
}
Type previous = this.type;
Type newType;
Set<Type> subTypes;
if (this.getInitializers().contains(src)) {
Set<Type> types = this.initializers.parallelStream().map(PropertyEdge::getEnd).map(Expression::getType).filter(Objects::nonNull).map(t -> TypeManager.getInstance().registerType(t.reference(PointerOrigin.ARRAY))).collect(Collectors.toSet());
Type alternative = !types.isEmpty() ? types.iterator().next() : UnknownType.getUnknownType();
newType = TypeManager.getInstance().getCommonType(types).orElse(alternative);
subTypes = new HashSet<>(getPossibleSubTypes());
subTypes.remove(oldType);
subTypes.addAll(types);
} else {
newType = src.getType();
subTypes = new HashSet<>(getPossibleSubTypes());
subTypes.remove(oldType);
subTypes.add(newType);
}
setType(newType, root);
setPossibleSubTypes(subTypes, root);
if (!previous.equals(this.type)) {
this.type.setTypeOrigin(Type.Origin.DATAFLOW);
}
}
use of de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge in project cpg by Fraunhofer-AISEC.
the class ExpressionList method addExpression.
public void addExpression(Statement expression) {
if (!this.expressions.isEmpty()) {
Statement lastExpression = this.expressions.get(this.expressions.size() - 1).getEnd();
if (lastExpression instanceof HasType)
((HasType) lastExpression).unregisterTypeListener(this);
this.removePrevDFG(lastExpression);
}
PropertyEdge<Statement> propertyEdge = new PropertyEdge<>(this, expression);
propertyEdge.addProperty(Properties.INDEX, this.expressions.size());
this.expressions.add(propertyEdge);
this.addPrevDFG(expression);
if (expression instanceof HasType) {
((HasType) expression).registerTypeListener(this);
}
}
use of de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge in project cpg by Fraunhofer-AISEC.
the class FunctionDeclaration method hasSignature.
public boolean hasSignature(List<Type> targetSignature) {
List<ParamVariableDeclaration> signature = parameters.stream().map(PropertyEdge::getEnd).sorted(Comparator.comparingInt(ParamVariableDeclaration::getArgumentIndex)).collect(Collectors.toList());
if (targetSignature.size() < signature.size()) {
return false;
} else {
// signature is a collection of positional arguments, so the order must be preserved
for (int i = 0; i < signature.size(); i++) {
ParamVariableDeclaration declared = signature.get(i);
if (declared.isVariadic() && targetSignature.size() >= signature.size()) {
// different vararg types, in C++ we can't, as vararg types are not defined here anyways)
return true;
}
Type provided = targetSignature.get(i);
if (!TypeManager.getInstance().isSupertypeOf(declared.getType(), provided)) {
return false;
}
}
// vararg has been encountered
return targetSignature.size() == signature.size();
}
}
use of de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge in project cpg by Fraunhofer-AISEC.
the class InitializerListExpression method typeChanged.
@Override
public void typeChanged(HasType src, Collection<HasType> root, Type oldType) {
if (!TypeManager.isTypeSystemActive()) {
return;
}
if (!TypeManager.getInstance().isUnknown(this.type) && src.getPropagationType().equals(oldType)) {
return;
}
Type previous = this.type;
Type newType;
Set<Type> subTypes;
if (this.getInitializers().contains(src)) {
Set<Type> types = this.initializers.parallelStream().map(PropertyEdge::getEnd).map(Expression::getType).filter(Objects::nonNull).map(t -> TypeManager.getInstance().registerType(t.reference(PointerOrigin.ARRAY))).collect(Collectors.toSet());
Type alternative = !types.isEmpty() ? types.iterator().next() : UnknownType.getUnknownType();
newType = TypeManager.getInstance().getCommonType(types).orElse(alternative);
subTypes = new HashSet<>(getPossibleSubTypes());
subTypes.remove(oldType);
subTypes.addAll(types);
} else {
newType = src.getType();
subTypes = new HashSet<>(getPossibleSubTypes());
subTypes.remove(oldType);
subTypes.add(newType);
}
setType(newType, root);
setPossibleSubTypes(subTypes, root);
if (!previous.equals(this.type)) {
this.type.setTypeOrigin(Type.Origin.DATAFLOW);
}
}
Aggregations