use of de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement in project cpg by Fraunhofer-AISEC.
the class CXXLanguageFrontendTest method testDeclarationStatement.
@Test
void testDeclarationStatement() throws Exception {
File file = new File("src/test/resources/declstmt.cpp");
TranslationUnitDeclaration declaration = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
FunctionDeclaration function = declaration.getDeclarationAs(0, FunctionDeclaration.class);
List<Statement> statements = getStatementsOfFunction(function);
statements.forEach(node -> {
LOGGER.debug("{}", node);
assertTrue(node instanceof DeclarationStatement || statements.indexOf(node) == statements.size() - 1 && node instanceof ReturnStatement);
});
VariableDeclaration declFromMultiplicateExpression = ((DeclarationStatement) statements.get(0)).getSingleDeclarationAs(VariableDeclaration.class);
assertEquals(TypeParser.createFrom("SSL_CTX*", true), declFromMultiplicateExpression.getType());
assertEquals("ptr", declFromMultiplicateExpression.getName());
VariableDeclaration withInitializer = ((DeclarationStatement) statements.get(1)).getSingleDeclarationAs(VariableDeclaration.class);
Expression initializer = withInitializer.getInitializer();
assertNotNull(initializer);
assertTrue(initializer instanceof Literal);
assertEquals(1, ((Literal) initializer).getValue());
List<Declaration> twoDeclarations = statements.get(2).getDeclarations();
assertEquals(2, twoDeclarations.size());
VariableDeclaration b = (VariableDeclaration) twoDeclarations.get(0);
assertNotNull(b);
assertEquals("b", b.getName());
assertEquals(TypeParser.createFrom("int*", false), b.getType());
VariableDeclaration c = (VariableDeclaration) twoDeclarations.get(1);
assertNotNull(c);
assertEquals("c", c.getName());
assertEquals(TypeParser.createFrom("int", false), c.getType());
VariableDeclaration withoutInitializer = ((DeclarationStatement) statements.get(3)).getSingleDeclarationAs(VariableDeclaration.class);
initializer = withoutInitializer.getInitializer();
assertEquals(TypeParser.createFrom("int*", true), withoutInitializer.getType());
assertEquals("d", withoutInitializer.getName());
assertNull(initializer);
VariableDeclaration qualifiedType = ((DeclarationStatement) statements.get(4)).getSingleDeclarationAs(VariableDeclaration.class);
assertEquals(TypeParser.createFrom("std.string", true), qualifiedType.getType());
assertEquals("text", qualifiedType.getName());
assertTrue(qualifiedType.getInitializer() instanceof Literal);
assertEquals("some text", ((Literal) qualifiedType.getInitializer()).getValue());
VariableDeclaration pointerWithAssign = ((DeclarationStatement) statements.get(5)).getSingleDeclarationAs(VariableDeclaration.class);
assertEquals(TypeParser.createFrom("void*", true), pointerWithAssign.getType());
assertEquals("ptr2", pointerWithAssign.getName());
assertEquals("NULL", pointerWithAssign.getInitializer().getName());
List<Declaration> classWithVariable = statements.get(6).getDeclarations();
assertEquals(2, classWithVariable.size());
RecordDeclaration classA = (RecordDeclaration) classWithVariable.get(0);
assertNotNull(classA);
assertEquals("A", classA.getName());
VariableDeclaration myA = (VariableDeclaration) classWithVariable.get(1);
assertNotNull(myA);
assertEquals("myA", myA.getName());
assertEquals(classA, ((ObjectType) myA.getType()).getRecordDeclaration());
}
use of de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement in project cpg by Fraunhofer-AISEC.
the class ExpressionHandler method handleVariableDeclarationExpr.
// Not sure how to handle this exactly yet
private DeclarationStatement handleVariableDeclarationExpr(Expression expr) {
VariableDeclarationExpr variableDeclarationExpr = expr.asVariableDeclarationExpr();
DeclarationStatement declarationStatement = NodeBuilder.newDeclarationStatement(variableDeclarationExpr.toString());
for (VariableDeclarator variable : variableDeclarationExpr.getVariables()) {
ResolvedValueDeclaration resolved = variable.resolve();
Type declarationType = this.lang.getTypeAsGoodAsPossible(variable, resolved);
declarationType.setAdditionalTypeKeywords(variableDeclarationExpr.getModifiers().stream().map(m -> m.getKeyword().asString()).collect(Collectors.joining(" ")));
VariableDeclaration declaration = NodeBuilder.newVariableDeclaration(resolved.getName(), declarationType, variable.toString(), false, lang, variable);
if (declarationType instanceof PointerType && ((PointerType) declarationType).isArray()) {
declaration.setIsArray(true);
}
Optional<Expression> oInitializer = variable.getInitializer();
if (oInitializer.isPresent()) {
de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression initializer = (de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression) handle(oInitializer.get());
if (initializer instanceof ArrayCreationExpression) {
declaration.setIsArray(true);
}
declaration.setInitializer(initializer);
} else {
de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression uninitialzedInitializer = new UninitializedValue();
declaration.setInitializer(uninitialzedInitializer);
}
lang.setCodeAndRegion(declaration, variable);
declarationStatement.addToPropertyEdgeDeclaration(declaration);
lang.processAnnotations(declaration, variableDeclarationExpr);
lang.getScopeManager().addDeclaration(declaration);
}
return declarationStatement;
}
use of de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement in project cpg by Fraunhofer-AISEC.
the class CXXLanguageFrontendTest method testAssignmentExpression.
@Test
void testAssignmentExpression() throws Exception {
File file = new File("src/test/resources/assignmentexpression.cpp");
TranslationUnitDeclaration declaration = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
// just take a look at the second function
FunctionDeclaration functionDeclaration = declaration.getDeclarationAs(1, FunctionDeclaration.class);
List<Statement> statements = getStatementsOfFunction(functionDeclaration);
Statement declareA = statements.get(0);
Declaration a = ((DeclarationStatement) declareA).getSingleDeclaration();
Statement assignA = statements.get(1);
assertTrue(assignA instanceof BinaryOperator);
Expression lhs = ((BinaryOperator) assignA).getLhs();
Expression rhs = ((BinaryOperator) assignA).getRhs();
assertEquals("a", ((BinaryOperator) assignA).getLhs().getName());
assertEquals(2, ((Literal) ((BinaryOperator) assignA).getRhs()).getValue());
assertRefersTo(((BinaryOperator) assignA).getLhs(), a);
Statement declareB = statements.get(2);
assertTrue(declareB instanceof DeclarationStatement);
Declaration b = ((DeclarationStatement) declareB).getSingleDeclaration();
// a = b
Statement assignB = statements.get(3);
assertTrue(assignB instanceof BinaryOperator);
lhs = ((BinaryOperator) assignB).getLhs();
rhs = ((BinaryOperator) assignB).getRhs();
assertEquals("a", lhs.getName());
assertTrue(rhs instanceof DeclaredReferenceExpression);
assertEquals("b", rhs.getName());
assertRefersTo(rhs, b);
Statement assignBWithFunction = statements.get(4);
assertTrue(assignBWithFunction instanceof BinaryOperator);
assertEquals("a", ((BinaryOperator) assignBWithFunction).getLhs().getName());
assertTrue(((BinaryOperator) assignBWithFunction).getRhs() instanceof CallExpression);
CallExpression call = (CallExpression) ((BinaryOperator) assignBWithFunction).getRhs();
assertEquals("someFunction", call.getName());
assertRefersTo(call.getArguments().get(0), b);
}
use of de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement in project cpg by Fraunhofer-AISEC.
the class CXXLanguageFrontendTest method testBinaryOperator.
@Test
void testBinaryOperator() throws Exception {
File file = new File("src/test/resources/binaryoperator.cpp");
TranslationUnitDeclaration declaration = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
List<Statement> statements = getStatementsOfFunction(declaration.getDeclarationAs(0, FunctionDeclaration.class));
// first two statements are just declarations
// a = b * 2
BinaryOperator operator = (BinaryOperator) statements.get(2);
assertEquals("a", operator.getLhs().getName());
assertTrue(operator.getRhs() instanceof BinaryOperator);
BinaryOperator rhs = (BinaryOperator) operator.getRhs();
assertTrue(rhs.getLhs() instanceof DeclaredReferenceExpression);
assertEquals("b", rhs.getLhs().getName());
assertTrue(rhs.getRhs() instanceof Literal);
assertEquals(2, ((Literal) rhs.getRhs()).getValue());
// a = 1 * 1
operator = (BinaryOperator) statements.get(3);
assertEquals("a", operator.getLhs().getName());
assertTrue(operator.getRhs() instanceof BinaryOperator);
rhs = (BinaryOperator) operator.getRhs();
assertTrue(rhs.getLhs() instanceof Literal);
assertEquals(1, ((Literal) rhs.getLhs()).getValue());
assertTrue(rhs.getRhs() instanceof Literal);
assertEquals(1, ((Literal) rhs.getRhs()).getValue());
// std::string* notMultiplication
// this is not a multiplication, but a variable declaration with a pointer type, but
// syntactically no different than the previous ones
DeclarationStatement stmt = (DeclarationStatement) statements.get(4);
VariableDeclaration decl = (VariableDeclaration) stmt.getSingleDeclaration();
assertEquals(TypeParser.createFrom("std.string*", true), decl.getType());
assertEquals("notMultiplication", decl.getName());
assertTrue(decl.getInitializer() instanceof BinaryOperator);
operator = (BinaryOperator) decl.getInitializer();
assertTrue(operator.getLhs() instanceof Literal);
assertEquals(0, ((Literal) operator.getLhs()).getValue());
assertTrue(operator.getRhs() instanceof Literal);
assertEquals(0, ((Literal) operator.getRhs()).getValue());
}
use of de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement in project cpg by Fraunhofer-AISEC.
the class CXXLanguageFrontendTest method testForEach.
/*@Test
void testFF() throws Exception {
File file = new File("src/test/resources/hqxvlc.c");
TranslationUnitDeclaration tu =
TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
assertNotNull(tu);
var nodes = SubgraphWalker.flattenAST(tu);
var types = new HashMap<String, Integer>();
var codes = new HashMap<String, Set<Integer>>();
for (var n : nodes) {
var count = types.computeIfAbsent(n.getClass().getSimpleName(), key -> 0);
types.put(n.getClass().getSimpleName(), count + 1);
var code = codes.computeIfAbsent(n.getClass().getSimpleName(), key -> new HashSet<>());
code.add(n.hashCode());
}
for (var t : types.keySet()) {
System.out.println(t + ": " + types.get(t) + " | unique hash codes: " + codes.get(t).size());
}
}*/
@Test
void testForEach() throws Exception {
File file = new File("src/test/resources/components/foreachstmt.cpp");
TranslationUnitDeclaration tu = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
@NonNull Set<FunctionDeclaration> main = tu.getDeclarationsByName("main", FunctionDeclaration.class);
assertFalse(main.isEmpty());
FunctionDeclaration decl = main.iterator().next();
VariableDeclaration ls = decl.getVariableDeclarationByName("ls").orElse(null);
assertNotNull(ls);
assertEquals(TypeParser.createFrom("std::vector<int>", true), ls.getType());
assertEquals("ls", ls.getName());
ForEachStatement forEachStatement = decl.getBodyStatementAs(1, ForEachStatement.class);
assertNotNull(forEachStatement);
// should loop over ls
assertEquals(ls, ((DeclaredReferenceExpression) forEachStatement.getIterable()).getRefersTo());
// should declare auto i (so far no concrete type inferrable)
Statement stmt = forEachStatement.getVariable();
assertNotNull(stmt);
assertTrue(stmt instanceof DeclarationStatement);
assertTrue(((DeclarationStatement) stmt).isSingleDeclaration());
VariableDeclaration i = (VariableDeclaration) ((DeclarationStatement) stmt).getSingleDeclaration();
assertNotNull(i);
assertEquals("i", i.getName());
assertEquals(UnknownType.getUnknownType(), i.getType());
}
Aggregations