Skip to content

Fix #4306: Implement -scansource for Java files #4861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 31, 2018
Merged
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
7 changes: 5 additions & 2 deletions compiler/src/dotty/tools/dotc/Run.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import transform.TreeChecker
import rewrite.Rewrites
import java.io.{BufferedWriter, OutputStreamWriter}

import dotty.tools.dotc.profile.Profiler
import profile.Profiler
import printing.XprintMode
import parsing.Parsers.Parser
import parsing.JavaParsers.JavaParser
import typer.ImplicitRunInfo
import collection.mutable

Expand Down Expand Up @@ -209,7 +210,9 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
lateFiles += file
val unit = new CompilationUnit(getSource(file.path))
def process()(implicit ctx: Context) = {
unit.untpdTree = new Parser(unit.source).parse()
unit.untpdTree =
if (unit.isJava) new JavaParser(unit.source).parse()
else new Parser(unit.source).parse()
ctx.typer.lateEnter(unit.untpdTree)
def typeCheckUnit() = unit.tpdTree = ctx.typer.typedExpr(unit.untpdTree)
if (typeCheck)
Expand Down
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/core/SymbolLoaders.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Decorators._
import scala.util.control.NonFatal
import ast.Trees._
import ast.tpd
import parsing.JavaParsers.OutlineJavaParser
import parsing.Parsers.OutlineParser
import reporting.trace

Expand Down Expand Up @@ -154,7 +155,10 @@ object SymbolLoaders {
case _ =>
}

traverse(new OutlineParser(unit.source).parse(), Nil)
traverse(
if (unit.isJava) new OutlineJavaParser(unit.source).parse()
else new OutlineParser(unit.source).parse(),
Nil)
}

val unit = new CompilationUnit(ctx.run.getSource(src.path))
Expand Down
14 changes: 14 additions & 0 deletions compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -907,4 +907,18 @@ object JavaParsers {
unit
}
}


/** OutlineJavaParser parses top-level declarations in `source` to find declared classes, ignoring their bodies (which
* must only have balanced braces). This is used to map class names to defining sources.
* This is necessary even for Java, because the filename defining a non-public classes cannot be determined from the
* classname alone.
*/
class OutlineJavaParser(source: SourceFile)(implicit ctx: Context) extends JavaParser(source) with OutlineParserCommon {
override def skipBracesHook() = None
override def typeBody(leadingToken: Int, parentName: Name, parentTParams: List[TypeDef]): (List[Tree], List[Tree]) = {
skipBraces()
(List(EmptyValDef), List(EmptyTree))
}
}
}
47 changes: 31 additions & 16 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,23 @@ object Parsers {
ctx.error(msg, source atPos pos)
}

trait OutlineParserCommon extends ParserCommon {
def accept(token: Int): Int

def skipBracesHook(): Option[Tree]
def skipBraces(): Unit = {
accept(LBRACE)
var openBraces = 1
while (in.token != EOF && openBraces > 0) {
skipBracesHook() getOrElse {
if (in.token == LBRACE) openBraces += 1
else if (in.token == RBRACE) openBraces -= 1
in.nextToken()
}
}
}
}

class Parser(source: SourceFile)(implicit ctx: Context) extends ParserCommon(source) {

val in: Scanner = new Scanner(source)
Expand Down Expand Up @@ -2638,24 +2655,22 @@ object Parsers {
}


class OutlineParser(source: SourceFile)(implicit ctx: Context) extends Parser(source) {
/** OutlineParser parses top-level declarations in `source` to find declared classes, ignoring their bodies (which
* must only have balanced braces). This is used to map class names to defining sources.
*/
class OutlineParser(source: SourceFile)(implicit ctx: Context) extends Parser(source) with OutlineParserCommon {

def skipBraces[T](body: T): T = {
accept(LBRACE)
var openBraces = 1
while (in.token != EOF && openBraces > 0) {
if (in.token == XMLSTART) xmlLiteral()
else {
if (in.token == LBRACE) openBraces += 1
else if (in.token == RBRACE) openBraces -= 1
in.nextToken()
}
}
body
}
def skipBracesHook(): Option[Tree] =
if (in.token == XMLSTART) Some(xmlLiteral()) else None

override def blockExpr(): Tree = skipBraces(EmptyTree)
override def blockExpr(): Tree = {
skipBraces()
EmptyTree
}

override def templateBody() = skipBraces((EmptyValDef, List(EmptyTree)))
override def templateBody() = {
skipBraces()
(EmptyValDef, List(EmptyTree))
}
}
}
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/parsing/Scanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ object Scanners {

abstract class ScannerCommon(source: SourceFile)(implicit ctx: Context) extends CharArrayReader with TokenData {
val buf = source.content
def nextToken(): Unit

// Errors -----------------------------------------------------------------

Expand Down
4 changes: 3 additions & 1 deletion compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class CompilationTests extends ParallelTesting {
compileFile("tests/pos-special/completeFromSource/Test.scala", defaultOptions.and("-sourcepath", "tests/pos-special")) +
compileFile("tests/pos-special/completeFromSource/Test2.scala", defaultOptions.and("-sourcepath", "tests/pos-special")) +
compileFile("tests/pos-special/completeFromSource/Test3.scala", defaultOptions.and("-sourcepath", "tests/pos-special", "-scansource")) +
compileFile("tests/pos-special/completeFromSource/nested/Test4.scala", defaultOptions.and("-sourcepath", "tests/pos-special", "-scansource")) +
compileFilesInDir("tests/pos-special/fatal-warnings", defaultOptions.and("-Xfatal-warnings")) +
compileList(
"compileMixed",
Expand Down Expand Up @@ -158,7 +159,8 @@ class CompilationTests extends ParallelTesting {
compileFile("tests/neg-custom-args/i4372.scala", allowDeepSubtypes) +
compileFile("tests/neg-custom-args/i1754.scala", allowDeepSubtypes) +
compileFilesInDir("tests/neg-custom-args/isInstanceOf", allowDeepSubtypes and "-Xfatal-warnings") +
compileFile("tests/neg-custom-args/i3627.scala", allowDeepSubtypes)
compileFile("tests/neg-custom-args/i3627.scala", allowDeepSubtypes) +
compileFile("tests/neg-custom-args/completeFromSource/nested/Test1.scala", defaultOptions.and("-sourcepath", "tests/neg-custom-args", "-scansource"))
}.checkExpectedErrors()

// Run tests -----------------------------------------------------------------
Expand Down
18 changes: 18 additions & 0 deletions tests/neg-custom-args/completeFromSource/nested/D.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package completeFromSource.nested;

public class D {
public D(int i) {

}
public String thisIsD(int i) {
return java.lang.Integer.toString(i);
}
}

class E {
public E(String s) {}

public String thisIsE(int i) {
return java.lang.Integer.toString(i);
}
}
10 changes: 10 additions & 0 deletions tests/neg-custom-args/completeFromSource/nested/Test1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package completeFromSource.nested

class Test4 {

val d = new D(1)
val e = new E("xx")

d.thisIsD(1)
e.thisIsE() // error
}
18 changes: 18 additions & 0 deletions tests/pos-special/completeFromSource/nested/D.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package completeFromSource.nested;

public class D {
public D(int i) {

}
public String thisIsD(int i) {
return java.lang.Integer.toString(i);
}
}

class E {
public E(String s) {}

public String thisIsE(int i) {
return java.lang.Integer.toString(i);
}
}
10 changes: 10 additions & 0 deletions tests/pos-special/completeFromSource/nested/Test4.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package completeFromSource.nested

class Test4 {

val d = new D(1)
val e = new E("xx")

d.thisIsD(1)
e.thisIsE(2)
}