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;
}
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;
}
}
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()));
}
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());
}
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);
}
Aggregations