Search in sources :

Example 1 with TranslationResult

use of de.fraunhofer.aisec.cpg.TranslationResult 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);
}
Also used : TranslationResult(de.fraunhofer.aisec.cpg.TranslationResult) File(java.io.File) TranslationConfiguration(de.fraunhofer.aisec.cpg.TranslationConfiguration) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 2 with TranslationResult

use of de.fraunhofer.aisec.cpg.TranslationResult in project cpg by Fraunhofer-AISEC.

the class DemoTests method testPartial.

@Test
void testPartial() throws Exception {
    Path topLevel = Paths.get("src/test/resources/partial");
    File[] files = Files.walk(topLevel, Integer.MAX_VALUE).map(Path::toFile).filter(File::isFile).filter(f -> f.getName().endsWith(".java")).toArray(File[]::new);
    TranslationConfiguration config = TranslationConfiguration.builder().sourceLocations(files).topLevel(topLevel.toFile()).defaultPasses().defaultLanguages().debugParser(true).failOnError(true).build();
    TranslationManager analyzer = TranslationManager.builder().config(config).build();
    TranslationResult result = analyzer.analyze().get();
    for (Node node : result.getTranslationUnits()) {
        assertNotNull(node);
    }
}
Also used : Path(java.nio.file.Path) Test(org.junit.jupiter.api.Test) TranslationConfiguration(de.fraunhofer.aisec.cpg.TranslationConfiguration) Files(java.nio.file.Files) Paths(java.nio.file.Paths) Assertions(org.junit.jupiter.api.Assertions) TranslationManager(de.fraunhofer.aisec.cpg.TranslationManager) Node(de.fraunhofer.aisec.cpg.graph.Node) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Path(java.nio.file.Path) TranslationResult(de.fraunhofer.aisec.cpg.TranslationResult) File(java.io.File) Node(de.fraunhofer.aisec.cpg.graph.Node) TranslationManager(de.fraunhofer.aisec.cpg.TranslationManager) TranslationResult(de.fraunhofer.aisec.cpg.TranslationResult) File(java.io.File) TranslationConfiguration(de.fraunhofer.aisec.cpg.TranslationConfiguration) Test(org.junit.jupiter.api.Test) BaseTest(de.fraunhofer.aisec.cpg.BaseTest)

Example 3 with TranslationResult

use of de.fraunhofer.aisec.cpg.TranslationResult in project cpg by Fraunhofer-AISEC.

the class VariableUsageResolver method accept.

@Override
public void accept(TranslationResult result) {
    walker = new ScopedWalker(lang);
    for (TranslationUnitDeclaration tu : result.getTranslationUnits()) {
        currTu = tu;
        walker.clearCallbacks();
        walker.registerHandler((currClass, parent, currNode) -> walker.collectDeclarations(currNode));
        walker.registerHandler(this::findRecordsAndEnums);
        walker.iterate(currTu);
    }
    Map<Type, List<Type>> currSuperTypes = recordMap.values().stream().collect(Collectors.toMap(r -> TypeParser.createFrom(r.getName(), true), RecordDeclaration::getSuperTypes));
    superTypesMap.putAll(currSuperTypes);
    for (TranslationUnitDeclaration tu : result.getTranslationUnits()) {
        walker.clearCallbacks();
        walker.registerHandler(this::resolveFieldUsages);
        walker.iterate(tu);
    }
    for (TranslationUnitDeclaration tu : result.getTranslationUnits()) {
        walker.clearCallbacks();
        walker.registerHandler(this::resolveLocalVarUsage);
        walker.iterate(tu);
    }
}
Also used : Util.warnWithFileLocation(de.fraunhofer.aisec.cpg.helpers.Util.warnWithFileLocation) java.util(java.util) DeclaredReferenceExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) de.fraunhofer.aisec.cpg.graph(de.fraunhofer.aisec.cpg.graph) ScopedWalker(de.fraunhofer.aisec.cpg.helpers.SubgraphWalker.ScopedWalker) NodeBuilder.newRecordDeclaration(de.fraunhofer.aisec.cpg.graph.NodeBuilder.newRecordDeclaration) Collectors(java.util.stream.Collectors) TranslationResult(de.fraunhofer.aisec.cpg.TranslationResult) MemberCallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberCallExpression) Matcher(java.util.regex.Matcher) de.fraunhofer.aisec.cpg.graph.declarations(de.fraunhofer.aisec.cpg.graph.declarations) MemberExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression) Util(de.fraunhofer.aisec.cpg.helpers.Util) de.fraunhofer.aisec.cpg.graph.types(de.fraunhofer.aisec.cpg.graph.types) Pattern(java.util.regex.Pattern) JavaLanguageFrontend(de.fraunhofer.aisec.cpg.frontends.java.JavaLanguageFrontend) Nullable(org.checkerframework.checker.nullness.qual.Nullable) ScopedWalker(de.fraunhofer.aisec.cpg.helpers.SubgraphWalker.ScopedWalker)

Aggregations

TranslationResult (de.fraunhofer.aisec.cpg.TranslationResult)3 TranslationConfiguration (de.fraunhofer.aisec.cpg.TranslationConfiguration)2 File (java.io.File)2 BaseTest (de.fraunhofer.aisec.cpg.BaseTest)1 TranslationManager (de.fraunhofer.aisec.cpg.TranslationManager)1 JavaLanguageFrontend (de.fraunhofer.aisec.cpg.frontends.java.JavaLanguageFrontend)1 de.fraunhofer.aisec.cpg.graph (de.fraunhofer.aisec.cpg.graph)1 Node (de.fraunhofer.aisec.cpg.graph.Node)1 NodeBuilder.newRecordDeclaration (de.fraunhofer.aisec.cpg.graph.NodeBuilder.newRecordDeclaration)1 de.fraunhofer.aisec.cpg.graph.declarations (de.fraunhofer.aisec.cpg.graph.declarations)1 TranslationUnitDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration)1 DeclaredReferenceExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression)1 MemberCallExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberCallExpression)1 MemberExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression)1 de.fraunhofer.aisec.cpg.graph.types (de.fraunhofer.aisec.cpg.graph.types)1 ScopedWalker (de.fraunhofer.aisec.cpg.helpers.SubgraphWalker.ScopedWalker)1 Util (de.fraunhofer.aisec.cpg.helpers.Util)1 Util.warnWithFileLocation (de.fraunhofer.aisec.cpg.helpers.Util.warnWithFileLocation)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1