aboutsummaryrefslogtreecommitdiff
path: root/src/dotty/tools/dotc/reporting/Reporter.scala
blob: 851aa084d78db16381f51788e1e23b28f5ae61ab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package dotty.tools
package dotc
package reporting

import core.Contexts._
import util.{SourcePosition, NoSourcePosition}
import util.{SourceFile, NoSource}
import core.Decorators.PhaseListDecorator
import collection.mutable
import config.Settings.Setting
import java.lang.System.currentTimeMillis

trait Reporting { this: Context =>
  def error(msg: String, pos: SourcePosition = NoSourcePosition): Unit = reporter.error(msg, pos)
  def warning(msg: String, pos: SourcePosition = NoSourcePosition): Unit = reporter.warning(msg, pos)
  def inform(msg: String, pos: SourcePosition = NoSourcePosition): Unit = reporter.info(msg, pos)

  def log(msg: => String): Unit =
    if (this.settings.log.value.containsPhase(phase))
      inform(s"[log ${ctx.phasesStack.reverse.mkString(" -> ")}] $msg")

  def debuglog(msg: => String): Unit =
    if (ctx.debug) log(msg)

  def informTime(msg: => String, start: Long): Unit =
    informProgress(msg + elapsed(start))

  def deprecation = reporter.deprecation
  def unchecked = reporter.unchecked
  def feature = reporter.feature

  private def elapsed(start: Long) =
    " in " + (currentTimeMillis - start) + "ms"

  def informProgress(msg: => String) =
    if (this.settings.verbose.value) inform("[" + msg + "]")

  def trace[T](msg: => String)(value: T) = {
    log(msg + " " + value)
    value
  }

  def debugwarn(msg: String, pos: SourcePosition = NoSourcePosition): Unit =
    if (this.settings.debug.value) warning(msg, pos)

  def debugTraceIndented[T](question: => String)(op: => T): T =
    if (this.settings.debugTrace.value) traceIndented(question)(op)
    else op

  def traceIndented[T](question: => String)(op: => T): T =
    traceIndented[T](s"==> $question?", (res: Any) => s"<== $question = $res")(op)

  def traceIndented[T](leading: => String, trailing: Any => String)(op: => T): T = {
    var finalized = false
    def finalize(result: Any, note: String) =
      if (!finalized) {
        base.indent -= 1
        log(s"${base.indentTab * base.indent}${trailing(result)}$note")
        finalized = true
      }
    try {
      log(s"${base.indentTab * base.indent}$leading")
      base.indent += 1
      val res = op
      finalize(res, "")
      res
    } catch {
      case ex: Throwable =>
        finalize("<missing>", s" (with exception $ex)")
        throw ex
    }
  }
}

object Reporter {
  object Severity extends Enumeration {
    val INFO, WARNING, ERROR = Value
  }
}

/**
 * This interface provides methods to issue information, warning and
 * error messages.
 */
abstract class Reporter(ctx: Context) {

  import Reporter.Severity.{Value => Severity, _}

  protected def report(msg: String, severity: Severity, pos: SourcePosition)(implicit ctx: Context): Unit

  protected def isHidden(severity: Severity, pos: SourcePosition)(implicit ctx: Context) = false

  val count = new mutable.HashMap[Severity, Int]() {
    override def default(key: Severity) = 0
  }

  /** Whether very long lines can be truncated.  This exists so important
   *  debugging information (like printing the classpath) is not rendered
   *  invisible due to the max message length.
   */
  private var _truncationOK: Boolean = true
  def truncationOK = _truncationOK
  def withoutTruncating[T](body: => T): T = {
    val saved = _truncationOK
    _truncationOK = false
    try body
    finally _truncationOK = saved
  }

  type ErrorHandler = (String, SourcePosition, Context) => Unit
  private var incompleteHandler: ErrorHandler = error(_, _)(_)
  def withIncompleteHandler[T](handler: ErrorHandler)(op: => T): T = {
    val saved = incompleteHandler
    incompleteHandler = handler
    try op
    finally incompleteHandler = saved
  }

  def hasErrors   = count(ERROR) > 0
  def hasWarnings = count(WARNING) > 0

  /** For sending messages that are printed only if -verbose is set */
  def info(msg: String, pos: SourcePosition = NoSourcePosition)(implicit ctx: Context): Unit =
    if (ctx.settings.verbose.value) info0(msg, INFO, pos)

  /** For sending a message which should not be labeled as a warning/error,
   *  but also shouldn't require -verbose to be visible.
   */
  def echo(msg: String, pos: SourcePosition = NoSourcePosition)(implicit ctx: Context): Unit =
    info0(msg, INFO, pos)

  def warning(msg: String, pos: SourcePosition = NoSourcePosition)(implicit ctx: Context): Unit =
    if (!ctx.settings.nowarn.value)
      withoutTruncating(info0(msg, WARNING, pos))

  def error(msg: String, pos: SourcePosition = NoSourcePosition)(implicit ctx: Context): Unit =
    withoutTruncating(info0(msg, ERROR, pos))

  def incompleteInputError(msg: String, pos: SourcePosition = NoSourcePosition)(implicit ctx: Context): Unit =
    incompleteHandler(msg, pos, ctx)

  private def info0(msg: String, severity: Severity, pos: SourcePosition)(implicit ctx: Context): Unit = {
    if (!isHidden(severity, pos)) {
      count(severity) += 1
      report(msg, severity, pos)
    }
  }

  /** Returns a string meaning "n elements". */
  private def countElementsAsString(n: Int, elements: String): String =
    n match {
      case 0 => "no "    + elements + "s"
      case 1 => "one "   + elements
      case 2 => "two "   + elements + "s"
      case 3 => "three " + elements + "s"
      case 4 => "four "  + elements + "s"
      case _ => n + " " + elements + "s"
    }

  protected def label(severity: Severity): String = severity match {
    case INFO    => ""
    case ERROR   => "error: "
    case WARNING => "warning: "
  }

  protected def countString(severity: Severity) = {
    assert(severity != INFO)
    countElementsAsString(count(severity), label(severity).dropRight(2))
  }

  def printSummary(implicit ctx: Context) {
    if (count(WARNING) > 0) info(countString(WARNING) + " found")
    if (  count(ERROR) > 0) info(countString(ERROR  ) + " found")
    allConditionalWarnings foreach (_.summarize)
  }

  def flush(): Unit = {}

  def reset(): Unit = {
    count.clear()
    allConditionalWarnings foreach (_.clear())
  }

  protected val allConditionalWarnings = new mutable.ListBuffer[ConditionalWarning]

  val deprecation = new ConditionalWarning("deprecation", ctx.settings.deprecation)
  val unchecked = new ConditionalWarning("unchecked", ctx.settings.unchecked)
  val feature = new ConditionalWarning("feature", ctx.settings.feature)

  /** Collects for certain classes of warnings during this run. */
  class ConditionalWarning(what: String, option: Setting[Boolean]) {
    private var unreported: Int = 0
    def clear() =
      unreported = 0
    def warning(msg: String, pos: SourcePosition = NoSourcePosition)(implicit ctx: Context) =
      if (option.value) Reporter.this.warning(msg, pos)
      else unreported += 1
    def summarize(implicit ctx: Context) =
      if (unreported > 0)
        Reporter.this.warning(s"there were $unreported $what warning(s); re-run with ${option.name} for details")
    allConditionalWarnings += this
  }
}