use of com.progress.xref.CrossReference in project sonar-openedge by Riverside-Software.
the class OpenEdgeProparseSensor method parseMainFile.
@SuppressWarnings({ "unchecked", "rawtypes" })
private void parseMainFile(SensorContext context, InputFile file, IProparseEnvironment session) {
CrossReference xref = null;
Document doc = null;
if (context.runtime().getProduct() == SonarProduct.SONARQUBE) {
long startTime = System.currentTimeMillis();
xref = CrossReferenceUtils.parseXREF(settings.getXrefFile(file));
if (settings.parseXrefDocument())
doc = parseXREF(settings.getXrefFile(file));
xmlParseTime += (System.currentTimeMillis() - startTime);
} else if (context.runtime().getProduct() == SonarProduct.SONARLINT) {
long startTime = System.currentTimeMillis();
xref = CrossReferenceUtils.parseXREF(settings.getSonarlintXrefFile(file));
if (settings.parseXrefDocument())
doc = parseXREF(settings.getSonarlintXrefFile(file));
xmlParseTime += (System.currentTimeMillis() - startTime);
settings.parseHierarchy(file);
}
if (!xref.getSource().isEmpty())
numXREF++;
File listingFile = settings.getListingFile(file);
List<Integer> trxBlocks = new ArrayList<>();
if ((listingFile != null) && listingFile.exists() && (listingFile.getAbsolutePath().indexOf(' ') == -1)) {
try {
ListingParser parser = new ListingParser(listingFile.toPath(), InputFileUtils.getRelativePath(file, context.fileSystem()));
for (CodeBlock block : parser.getTransactionBlocks()) {
trxBlocks.add(block.getLineNumber());
}
numListings++;
} catch (IOException caught) {
LOG.error("Unable to parse listing file for " + file, caught);
}
} else {
LOG.debug("Listing file for '{}' not found or contains space character - Was looking for '{}'", file, listingFile);
}
// Shared objects
long numShrTT = xref.getSource().stream().mapToLong(src -> src.getReference().stream().filter(ref -> "NEW-SHR-TEMPTABLE".equalsIgnoreCase(ref.getReferenceType())).count()).sum();
long numShrDS = xref.getSource().stream().mapToLong(src -> src.getReference().stream().filter(ref -> "NEW-SHR-DATASET".equalsIgnoreCase(ref.getReferenceType())).count()).sum();
long numShrVar = xref.getSource().stream().mapToLong(src -> src.getReference().stream().filter(ref -> "NEW-SHR-VARIABLE".equalsIgnoreCase(ref.getReferenceType())).count()).sum();
context.newMeasure().on(file).forMetric((Metric) OpenEdgeMetrics.NUM_TRANSACTIONS).withValue(trxBlocks.size()).save();
context.newMeasure().on(file).forMetric((Metric) OpenEdgeMetrics.SHR_TT).withValue((int) numShrTT).save();
context.newMeasure().on(file).forMetric((Metric) OpenEdgeMetrics.SHR_DS).withValue((int) numShrDS).save();
context.newMeasure().on(file).forMetric((Metric) OpenEdgeMetrics.SHR_VAR).withValue((int) numShrVar).save();
ParseUnit unit = null;
long startTime = System.currentTimeMillis();
try {
unit = new ParseUnit(InputFileUtils.getInputStream(file), InputFileUtils.getRelativePath(file, context.fileSystem()), session);
unit.attachXref(doc);
unit.attachXref(xref);
unit.parse();
unit.treeParser01();
for (Class<? extends ProparseListener> clz : components.getProparseListeners()) {
Injector injector = Guice.createInjector(new TreeParserModule(clz, unit));
ProparseListener listener = injector.getInstance(ProparseListener.class);
unit.treeParser(listener);
}
unit.attachTransactionBlocks(trxBlocks);
unit.attachTypeInfo(session.getTypeInfo(unit.getClassName()));
updateParseTime(System.currentTimeMillis() - startTime);
} catch (UncheckedIOException caught) {
numFailures++;
if (caught.getCause() instanceof XCodedFileException) {
XCodedFileException cause = (XCodedFileException) caught.getCause();
LOG.error("Unable to parse {} - Can't read xcode'd file {}", file, cause.getFileName());
} else if (caught.getCause() instanceof IncludeFileNotFoundException) {
IncludeFileNotFoundException cause = (IncludeFileNotFoundException) caught.getCause();
LOG.error("Unable to parse {} - Can't find include file '{}' from '{}'", file, cause.getIncludeName(), cause.getFileName());
} else {
LOG.error("Unable to parse " + file + " - IOException was caught - Please report this issue", caught);
}
return;
} catch (ParseCancellationException caught) {
RecognitionException cause = (RecognitionException) caught.getCause();
ProToken tok = (ProToken) cause.getOffendingToken();
if (settings.displayStackTraceOnError()) {
LOG.error("Parser error in '" + file + "' at position " + tok.getFileName() + ":" + tok.getLine() + ":" + tok.getCharPositionInLine(), cause);
} else {
LOG.error("Parser error in '{}' at position {}:{}:{}", file, tok.getFileName(), tok.getLine(), tok.getCharPositionInLine());
}
numFailures++;
TextPointer strt = null;
TextPointer end = null;
if (InputFileUtils.getRelativePath(file, context.fileSystem()).equals(tok.getFileName())) {
try {
strt = file.newPointer(tok.getLine(), tok.getCharPositionInLine() - 1);
end = file.newPointer(tok.getLine(), tok.getCharPositionInLine());
} catch (IllegalArgumentException uncaught) {
// Nothing
}
}
if (context.runtime().getProduct() == SonarProduct.SONARLINT) {
NewAnalysisError analysisError = context.newAnalysisError();
analysisError.onFile(file);
analysisError.message(Strings.nullToEmpty(cause.getMessage()) + " in " + tok.getFileName() + ":" + tok.getLine() + ":" + tok.getCharPositionInLine());
if (strt != null)
analysisError.at(strt);
analysisError.save();
} else {
NewIssue issue = context.newIssue().forRule(RuleKey.of(Constants.STD_REPOSITORY_KEY, OpenEdgeRulesDefinition.PROPARSE_ERROR_RULEKEY));
NewIssueLocation loc = issue.newLocation().on(file).message(Strings.nullToEmpty(caught.getMessage()) + " in " + tok.getFileName() + ":" + tok.getLine() + ":" + tok.getCharPositionInLine());
if ((strt != null) && (end != null))
loc.at(file.newRange(strt, end));
issue.at(loc);
issue.save();
}
return;
} catch (RuntimeException caught) {
LOG.error("Parser error in '" + InputFileUtils.getRelativePath(file, context.fileSystem()) + "'", caught);
numFailures++;
NewIssue issue = context.newIssue();
issue.forRule(RuleKey.of(Constants.STD_REPOSITORY_KEY, OpenEdgeRulesDefinition.PROPARSE_ERROR_RULEKEY)).at(issue.newLocation().on(file).message(Strings.nullToEmpty(caught.getMessage()))).save();
return;
}
if (context.runtime().getProduct() == SonarProduct.SONARQUBE) {
if (!settings.useSimpleCPD()) {
computeCpd(context, file, unit);
}
computeSimpleMetrics(context, file, unit);
computeCommonMetrics(context, file, unit);
computeComplexity(context, file, unit);
}
if (settings.useProparseDebug()) {
generateProparseDebugFile(file, unit);
}
try {
for (Map.Entry<ActiveRule, OpenEdgeProparseCheck> entry : components.getProparseRules().entrySet()) {
LOG.debug("ActiveRule - Internal key {} - Repository {} - Rule {}", entry.getKey().internalKey(), entry.getKey().ruleKey().repository(), entry.getKey().ruleKey().rule());
startTime = System.currentTimeMillis();
entry.getValue().sensorExecute(file, unit);
ruleTime.put(entry.getKey().ruleKey().toString(), ruleTime.get(entry.getKey().ruleKey().toString()) + System.currentTimeMillis() - startTime);
}
} catch (RuntimeException caught) {
LOG.error("Error during rule execution for " + file, caught);
}
}
use of com.progress.xref.CrossReference in project sonar-openedge by Riverside-Software.
the class JPNodeTest method testXref01.
@Test
public void testXref01() throws JAXBException, IOException {
CrossReference xref = CrossReferenceUtils.parseXREF(Paths.get(SRC_DIR + "/xref01.p.xref"));
ParseUnit unit = genericTest("xref01.p", xref);
unit.treeParser01();
assertFalse(unit.hasSyntaxError());
List<JPNode> nodes = unit.getTopNode().query(ABLNodeType.RECORD_NAME);
assertEquals(nodes.size(), 5);
RecordNameNode warehouse = (RecordNameNode) nodes.get(0);
RecordNameNode customer = (RecordNameNode) nodes.get(1);
RecordNameNode item = (RecordNameNode) nodes.get(2);
assertTrue(warehouse.isWholeIndex());
assertEquals(warehouse.getSearchIndexName(), "Warehouse.warehousenum");
assertFalse(customer.isWholeIndex());
assertEquals(customer.getSearchIndexName(), "Customer.CountryPost");
assertEquals(customer.getSortAccess(), "Address");
assertTrue(item.isWholeIndex());
assertEquals(item.getSearchIndexName(), "Item.ItemNum");
}
use of com.progress.xref.CrossReference in project sonar-openedge by Riverside-Software.
the class JPNodeTest method testXref02.
@Test
public void testXref02() throws JAXBException, IOException {
CrossReference xref = CrossReferenceUtils.parseXREF(Paths.get(SRC_DIR + "/xref02.cls.xref"));
ParseUnit unit = genericTest("xref02.cls", xref);
unit.treeParser01();
assertFalse(unit.hasSyntaxError());
assertEquals(unit.getTopNode().query(ABLNodeType.RECORD_NAME).size(), 3);
for (JPNode node : unit.getTopNode().query(ABLNodeType.RECORD_NAME)) {
RecordNameNode rec = (RecordNameNode) node;
assertEquals(rec.getTableBuffer().getTable().getName(), "ttFoo");
assertTrue(rec.isWholeIndex());
}
}
use of com.progress.xref.CrossReference in project sonar-openedge by Riverside-Software.
the class JPNodeTest method testXref03.
@Test
public void testXref03() throws JAXBException, IOException {
CrossReference xref = CrossReferenceUtils.parseXREF(Paths.get(SRC_DIR + "/xref03.p.xref"));
ParseUnit unit = genericTest("xref03.p", xref);
unit.treeParser01();
assertFalse(unit.hasSyntaxError());
List<JPNode> recNodes = unit.getTopNode().query(ABLNodeType.RECORD_NAME);
assertEquals(recNodes.size(), 10);
// One can-find, search index should be set
assertEquals(((RecordNameNode) recNodes.get(3)).getSearchIndexName(), "Customer.Name");
// Two can-find on different tables, should be ok
assertEquals(((RecordNameNode) recNodes.get(4)).getSearchIndexName(), "Customer.Name");
assertEquals(((RecordNameNode) recNodes.get(5)).getSearchIndexName(), "Item.ItemNum");
// Two can-find on same buffer, not ok
assertEquals(((RecordNameNode) recNodes.get(6)).getSearchIndexName(), "");
assertEquals(((RecordNameNode) recNodes.get(7)).getSearchIndexName(), "");
// Two can-find on different buffer, but same table, not ok
assertEquals(((RecordNameNode) recNodes.get(8)).getSearchIndexName(), "");
assertEquals(((RecordNameNode) recNodes.get(9)).getSearchIndexName(), "");
}
Aggregations