Search in sources :

Example 1 with OpenEdgeProparseCheck

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);
    }
}
Also used : JsonNodeLister(org.prorefactor.core.JsonNodeLister) Metric(org.sonar.api.batch.measure.Metric) ActiveRule(org.sonar.api.batch.rule.ActiveRule) Module(com.google.inject.Module) JPNode(org.prorefactor.core.JPNode) Type(org.sonar.api.batch.fs.InputFile.Type) NewIssueLocation(org.sonar.api.batch.sensor.issue.NewIssueLocation) FilePredicates(org.sonar.api.batch.fs.FilePredicates) ProparseListener(org.prorefactor.proparse.antlr4.ProparseListener) Loggers(org.sonar.api.utils.log.Loggers) Proparse(org.prorefactor.proparse.antlr4.Proparse) Document(org.w3c.dom.Document) Map(java.util.Map) OpenEdgeComponents(org.sonar.plugins.openedge.foundation.OpenEdgeComponents) PrintWriter(java.io.PrintWriter) Predicate(java.util.function.Predicate) CPDCallback(org.sonar.plugins.openedge.foundation.CPDCallback) SensorContext(org.sonar.api.batch.sensor.SensorContext) CoreMetrics(org.sonar.api.measures.CoreMetrics) TextPointer(org.sonar.api.batch.fs.TextPointer) SensorDescriptor(org.sonar.api.batch.sensor.SensorDescriptor) UncheckedIOException(java.io.UncheckedIOException) IRefactorSessionEnv(org.sonar.plugins.openedge.foundation.IRefactorSessionEnv) List(java.util.List) SAXException(org.xml.sax.SAXException) Entry(java.util.Map.Entry) RuleKey(org.sonar.api.rule.RuleKey) ByteStreams(com.google.common.io.ByteStreams) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) OpenEdgeProparseCheck(org.sonar.plugins.openedge.api.checks.OpenEdgeProparseCheck) CrossReferenceUtils(com.progress.xref.CrossReferenceUtils) OpenEdgeRulesDefinition(org.sonar.plugins.openedge.foundation.OpenEdgeRulesDefinition) ABLNodeType(org.prorefactor.core.ABLNodeType) InputFile(org.sonar.api.batch.fs.InputFile) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) CodeBlock(eu.rssw.listing.CodeBlock) ParseUnit(org.prorefactor.treeparser.ParseUnit) HashMap(java.util.HashMap) Sensor(org.sonar.api.batch.sensor.Sensor) InvalidXMLFilterStream(com.progress.xref.InvalidXMLFilterStream) ArrayList(java.util.ArrayList) ProparseRuntimeException(org.prorefactor.core.ProparseRuntimeException) Strings(com.google.common.base.Strings) IProparseEnvironment(org.prorefactor.proparse.support.IProparseEnvironment) Binder(com.google.inject.Binder) StreamSupport(java.util.stream.StreamSupport) ListingParser(eu.rssw.listing.ListingParser) NewAnalysisError(org.sonar.api.batch.sensor.error.NewAnalysisError) ProToken(org.prorefactor.core.ProToken) Logger(org.sonar.api.utils.log.Logger) OutputStream(java.io.OutputStream) IncludeFileNotFoundException(org.prorefactor.proparse.IncludeFileNotFoundException) InputFileUtils(org.sonar.plugins.openedge.foundation.InputFileUtils) OpenEdgeSettings(org.sonar.plugins.openedge.foundation.OpenEdgeSettings) SonarProduct(org.sonar.api.SonarProduct) OpenEdgeMetrics(org.sonar.plugins.openedge.foundation.OpenEdgeMetrics) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) XCodedFileException(org.prorefactor.proparse.XCodedFileException) CrossReference(com.progress.xref.CrossReference) TokenSource(org.antlr.v4.runtime.TokenSource) File(java.io.File) Injector(com.google.inject.Injector) RecognitionException(org.antlr.v4.runtime.RecognitionException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Guice(com.google.inject.Guice) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) Constants(org.sonar.plugins.openedge.api.Constants) InputStream(java.io.InputStream) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) ArrayList(java.util.ArrayList) TextPointer(org.sonar.api.batch.fs.TextPointer) UncheckedIOException(java.io.UncheckedIOException) Document(org.w3c.dom.Document) ProToken(org.prorefactor.core.ProToken) ProparseRuntimeException(org.prorefactor.core.ProparseRuntimeException) OpenEdgeProparseCheck(org.sonar.plugins.openedge.api.checks.OpenEdgeProparseCheck) Injector(com.google.inject.Injector) ParseUnit(org.prorefactor.treeparser.ParseUnit) ProparseListener(org.prorefactor.proparse.antlr4.ProparseListener) ActiveRule(org.sonar.api.batch.rule.ActiveRule) ListingParser(eu.rssw.listing.ListingParser) CodeBlock(eu.rssw.listing.CodeBlock) XCodedFileException(org.prorefactor.proparse.XCodedFileException) NewAnalysisError(org.sonar.api.batch.sensor.error.NewAnalysisError) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) NewIssueLocation(org.sonar.api.batch.sensor.issue.NewIssueLocation) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) CrossReference(com.progress.xref.CrossReference) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) IncludeFileNotFoundException(org.prorefactor.proparse.IncludeFileNotFoundException) Map(java.util.Map) HashMap(java.util.HashMap) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 2 with OpenEdgeProparseCheck

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();
}
Also used : OpenEdgeProparseCheck(org.sonar.plugins.openedge.api.checks.OpenEdgeProparseCheck) IProparseEnvironment(org.prorefactor.proparse.support.IProparseEnvironment) ActiveRule(org.sonar.api.batch.rule.ActiveRule) FilePredicates(org.sonar.api.batch.fs.FilePredicates) IRefactorSessionEnv(org.sonar.plugins.openedge.foundation.IRefactorSessionEnv) Map(java.util.Map) HashMap(java.util.HashMap) InputFile(org.sonar.api.batch.fs.InputFile)

Aggregations

HashMap (java.util.HashMap)2 Map (java.util.Map)2 IProparseEnvironment (org.prorefactor.proparse.support.IProparseEnvironment)2 FilePredicates (org.sonar.api.batch.fs.FilePredicates)2 InputFile (org.sonar.api.batch.fs.InputFile)2 ActiveRule (org.sonar.api.batch.rule.ActiveRule)2 OpenEdgeProparseCheck (org.sonar.plugins.openedge.api.checks.OpenEdgeProparseCheck)2 IRefactorSessionEnv (org.sonar.plugins.openedge.foundation.IRefactorSessionEnv)2 Strings (com.google.common.base.Strings)1 ByteStreams (com.google.common.io.ByteStreams)1 Binder (com.google.inject.Binder)1 Guice (com.google.inject.Guice)1 Injector (com.google.inject.Injector)1 Module (com.google.inject.Module)1 CrossReference (com.progress.xref.CrossReference)1 CrossReferenceUtils (com.progress.xref.CrossReferenceUtils)1 InvalidXMLFilterStream (com.progress.xref.InvalidXMLFilterStream)1 CodeBlock (eu.rssw.listing.CodeBlock)1 ListingParser (eu.rssw.listing.ListingParser)1 File (java.io.File)1