Skip to content

Commit 9499abe

Browse files
committed
Improve test infrastructure and fix build errors:
- automatic scala version inference in test suite - remove error message in test suite - implemented derived statements in classdef - fix bugs shown by rebase
1 parent 7f3f5be commit 9499abe

File tree

4 files changed

+55
-58
lines changed

4 files changed

+55
-58
lines changed

semanticdb/src/dotty/semanticdb/SemanticdbConsumer.scala

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,19 @@ class SemanticdbConsumer(sourceFile: java.nio.file.Path) extends TastyConsumer {
6161
}
6262
}
6363

64+
def arePositionEqual(p1 : Position, p2 : Position) : Boolean = {
65+
p1.start == p2.start &&
66+
p1.end == p2.end &&
67+
p1.sourceFile == p2.sourceFile
68+
}
69+
6470
object Traverser extends TreeTraverser {
6571
implicit class TreeExtender(tree: Tree) {
6672
def isUserCreated: Boolean = {
6773
val children: List[Position] =
6874
ChildTraverser.getChildren(tree)(reflect.rootContext).map(_.pos)
69-
return !((tree.pos.exists && tree.pos.start == tree.pos.end && children == Nil) || children
70-
.exists(_ == tree.pos))
75+
return !((tree.pos.exists && tree.pos.start == tree.pos.end && children == Nil) ||
76+
children.exists(arePositionEqual(tree.pos, _)))
7177
}
7278
}
7379

@@ -76,8 +82,8 @@ class SemanticdbConsumer(sourceFile: java.nio.file.Path) extends TastyConsumer {
7682
val children: List[Position] =
7783
ChildTraverser.getChildrenType(tree)(reflect.rootContext).collect(_ match {
7884
case IsTypeTree(tt) => tt.pos})
79-
return !((tree.pos.exists && tree.pos.start == tree.pos.end && children == Nil) || children
80-
.exists(_ == tree.pos))
85+
return !((tree.pos.exists && tree.pos.start == tree.pos.end && children == Nil) ||
86+
children.exists(arePositionEqual(tree.pos, _)))
8187
}
8288
}
8389

@@ -457,7 +463,6 @@ class SemanticdbConsumer(sourceFile: java.nio.file.Path) extends TastyConsumer {
457463
// dotty will generate a ValDef for the x, but the x will also
458464
// be present in the constructor, thus making a double definition
459465
if (symbolPathsMap.contains(key)) return
460-
461466
symbolPathsMap += key
462467
occurrences =
463468
occurrences :+
@@ -508,7 +513,7 @@ class SemanticdbConsumer(sourceFile: java.nio.file.Path) extends TastyConsumer {
508513
def addOccurencePatternTree(tree: Pattern,
509514
type_symbol: s.SymbolOccurrence.Role,
510515
range: s.Range): Unit = {
511-
if (tree.isUserCreated) {
516+
if (tree.symbol.isUseful && tree.isUserCreated) {
512517
addOccurence(tree.symbol, type_symbol, range)
513518
}
514519
}
@@ -780,7 +785,7 @@ class SemanticdbConsumer(sourceFile: java.nio.file.Path) extends TastyConsumer {
780785
case Term.Apply(_, _) => {
781786
super.traverseTree(tree)
782787
}
783-
case cl @ ClassDef(classname, constr, parents, selfopt, statements) => {
788+
case ClassDef(classname, constr, parents, derived, selfopt, statements) => {
784789
// we first add the class to the symbol list
785790
addOccurenceTree(tree,
786791
s.SymbolOccurrence.Role.DEFINITION,
@@ -798,27 +803,12 @@ class SemanticdbConsumer(sourceFile: java.nio.file.Path) extends TastyConsumer {
798803
val rightmost = typesParameters.reverse.head.pos.end
799804
val end_ = nextCharacterSkipComments(sourceCode, rightmost) + 1
800805
fittedInitClassRange = Some(
801-
s.Range(tree.symbol.pos.startLine,
806+
s.Range(0,
802807
end_,
803-
tree.symbol.pos.startLine,
808+
0,
804809
end_))
805810
}
806811

807-
/*s
808-
if (!constr.isUserCreated) {
809-
fittedInitClassRange = Some(
810-
s.Range(tree.symbol.pos.startLine,
811-
tree.symbol.pos.startColumn + classname.length + 1,
812-
tree.symbol.pos.startLine,
813-
tree.symbol.pos.startColumn + classname.length + 1))
814-
} else {
815-
fittedInitClassRange = Some(
816-
s.Range(constr.symbol.pos.startLine,
817-
constr.symbol.pos.startColumn,
818-
constr.symbol.pos.endLine,
819-
constr.symbol.pos.endColumn))
820-
}*/
821-
822812
disableConstrParamTraversal = true
823813
traverseTree(constr)
824814
disableConstrParamTraversal = false
@@ -865,6 +855,8 @@ class SemanticdbConsumer(sourceFile: java.nio.file.Path) extends TastyConsumer {
865855
case _ =>
866856
}
867857

858+
derived.foreach(traverseTypeTree)
859+
868860
classStacks = tree.symbol :: classStacks
869861

870862
val paramsPosMapping = generateParamsPosMapping(constr)
@@ -1058,7 +1050,6 @@ class SemanticdbConsumer(sourceFile: java.nio.file.Path) extends TastyConsumer {
10581050
}
10591051

10601052
}
1061-
10621053
Traverser.traverseTree(root)(reflect.rootContext)
10631054
}
10641055

semanticdb/src/dotty/semanticdb/Utils.scala

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ object Utils {
3131

3232
/** List all tasty files occuring in the folder f or one of its subfolders */
3333
def recursiveListFiles(f: File, prefix : String = ""): Array[File] = {
34-
val pattern = (prefix + ".*\\.tasty").r
34+
val pattern = (".*" + prefix + ".*\\.tasty").r
3535
val files = f.listFiles
3636
val folders = files.filter(_.isDirectory)
37-
val tastyfiles = files.filter(_.toString match {
37+
val tastyfiles = files.filter(_.toPath.toString match {
3838
case pattern(x: _*) => true
3939
case _ => false
4040
})
@@ -45,7 +45,7 @@ object Utils {
4545
def getTastyFiles(classPath: Path, prefix : String = ""): HashMap[String, List[Path]] = {
4646
val sourceToTasty: HashMap[String, List[Path]] = HashMap()
4747
val tastyfiles = recursiveListFiles(classPath.toFile(), prefix)
48-
recursiveListFiles(classPath.toFile()).map(tastyPath => {
48+
tastyfiles.map(tastyPath => {
4949
val (classpath, classname) = getClasspathClassname(tastyPath.toPath())
5050
// We add an exception here to avoid crashing if we encountered
5151
// a bad tasty file
@@ -58,7 +58,7 @@ object Utils {
5858
(source -> (tastyPath
5959
.toPath().toAbsolutePath :: sourceToTasty.getOrElse(source, Nil))))
6060
} catch {
61-
case _: InvocationTargetException => println(tastyPath)
61+
case _: InvocationTargetException => ()
6262
}
6363
})
6464
sourceToTasty
@@ -69,8 +69,13 @@ object Utils {
6969
extracted from the compilation artifacts found in [classPath].
7070
*/
7171
def getClassNames(classPath: Path, scalaFile: Path, prefix : String = ""): List[String] = {
72+
val tastyFiles = getTastyFiles(classPath.toAbsolutePath, prefix)
73+
getClassNamesCached(scalaFile, tastyFiles)
74+
}
75+
76+
def getClassNamesCached(scalaFile: Path, allFiles : HashMap[String, List[Path]]): List[String] = {
7277
val tastyFiles =
73-
getTastyFiles(classPath.toAbsolutePath, prefix)
78+
allFiles
7479
.getOrElse(scalaFile.toString, Nil)
7580

7681
val tastyClasses = tastyFiles.map(getClasspathClassname)

semanticdb/test/dotty/semanticdb/Semanticdbs.scala

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ object Semanticdbs {
8181
**/
8282
def printTextDocument(doc: s.TextDocument): String = {
8383
val sb = new StringBuilder
84-
val occurrences = doc.occurrences.sorted
8584
val sourceFile = SourceFile.virtual(doc.uri, doc.text)
85+
implicit val occurrenceOrdering: Ordering[s.SymbolOccurrence] =
86+
buildOccurrenceOrdering(sourceFile)
87+
val occurrences = doc.occurrences.sorted
8688
var offset = 0
8789
occurrences.foreach { occ =>
8890
val range = occ.range.get
@@ -98,35 +100,28 @@ object Semanticdbs {
98100
}
99101

100102
/** Sort symbol occurrences by their start position. */
101-
implicit val occurrenceOrdering: Ordering[s.SymbolOccurrence] =
103+
def buildOccurrenceOrdering(sourceFile: SourceFile): Ordering[s.SymbolOccurrence] = {
102104
new Ordering[s.SymbolOccurrence] {
105+
def rangeToTuple(r : s.Range): (Int, Int) = {
106+
val start = sourceFile.lineToOffset(r.startLine) + r.startCharacter
107+
val end = sourceFile.lineToOffset(r.endLine) + r.endCharacter
108+
(start, end)
109+
}
110+
103111
override def compare(x: s.SymbolOccurrence, y: s.SymbolOccurrence): Int = {
104112
if (x.range.isEmpty) 0
105113
else if (y.range.isEmpty) 0
106114
else {
107-
val a = x.range.get
108-
val b = y.range.get
109-
val byLine = Integer.compare(
110-
a.startLine,
111-
b.startLine
112-
)
113-
if (byLine != 0) {
114-
byLine
115+
val (as, ae) = rangeToTuple(x.range.get)
116+
val (bs, be) = rangeToTuple(y.range.get)
117+
val byStart = Integer.compare(as, bs)
118+
if (byStart != 0) {
119+
byStart
115120
} else {
116-
val byCharacter = Integer.compare(
117-
a.startCharacter,
118-
b.startCharacter
119-
)
120-
if (byCharacter == 0) {
121-
Integer.compare(
122-
a.endCharacter,
123-
b.endCharacter
124-
)
125-
} else {
126-
byCharacter
127-
}
121+
Integer.compare(ae, be)
128122
}
129123
}
130124
}
131125
}
126+
}
132127
}

semanticdb/test/dotty/semanticdb/Tests.scala

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,15 @@ class Tests {
8282
}
8383
}
8484

85-
// TODO: update scala-0.13 on version change (or resolve automatically)
86-
final def tastyClassDirectory =
87-
Paths.get("out/bootstrap/dotty-semanticdb/scala-0.12/test-classes/")
85+
final def tastyClassDirectory = {
86+
val root = "out/bootstrap/dotty-semanticdb/"
87+
val files = Paths.get(root).toFile().listFiles
88+
val scalaFolderReg = """scala-(\d+)\.(\d+)""".r
89+
val (_, _, path) = files.collect(file => file.getName match {
90+
case scalaFolderReg(major, minor) => (major, minor, file.getName)
91+
}).max
92+
Paths.get(root, path, "test-classes")
93+
}
8894

8995
val sourceroot = Paths.get("semanticdb/input").toAbsolutePath
9096
val sourceDirectory = sourceroot.resolve("src/main/scala")
@@ -97,9 +103,11 @@ class Tests {
97103
semanticdbLoader.resolve(scalaFile).get
98104
}
99105

106+
final def allTastyFiles = Utils.getTastyFiles(tastyClassDirectory, "example")
107+
100108
/** Returns the SemanticDB for this Scala source file. */
101109
def getTastySemanticdb(classPath: Path, scalaFile: Path) : s.TextDocument = {
102-
val classNames = Utils.getClassNames(classPath, scalaFile, "example/")
110+
val classNames = Utils.getClassNamesCached(scalaFile, allTastyFiles)
103111
val sdbconsumer = new SemanticdbConsumer(scalaFile)
104112

105113
val _ = ConsumeTasty(classPath.toString, classNames, sdbconsumer)
@@ -144,7 +152,6 @@ class Tests {
144152
}
145153
}
146154

147-
148155
@Test def testAccess(): Unit = checkFile("example/Access.scala")
149156
@Test def testAdvanced(): Unit = checkFile("example/Advanced.scala")
150157
@Test def testAnonymous(): Unit = checkFile("example/Anonymous.scala")
@@ -181,5 +188,4 @@ class Tests {
181188
@Test def testSynthetic(): Unit = checkFile("example/Synthetic.scala")
182189
@Test def testBinaryOp(): Unit = checkFile("example/BinaryOp.scala")
183190
@Test def testDottyPredef(): Unit = checkFile("example/DottyPredef.scala")
184-
185191
}

0 commit comments

Comments
 (0)