use of de.fraunhofer.aisec.cpg.graph.statements.CatchClause in project cpg by Fraunhofer-AISEC.
the class StatementHandler method handleCatchClause.
private CatchClause handleCatchClause(com.github.javaparser.ast.stmt.CatchClause catchCls) {
CatchClause cClause = NodeBuilder.newCatchClause(catchCls.toString());
lang.getScopeManager().enterScope(cClause);
HashSet<Type> possibleTypes = new HashSet<>();
Type concreteType;
if (catchCls.getParameter().getType() instanceof UnionType) {
for (ReferenceType t : ((UnionType) catchCls.getParameter().getType()).getElements()) {
possibleTypes.add(lang.getTypeAsGoodAsPossible(t));
}
// we do not know which of the exceptions was actually thrown, so we assume this might be any
concreteType = TypeParser.createFrom("java.lang.Throwable", true);
concreteType.setTypeOrigin(Type.Origin.GUESSED);
} else {
concreteType = lang.getTypeAsGoodAsPossible(catchCls.getParameter().getType());
possibleTypes.add(concreteType);
}
VariableDeclaration parameter = NodeBuilder.newVariableDeclaration(catchCls.getParameter().getName().toString(), concreteType, catchCls.getParameter().toString(), false);
parameter.setPossibleSubTypes(possibleTypes);
CompoundStatement body = handleBlockStatement(catchCls.getBody());
cClause.setBody(body);
cClause.setParameter(parameter);
lang.getScopeManager().addDeclaration(parameter);
lang.getScopeManager().leaveScope(cClause);
return cClause;
}
use of de.fraunhofer.aisec.cpg.graph.statements.CatchClause 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.statements.CatchClause in project cpg by Fraunhofer-AISEC.
the class CXXLanguageFrontendTest method testTryCatch.
@Test
void testTryCatch() throws Exception {
File file = new File("src/test/resources/components/trystmt.cpp");
TranslationUnitDeclaration tu = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
Set<FunctionDeclaration> main = tu.getDeclarationsByName("main", FunctionDeclaration.class);
assertFalse(main.isEmpty());
TryStatement tryStatement = main.iterator().next().getBodyStatementAs(0, TryStatement.class);
assertNotNull(tryStatement);
List<CatchClause> catchClauses = tryStatement.getCatchClauses();
// should have 3 catch clauses
assertEquals(3, catchClauses.size());
// declared exception variable
VariableDeclaration parameter = catchClauses.get(0).getParameter();
assertNotNull(parameter);
assertEquals("e", parameter.getName());
assertEquals(TypeParser.createFrom("const std::exception&", true), parameter.getType());
// anonymous variable (this is not 100% handled correctly but will do for now)
parameter = catchClauses.get(1).getParameter();
assertNotNull(parameter);
// this is currently our 'unnamed' parameter
assertEquals("", parameter.getName());
assertEquals(TypeParser.createFrom("const std::exception&", true), parameter.getType());
// catch all
parameter = catchClauses.get(2).getParameter();
assertNull(parameter);
}
Aggregations