use of de.fraunhofer.aisec.cpg.graph.declarations.ParamVariableDeclaration in project cpg by Fraunhofer-AISEC.
the class VariableResolverCppTest method testParamVarNameAccessed.
@Test
void testParamVarNameAccessed() {
ParamVariableDeclaration declaration = TestUtils.getSubnodeOfTypeWithName(outerFunction2, ParamVariableDeclaration.class, "varName");
VRUtil.assertUsageOf(callParamMap.get("func2_param_varName"), declaration);
}
use of de.fraunhofer.aisec.cpg.graph.declarations.ParamVariableDeclaration in project cpg by Fraunhofer-AISEC.
the class DeclarationHandler method handleMethodDeclaration.
public de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration handleMethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration methodDecl) {
ResolvedMethodDeclaration resolvedMethod = methodDecl.resolve();
var record = lang.getScopeManager().getCurrentRecord();
de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration functionDeclaration = newMethodDeclaration(resolvedMethod.getName(), methodDecl.toString(), methodDecl.isStatic(), record);
// create the receiver
var receiver = newVariableDeclaration("this", record != null ? TypeParser.createFrom(record.getName(), false) : UnknownType.getUnknownType(), "this", false);
functionDeclaration.setReceiver(receiver);
lang.getScopeManager().enterScope(functionDeclaration);
functionDeclaration.addThrowTypes(methodDecl.getThrownExceptions().stream().map(type -> TypeParser.createFrom(type.asString(), true)).collect(Collectors.toList()));
for (Parameter parameter : methodDecl.getParameters()) {
Type resolvedType = TypeManager.getInstance().getTypeParameter(functionDeclaration.getRecordDeclaration(), parameter.getType().toString());
if (resolvedType == null) {
resolvedType = this.lang.getTypeAsGoodAsPossible(parameter, parameter.resolve());
}
ParamVariableDeclaration param = newMethodParameterIn(parameter.getNameAsString(), resolvedType, parameter.isVarArgs(), parameter.toString());
functionDeclaration.addParameter(param);
lang.setCodeAndRegion(param, parameter);
lang.processAnnotations(param, parameter);
lang.getScopeManager().addDeclaration(param);
}
functionDeclaration.setType(this.lang.getReturnTypeAsGoodAsPossible(methodDecl, resolvedMethod));
// check, if method has body (i.e. its not abstract or something)
Optional<BlockStmt> o = methodDecl.getBody();
if (o.isEmpty()) {
lang.getScopeManager().leaveScope(functionDeclaration);
return functionDeclaration;
}
BlockStmt body = o.get();
addImplicitReturn(body);
functionDeclaration.setBody(this.lang.getStatementHandler().handle(body));
lang.processAnnotations(functionDeclaration, methodDecl);
lang.getScopeManager().leaveScope(functionDeclaration);
return functionDeclaration;
}
use of de.fraunhofer.aisec.cpg.graph.declarations.ParamVariableDeclaration in project cpg by Fraunhofer-AISEC.
the class DeclarationHandler method handleConstructorDeclaration.
public de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration handleConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration constructorDecl) {
ResolvedConstructorDeclaration resolvedConstructor = constructorDecl.resolve();
de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration declaration = newConstructorDeclaration(resolvedConstructor.getName(), constructorDecl.toString(), lang.getScopeManager().getCurrentRecord());
lang.getScopeManager().addDeclaration(declaration);
lang.getScopeManager().enterScope(declaration);
declaration.addThrowTypes(constructorDecl.getThrownExceptions().stream().map(type -> TypeParser.createFrom(type.asString(), true)).collect(Collectors.toList()));
for (Parameter parameter : constructorDecl.getParameters()) {
ParamVariableDeclaration param = newMethodParameterIn(parameter.getNameAsString(), this.lang.getTypeAsGoodAsPossible(parameter, parameter.resolve()), parameter.isVarArgs(), parameter.toString());
declaration.addParameter(param);
lang.setCodeAndRegion(param, parameter);
lang.getScopeManager().addDeclaration(param);
}
Type type = TypeParser.createFrom(lang.getScopeManager().firstScopeOrNull(RecordScope.class::isInstance).getAstNode().getName(), true);
declaration.setType(type);
// check, if constructor has body (i.e. its not abstract or something)
BlockStmt body = constructorDecl.getBody();
addImplicitReturn(body);
declaration.setBody(this.lang.getStatementHandler().handle(body));
lang.processAnnotations(declaration, constructorDecl);
lang.getScopeManager().leaveScope(declaration);
return declaration;
}
use of de.fraunhofer.aisec.cpg.graph.declarations.ParamVariableDeclaration in project cpg by Fraunhofer-AISEC.
the class Util method createInferredParameters.
public static List<ParamVariableDeclaration> createInferredParameters(List<Type> signature) {
List<ParamVariableDeclaration> params = new ArrayList<>();
for (int i = 0; i < signature.size(); i++) {
Type targetType = signature.get(i);
String paramName = generateParamName(i, targetType);
ParamVariableDeclaration param = newMethodParameterIn(paramName, targetType, false, "");
param.setInferred(true);
param.setArgumentIndex(i);
params.add(param);
}
return params;
}
Aggregations