Search in sources :

Example 1 with CrossReference

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);
    }
}
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 CrossReference

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");
}
Also used : ParseUnit(org.prorefactor.treeparser.ParseUnit) EmptyCrossReference(com.progress.xref.EmptyCrossReference) CrossReference(com.progress.xref.CrossReference) RecordNameNode(org.prorefactor.core.nodetypes.RecordNameNode) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) AfterTest(org.testng.annotations.AfterTest)

Example 3 with CrossReference

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());
    }
}
Also used : ParseUnit(org.prorefactor.treeparser.ParseUnit) EmptyCrossReference(com.progress.xref.EmptyCrossReference) CrossReference(com.progress.xref.CrossReference) RecordNameNode(org.prorefactor.core.nodetypes.RecordNameNode) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) AfterTest(org.testng.annotations.AfterTest)

Example 4 with CrossReference

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(), "");
}
Also used : ParseUnit(org.prorefactor.treeparser.ParseUnit) EmptyCrossReference(com.progress.xref.EmptyCrossReference) CrossReference(com.progress.xref.CrossReference) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) AfterTest(org.testng.annotations.AfterTest)

Aggregations

CrossReference (com.progress.xref.CrossReference)4 ParseUnit (org.prorefactor.treeparser.ParseUnit)4 EmptyCrossReference (com.progress.xref.EmptyCrossReference)3 RecordNameNode (org.prorefactor.core.nodetypes.RecordNameNode)2 AfterTest (org.testng.annotations.AfterTest)2 BeforeTest (org.testng.annotations.BeforeTest)2 Test (org.testng.annotations.Test)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 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 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1