Skip to content

Commit

Permalink
add more semanticdb testing infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
bishabosha committed Aug 26, 2021
1 parent ec3a21a commit 733200e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
5 changes: 5 additions & 0 deletions compiler/src/dotty/tools/dotc/semanticdb/Tools.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ object Tools:
document.copy(text = text)
end loadTextDocument

def loadTextDocumentUnsafe(scalaAbsolutePath: Path, semanticdbAbsolutePath: Path): TextDocument =
val docs = parseTextDocuments(semanticdbAbsolutePath).documents
assert(docs.length == 1)
docs.head.copy(text = new String(Files.readAllBytes(scalaAbsolutePath), StandardCharsets.UTF_8))

/** Parses SemanticDB text documents from an absolute path to a `*.semanticdb` file. */
private def parseTextDocuments(path: Path): TextDocuments =
val bytes = Files.readAllBytes(path) // NOTE: a semanticdb file is a TextDocuments message, not TextDocument
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/SymUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ object SymUtils:

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

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

/** Is this the method that summons a structural given instance? */
def isGivenInstanceSummoner(using Context): Boolean =
def isCodefined(info: Type): Boolean = info.stripPoly match
case mt: MethodType =>
Expand Down
24 changes: 24 additions & 0 deletions compiler/test/dotty/tools/dotc/semanticdb/SemanticdbTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@ import dotty.tools.dotc.util.SourceFile
@main def updateExpect =
SemanticdbTests().runExpectTest(updateExpectFiles = true)

/** Useful for printing semanticdb metac output for one file
*
* @param root the output directory containing semanticdb output,
* only 1 semanticdb file should be present
* @param source the single source file producing the semanticdb
*/
@main def metac(root: String, source: String) =
val rootSrc = Paths.get(root)
val sourceSrc = Paths.get(source)
val semanticFile = FileSystems.getDefault.getPathMatcher("glob:**.semanticdb")
def inputFile(): Path =
val ls = Files.walk(rootSrc.resolve("META-INF").resolve("semanticdb"))
val files =
try ls.filter(p => semanticFile.matches(p)).collect(Collectors.toList).asScala
finally ls.close()
require(files.sizeCompare(1) == 0, s"No semanticdb files! $rootSrc")
files.head
val metacSb: StringBuilder = StringBuilder(5000)
val semanticdbPath = inputFile()
val doc = Tools.loadTextDocumentUnsafe(sourceSrc.toAbsolutePath, semanticdbPath)
Tools.metac(doc, Paths.get(doc.uri))(using metacSb)
Files.write(rootSrc.resolve("metac.expect"), metacSb.toString.getBytes(StandardCharsets.UTF_8))


@Category(Array(classOf[BootstrappedOnlyTests]))
class SemanticdbTests:
val javaFile = FileSystems.getDefault.getPathMatcher("glob:**.java")
Expand Down

0 comments on commit 733200e

Please sign in to comment.