Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/org/opensolaris/opengrok/analysis/JFlexXref.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ protected String getProjectPostfix(boolean encoded) {
}

protected void startScope() {
if (scopesEnabled && scope == null) {
if (scopesEnabled && scope == null && defs != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this fixing any misbehavior ?

int line = getLineNumber();
List<Tag> tags = defs.getTags(line);
if (tags != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.opensolaris.opengrok.analysis.JFlexXref;
import org.opensolaris.opengrok.analysis.StreamSource;
import org.opensolaris.opengrok.configuration.Project;
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;
import org.opensolaris.opengrok.history.Annotation;

/**
Expand Down Expand Up @@ -76,6 +77,8 @@ static protected void writeXref(JFlexXref lxref, Reader in, Writer out, Definiti
lxref.reInit(in);
lxref.annotation = annotation;
lxref.project = project;
lxref.setScopesEnabled(RuntimeEnvironment.getInstance().isScopesEnabled());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I understand this and AnalyzerGuru.java#writeXref() are used only for testing. Otherwise during normal indexing the xref is created via this path:

IndexDatabase#addFile()
  FileAnalyzer fa = AnalyzerGuru.getAnalyzer() // fa can be e.g. CAnalyzer
  AnalyzerGuru#populateDocument()
    fa.analyze()
    AbstractSourceCodeAnalyzer#analyze()
      super.analyze()
        PlainAnalyzer#analyze()
          writeXref()
            xref = newXref() // xref can be e.g. CXref
            JFlexXref.write()

where the scopes/folding properties are in effect thanks to the inheritance and these lines in IndexDatabase#addFile():

662          fa.setScopesEnabled(RuntimeEnvironment.getInstance().isScopesEnabled());
663          fa.setFoldingEnabled(RuntimeEnvironment.getInstance().isFoldingEnabled());

So having these properties set into the writeXref() seems like short-circuiting however there is no other way given the method is static.

The only nit I have is that the call to reInit() in JFlexXref above contains:

        scopes = new Scopes();

which is not necessary when scopes are disabled. Granted, this method does not do anything however it's a bit of wasted memory.

lxref.setFoldingEnabled(RuntimeEnvironment.getInstance().isFoldingEnabled());
lxref.setDefs(defs);
lxref.write(out);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
*/
package org.opensolaris.opengrok.analysis.haskell;

Expand All @@ -30,8 +30,10 @@
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import org.junit.Before;
import org.junit.Test;
import org.opensolaris.opengrok.analysis.Definitions;
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
Expand All @@ -43,6 +45,11 @@
*/
public class HaskellXrefTest {

@Before
public void setUp() {
RuntimeEnvironment.getInstance().setScopesEnabled(false);
}

@Test
public void basicTest() throws IOException {
String s = "putStrLn \"Hello, world!\"";
Expand Down Expand Up @@ -104,4 +111,45 @@ public void sampleTest() throws IOException {
String expected[] = new String(expectedOutputSteam.toByteArray(), "UTF-8").split("\n");
assertArrayEquals(expected, actual);
}

@Test
public void sampleTestWithScopes() throws IOException {
// enable scopes
RuntimeEnvironment.getInstance().setScopesEnabled(true);
// load sample source
InputStream sampleInputStream = getClass().getClassLoader().getResourceAsStream(
"org/opensolaris/opengrok/analysis/haskell/sample.hs");
ByteArrayOutputStream sampleOutputStream = new ByteArrayOutputStream();

Definitions defs = new Definitions();
defs.addTag(6, "x'y'", "functions", "x'y' = let f' = 1; g'h = 2 in f' + g'h");
try {
writeHaskellXref(sampleInputStream, new PrintStream(sampleOutputStream), defs);
} finally {
sampleInputStream.close();
sampleOutputStream.close();
}

// load expected xref
InputStream expectedInputStream = getClass().getClassLoader().getResourceAsStream(
"org/opensolaris/opengrok/analysis/haskell/sampleXrefWithScopesExpected.html");
ByteArrayOutputStream expectedOutputSteam = new ByteArrayOutputStream();
try {
byte buffer[] = new byte[8192];
int numBytesRead;
do {
numBytesRead = expectedInputStream.read(buffer, 0, buffer.length);
if (numBytesRead > 0) {
expectedOutputSteam.write(buffer, 0, numBytesRead);
}
} while (numBytesRead >= 0);
} finally {
expectedInputStream.close();
expectedOutputSteam.close();
}

String actual[] = new String(sampleOutputStream.toByteArray(), "UTF-8").split("\n");
String expected[] = new String(expectedOutputSteam.toByteArray(), "UTF-8").split("\n");
assertArrayEquals(expected, actual);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html><html><head><meta http-equiv="content-type" content="text/html;charset=UTF-8" /><link rel="stylesheet" type="text/css" href="http://localhost:8080/source/default/style.css" /><title>Haskell Xref Test</title></head>
<body><div id="src"><pre>
<script type="text/javascript">/* <![CDATA[ */
function get_sym_list(){return [["Function","xf",[["x'y'",6]]]];} /* ]]> */</script><a class="l" name="1" href="#1">1</a><span class='fold-space'>&nbsp;</span><span class="c">-- &#99;&#108;&#97;&#115;&#115;&#105;&#99; &#113;&#117;&#105;&#99;&#107; &#115;&#111;&#114;&#116; &#40;&#115;&#104;&#111;&#114;&#116; &#98;&#117;&#116; &#110;&#111;&#116; &#115;&#111; &#101;&#102;&#102;&#105;&#99;&#105;&#101;&#110;&#116;&#41;</span>
<a class="l" name="2" href="#2">2</a><span class='fold-space'>&nbsp;</span><a href="/source/s?defs=qsort" class="intelliWindow-symbol" data-definition-place="undefined-in-file">qsort</a> &#91;&#93; &#61; &#91;&#93;
<a class="l" name="3" href="#3">3</a><span class='fold-space'>&nbsp;</span><a href="/source/s?defs=qsort" class="intelliWindow-symbol" data-definition-place="undefined-in-file">qsort</a> &#40;<a href="/source/s?defs=x" class="intelliWindow-symbol" data-definition-place="undefined-in-file">x</a>&#58;<a href="/source/s?defs=xs" class="intelliWindow-symbol" data-definition-place="undefined-in-file">xs</a>&#41; &#61; <a href="/source/s?defs=qsort" class="intelliWindow-symbol" data-definition-place="undefined-in-file">qsort</a> &#91; <a href="/source/s?defs=x'" class="intelliWindow-symbol" data-definition-place="undefined-in-file">x'</a> &#124; <a href="/source/s?defs=x'" class="intelliWindow-symbol" data-definition-place="undefined-in-file">x'</a> &lt;&#45; <a href="/source/s?defs=xs" class="intelliWindow-symbol" data-definition-place="undefined-in-file">xs</a>&#44; <a href="/source/s?defs=x'" class="intelliWindow-symbol" data-definition-place="undefined-in-file">x'</a> &lt; <a href="/source/s?defs=x" class="intelliWindow-symbol" data-definition-place="undefined-in-file">x</a> &#93; &#43;&#43; &#91;<a href="/source/s?defs=x" class="intelliWindow-symbol" data-definition-place="undefined-in-file">x</a>&#93; &#43;&#43; <a href="/source/s?defs=qsort" class="intelliWindow-symbol" data-definition-place="undefined-in-file">qsort</a> &#91; <a href="/source/s?defs=x'" class="intelliWindow-symbol" data-definition-place="undefined-in-file">x'</a> &#124; <a href="/source/s?defs=x'" class="intelliWindow-symbol" data-definition-place="undefined-in-file">x'</a> &lt;&#45; <a href="/source/s?defs=xs" class="intelliWindow-symbol" data-definition-place="undefined-in-file">xs</a>&#44; <a href="/source/s?defs=x'" class="intelliWindow-symbol" data-definition-place="undefined-in-file">x'</a> &gt;&#61; <a href="/source/s?defs=x" class="intelliWindow-symbol" data-definition-place="undefined-in-file">x</a> &#93;
<a class="l" name="4" href="#4">4</a><span class='fold-space'>&nbsp;</span>
<a class="l" name="5" href="#5">5</a><span class='fold-space'>&nbsp;</span><span class="c">-- &#119;&#101;&#105;&#114;&#100; &#98;&#117;&#116; &#108;&#101;&#103;&#97;&#108; &#105;&#100;&#101;&#110;&#116;&#105;&#102;&#105;&#101;&#114;&#115;</span>
<span id='scope_id_d7e336fa' class='scope-head'><span class='scope-signature'>x&apos;y&apos;null</span><a class="l" name="6" href="#6">6</a><a href="#" onclick='fold(this.parentNode.id)' id='scope_id_d7e336fa_fold_icon'><span class='fold-icon'>&nbsp;</span></a><a href="/source/s?refs=x'y'" class="xf intelliWindow-symbol" data-definition-place="def">x'y'</a> &#61; <b>let</b> <a href="/source/s?defs=f'" class="intelliWindow-symbol" data-definition-place="undefined-in-file">f'</a> &#61; <span class="n">1</span>&#59; <a href="/source/s?defs=g'h" class="intelliWindow-symbol" data-definition-place="undefined-in-file">g'h</a> &#61; <span class="n">2</span> <b>in</b> <a href="/source/s?defs=f'" class="intelliWindow-symbol" data-definition-place="undefined-in-file">f'</a> &#43; <a href="/source/s?defs=g'h" class="intelliWindow-symbol" data-definition-place="undefined-in-file">g'h</a></span>
<span id='scope_id_d7e336fa_fold' class='scope-body'><a class="l" name="7" href="#7">7</a><span class='fold-space'>&nbsp;</span></span></pre></div></body></html>
9 changes: 8 additions & 1 deletion test/org/opensolaris/opengrok/analysis/php/PhpXrefTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
*/
package org.opensolaris.opengrok.analysis.php;

Expand All @@ -32,7 +32,9 @@
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import org.junit.Before;
import org.junit.Test;
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;

import static org.junit.Assert.assertEquals;

Expand All @@ -43,6 +45,11 @@
*/
public class PhpXrefTest {

@Before
public void setUp() {
RuntimeEnvironment.getInstance().setScopesEnabled(false);
}

@Test
public void basicTest() throws IOException {
String s = "<?php foo bar";
Expand Down