Skip to content

Commit

Permalink
Merge branch 'master' into fix-npe-in-expression-handler
Browse files Browse the repository at this point in the history
  • Loading branch information
konradweiss authored Nov 28, 2020
2 parents 3e38574 + 8b91b4e commit 7a15078
Show file tree
Hide file tree
Showing 27 changed files with 418 additions and 474 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,12 @@ private FunctionDeclaration handleFunctionDefinition(CPPASTFunctionDefinition ct

if (bodyStatement instanceof CompoundStatement) {
CompoundStatement body = (CompoundStatement) bodyStatement;
List<PropertyEdge> statements = body.getStatementEdges();
List<PropertyEdge<Statement>> statements = body.getStatementEdges();

// get the last statement
Statement lastStatement = null;
if (!statements.isEmpty()) {
lastStatement = (Statement) statements.get(statements.size() - 1).getEnd();
lastStatement = statements.get(statements.size() - 1).getEnd();
}

// add an implicit return statement, if there is none
Expand Down Expand Up @@ -294,7 +294,7 @@ private Declaration handleSimpleDeclaration(CPPASTSimpleDeclaration ctx) {

this.lang.processAttributes(declaration, ctx);

sequence.add(declaration);
sequence.addDeclaration(declaration);
} else if (declSpecifier instanceof CPPASTElaboratedTypeSpecifier) {
warnWithFileLocation(
lang,
Expand Down Expand Up @@ -325,7 +325,7 @@ private Declaration handleSimpleDeclaration(CPPASTSimpleDeclaration ctx) {
// process attributes
this.lang.processAttributes(declaration, ctx);

sequence.add(declaration);
sequence.addDeclaration(declaration);
}

if (sequence.isSingle()) {
Expand Down
28 changes: 26 additions & 2 deletions src/main/java/de/fraunhofer/aisec/cpg/graph/DeclarationHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
package de.fraunhofer.aisec.cpg.graph;

import de.fraunhofer.aisec.cpg.graph.declarations.Declaration;
import de.fraunhofer.aisec.cpg.graph.edge.Properties;
import de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge;
import java.util.Collection;
import org.checkerframework.checker.nullness.qual.NonNull;
Expand All @@ -48,9 +49,32 @@ default <N extends Declaration> void addIfNotContains(Collection<N> collection,
}
}

default void addIfNotContains(Collection<PropertyEdge> collection, PropertyEdge propertyEdge) {
default <T extends Node> void addIfNotContains(
Collection<PropertyEdge<T>> collection, T declaration) {
addIfNotContains(collection, declaration, true);
}

/**
* Adds a declaration to a collection of property edges, which contain the declarations
*
* @param collection the collection
* @param declaration the declaration
* @param <T> the type of the declaration
* @param outgoing whether the property is outgoing
*/
default <T extends Node> void addIfNotContains(
Collection<PropertyEdge<T>> collection, T declaration, boolean outgoing) {
// create a new property edge
var propertyEdge =
outgoing
? new PropertyEdge<>((Node) this, declaration)
: new PropertyEdge<>(declaration, (T) this);

// set the index property
propertyEdge.addProperty(Properties.INDEX, collection.size());

boolean contains = false;
for (PropertyEdge element : collection) {
for (PropertyEdge<T> element : collection) {
if (element.getEnd().equals(propertyEdge.getEnd())) {
contains = true;
break;
Expand Down
44 changes: 21 additions & 23 deletions src/main/java/de/fraunhofer/aisec/cpg/graph/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

package de.fraunhofer.aisec.cpg.graph;

import static de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge.unwrap;

import de.fraunhofer.aisec.cpg.graph.declarations.TypedefDeclaration;
import de.fraunhofer.aisec.cpg.graph.edge.Properties;
import de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge;
Expand Down Expand Up @@ -78,17 +80,17 @@ public class Node implements IVisitable<Node>, Persistable {
/** Incoming control flow edges. */
@NonNull
@Relationship(value = "EOG", direction = "INCOMING")
protected List<PropertyEdge> prevEOG = new ArrayList<>();
protected List<PropertyEdge<Node>> prevEOG = new ArrayList<>();

/** outgoing control flow edges. */
@Relationship(value = "EOG", direction = "OUTGOING")
@NonNull
protected List<PropertyEdge> nextEOG = new ArrayList<>();
protected List<PropertyEdge<Node>> nextEOG = new ArrayList<>();

/** outgoing control flow edges. */
@NonNull
@Relationship(value = "CFG", direction = "OUTGOING")
protected List<PropertyEdge> nextCFG = new ArrayList<>();
protected List<PropertyEdge<Node>> nextCFG = new ArrayList<>();

@NonNull
@Relationship(value = "DFG", direction = "INCOMING")
Expand Down Expand Up @@ -162,16 +164,16 @@ public void setLocation(@Nullable PhysicalLocation location) {
}

@NonNull
public List<PropertyEdge> getPrevEOGProperties() {
public List<PropertyEdge<Node>> getPrevEOGProperties() {
return this.prevEOG;
}

@NonNull
public List<PropertyEdge> getNextEOGProperties() {
public List<PropertyEdge<Node>> getNextEOGProperties() {
return this.nextEOG;
}

public void setPrevEOGProperties(@NonNull List<PropertyEdge> prevEOG) {
public void setPrevEOGProperties(@NonNull List<PropertyEdge<Node>> prevEOG) {
this.prevEOG = prevEOG;
}

Expand All @@ -181,7 +183,7 @@ public void removePrevEOGEntry(@NonNull Node eog) {

public void removePrevEOGEntries(@NonNull List<Node> prevEOGs) {
for (Node n : prevEOGs) {
List<PropertyEdge> remove =
List<PropertyEdge<Node>> remove =
PropertyEdge.findPropertyEdgesByPredicate(this.prevEOG, e -> e.getStart().equals(n));
this.prevEOG.removeAll(remove);
}
Expand All @@ -195,11 +197,11 @@ public List<Node> getPrevEOG() {
}

public void setPrevEOG(@NonNull List<Node> prevEOG) {
List<PropertyEdge> propertyEdgesEOG = new ArrayList<>();
List<PropertyEdge<Node>> propertyEdgesEOG = new ArrayList<>();
int idx = 0;

for (Node prev : prevEOG) {
PropertyEdge propertyEdge = new PropertyEdge(prev, this);
var propertyEdge = new PropertyEdge<>(prev, this);
propertyEdge.addProperty(Properties.INDEX, idx);
propertyEdgesEOG.add(propertyEdge);
idx++;
Expand All @@ -208,12 +210,12 @@ public void setPrevEOG(@NonNull List<Node> prevEOG) {
this.prevEOG = propertyEdgesEOG;
}

public void addPrevEOG(@NonNull PropertyEdge propertyEdge) {
public void addPrevEOG(@NonNull PropertyEdge<Node> propertyEdge) {
this.prevEOG.add(propertyEdge);
}

@NonNull
public List<PropertyEdge> getNextEOGPropertyEdge() {
public List<PropertyEdge<Node>> getNextEOGPropertyEdge() {
return this.nextEOG;
}

Expand All @@ -225,10 +227,10 @@ public List<Node> getNextEOG() {
}

public void setNextEOG(@NonNull List<Node> nextEOG) {
this.nextEOG = PropertyEdge.transformIntoPropertyEdgeList(nextEOG, this, true);
this.nextEOG = PropertyEdge.transformIntoOutgoingPropertyEdgeList(nextEOG, this);
}

public void addNextEOG(@NonNull PropertyEdge propertyEdge) {
public void addNextEOG(@NonNull PropertyEdge<Node> propertyEdge) {
this.nextEOG.add(propertyEdge);
}

Expand All @@ -238,15 +240,11 @@ public void clearNextEOG() {

@NonNull
public List<Node> getNextCFG() {
List<Node> target = new ArrayList<>();
for (PropertyEdge propertyEdge : this.nextCFG) {
target.add(propertyEdge.getEnd());
}
return Collections.unmodifiableList(target);
return unwrap(this.nextCFG);
}

public void addNextCFG(Node node) {
PropertyEdge propertyEdge = new PropertyEdge(this, node);
var propertyEdge = new PropertyEdge<>(this, node);
propertyEdge.addProperty(Properties.INDEX, this.nextCFG.size());
this.nextCFG.add(propertyEdge);
}
Expand Down Expand Up @@ -370,15 +368,15 @@ public void disconnectFromGraph() {
n.nextDFG.remove(this);
}
prevDFG.clear();
for (PropertyEdge n : nextEOG) {
List<PropertyEdge> remove =
for (var n : nextEOG) {
List<PropertyEdge<Node>> remove =
PropertyEdge.findPropertyEdgesByPredicate(
n.getEnd().prevEOG, e -> e.getStart().equals(this));
n.getEnd().prevEOG.removeAll(remove);
}
nextEOG.clear();
for (PropertyEdge n : prevEOG) {
List<PropertyEdge> remove =
for (var n : prevEOG) {
List<PropertyEdge<Node>> remove =
PropertyEdge.findPropertyEdgesByPredicate(
n.getStart().nextEOG, e -> e.getEnd().equals(this));
n.getStart().nextEOG.removeAll(remove);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

package de.fraunhofer.aisec.cpg.graph.declarations;

import de.fraunhofer.aisec.cpg.graph.edge.Properties;
import de.fraunhofer.aisec.cpg.graph.DeclarationHolder;
import de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -40,35 +40,31 @@
* will be converted into a list-structure and all its children will be added to the parent, i.e.
* the translation unit. It should not end up in the final graph.
*/
public class DeclarationSequence extends Declaration {
public class DeclarationSequence extends Declaration implements DeclarationHolder {

@Relationship(value = "children", direction = "OUTGOING")
private final List<PropertyEdge> children = new ArrayList<>();
@Relationship(value = "CHILDREN", direction = "OUTGOING")
private final List<PropertyEdge<Declaration>> children = new ArrayList<>();

public List<PropertyEdge> getChildrenPropertyEdge() {
public List<PropertyEdge<Declaration>> getChildrenPropertyEdge() {
return this.children;
}

public List<Declaration> getChildren() {
List<Declaration> target = new ArrayList<>();
for (PropertyEdge propertyEdge : this.children) {
target.add((Declaration) propertyEdge.getEnd());
for (PropertyEdge<Declaration> propertyEdge : this.children) {
target.add(propertyEdge.getEnd());
}
return Collections.unmodifiableList(target);
}

public void add(@NonNull Declaration declaration) {
PropertyEdge propertyEdge;
public void addDeclaration(@NonNull Declaration declaration) {
if (declaration instanceof DeclarationSequence) {
for (Declaration declarationChild : ((DeclarationSequence) declaration).getChildren()) {
propertyEdge = new PropertyEdge(this, declarationChild);
propertyEdge.addProperty(Properties.INDEX, this.children.size());
children.add(propertyEdge);
addIfNotContains(this.children, declarationChild);
}
}
propertyEdge = new PropertyEdge(this, declaration);
propertyEdge.addProperty(Properties.INDEX, this.children.size());
children.add(propertyEdge);

addIfNotContains(this.children, declaration);
}

public List<Declaration> asList() {
Expand All @@ -80,6 +76,6 @@ public boolean isSingle() {
}

public Declaration first() {
return (Declaration) children.get(0).getEnd();
return children.get(0).getEnd();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

package de.fraunhofer.aisec.cpg.graph.declarations;

import static de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge.unwrap;

import de.fraunhofer.aisec.cpg.graph.Node;
import de.fraunhofer.aisec.cpg.graph.SubGraph;
import de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge;
Expand All @@ -36,45 +38,37 @@

public class EnumDeclaration extends Declaration {

@Relationship(value = "entries", direction = "OUTGOING")
@Relationship(value = "ENTRIES", direction = "OUTGOING")
@SubGraph("AST")
private List<PropertyEdge> entries = new ArrayList<>();
private List<PropertyEdge<EnumConstantDeclaration>> entries = new ArrayList<>();

@Relationship(value = "superTypes", direction = "OUTGOING")
private List<PropertyEdge> superTypes = new ArrayList<>();
@Relationship(value = "SUPER_TYPES", direction = "OUTGOING")
private List<PropertyEdge<Type>> superTypes = new ArrayList<>();

@Relationship private Set<RecordDeclaration> superTypeDeclarations = new HashSet<>();

public List<PropertyEdge> getEntriesPropertyEdge() {
public List<PropertyEdge<EnumConstantDeclaration>> getEntriesPropertyEdge() {
return this.entries;
}

public List<EnumConstantDeclaration> getEntries() {
List<EnumConstantDeclaration> target = new ArrayList<>();
for (PropertyEdge propertyEdge : this.entries) {
target.add((EnumConstantDeclaration) propertyEdge.getEnd());
}
return Collections.unmodifiableList(target);
return unwrap(this.entries);
}

public void setEntries(List<EnumConstantDeclaration> entries) {
this.entries = PropertyEdge.transformIntoPropertyEdgeList(entries, this, true);
this.entries = PropertyEdge.transformIntoOutgoingPropertyEdgeList(entries, this);
}

public List<Type> getSuperTypes() {
List<Type> target = new ArrayList<>();
for (PropertyEdge propertyEdge : this.superTypes) {
target.add((Type) propertyEdge.getEnd());
}
return Collections.unmodifiableList(target);
return unwrap(this.superTypes);
}

public List<PropertyEdge> getSuperTypesPropertyEdge() {
public List<PropertyEdge<Type>> getSuperTypesPropertyEdge() {
return this.superTypes;
}

public void setSuperTypes(List<Type> superTypes) {
this.superTypes = PropertyEdge.transformIntoPropertyEdgeList(superTypes, this, true);
this.superTypes = PropertyEdge.transformIntoOutgoingPropertyEdgeList(superTypes, this);
}

public Set<RecordDeclaration> getSuperTypeDeclarations() {
Expand Down
Loading

0 comments on commit 7a15078

Please sign in to comment.