use of de.fraunhofer.aisec.cpg.graph.types.PointerType in project cpg by Fraunhofer-AISEC.
the class ClassTemplateTest method testReferenceInTemplates.
@Test
void testReferenceInTemplates() throws Exception {
// Test array.cpp: checks usage of referencetype of parameterized type (T[])
TypeManager.reset();
List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(Path.of(topLevel.toString(), "array.cpp").toFile()), topLevel, true);
ClassTemplateDeclaration template = TestUtils.findByUniqueName(TestUtils.subnodesOfType(result, ClassTemplateDeclaration.class), "template<typename T, int N=10> class Array");
RecordDeclaration array = TestUtils.findByUniqueName(TestUtils.subnodesOfType(result, RecordDeclaration.class), "Array");
ParamVariableDeclaration N = TestUtils.findByUniqueName(TestUtils.subnodesOfType(result, ParamVariableDeclaration.class), "N");
TypeParamDeclaration T = TestUtils.findByUniqueName(TestUtils.subnodesOfType(result, TypeParamDeclaration.class), "typename T");
Literal<?> literal10 = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(10));
FieldDeclaration thisField = TestUtils.findByUniqueName(TestUtils.subnodesOfType(result, FieldDeclaration.class), "this");
FieldDeclaration m_Array = TestUtils.findByUniqueName(TestUtils.subnodesOfType(result, FieldDeclaration.class), "m_Array");
assertEquals(2, template.getParameters().size());
assertEquals(T, template.getParameters().get(0));
assertEquals(N, template.getParameters().get(1));
assertEquals(literal10, N.getDefault());
assertEquals(2, array.getFields().size());
assertEquals(thisField, array.getFields().get(0));
assertEquals(m_Array, array.getFields().get(1));
ObjectType arrayType = (ObjectType) thisField.getType();
assertEquals(1, arrayType.getGenerics().size());
assertEquals("T", arrayType.getGenerics().get(0).getName());
ParameterizedType typeT = (ParameterizedType) arrayType.getGenerics().get(0);
assertEquals(typeT, T.getType());
assertTrue(m_Array.getType() instanceof PointerType);
PointerType tArray = (PointerType) m_Array.getType();
assertEquals(typeT, tArray.getElementType());
ConstructExpression constructExpression = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, ConstructExpression.class), c -> c.getCode().equals("()"));
assertEquals(template, constructExpression.getTemplateInstantiation());
assertEquals(array, constructExpression.getInstantiates());
assertEquals("int", constructExpression.getTemplateParameters().get(0).getName());
assertEquals(literal10, constructExpression.getTemplateParameters().get(1));
assertEquals("Array", constructExpression.getType().getName());
ObjectType instantiatedType = (ObjectType) constructExpression.getType();
assertEquals(1, instantiatedType.getGenerics().size());
assertEquals("int", instantiatedType.getGenerics().get(0).getName());
}
use of de.fraunhofer.aisec.cpg.graph.types.PointerType in project cpg by Fraunhofer-AISEC.
the class ExpressionHandler method handleVariableDeclarationExpr.
// Not sure how to handle this exactly yet
private DeclarationStatement handleVariableDeclarationExpr(Expression expr) {
VariableDeclarationExpr variableDeclarationExpr = expr.asVariableDeclarationExpr();
DeclarationStatement declarationStatement = NodeBuilder.newDeclarationStatement(variableDeclarationExpr.toString());
for (VariableDeclarator variable : variableDeclarationExpr.getVariables()) {
ResolvedValueDeclaration resolved = variable.resolve();
Type declarationType = this.lang.getTypeAsGoodAsPossible(variable, resolved);
declarationType.setAdditionalTypeKeywords(variableDeclarationExpr.getModifiers().stream().map(m -> m.getKeyword().asString()).collect(Collectors.joining(" ")));
VariableDeclaration declaration = NodeBuilder.newVariableDeclaration(resolved.getName(), declarationType, variable.toString(), false, lang, variable);
if (declarationType instanceof PointerType && ((PointerType) declarationType).isArray()) {
declaration.setIsArray(true);
}
Optional<Expression> oInitializer = variable.getInitializer();
if (oInitializer.isPresent()) {
de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression initializer = (de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression) handle(oInitializer.get());
if (initializer instanceof ArrayCreationExpression) {
declaration.setIsArray(true);
}
declaration.setInitializer(initializer);
} else {
de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression uninitialzedInitializer = new UninitializedValue();
declaration.setInitializer(uninitialzedInitializer);
}
lang.setCodeAndRegion(declaration, variable);
declarationStatement.addToPropertyEdgeDeclaration(declaration);
lang.processAnnotations(declaration, variableDeclarationExpr);
lang.getScopeManager().addDeclaration(declaration);
}
return declarationStatement;
}
use of de.fraunhofer.aisec.cpg.graph.types.PointerType in project cpg by Fraunhofer-AISEC.
the class Util method generateParamName.
private static String generateParamName(int i, @NonNull Type targetType) {
Deque<String> hierarchy = new ArrayDeque<>();
Type currLevel = targetType;
while (currLevel != null) {
if (currLevel instanceof FunctionPointerType) {
hierarchy.push("Fptr");
currLevel = null;
} else if (currLevel instanceof PointerType) {
hierarchy.push("Ptr");
currLevel = ((PointerType) currLevel).getElementType();
} else if (currLevel instanceof ReferenceType) {
hierarchy.push("Ref");
currLevel = ((ReferenceType) currLevel).getElementType();
} else {
hierarchy.push(currLevel.getTypeName());
currLevel = null;
}
}
StringBuilder paramName = new StringBuilder();
while (!hierarchy.isEmpty()) {
String part = hierarchy.pop();
if (part.isEmpty()) {
continue;
}
if (paramName.length() > 0) {
paramName.append(part.substring(0, 1).toUpperCase());
if (part.length() >= 2) {
paramName.append(part.substring(1));
}
} else {
paramName.append(part.toLowerCase());
}
}
paramName.append(i);
return paramName.toString();
}
Aggregations