Skip to content

Commit 733200e

Browse files
committed
add more semanticdb testing infrastructure
1 parent ec3a21a commit 733200e

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

compiler/src/dotty/tools/dotc/semanticdb/Tools.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ object Tools:
4141
document.copy(text = text)
4242
end loadTextDocument
4343

44+
def loadTextDocumentUnsafe(scalaAbsolutePath: Path, semanticdbAbsolutePath: Path): TextDocument =
45+
val docs = parseTextDocuments(semanticdbAbsolutePath).documents
46+
assert(docs.length == 1)
47+
docs.head.copy(text = new String(Files.readAllBytes(scalaAbsolutePath), StandardCharsets.UTF_8))
48+
4449
/** Parses SemanticDB text documents from an absolute path to a `*.semanticdb` file. */
4550
private def parseTextDocuments(path: Path): TextDocuments =
4651
val bytes = Files.readAllBytes(path) // NOTE: a semanticdb file is a TextDocuments message, not TextDocument

compiler/src/dotty/tools/dotc/transform/SymUtils.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ object SymUtils:
8787

8888
def isGenericProduct(using Context): Boolean = whyNotGenericProduct.isEmpty
8989

90-
/** Is this the an old style implicit conversion?
90+
/** Is this an old style implicit conversion?
9191
* @param directOnly only consider explicitly written methods
9292
* @param forImplicitClassOnly only consider methods generated from implicit classes
9393
*/
@@ -100,6 +100,7 @@ object SymUtils:
100100
case _ =>
101101
false
102102

103+
/** Is this the method that summons a structural given instance? */
103104
def isGivenInstanceSummoner(using Context): Boolean =
104105
def isCodefined(info: Type): Boolean = info.stripPoly match
105106
case mt: MethodType =>

compiler/test/dotty/tools/dotc/semanticdb/SemanticdbTests.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,30 @@ import dotty.tools.dotc.util.SourceFile
2525
@main def updateExpect =
2626
SemanticdbTests().runExpectTest(updateExpectFiles = true)
2727

28+
/** Useful for printing semanticdb metac output for one file
29+
*
30+
* @param root the output directory containing semanticdb output,
31+
* only 1 semanticdb file should be present
32+
* @param source the single source file producing the semanticdb
33+
*/
34+
@main def metac(root: String, source: String) =
35+
val rootSrc = Paths.get(root)
36+
val sourceSrc = Paths.get(source)
37+
val semanticFile = FileSystems.getDefault.getPathMatcher("glob:**.semanticdb")
38+
def inputFile(): Path =
39+
val ls = Files.walk(rootSrc.resolve("META-INF").resolve("semanticdb"))
40+
val files =
41+
try ls.filter(p => semanticFile.matches(p)).collect(Collectors.toList).asScala
42+
finally ls.close()
43+
require(files.sizeCompare(1) == 0, s"No semanticdb files! $rootSrc")
44+
files.head
45+
val metacSb: StringBuilder = StringBuilder(5000)
46+
val semanticdbPath = inputFile()
47+
val doc = Tools.loadTextDocumentUnsafe(sourceSrc.toAbsolutePath, semanticdbPath)
48+
Tools.metac(doc, Paths.get(doc.uri))(using metacSb)
49+
Files.write(rootSrc.resolve("metac.expect"), metacSb.toString.getBytes(StandardCharsets.UTF_8))
50+
51+
2852
@Category(Array(classOf[BootstrappedOnlyTests]))
2953
class SemanticdbTests:
3054
val javaFile = FileSystems.getDefault.getPathMatcher("glob:**.java")

0 commit comments

Comments
 (0)