Search in sources :

Example 6 with Region

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

the class StatementHandler method getLocationsFromTokens.

@Nullable
public PhysicalLocation getLocationsFromTokens(PhysicalLocation parentLocation, JavaToken startToken, JavaToken endToken) {
    // cannot construct location without parent location
    if (parentLocation == null) {
        return null;
    }
    if (startToken != null && endToken != null) {
        Optional<Range> startOpt = startToken.getRange();
        Optional<Range> endOpt = endToken.getRange();
        if (startOpt.isPresent() && endOpt.isPresent()) {
            Range rstart = startOpt.get();
            Range rend = endOpt.get();
            Region region = new Region(rstart.begin.line, rstart.begin.column, rend.end.line, rend.end.column + 1);
            return new PhysicalLocation(parentLocation.getArtifactLocation().getUri(), region);
        }
    }
    return null;
}
Also used : Region(de.fraunhofer.aisec.cpg.sarif.Region) Range(com.github.javaparser.Range) TokenRange(com.github.javaparser.TokenRange) PhysicalLocation(de.fraunhofer.aisec.cpg.sarif.PhysicalLocation) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 7 with Region

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

the class LocationConverter method toEntityAttribute.

@Override
public PhysicalLocation toEntityAttribute(Map<String, ?> value) {
    try {
        final int startLine = toInt(value.get(START_LINE));
        final int endLine = toInt(value.get(END_LINE));
        final int startColumn = toInt(value.get(START_COLUMN));
        final int endColumn = toInt(value.get(END_COLUMN));
        final URI uri = URI.create((String) value.get(ARTIFACT));
        return new PhysicalLocation(uri, new Region(startLine, startColumn, endLine, endColumn));
    } catch (NullPointerException e) {
        return null;
    }
}
Also used : Region(de.fraunhofer.aisec.cpg.sarif.Region) URI(java.net.URI) PhysicalLocation(de.fraunhofer.aisec.cpg.sarif.PhysicalLocation)

Example 8 with Region

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

the class CXXLanguageFrontendTest method testRegionsCfg.

@Test
void testRegionsCfg() throws Exception {
    File file = new File("src/test/resources/cfg.cpp");
    TranslationUnitDeclaration declaration = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
    FunctionDeclaration fdecl = declaration.getDeclarationAs(0, FunctionDeclaration.class);
    CompoundStatement body = (CompoundStatement) fdecl.getBody();
    Map<String, Region> expected = new HashMap<>();
    expected.put("cout << \"bla\";", new Region(4, 3, 4, 17));
    expected.put("cout << \"blubb\";", new Region(5, 3, 5, 19));
    expected.put("return 0;", new Region(15, 3, 15, 12));
    for (Node d : body.getStatements()) {
        if (expected.containsKey(d.getCode())) {
            assertEquals(expected.get(d.getCode()), d.getLocation().getRegion(), d.getCode());
            expected.remove(d.getCode());
        }
    }
    assertTrue(expected.isEmpty(), String.join(", ", expected.keySet()));
}
Also used : FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) CompoundStatement(de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement) Node(de.fraunhofer.aisec.cpg.graph.Node) Region(de.fraunhofer.aisec.cpg.sarif.Region) File(java.io.File) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 9 with Region

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

the class CXXLanguageFrontendTest method testLocation.

@Test
void testLocation() throws Exception {
    File file = new File("src/test/resources/components/foreachstmt.cpp");
    TranslationUnitDeclaration tu = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
    Set<FunctionDeclaration> main = tu.getDeclarationsByName("main", FunctionDeclaration.class);
    assertFalse(main.isEmpty());
    PhysicalLocation location = main.iterator().next().getLocation();
    assertNotNull(location);
    Path path = Path.of(location.getArtifactLocation().getUri());
    assertEquals("foreachstmt.cpp", path.getFileName().toString());
    assertEquals(new Region(4, 1, 8, 2), location.getRegion());
}
Also used : Path(java.nio.file.Path) FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) Region(de.fraunhofer.aisec.cpg.sarif.Region) File(java.io.File) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) PhysicalLocation(de.fraunhofer.aisec.cpg.sarif.PhysicalLocation) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 10 with Region

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

the class LocationConverterTest method toEntityAttributeWithCustomTypes.

@Test
void toEntityAttributeWithCustomTypes() {
    // arrange
    final CompositeAttributeConverter<PhysicalLocation> sut = getSut();
    final Map<String, Object> value = new HashMap<>();
    final Object startLineValue = new CustomNumber(1);
    value.put(LocationConverter.START_LINE, startLineValue);
    final Object endLineValue = new CustomNumber(2);
    value.put(LocationConverter.END_LINE, endLineValue);
    final Object startColumnValue = new CustomNumber(3);
    value.put(LocationConverter.START_COLUMN, startColumnValue);
    final Object endColumnValue = new CustomNumber(4);
    value.put(LocationConverter.END_COLUMN, endColumnValue);
    value.put(LocationConverter.ARTIFACT, URI_STRING);
    final Region region = new Region(Integer.parseInt(startLineValue.toString()), Integer.parseInt(startColumnValue.toString()), Integer.parseInt(endLineValue.toString()), Integer.parseInt(endColumnValue.toString()));
    final PhysicalLocation want = new PhysicalLocation(URI_TO_TEST, region);
    // act
    final PhysicalLocation have = sut.toEntityAttribute(value);
    // assert
    Assertions.assertEquals(want, have);
}
Also used : HashMap(java.util.HashMap) Region(de.fraunhofer.aisec.cpg.sarif.Region) PhysicalLocation(de.fraunhofer.aisec.cpg.sarif.PhysicalLocation) Test(org.junit.jupiter.api.Test)

Aggregations

Region (de.fraunhofer.aisec.cpg.sarif.Region)12 PhysicalLocation (de.fraunhofer.aisec.cpg.sarif.PhysicalLocation)10 Test (org.junit.jupiter.api.Test)9 HashMap (java.util.HashMap)6 BaseTest (de.fraunhofer.aisec.cpg.BaseTest)3 File (java.io.File)3 FunctionDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration)2 TranslationUnitDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration)2 Range (com.github.javaparser.Range)1 TokenRange (com.github.javaparser.TokenRange)1 Node (de.fraunhofer.aisec.cpg.graph.Node)1 CompoundStatement (de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement)1 URI (java.net.URI)1 Path (java.nio.file.Path)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1