use of de.fraunhofer.aisec.cpg.sarif.Region in project cpg by Fraunhofer-AISEC.
the class CXXIncludeTest method testCodeAndRegionInInclude.
@Test
void testCodeAndRegionInInclude() throws Exception {
// checks, whether code and region for nodes in includes are properly set
File file = new File("src/test/resources/include.cpp");
TranslationUnitDeclaration tu = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
Set<RecordDeclaration> someClass = tu.getDeclarationsByName("SomeClass", RecordDeclaration.class);
assertFalse(someClass.isEmpty());
ConstructorDeclaration decl = someClass.iterator().next().getConstructors().get(0);
assertEquals("SomeClass();", decl.getCode());
PhysicalLocation location = decl.getLocation();
assertNotNull(location);
assertEquals(new Region(16, 3, 16, 15), location.getRegion());
}
use of de.fraunhofer.aisec.cpg.sarif.Region in project cpg by Fraunhofer-AISEC.
the class LocationConverterTest method toEntityAttributeWithInteger.
@Test
void toEntityAttributeWithInteger() {
// arrange
final CompositeAttributeConverter<PhysicalLocation> sut = getSut();
final Map<String, Object> value = new HashMap<>();
final int startLineValue = 1;
// autoboxing to Integer
value.put(LocationConverter.START_LINE, startLineValue);
final int endLineValue = 2;
value.put(LocationConverter.END_LINE, endLineValue);
final int startColumnValue = 3;
value.put(LocationConverter.START_COLUMN, startColumnValue);
final int endColumnValue = 4;
value.put(LocationConverter.END_COLUMN, endColumnValue);
value.put(LocationConverter.ARTIFACT, URI_STRING);
final Region region = new Region(startLineValue, startColumnValue, endLineValue, endColumnValue);
final PhysicalLocation want = new PhysicalLocation(URI_TO_TEST, region);
// act
final PhysicalLocation have = sut.toEntityAttribute(value);
// assert
Assertions.assertEquals(want, have);
}
use of de.fraunhofer.aisec.cpg.sarif.Region in project cpg by Fraunhofer-AISEC.
the class LocationConverterTest method toEntityAttributeWithMixedTypes.
@Test
void toEntityAttributeWithMixedTypes() {
// arrange
final CompositeAttributeConverter<PhysicalLocation> sut = getSut();
final Map<String, Object> value = new HashMap<>();
final Object startLineValue = 1;
value.put(LocationConverter.START_LINE, startLineValue);
final Object endLineValue = (long) 2;
value.put(LocationConverter.END_LINE, endLineValue);
final Object startColumnValue = "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);
}
use of de.fraunhofer.aisec.cpg.sarif.Region in project cpg by Fraunhofer-AISEC.
the class LocationConverterTest method toEntityAttributeWithLong.
@Test
void toEntityAttributeWithLong() {
// arrange
final CompositeAttributeConverter<PhysicalLocation> sut = getSut();
final Map<String, Object> value = new HashMap<>();
final long startLineValue = 1;
// autoboxing to Long
value.put(LocationConverter.START_LINE, startLineValue);
final long endLineValue = 2;
value.put(LocationConverter.END_LINE, endLineValue);
final long startColumnValue = 3;
value.put(LocationConverter.START_COLUMN, startColumnValue);
final long endColumnValue = 4;
value.put(LocationConverter.END_COLUMN, endColumnValue);
value.put(LocationConverter.ARTIFACT, URI_STRING);
final Region region = new Region((int) startLineValue, (int) startColumnValue, (int) endLineValue, (int) endColumnValue);
final PhysicalLocation want = new PhysicalLocation(URI_TO_TEST, region);
// act
final PhysicalLocation have = sut.toEntityAttribute(value);
// assert
Assertions.assertEquals(want, have);
}
use of de.fraunhofer.aisec.cpg.sarif.Region in project cpg by Fraunhofer-AISEC.
the class LanguageFrontend method mergeRegions.
/**
* Merges two regions. The new region contains both and is the minimal region to do so.
*
* @param regionOne the first region
* @param regionTwo the second region
* @return the merged region
*/
public Region mergeRegions(Region regionOne, Region regionTwo) {
Region ret = new Region();
if (regionOne.getStartLine() < regionTwo.getStartLine() || regionOne.getStartLine() == regionTwo.getStartLine() && regionOne.getStartColumn() < regionTwo.getStartColumn()) {
ret.setStartLine(regionOne.getStartLine());
ret.setStartColumn(regionOne.getStartColumn());
} else {
ret.setStartLine(regionTwo.getStartLine());
ret.setStartColumn(regionTwo.getStartColumn());
}
if (regionOne.getEndLine() > regionTwo.getEndLine() || regionOne.getEndLine() == regionTwo.getEndLine() && regionOne.getEndColumn() > regionTwo.getEndColumn()) {
ret.setEndLine(regionOne.getEndLine());
ret.setEndColumn(regionOne.getStartColumn());
} else {
ret.setEndLine(regionTwo.getEndLine());
ret.setEndColumn(regionTwo.getEndColumn());
}
return ret;
}
Aggregations