use of de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration in project cpg by Fraunhofer-AISEC.
the class CXXLiteralTest method testDecimalIntegerLiterals.
@Test
void testDecimalIntegerLiterals() throws Exception {
File file = new File("src/test/resources/integer_literals.cpp");
TranslationUnitDeclaration tu = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
Set<FunctionDeclaration> decimal = tu.getDeclarationsByName("decimal", FunctionDeclaration.class);
assertFalse(decimal.isEmpty());
FunctionDeclaration funcDecl = decimal.iterator().next();
assertEquals("decimal", funcDecl.getName());
assertLiteral(42, TypeParser.createFrom("int", true), funcDecl, "i");
assertLiteral(9223372036854775807L, TypeParser.createFrom("long", true), funcDecl, "l");
assertLiteral(9223372036854775807L, TypeParser.createFrom("long", true), funcDecl, "l_with_suffix");
assertLiteral(9223372036854775807L, TypeParser.createFrom("long long", true), funcDecl, "l_long_long_with_suffix");
assertLiteral(new BigInteger("9223372036854775809"), TypeParser.createFrom("unsigned long", true), funcDecl, "l_unsigned_long_with_suffix");
assertLiteral(new BigInteger("9223372036854775808"), TypeParser.createFrom("unsigned long long", true), funcDecl, "l_long_long_implicit");
assertLiteral(new BigInteger("9223372036854775809"), TypeParser.createFrom("unsigned long long", true), funcDecl, "l_unsigned_long_long_with_suffix");
}
use of de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration in project cpg by Fraunhofer-AISEC.
the class CXXLiteralTest method testLargeNegativeNumber.
@Test
void testLargeNegativeNumber() throws Exception {
File file = new File("src/test/resources/largenegativenumber.cpp");
TranslationUnitDeclaration tu = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
Set<FunctionDeclaration> main = tu.getDeclarationsByName("main", FunctionDeclaration.class);
assertFalse(main.isEmpty());
FunctionDeclaration funcDecl = main.iterator().next();
VariableDeclaration a = funcDecl.getVariableDeclarationByName("a").orElse(null);
assertNotNull(a);
assertEquals(1, ((Literal) Objects.requireNonNull(a.getInitializerAs(UnaryOperator.class)).getInput()).getValue());
// there are no negative literals, so the construct "-2147483648" is
// a unary expression and the literal "2147483648". Since "2147483648" is too large to fit
// in an integer, it should be automatically converted to a long. The resulting value
// -2147483648 however is small enough to fit into an int, so it is ok for the variable a to
// have an int type
VariableDeclaration b = funcDecl.getVariableDeclarationByName("b").orElse(null);
assertNotNull(b);
assertEquals(2147483648L, ((Literal) Objects.requireNonNull(b.getInitializerAs(UnaryOperator.class)).getInput()).getValue());
VariableDeclaration c = funcDecl.getVariableDeclarationByName("c").orElse(null);
assertNotNull(c);
assertEquals(2147483649L, ((Literal) Objects.requireNonNull(c.getInitializerAs(UnaryOperator.class)).getInput()).getValue());
VariableDeclaration d = funcDecl.getVariableDeclarationByName("d").orElse(null);
assertNotNull(d);
assertEquals(new BigInteger("9223372036854775808"), ((Literal) Objects.requireNonNull(d.getInitializerAs(UnaryOperator.class)).getInput()).getValue());
}
use of de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration in project cpg by Fraunhofer-AISEC.
the class CallExpression method typeChanged.
@Override
public void typeChanged(HasType src, HasType root, Type oldType) {
if (!TypeManager.isTypeSystemActive()) {
return;
}
if (src == base) {
setFqn(src.getType().getRoot().getTypeName() + "." + this.getName());
} else {
Type previous = this.type;
List<Type> types = invokes.stream().map(PropertyEdge::getEnd).map(FunctionDeclaration::getType).filter(Objects::nonNull).collect(Collectors.toList());
Type alternative = !types.isEmpty() ? types.get(0) : null;
Type commonType = TypeManager.getInstance().getCommonType(types).orElse(alternative);
Set<Type> subTypes = new HashSet<>(getPossibleSubTypes());
subTypes.remove(oldType);
subTypes.addAll(types);
setType(commonType, root);
setPossibleSubTypes(subTypes, root);
if (!previous.equals(this.type)) {
this.type.setTypeOrigin(Type.Origin.DATAFLOW);
}
}
}
use of de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration 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.FunctionDeclaration in project cpg by Fraunhofer-AISEC.
the class FunctionPointerTest method test.
void test(String language) throws Exception {
List<TranslationUnitDeclaration> result = analyze(language);
List<FunctionDeclaration> functions = TestUtils.subnodesOfType(result, FunctionDeclaration.class);
FunctionDeclaration main = TestUtils.findByUniqueName(functions, "main");
List<CallExpression> calls = TestUtils.subnodesOfType(main, CallExpression.class);
FunctionDeclaration noParam = TestUtils.findByUniquePredicate(functions, f -> f.getName().equals("target") && f.getParameters().isEmpty());
FunctionDeclaration singleParam = TestUtils.findByUniquePredicate(functions, f -> f.getName().equals("target") && f.getParameters().size() == 1);
FunctionDeclaration noParamUnknown = TestUtils.findByUniquePredicate(functions, f -> f.getName().equals("fun") && f.getParameters().isEmpty());
FunctionDeclaration singleParamUnknown = TestUtils.findByUniquePredicate(functions, f -> f.getName().equals("fun") && f.getParameters().size() == 1);
Pattern pattern = Pattern.compile("\\((?<member>.+)?\\*(?<obj>.+\\.)?(?<func>.+)\\)");
for (CallExpression call : calls) {
if (call instanceof ConstructExpression) {
continue;
}
String func;
if (!call.getName().contains("(")) {
func = call.getName();
assertNotEquals("", func, "Unexpected call " + func);
} else {
Matcher matcher = pattern.matcher(call.getName());
assertTrue(matcher.matches(), "Unexpected call " + call.getName());
func = matcher.group("func");
}
switch(func) {
case "no_param":
case "no_param_uninitialized":
case "no_param_field":
case "no_param_field_uninitialized":
assertEquals(List.of(noParam), call.getInvokes());
break;
case "single_param":
case "single_param_uninitialized":
case "single_param_field":
case "single_param_field_uninitialized":
assertEquals(List.of(singleParam), call.getInvokes());
break;
case "no_param_unknown":
case "no_param_unknown_uninitialized":
case "no_param_unknown_field":
case "no_param_unknown_field_uninitialized":
assertEquals(List.of(noParamUnknown), call.getInvokes());
assertTrue(noParamUnknown.isInferred());
break;
case "single_param_unknown":
case "single_param_unknown_uninitialized":
case "single_param_unknown_field":
case "single_param_unknown_field_uninitialized":
assertEquals(List.of(singleParamUnknown), call.getInvokes());
assertTrue(singleParamUnknown.isInferred());
break;
default:
fail("Unexpected call " + call.getName());
}
List<VariableDeclaration> variables = TestUtils.subnodesOfType(result, VariableDeclaration.class);
for (VariableDeclaration variable : variables) {
switch(variable.getName()) {
case "no_param_unused":
case "no_param_unused_field":
case "no_param_unused_uninitialized":
assertEquals(noParam, getSourceFunction(variable));
break;
case "single_param_unused":
case "single_param_unused_field":
case "single_param_unused_field_uninitialized":
assertEquals(singleParam, getSourceFunction(variable));
break;
}
}
}
}
Aggregations