use of de.fraunhofer.aisec.cpg.graph.types.ObjectType in project cpg by Fraunhofer-AISEC.
the class FunctionTemplateTest method testInvocationWithImplicitCastToOverridenTemplateParameter.
@Test
void testInvocationWithImplicitCastToOverridenTemplateParameter() throws Exception {
// test invocation target when template parameter produces a cast in an argument
List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(Path.of(topLevel.toString(), "functionTemplateInvocation6.cpp").toFile()), topLevel, true);
FunctionTemplateDeclaration templateDeclaration = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, FunctionTemplateDeclaration.class), t -> t.getName().equals("fixed_multiply"));
FunctionDeclaration fixedMultiply = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, FunctionDeclaration.class), f -> f.getName().equals("fixed_multiply") && f.getType().getName().equals("T"));
// Check realization of template maps to our target function
assertEquals(1, templateDeclaration.getRealization().size());
assertEquals(fixedMultiply, templateDeclaration.getRealization().get(0));
CallExpression call = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, CallExpression.class), c -> c.getName().equals("fixed_multiply"));
// Check invocation target
assertEquals(1, call.getInvokes().size());
assertEquals(fixedMultiply, call.getInvokes().get(0));
// Check template parameters
ObjectType intType = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, ObjectType.class), t -> t.getName().equals("int"));
Literal<?> literal5 = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(5));
assertEquals(2, call.getTemplateParameters().size());
assertEquals(intType, ((TypeExpression) call.getTemplateParameters().get(0)).getType());
assertEquals(literal5, call.getTemplateParameters().get(1));
// Check return value
assertEquals(intType, call.getType());
// Check cast
assertEquals(1, call.getArguments().size());
assertTrue(call.getArguments().get(0) instanceof CastExpression);
CastExpression arg = (CastExpression) call.getArguments().get(0);
assertEquals(intType, arg.getCastType());
assertEquals(20.3, ((Literal) arg.getExpression()).getValue());
}
use of de.fraunhofer.aisec.cpg.graph.types.ObjectType in project cpg by Fraunhofer-AISEC.
the class FunctionTemplateTest method testInvocationWithPartialDefaults.
@Test
void testInvocationWithPartialDefaults() throws Exception {
// test invocation target when no autodeduction is possible, but defaults are partially used
List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(Path.of(topLevel.toString(), "functionTemplateInvocation5.cpp").toFile()), topLevel, true);
FunctionTemplateDeclaration templateDeclaration = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, FunctionTemplateDeclaration.class), t -> t.getName().equals("fixed_multiply"));
FunctionDeclaration fixedMultiply = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, FunctionDeclaration.class), f -> f.getName().equals("fixed_multiply") && f.getType().getName().equals("T"));
// Check realization of template maps to our target function
assertEquals(1, templateDeclaration.getRealization().size());
assertEquals(fixedMultiply, templateDeclaration.getRealization().get(0));
CallExpression call = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, CallExpression.class), c -> c.getName().equals("fixed_multiply"));
// Check invocation target
assertEquals(1, call.getInvokes().size());
assertEquals(fixedMultiply, call.getInvokes().get(0));
// Check template parameters
ObjectType doubleType = new ObjectType("double", Type.Storage.AUTO, new Type.Qualifier(), new ArrayList<>(), ObjectType.Modifier.SIGNED, true);
Literal<?> literal5 = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(5));
assertEquals(2, call.getTemplateParameters().size());
assertEquals(doubleType, ((TypeExpression) call.getTemplateParameters().get(0)).getType());
assertEquals(literal5, call.getTemplateParameters().get(1));
// Check return value
assertEquals(doubleType, call.getType());
}
use of de.fraunhofer.aisec.cpg.graph.types.ObjectType in project cpg by Fraunhofer-AISEC.
the class FunctionTemplateTest method testInvocationWithoutCallTarget.
@Test
void testInvocationWithoutCallTarget() throws Exception {
// Check if a CallExpression is converted to a TemplateCallExpression if a compatible target
// exists
List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(Path.of(topLevel.toString(), "functionTemplateInvocation2.cpp").toFile()), topLevel, true);
FunctionTemplateDeclaration templateDeclaration = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, FunctionTemplateDeclaration.class), t -> t.getName().equals("fixed_multiply"));
FunctionDeclaration fixedMultiply = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, FunctionDeclaration.class), f -> f.getName().equals("fixed_multiply") && f.getType().getName().equals("T"));
// Check realization of template maps to our target function
assertEquals(1, templateDeclaration.getRealization().size());
assertEquals(fixedMultiply, templateDeclaration.getRealization().get(0));
CallExpression call = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, CallExpression.class), c -> c.getName().equals("fixed_multiply"));
// Check invocation target
assertEquals(1, call.getInvokes().size());
assertEquals(fixedMultiply, call.getInvokes().get(0));
// Check template parameters
ObjectType doubleType = new ObjectType("double", Type.Storage.AUTO, new Type.Qualifier(), new ArrayList<>(), ObjectType.Modifier.SIGNED, true);
Literal<?> literal5 = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(5));
assertEquals(2, call.getTemplateParameters().size());
assertEquals(doubleType, ((TypeExpression) call.getTemplateParameters().get(0)).getType());
assertEquals(literal5, call.getTemplateParameters().get(1));
// Check return value
assertEquals(doubleType, call.getType());
}
use of de.fraunhofer.aisec.cpg.graph.types.ObjectType in project cpg by Fraunhofer-AISEC.
the class FunctionTemplateTest method testInvocationWithDefaults.
@Test
void testInvocationWithDefaults() throws Exception {
// test invocation target when no autodeduction is possible, but defaults are provided
List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(Path.of(topLevel.toString(), "functionTemplateInvocation4.cpp").toFile()), topLevel, true);
FunctionTemplateDeclaration templateDeclaration = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, FunctionTemplateDeclaration.class), t -> t.getName().equals("fixed_multiply"));
FunctionDeclaration fixedMultiply = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, FunctionDeclaration.class), f -> f.getName().equals("fixed_multiply") && f.getType().getName().equals("T"));
// Check realization of template maps to our target function
assertEquals(1, templateDeclaration.getRealization().size());
assertEquals(fixedMultiply, templateDeclaration.getRealization().get(0));
CallExpression call = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, CallExpression.class), c -> c.getName().equals("fixed_multiply"));
// Check invocation target
assertEquals(1, call.getInvokes().size());
assertEquals(fixedMultiply, call.getInvokes().get(0));
// Check template parameters
ObjectType intType = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, ObjectType.class), t -> t.getName().equals("int"));
Literal<?> literal5 = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(5));
assertEquals(2, call.getTemplateParameters().size());
assertEquals(intType, ((TypeExpression) call.getTemplateParameters().get(0)).getType());
assertEquals(literal5, call.getTemplateParameters().get(1));
// Check return value
assertEquals(intType, call.getType());
}
use of de.fraunhofer.aisec.cpg.graph.types.ObjectType in project cpg by Fraunhofer-AISEC.
the class FunctionTemplateTest method testFunctionTemplateStructure.
@Test
void testFunctionTemplateStructure() throws Exception {
List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(Path.of(topLevel.toString(), "functionTemplate.cpp").toFile()), topLevel, true);
// This test checks the structure of FunctionTemplates without the TemplateExpansionPass
FunctionTemplateDeclaration functionTemplateDeclaration = TestUtils.subnodesOfType(result, FunctionTemplateDeclaration.class).get(0);
// Check FunctionTemplate Parameters
List<TypeParamDeclaration> typeParamDeclarations = TestUtils.subnodesOfType(result, TypeParamDeclaration.class);
assertEquals(1, typeParamDeclarations.size());
TypeParamDeclaration typeParamDeclaration = typeParamDeclarations.get(0);
assertEquals(typeParamDeclaration, functionTemplateDeclaration.getParameters().get(0));
ParameterizedType T = new ParameterizedType("T");
ObjectType intType = new ObjectType("int", Type.Storage.AUTO, new Type.Qualifier(), new ArrayList<>(), ObjectType.Modifier.SIGNED, true);
ObjectType floatType = new ObjectType("float", Type.Storage.AUTO, new Type.Qualifier(), new ArrayList<>(), ObjectType.Modifier.SIGNED, true);
assertEquals(T, typeParamDeclaration.getType());
assertEquals(intType, typeParamDeclaration.getDefault());
ParamVariableDeclaration N = TestUtils.findByUniqueName(TestUtils.subnodesOfType(result, ParamVariableDeclaration.class), "N");
Literal<Integer> int2 = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(2));
Literal<Integer> int3 = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(3));
Literal<Integer> int5 = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(5));
assertEquals(N, functionTemplateDeclaration.getParameters().get(1));
assertEquals(intType, N.getType());
assertEquals(5, ((Literal) N.getDefault()).getValue());
assertTrue(N.getPrevDFG().contains(int5));
assertTrue(N.getPrevDFG().contains(int3));
assertTrue(N.getPrevDFG().contains(int2));
// Check the realization
assertEquals(1, functionTemplateDeclaration.getRealization().size());
FunctionDeclaration fixed_multiply = functionTemplateDeclaration.getRealization().get(0);
assertEquals(T, fixed_multiply.getType());
ParamVariableDeclaration val = fixed_multiply.getParameters().get(0);
assertEquals(T, val.getType());
// Check the invokes
CallExpression callInt2 = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, CallExpression.class), c -> c.getLocation().getRegion().getStartLine() == 12);
assertEquals(1, callInt2.getInvokes().size());
assertEquals(fixed_multiply, callInt2.getInvokes().get(0));
CallExpression callFloat3 = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, CallExpression.class), c -> c.getLocation().getRegion().getStartLine() == 13);
assertEquals(1, callFloat3.getInvokes().size());
assertEquals(fixed_multiply, callFloat3.getInvokes().get(0));
// Check return values
assertEquals(intType, callInt2.getType());
assertEquals(floatType, callFloat3.getType());
// Check template arguments
testFunctionTemplateArguments(callFloat3, floatType, int3);
}
Aggregations