use of de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration in project cpg by Fraunhofer-AISEC.
the class TypedefTest method testMultiple.
@Test
void testMultiple() throws Exception {
List<TranslationUnitDeclaration> result = TestUtils.analyze("cpp", topLevel, true);
List<ValueDeclaration> variables = TestUtils.subnodesOfType(result, ValueDeclaration.class);
// simple type
ValueDeclaration i1 = TestUtils.findByUniqueName(variables, "i1");
ValueDeclaration i2 = TestUtils.findByUniqueName(variables, "i2");
assertEquals(i1.getType(), i2.getType());
// array
ValueDeclaration a1 = TestUtils.findByUniqueName(variables, "a1");
ValueDeclaration a2 = TestUtils.findByUniqueName(variables, "a2");
assertEquals(a1.getType(), a2.getType());
// pointer
ValueDeclaration intPtr1 = TestUtils.findByUniqueName(variables, "intPtr1");
ValueDeclaration intPtr2 = TestUtils.findByUniqueName(variables, "intPtr2");
assertEquals(intPtr1.getType(), intPtr2.getType());
// function pointer
ValueDeclaration fPtr1 = TestUtils.findByUniqueName(variables, "intFptr1");
ValueDeclaration fPtr2 = TestUtils.findByUniqueName(variables, "intFptr2");
assertEquals(fPtr1.getType(), fPtr2.getType());
}
use of de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration in project cpg by Fraunhofer-AISEC.
the class TypedefTest method testMemberTypeDef.
@Test
void testMemberTypeDef() throws Exception {
List<TranslationUnitDeclaration> result = TestUtils.analyze("cpp", topLevel, true);
List<ValueDeclaration> variables = TestUtils.subnodesOfType(result, ValueDeclaration.class);
List<RecordDeclaration> records = TestUtils.subnodesOfType(result, RecordDeclaration.class);
RecordDeclaration addConst = TestUtils.findByUniqueName(records, "add_const");
ValueDeclaration typeMember1 = TestUtils.findByUniqueName(addConst.getFields(), "typeMember1");
ValueDeclaration typeMember2 = TestUtils.findByUniqueName(addConst.getFields(), "typeMember2");
assertEquals(typeMember1.getType(), typeMember2.getType());
ValueDeclaration typeMemberOutside = TestUtils.findByUniqueName(variables, "typeMemberOutside");
assertNotEquals(typeMemberOutside.getType(), typeMember2.getType());
ValueDeclaration cptr1 = TestUtils.findByUniqueName(variables, "cptr1");
ValueDeclaration cptr2 = TestUtils.findByUniqueName(variables, "cptr2");
assertEquals(cptr1.getType(), cptr2.getType());
assertNotEquals(typeMemberOutside.getType(), cptr2.getType());
}
use of de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration in project cpg by Fraunhofer-AISEC.
the class TypedefTest method testWithModifier.
@Test
void testWithModifier() throws Exception {
List<TranslationUnitDeclaration> result = TestUtils.analyze("cpp", topLevel, true);
List<ValueDeclaration> variables = TestUtils.subnodesOfType(result, ValueDeclaration.class);
// pointer
ValueDeclaration l1ptr = TestUtils.findByUniqueName(variables, "l1ptr");
ValueDeclaration l2ptr = TestUtils.findByUniqueName(variables, "l2ptr");
ValueDeclaration l3ptr = TestUtils.findByUniqueName(variables, "l3ptr");
ValueDeclaration l4ptr = TestUtils.findByUniqueName(variables, "l4ptr");
assertEquals(l1ptr.getType(), l2ptr.getType());
assertEquals(l1ptr.getType(), l3ptr.getType());
assertEquals(l1ptr.getType(), l4ptr.getType());
// arrays
ValueDeclaration l1arr = TestUtils.findByUniqueName(variables, "l1arr");
ValueDeclaration l2arr = TestUtils.findByUniqueName(variables, "l2arr");
ValueDeclaration l3arr = TestUtils.findByUniqueName(variables, "l3arr");
ValueDeclaration l4arr = TestUtils.findByUniqueName(variables, "l4arr");
assertTrue(TypeManager.getInstance().checkArrayAndPointer(l1arr.getType(), l2arr.getType()));
assertTrue(TypeManager.getInstance().checkArrayAndPointer(l1arr.getType(), l3arr.getType()));
assertTrue(TypeManager.getInstance().checkArrayAndPointer(l1arr.getType(), l4arr.getType()));
}
use of de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration in project cpg by Fraunhofer-AISEC.
the class VisitorTest method setup.
@BeforeAll
public static void setup() throws TranslationException, InterruptedException, ExecutionException, TimeoutException {
File file = new File("src/test/resources/compiling/RecordDeclaration.java");
TranslationConfiguration config = TranslationConfiguration.builder().sourceLocations(file).defaultPasses().defaultLanguages().build();
TranslationResult result = TranslationManager.builder().config(config).build().analyze().get(20, TimeUnit.SECONDS);
TranslationUnitDeclaration tu = result.getTranslationUnits().get(0);
namespace = (NamespaceDeclaration) tu.getDeclarations().get(0);
}
use of de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration in project cpg by Fraunhofer-AISEC.
the class ConstructorsTest method testCPPFullDefault.
@Test
void testCPPFullDefault() throws Exception {
List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(Path.of(topLevel.toString(), "defaultarg", "constructorDefault.cpp").toFile()), topLevel, true);
List<ConstructorDeclaration> constructors = TestUtils.subnodesOfType(result, ConstructorDeclaration.class);
List<VariableDeclaration> variables = TestUtils.subnodesOfType(result, VariableDeclaration.class);
ConstructorDeclaration twoDefaultArg = TestUtils.findByUniquePredicate(constructors, c -> c.getDefaultParameters().size() == 2 && c.getName().equals("D"));
Literal<?> literal0 = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(0));
Literal<?> literal1 = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(1));
VariableDeclaration d1 = TestUtils.findByUniqueName(variables, "d1");
assertTrue(d1.getInitializer() instanceof ConstructExpression);
ConstructExpression d1Initializer = (ConstructExpression) d1.getInitializer();
assertEquals(twoDefaultArg, d1Initializer.getConstructor());
assertEquals(0, d1Initializer.getArguments().size());
assertTrue(twoDefaultArg.getNextEOG().contains(literal0));
assertTrue(twoDefaultArg.getNextEOG().contains(literal1));
assertTrue(literal0.getNextEOG().contains(literal1));
for (Node node : twoDefaultArg.getNextEOG()) {
if (!(node.equals(literal0) || node.equals(literal1))) {
assertTrue(literal1.getNextEOG().contains(node));
}
}
VariableDeclaration d2 = TestUtils.findByUniqueName(variables, "d2");
assertTrue(d2.getInitializer() instanceof ConstructExpression);
ConstructExpression d2Initializer = (ConstructExpression) d2.getInitializer();
assertEquals(twoDefaultArg, d2Initializer.getConstructor());
assertEquals(1, d2Initializer.getArguments().size());
assertEquals(2, ((Literal) d2Initializer.getArguments().get(0)).getValue());
VariableDeclaration d3 = TestUtils.findByUniqueName(variables, "d3");
assertTrue(d3.getInitializer() instanceof ConstructExpression);
ConstructExpression d3Initializer = (ConstructExpression) d3.getInitializer();
assertEquals(twoDefaultArg, d3Initializer.getConstructor());
assertEquals(2, d3Initializer.getArguments().size());
assertEquals(3, ((Literal) d3Initializer.getArguments().get(0)).getValue());
assertEquals(4, ((Literal) d3Initializer.getArguments().get(1)).getValue());
}
Aggregations