use of de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression in project cpg by Fraunhofer-AISEC.
the class VariableResolverJavaTest method initTests.
@BeforeAll
void initTests() throws ExecutionException, InterruptedException {
final String topLevelPath = "src/test/resources/variables_extended/java/";
List<String> fileNames = Arrays.asList("ScopeVariables.java", "ExternalClass.java");
List<File> fileLocations = fileNames.stream().map(fileName -> new File(topLevelPath + fileName)).collect(Collectors.toList());
TranslationConfiguration config = TranslationConfiguration.builder().sourceLocations(fileLocations.toArray(new File[fileNames.size()])).topLevel(new File(topLevelPath)).defaultPasses().defaultLanguages().debugParser(true).failOnError(true).build();
TranslationManager analyzer = TranslationManager.builder().config(config).build();
List<TranslationUnitDeclaration> tu = analyzer.analyze().get().getTranslationUnits();
List<Node> nodes = tu.stream().flatMap(tUnit -> SubgraphWalker.flattenAST(tUnit).stream()).collect(Collectors.toList());
List<CallExpression> calls = TestUtils.findByName(Util.filterCast(nodes, CallExpression.class), "printLog");
calls.sort(new NodeComparator());
List<RecordDeclaration> records = Util.filterCast(nodes, RecordDeclaration.class);
// Extract all Variable declarations and field declarations for matching
externalClass = TestUtils.getOfTypeWithName(nodes, RecordDeclaration.class, "variables_extended.ExternalClass");
externVarName = TestUtils.getSubnodeOfTypeWithName(externalClass, FieldDeclaration.class, "varName");
externStaticVarName = TestUtils.getSubnodeOfTypeWithName(externalClass, FieldDeclaration.class, "staticVarName");
outerClass = TestUtils.getOfTypeWithName(nodes, RecordDeclaration.class, "variables_extended.ScopeVariables");
outerVarName = outerClass.getFields().stream().filter(n -> n.getName().equals("varName")).findFirst().get();
outerStaticVarName = outerClass.getFields().stream().filter(n -> n.getName().equals("staticVarName")).findFirst().get();
outerImpThis = outerClass.getFields().stream().filter(n -> n.getName().equals("this")).findFirst().get();
// Inner class and its fields
innerClass = TestUtils.getOfTypeWithName(nodes, RecordDeclaration.class, "variables_extended.ScopeVariables.InnerClass");
innerVarName = innerClass.getFields().stream().filter(n -> n.getName().equals("varName")).findFirst().get();
innerStaticVarName = TestUtils.getSubnodeOfTypeWithName(innerClass, FieldDeclaration.class, "staticVarName");
innerImpThis = TestUtils.getSubnodeOfTypeWithName(innerClass, FieldDeclaration.class, "this");
innerImpOuter = TestUtils.getSubnodeOfTypeWithName(innerClass, FieldDeclaration.class, "ScopeVariables.this");
main = TestUtils.getSubnodeOfTypeWithName(outerClass, MethodDeclaration.class, "main");
outerFunction1 = outerClass.getMethods().stream().filter(method -> method.getName().equals("function1")).collect(Collectors.toList()).get(0);
forStatements = Util.filterCast(SubgraphWalker.flattenAST(outerFunction1), ForStatement.class);
// Functions i nthe outer and inner object
outerFunction2 = outerClass.getMethods().stream().filter(method -> method.getName().equals("function2")).collect(Collectors.toList()).get(0);
outerFunction3 = outerClass.getMethods().stream().filter(method -> method.getName().equals("function3")).collect(Collectors.toList()).get(0);
outerFunction4 = outerClass.getMethods().stream().filter(method -> method.getName().equals("function4")).collect(Collectors.toList()).get(0);
innerFunction1 = innerClass.getMethods().stream().filter(method -> method.getName().equals("function1")).collect(Collectors.toList()).get(0);
innerFunction2 = innerClass.getMethods().stream().filter(method -> method.getName().equals("function2")).collect(Collectors.toList()).get(0);
innerFunction3 = innerClass.getMethods().stream().filter(method -> method.getName().equals("function3")).collect(Collectors.toList()).get(0);
for (CallExpression call : calls) {
Expression first = call.getArguments().get(0);
String logId = ((Literal) first).getValue().toString();
Expression second = call.getArguments().get(1);
callParamMap.put(logId, second);
}
}
use of de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression in project cpg by Fraunhofer-AISEC.
the class DeclarationHandler method handleFieldDeclaration.
public de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration handleFieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration fieldDecl) {
// TODO: can field have more than one variable?
VariableDeclarator variable = fieldDecl.getVariable(0);
List<String> modifiers = fieldDecl.getModifiers().stream().map(modifier -> modifier.getKeyword().asString()).collect(Collectors.toList());
String joinedModifiers = String.join(" ", modifiers) + " ";
PhysicalLocation location = this.lang.getLocationFromRawNode(fieldDecl);
Expression initializer = (Expression) variable.getInitializer().map(this.lang.getExpressionHandler()::handle).orElse(null);
Type type;
try {
// Resolve type first with ParameterizedType
type = TypeManager.getInstance().getTypeParameter(this.lang.getScopeManager().getCurrentRecord(), variable.resolve().getType().describe());
if (type == null) {
type = TypeParser.createFrom(joinedModifiers + variable.resolve().getType().describe(), true);
}
} catch (UnsolvedSymbolException | UnsupportedOperationException e) {
String t = this.lang.recoverTypeFromUnsolvedException(e);
if (t == null) {
log.warn("Could not resolve type for {}", variable);
type = TypeParser.createFrom(joinedModifiers + variable.getType().asString(), true);
} else {
type = TypeParser.createFrom(joinedModifiers + t, true);
type.setTypeOrigin(Type.Origin.GUESSED);
}
}
de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration fieldDeclaration = newFieldDeclaration(variable.getName().asString(), type, modifiers, variable.toString(), location, initializer, false);
lang.getScopeManager().addDeclaration(fieldDeclaration);
this.lang.processAnnotations(fieldDeclaration, fieldDecl);
return fieldDeclaration;
}
use of de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression in project cpg by Fraunhofer-AISEC.
the class VariableResolverCppTest method initTests.
@BeforeAll
void initTests() throws ExecutionException, InterruptedException {
final String topLevelPath = "src/test/resources/variables_extended/cpp/";
List<String> fileNames = Arrays.asList("scope_variables.cpp", "external_class.cpp");
List<File> fileLocations = fileNames.stream().map(fileName -> new File(topLevelPath + fileName)).collect(Collectors.toList());
TranslationConfiguration config = TranslationConfiguration.builder().sourceLocations(fileLocations.toArray(new File[fileNames.size()])).topLevel(new File(topLevelPath)).defaultPasses().debugParser(true).defaultLanguages().failOnError(true).loadIncludes(true).build();
TranslationManager analyzer = TranslationManager.builder().config(config).build();
List<TranslationUnitDeclaration> tu = analyzer.analyze().get().getTranslationUnits();
List<Node> nodes = tu.stream().flatMap(tUnit -> SubgraphWalker.flattenAST(tUnit).stream()).collect(Collectors.toList());
List<CallExpression> calls = TestUtils.findByName(Util.filterCast(nodes, CallExpression.class), "printLog");
calls.sort(new NodeComparator());
List<RecordDeclaration> records = Util.filterCast(nodes, RecordDeclaration.class);
// Extract all Variable declarations and field declarations for matching
externalClass = TestUtils.getOfTypeWithName(nodes, RecordDeclaration.class, "ExternalClass");
externVarName = TestUtils.getSubnodeOfTypeWithName(externalClass, FieldDeclaration.class, "varName");
externStaticVarName = TestUtils.getSubnodeOfTypeWithName(externalClass, FieldDeclaration.class, "staticVarName");
outerClass = TestUtils.getOfTypeWithName(nodes, RecordDeclaration.class, "ScopeVariables");
outerVarName = outerClass.getFields().stream().filter(n -> n.getName().equals("varName")).findFirst().get();
outerStaticVarName = outerClass.getFields().stream().filter(n -> n.getName().equals("staticVarName")).findFirst().get();
outerImpThis = outerClass.getFields().stream().filter(n -> n.getName().equals("this")).findFirst().get();
List<RecordDeclaration> classes = Util.filterCast(nodes, RecordDeclaration.class);
// Inner class and its fields
innerClass = TestUtils.getOfTypeWithName(nodes, RecordDeclaration.class, "ScopeVariables::InnerClass");
innerVarName = innerClass.getFields().stream().filter(n -> n.getName().equals("varName")).findFirst().get();
innerStaticVarName = innerClass.getFields().stream().filter(n -> n.getName().equals("staticVarName")).findFirst().get();
innerImpThis = innerClass.getFields().stream().filter(n -> n.getName().equals("this")).findFirst().get();
main = TestUtils.getOfTypeWithName(nodes, FunctionDeclaration.class, "main");
// Functions in the outer and inner object
outerFunction1 = outerClass.getMethods().stream().filter(method -> method.getName().equals("function1")).collect(Collectors.toList()).get(0);
forStatements = Util.filterCast(SubgraphWalker.flattenAST(outerFunction1), ForStatement.class);
outerFunction2 = outerClass.getMethods().stream().filter(method -> method.getName().equals("function2")).collect(Collectors.toList()).get(0);
outerFunction3 = outerClass.getMethods().stream().filter(method -> method.getName().equals("function3")).collect(Collectors.toList()).get(0);
outerFunction4 = outerClass.getMethods().stream().filter(method -> method.getName().equals("function4")).collect(Collectors.toList()).get(0);
outerFunction5 = outerClass.getMethods().stream().filter(method -> method.getName().equals("function5")).collect(Collectors.toList()).get(0);
innerFunction1 = innerClass.getMethods().stream().filter(method -> method.getName().equals("function1")).collect(Collectors.toList()).get(0);
innerFunction2 = innerClass.getMethods().stream().filter(method -> method.getName().equals("function2")).collect(Collectors.toList()).get(0);
for (CallExpression call : calls) {
Expression first = call.getArguments().get(0);
String logId = ((Literal) first).getValue().toString();
Expression second = call.getArguments().get(1);
callParamMap.put(logId, second);
}
}
Aggregations