use of de.fraunhofer.aisec.cpg.graph.declarations.Declaration in project cpg by Fraunhofer-AISEC.
the class BotanExampleTest method testExample.
@Test
void testExample() throws Exception {
File file = new File("src/test/resources/botan/symm_block_cipher.cpp");
TranslationUnitDeclaration declaration = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), false);
assertNotNull(declaration);
List<Declaration> declarations = declaration.getDeclarations();
assertEquals(5, declarations.size());
Declaration doCrypt = declarations.stream().filter(decl -> decl.getName().equals("do_crypt")).findFirst().get();
assertTrue(doCrypt instanceof FunctionDeclaration);
assertEquals("do_crypt", doCrypt.getName());
Declaration encrypt = declarations.stream().filter(decl -> decl.getName().equals("encrypt")).findFirst().get();
assertTrue(encrypt instanceof FunctionDeclaration);
assertEquals("encrypt", encrypt.getName());
Declaration decrypt = declarations.stream().filter(decl -> decl.getName().equals("decrypt")).findFirst().get();
assertTrue(decrypt instanceof FunctionDeclaration);
assertEquals("decrypt", decrypt.getName());
Declaration main = declarations.stream().filter(decl -> decl.getName().equals("main")).findFirst().get();
assertTrue(main instanceof FunctionDeclaration);
assertEquals("main", main.getName());
}
use of de.fraunhofer.aisec.cpg.graph.declarations.Declaration 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.declarations.Declaration in project cpg by Fraunhofer-AISEC.
the class StatementHandler method handleTryStatement.
private TryStatement handleTryStatement(Statement stmt) {
TryStmt tryStmt = stmt.asTryStmt();
TryStatement tryStatement = NodeBuilder.newTryStatement(stmt.toString());
lang.getScopeManager().enterScope(tryStatement);
List<de.fraunhofer.aisec.cpg.graph.statements.Statement> resources = tryStmt.getResources().stream().map(lang.getExpressionHandler()::handle).collect(Collectors.toList());
CompoundStatement tryBlock = handleBlockStatement(tryStmt.getTryBlock());
List<CatchClause> catchClauses = tryStmt.getCatchClauses().stream().map(this::handleCatchClause).collect(Collectors.toList());
CompoundStatement finallyBlock = tryStmt.getFinallyBlock().map(this::handleBlockStatement).orElse(null);
lang.getScopeManager().leaveScope(tryStatement);
tryStatement.setResources(resources);
tryStatement.setTryBlock(tryBlock);
tryStatement.setFinallyBlock(finallyBlock);
tryStatement.setCatchClauses(catchClauses);
for (de.fraunhofer.aisec.cpg.graph.statements.Statement r : resources) {
if (r instanceof DeclarationStatement) {
for (Declaration d : r.getDeclarations()) {
if (d instanceof VariableDeclaration) {
lang.getScopeManager().addDeclaration(d);
}
}
}
}
return tryStatement;
}
use of de.fraunhofer.aisec.cpg.graph.declarations.Declaration 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);
}
Aggregations