Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Overview
The kinds of compilation-time data that `sourcecode` provides are:

- `sourcecode.File`: full path of the current file where the call occurs. This one results in non-reproducible byte-code, since the absolute code location is stored.
- `sourcecode.RelFile`: path of the current file where the call occurs, but relativized to a specific locations (given with compiler option `-Xmacro-settings:sourcecode.relfile.root=<path>`). Useful to keep significant source file path while also retaining reproducible byte code. Only works on Scala 3. On Scala 2 it behaves just like `sourcecode.File`.
- `sourcecode.RelFile`: path of the current file where the call occurs, but relativized to a specific locations (given with compiler option `-Xmacro-settings:sourcecode.relfile.root=<path>`). Useful to keep significant source file path while also retaining reproducible byte code.
- `sourcecode.FileName`: name of the current file where the call occurs; less
verbose than `sourcecode.File` or `sourcecode.RelFile` but often enough for debugging purposes
- `sourcecode.Line`: current line number
Expand Down
2 changes: 1 addition & 1 deletion build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ trait MimaCheck extends Mima { this: ScalaModule =>
else Seq()) ++ Seq(
"0.2.7", "0.2.8",
"0.3.0", "0.3.1",
"0.4.0", "0.4.1", "0.4.2", "0.4.4"
"0.4.0", "0.4.1", "0.4.2", "0.4.4", "0.4.5"
)
}

Expand Down
58 changes: 49 additions & 9 deletions sourcecode/src-2/sourcecode/Macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,21 @@ trait PkgMacros {

trait TextMacros {
implicit def generate[T](v: T): Text[T] = macro Macros.text[T]

def apply[T](v: T): Text[T] = macro Macros.text[T]
}

trait ArgsMacros {
implicit def generate: Args = macro Macros.argsImpl
}

object Util{
object Util {
def isSynthetic(c: Compat.Context)(s: c.Symbol) = isSyntheticName(getName(c)(s))

def isSyntheticName(name: String) = {
name == "<init>" || (name.startsWith("<local ") && name.endsWith(">")) || name == "$anonfun"
}

def getName(c: Compat.Context)(s: c.Symbol) = s.name.decoded.toString.trim
}

Expand All @@ -71,7 +74,7 @@ object Macros {
def nameImpl(c: Compat.Context): c.Expr[Name] = {
import c.universe._
var owner = Compat.enclosingOwner(c)
while(Util.isSynthetic(c)(owner)) owner = owner.owner
while (Util.isSynthetic(c)(owner)) owner = owner.owner
val simpleName = Util.getName(c)(owner)
c.Expr[sourcecode.Name](q"""${c.prefix}($simpleName)""")
}
Expand Down Expand Up @@ -104,6 +107,7 @@ object Macros {
private val filePrefix = "//SOURCECODE_ORIGINAL_FILE_PATH="
private val filePrefixCache =
new ConcurrentHashMap[scala.reflect.internal.util.SourceFile, Option[String]]()

private def findOriginalFile(chars: Array[Char]): Option[String] = {
new String(chars).linesIterator
.find(_.contains(filePrefix))
Expand All @@ -124,14 +128,41 @@ object Macros {

def relFileImpl(c: Compat.Context): c.Expr[sourcecode.RelFile] = {
import c.universe._
val prefix = "sourcecode.relfile.root="
val relBase = c.settings.collectFirst {
case s if s.startsWith(prefix) => s.stripPrefix(prefix)
}
// in case we got a posix path config on Windows
.map(_.replace("/", separator))

val file = filePrefixCache
.computeIfAbsent(c.enclosingPosition.source, source => findOriginalFile(source.content))
.getOrElse(c.enclosingPosition.source.path)

// FIXME: currently, we don't support relative file
if (relBase.isEmpty) c.warning(
c.enclosingPosition,
s"Base path not specified. Using path '${file}' as-is. Make sure to use compiler argument: -Xmacro-settings:${prefix}<path>"
)

val relFile0: Option[String] = relBase
.map { base =>
if (base.isEmpty) base
else if (!base.endsWith(separator)) base + separator
else base
}
.flatMap { base =>
if (file.startsWith(base)) Some(file.stripPrefix(base))
else None
}

if (relFile0.isEmpty) c.warning(
c.enclosingPosition,
s"Base path ${relBase.getOrElse("''")} is not a prefix of source file path ${file}"
)

c.Expr[sourcecode.RelFile](q"""${c.prefix}($file)""")
val relFile = relFile0.getOrElse(file)

c.Expr[sourcecode.RelFile](q"""${c.prefix}($relFile)""")
}

def fileNameImpl(c: Compat.Context): c.Expr[sourcecode.FileName] = {
Expand All @@ -146,14 +177,14 @@ object Macros {
private val linePrefix = "//SOURCECODE_ORIGINAL_CODE_START_MARKER"
private val linePrefixCache =
new ConcurrentHashMap[scala.reflect.internal.util.SourceFile, Int]()

def lineImpl(c: Compat.Context): c.Expr[sourcecode.Line] = {
import c.universe._
val offset = linePrefixCache.computeIfAbsent(
c.enclosingPosition.source,
source => new String(source.content)
.linesIterator
.indexWhere(_.contains(linePrefix)) match{
.indexWhere(_.contains(linePrefix)) match {
case -1 => 0
case n => n + 1
}
Expand Down Expand Up @@ -198,15 +229,24 @@ object Macros {
val tree = q"""${c.prefix}(${v.tree}, $txt)"""
c.Expr[sourcecode.Text[T]](tree)
}

sealed trait Chunk
object Chunk{

object Chunk {
case class Pkg(name: String) extends Chunk

case class Obj(name: String) extends Chunk

case class Cls(name: String) extends Chunk

case class Trt(name: String) extends Chunk

case class Val(name: String) extends Chunk

case class Var(name: String) extends Chunk

case class Lzy(name: String) extends Chunk

case class Def(name: String) extends Chunk

}
Expand All @@ -216,7 +256,7 @@ object Macros {
import c.universe._
var current = Compat.enclosingOwner(c)
var path = List.empty[Chunk]
while(current != NoSymbol && current.toString != "package <root>"){
while (current != NoSymbol && current.toString != "package <root>") {
if (filter(current)) {

val chunk = current match {
Expand All @@ -234,7 +274,7 @@ object Macros {
}
current = current.owner
}
val renderedPath = path.map{
val renderedPath = path.map {
case Chunk.Pkg(s) => s + "."
case Chunk.Obj(s) => s + "."
case Chunk.Cls(s) => s + "#"
Expand Down
4 changes: 2 additions & 2 deletions sourcecode/src-3/sourcecode/Macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ object Macros {
.getOrElse(sourceFile.path)

if (relBase.isEmpty) report.warning(
s"Base path not specified. Using absolute file path '${file}'. Make sure to use compiler argument: -Xmacro-settings:${prefix}<path>",
s"Base path not specified. Using path '${file}' as-is. Make sure to use compiler argument: -Xmacro-settings:${prefix}<path>",
Position.ofMacroExpansion
)
val relFile0: Option[String] = relBase
Expand All @@ -226,7 +226,7 @@ object Macros {
}

if (relFile0.isEmpty) report.warning(
s"Base path ${relBase.getOrElse("''")} is not an prefix of source file path ${file}",
s"Base path ${relBase.getOrElse("''")} is not a prefix of source file path ${file}",
Position.ofMacroExpansion
)

Expand Down
7 changes: 0 additions & 7 deletions sourcecode/test/src-2/sourcecode/TestUtil.scala

This file was deleted.

7 changes: 0 additions & 7 deletions sourcecode/test/src-3/sourcecode/TestUtil.scala

This file was deleted.

20 changes: 6 additions & 14 deletions sourcecode/test/src/sourcecode/Apply.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,14 @@ object Apply {
assert(posix(file).endsWith("/sourcecode/Apply.scala"))

val relFile = sourcecode.RelFile()
if (TestUtil.isScala3) {
val relFileExpected = "sourcecode/test/src/sourcecode/Apply.scala"
assertEquals(posix(relFile), relFileExpected)
} else {
assert(posix(relFile).endsWith("/sourcecode/Apply.scala"))
}
val relFileExpected = "sourcecode/test/src/sourcecode/Apply.scala"
assertEquals(posix(relFile), relFileExpected)

val fileName = sourcecode.FileName()
assertEquals(fileName, "Apply.scala")

val line = sourcecode.Line()
assertEquals(line, 33) // Line-sensitive magic number !
assertEquals(line, 29) // Line-sensitive magic number !

lazy val myLazy = {
/* Bar used to be a trait, but that ran into the upstream bug
Expand All @@ -49,18 +45,14 @@ object Apply {
assert(posix(file).endsWith("/sourcecode/Apply.scala"))

val relFile = sourcecode.RelFile()
if (TestUtil.isScala3) {
val relFileExpected = "sourcecode/test/src/sourcecode/Apply.scala"
assertEquals(posix(relFile), relFileExpected)
} else {
assert(posix(relFile).endsWith("/sourcecode/Apply.scala"))
}
val relFileExpected = "sourcecode/test/src/sourcecode/Apply.scala"
assertEquals(posix(relFile), relFileExpected)

val fileName = sourcecode.FileName()
assertEquals(fileName, "Apply.scala")

val line = sourcecode.Line()
assertEquals(line, 62) // Line-sensitive magic number !
assertEquals(line, 54) // Line-sensitive magic number !

val enclosing = sourcecode.Enclosing()
assert(
Expand Down
13 changes: 4 additions & 9 deletions sourcecode/test/src/sourcecode/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,14 @@ object Implicits {
assert(file.posix.endsWith("/sourcecode/Implicits.scala"))

val relFile = implicitly[sourcecode.RelFile]
if (TestUtil.isScala3) {
assertEquals(posix(relFile.value), "sourcecode/test/src/sourcecode/Implicits.scala")
assertEquals(relFile.posix, "sourcecode/test/src/sourcecode/Implicits.scala")
} else {
assert(posix(relFile.value).endsWith("sourcecode/test/src/sourcecode/Implicits.scala"))
assert(relFile.posix.endsWith("sourcecode/test/src/sourcecode/Implicits.scala"))
}
assertEquals(posix(relFile.value), "sourcecode/test/src/sourcecode/Implicits.scala")
assertEquals(relFile.posix, "sourcecode/test/src/sourcecode/Implicits.scala")

val fileName = implicitly[sourcecode.FileName]
assertEquals(fileName.value, "Implicits.scala")

val line = implicitly[sourcecode.Line]
assertEquals(line.value, 35) // Line-sensitive magic number !
assertEquals(line.value, 30) // Line-sensitive magic number !

lazy val myLazy = {
/* Bar used to be a trait, but that ran into the upstream bug
Expand All @@ -58,7 +53,7 @@ object Implicits {
assertEquals(fileName.value, "Implicits.scala")

val line = implicitly[sourcecode.Line]
assertEquals(line.value, 60) // Line-sensitive magic number !
assertEquals(line.value, 55) // Line-sensitive magic number !

val enclosing = implicitly[sourcecode.Enclosing]
assert(
Expand Down
Loading