-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Export diagnostics (including unused warnings) to SemanticDB #17835
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
Changes from 3 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
75e6bb6
Export diagnostics (including unused warnings) to SemanticDB
tanishiking c5a80c4
Add -Wunused:all to the compiler options to see Semanticdb export unused
tanishiking e1d0ec9
Add some comments
tanishiking f17fb5f
ExtractSemanticDB.PostInlining now update the .semanticdb file on disk
tanishiking d2b54c2
Parallelize read/write SemanticDB
tanishiking 56c5909
Run AppendDiagnostics phase after crossVersionChecks
tanishiking 50e0fd2
[WIP] Don't parse TextDocuments in AppendDiagnostics
tanishiking 2babbc4
Remove unnecessary suffix
tanishiking 5186b62
Remove unused local + Context
tanishiking 66a5306
Merge branch 'main' into export-diagnostics
tanishiking a414fae
Fix presentation compiler
tanishiking d5065ec
Parse TextDocuments instead of using `com.google.protobuf`
tanishiking d7258b4
Remove Context from relPath and so on
tanishiking 4b5d3e7
Remove from parallel
tanishiking 3008ed8
Remove ancii color code from SemanticDB
tanishiking c9de8e2
Extractor.extract uses unitCtx
tanishiking 40a2a00
Remove Context parameter from Scala3.range and toSemanticDbDiagnositcs
tanishiking 7b29f4c
Remove unused
tanishiking 4f6a092
Revert "Remove from parallel"
tanishiking a6dfec2
Merge branch 'main' into export-diagnostics
tanishiking 3daeaa6
Merge branch 'main' into export-diagnostics
tanishiking 499c347
Remove println
tanishiking 71a5cd0
Fix TastyBootstrapTests by adding sharable annotation to regex
tanishiking b28b425
Merge branch 'main' into export-diagnostics
tanishiking d54d8a2
Do not run toSemanticDiagnosic in parallel
tanishiking b8565a0
Fix wrong rebase
tanishiking 2857632
Merge branch 'main' into export-diagnostics
tanishiking 053e644
Restore removed comment
tanishiking 567d486
Fix writesToOutputDir
tanishiking 275e6fa
Uncomment the phase for appending warnings
tanishiking File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
compiler/src/dotty/tools/dotc/semanticdb/DiagnosticOps.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package dotty.tools.dotc.semanticdb | ||
|
||
import dotty.tools.dotc.reporting.Diagnostic | ||
import dotty.tools.dotc.{semanticdb => s} | ||
import dotty.tools.dotc.interfaces.Diagnostic.{ERROR, INFO, WARNING} | ||
import dotty.tools.dotc.core.Contexts.Context | ||
|
||
object DiagnosticOps: | ||
extension (d: Diagnostic) | ||
def toSemanticDiagnostic(using Context): s.Diagnostic = | ||
val severity = d.level match | ||
case ERROR => s.Diagnostic.Severity.ERROR | ||
case WARNING => s.Diagnostic.Severity.WARNING | ||
case INFO => s.Diagnostic.Severity.INFORMATION | ||
case _ => s.Diagnostic.Severity.INFORMATION | ||
s.Diagnostic( | ||
range = Scala3.range(d.pos.span, d.pos.source), | ||
severity = severity, | ||
message = d.msg.message | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,16 +25,30 @@ import scala.PartialFunction.condOpt | |
|
||
import dotty.tools.dotc.{semanticdb => s} | ||
import dotty.tools.io.{AbstractFile, JarArchive} | ||
import dotty.tools.dotc.util.Property | ||
tanishiking marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import dotty.tools.dotc.semanticdb.DiagnosticOps.* | ||
|
||
|
||
/** Extract symbol references and uses to semanticdb files. | ||
* See https://scalameta.org/docs/semanticdb/specification.html#symbol-1 | ||
* for a description of the format. | ||
* TODO: Also extract type information | ||
* | ||
* Here, we define two phases for "ExtractSemanticDB", "PostTyper" and "PostInlining". | ||
* | ||
* The "PostTyper" phase extracts SemanticDB information such as symbol | ||
* definitions, symbol occurrences, type information, and synthetics. | ||
* This phase does not write the information to a .semanticdb file; | ||
* instead, it attaches the SemanticDB information to the top-level tree. | ||
* | ||
* The "PostInlining" phase extracts diagnostics from "ctx.reporter" and | ||
* attaches them to the SemanticDB information extracted in the "PostTyper" phase. | ||
* Afterwards, it writes the SemanticDB to a ".semanticdb" file. | ||
* We need to run this phase after the "CheckUnused.PostInlining" phase | ||
* so that we can extract the warnings generated by "-Wunused". | ||
*/ | ||
class ExtractSemanticDB extends Phase: | ||
import Scala3.{_, given} | ||
class ExtractSemanticDB private (phaseMode: ExtractSemanticDB.PhaseMode, suffix: String, _key: Property.Key[TextDocument]) extends Phase: | ||
|
||
override val phaseName: String = ExtractSemanticDB.name | ||
override val phaseName: String = ExtractSemanticDB.phaseNamePrefix + suffix | ||
|
||
override val description: String = ExtractSemanticDB.description | ||
|
||
|
@@ -48,16 +62,94 @@ class ExtractSemanticDB extends Phase: | |
|
||
override def run(using Context): Unit = | ||
val unit = ctx.compilationUnit | ||
val extractor = Extractor() | ||
extractor.extract(unit.tpdTree) | ||
ExtractSemanticDB.write(unit.source, extractor.occurrences.toList, extractor.symbolInfos.toList, extractor.synthetics.toList) | ||
if (phaseMode == ExtractSemanticDB.PhaseMode.PostTyper) | ||
val extractor = ExtractSemanticDB.Extractor() | ||
extractor.extract(unit.tpdTree) | ||
unit.tpdTree.putAttachment(_key, extractor.toTextDocument(unit.source)) | ||
else | ||
unit.tpdTree.getAttachment(_key) match | ||
case None => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should say something when attachment is not found? |
||
case Some(doc) => | ||
val warnings = ctx.reporter.allWarnings.collect { | ||
case w if w.pos.source == ctx.source => w.toSemanticDiagnostic | ||
} | ||
ExtractSemanticDB.write(unit.source, doc.copy(diagnostics = warnings)) | ||
end ExtractSemanticDB | ||
|
||
object ExtractSemanticDB: | ||
import java.nio.file.Path | ||
import java.nio.file.Files | ||
import java.nio.file.Paths | ||
|
||
val phaseNamePrefix: String = "extractSemanticDB" | ||
val description: String = "extract info into .semanticdb files" | ||
|
||
enum PhaseMode: | ||
case PostTyper | ||
case PostInlining | ||
|
||
/** | ||
* The key used to retrieve the "unused entity" analysis metadata, | ||
* from the compilation `Context` | ||
*/ | ||
private val _key = Property.StickyKey[TextDocument] | ||
|
||
class PostTyper extends ExtractSemanticDB(PhaseMode.PostTyper, "PostTyper", _key) | ||
|
||
class PostInlining extends ExtractSemanticDB(PhaseMode.PostInlining, "PostInlining", _key) | ||
|
||
private def semanticdbTarget(using Context): Option[Path] = | ||
Option(ctx.settings.semanticdbTarget.value) | ||
.filterNot(_.isEmpty) | ||
.map(Paths.get(_)) | ||
|
||
private def outputDirectory(using Context): AbstractFile = ctx.settings.outputDir.value | ||
|
||
private def absolutePath(path: Path): Path = path.toAbsolutePath.normalize | ||
|
||
private def write( | ||
source: SourceFile, | ||
doc: TextDocument | ||
)(using Context): Unit = | ||
val relPath = SourceFile.relativePath(source, ctx.settings.sourceroot.value) | ||
val outpath = absolutePath(semanticdbTarget.getOrElse(outputDirectory.jpath)) | ||
.resolve("META-INF") | ||
.resolve("semanticdb") | ||
.resolve(relPath) | ||
.resolveSibling(source.name + ".semanticdb") | ||
Files.createDirectories(outpath.getParent()) | ||
val docs = TextDocuments(List(doc)) | ||
val out = Files.newOutputStream(outpath) | ||
try | ||
val stream = internal.SemanticdbOutputStream.newInstance(out) | ||
docs.writeTo(stream) | ||
stream.flush() | ||
finally | ||
out.close() | ||
end write | ||
|
||
|
||
/** Extractor of symbol occurrences from trees */ | ||
class Extractor extends TreeTraverser: | ||
private class Extractor extends TreeTraverser: | ||
import Scala3.{_, given} | ||
given s.SemanticSymbolBuilder = s.SemanticSymbolBuilder() | ||
val synth = SyntheticsExtractor() | ||
given converter: s.TypeOps = s.TypeOps() | ||
|
||
|
||
def toTextDocument(source: SourceFile)(using Context): TextDocument = | ||
val relPath = SourceFile.relativePath(source, ctx.settings.sourceroot.value) | ||
TextDocument( | ||
schema = Schema.SEMANTICDB4, | ||
language = Language.SCALA, | ||
uri = Tools.mkURIstring(Paths.get(relPath)), | ||
text = "", | ||
md5 = internal.MD5.compute(String(source.content)), | ||
symbols = symbolInfos.toList, | ||
occurrences = occurrences.toList, | ||
synthetics = synthetics.toList, | ||
) | ||
|
||
/** The bodies of synthetic locals */ | ||
private val localBodies = mutable.HashMap[Symbol, Tree]() | ||
|
||
|
@@ -468,52 +560,5 @@ class ExtractSemanticDB extends Phase: | |
registerSymbol(vparam.symbol, symkinds) | ||
traverse(vparam.tpt) | ||
tparams.foreach(tp => traverse(tp.rhs)) | ||
|
||
|
||
object ExtractSemanticDB: | ||
import java.nio.file.Path | ||
import java.nio.file.Files | ||
import java.nio.file.Paths | ||
|
||
val name: String = "extractSemanticDB" | ||
val description: String = "extract info into .semanticdb files" | ||
|
||
private def semanticdbTarget(using Context): Option[Path] = | ||
Option(ctx.settings.semanticdbTarget.value) | ||
.filterNot(_.isEmpty) | ||
.map(Paths.get(_)) | ||
|
||
private def outputDirectory(using Context): AbstractFile = ctx.settings.outputDir.value | ||
|
||
def write( | ||
source: SourceFile, | ||
occurrences: List[SymbolOccurrence], | ||
symbolInfos: List[SymbolInformation], | ||
synthetics: List[Synthetic], | ||
)(using Context): Unit = | ||
def absolutePath(path: Path): Path = path.toAbsolutePath.normalize | ||
val relPath = SourceFile.relativePath(source, ctx.settings.sourceroot.value) | ||
val outpath = absolutePath(semanticdbTarget.getOrElse(outputDirectory.jpath)) | ||
.resolve("META-INF") | ||
.resolve("semanticdb") | ||
.resolve(relPath) | ||
.resolveSibling(source.name + ".semanticdb") | ||
Files.createDirectories(outpath.getParent()) | ||
val doc: TextDocument = TextDocument( | ||
schema = Schema.SEMANTICDB4, | ||
language = Language.SCALA, | ||
uri = Tools.mkURIstring(Paths.get(relPath)), | ||
text = "", | ||
md5 = internal.MD5.compute(String(source.content)), | ||
symbols = symbolInfos, | ||
occurrences = occurrences, | ||
synthetics = synthetics, | ||
) | ||
val docs = TextDocuments(List(doc)) | ||
val out = Files.newOutputStream(outpath) | ||
try | ||
val stream = internal.SemanticdbOutputStream.newInstance(out) | ||
docs.writeTo(stream) | ||
stream.flush() | ||
finally | ||
out.close() | ||
end Extractor | ||
end ExtractSemanticDB |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.