use of org.sonar.plugins.openedge.api.checks.OpenEdgeProparseCheck 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 org.sonar.plugins.openedge.api.checks.OpenEdgeProparseCheck in project sonar-openedge by Riverside-Software.
the class OpenEdgeProparseSensor method execute.
@Override
public void execute(SensorContext context) {
if (settings.skipProparseSensor())
return;
settings.init();
components.init(context);
for (Map.Entry<ActiveRule, OpenEdgeProparseCheck> entry : components.getProparseRules().entrySet()) {
ruleTime.put(entry.getKey().ruleKey().toString(), 0L);
}
IRefactorSessionEnv sessions = settings.getProparseSessions();
FilePredicates predicates = context.fileSystem().predicates();
// Counting total number of files
long totFiles = StreamSupport.stream(context.fileSystem().inputFiles(predicates.and(predicates.hasLanguage(Constants.LANGUAGE_KEY), predicates.hasType(Type.MAIN))).spliterator(), false).count();
long prevMessage = System.currentTimeMillis();
for (InputFile file : context.fileSystem().inputFiles(predicates.and(predicates.hasLanguage(Constants.LANGUAGE_KEY), predicates.hasType(Type.MAIN)))) {
LOG.debug("Parsing {}", file);
numFiles++;
if (System.currentTimeMillis() - prevMessage > 30000L) {
prevMessage = System.currentTimeMillis();
LOG.info("{}/{} - Current file: {}", numFiles, totFiles, file.relativePath());
}
IProparseEnvironment session = sessions.getSession(file.relativePath());
if (settings.isIncludeFile(file.filename())) {
parseIncludeFile(context, file, session);
} else {
parseMainFile(context, file, session);
}
if (context.isCancelled()) {
LOG.info("Analysis cancelled...");
return;
}
}
computeAnalytics(context);
logStatistics();
context.addContextProperty("sonar.oe.ncloc", Integer.toString(ncLocs));
generateProparseDebugIndex();
}
Aggregations