aboutsummaryrefslogtreecommitdiff
path: root/java/com/google/turbine/diag/TurbineDiagnostic.java
blob: a38c98f23c9f7f7f1a4e9068d1cfc5ac88f3ba88 (plain)
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
/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.turbine.diag;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Iterables.getOnlyElement;
import static java.util.Objects.requireNonNull;

import com.google.common.base.CharMatcher;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.turbine.binder.sym.ClassSymbol;
import com.google.turbine.diag.TurbineError.ErrorKind;
import java.util.Objects;
import javax.tools.Diagnostic;
import org.checkerframework.checker.nullness.qual.Nullable;

/** A compilation error. */
public class TurbineDiagnostic {

  private final Diagnostic.Kind severity;
  private final ErrorKind kind;
  private final ImmutableList<Object> args;
  private final @Nullable SourceFile source;
  private final int position;

  private TurbineDiagnostic(
      Diagnostic.Kind severity,
      ErrorKind kind,
      ImmutableList<Object> args,
      @Nullable SourceFile source,
      int position) {
    this.severity = requireNonNull(severity);
    this.kind = requireNonNull(kind);
    this.args = requireNonNull(args);
    this.source = source;
    this.position = position;
  }

  /** The diagnostic kind. */
  public ErrorKind kind() {
    return kind;
  }

  /**
   * The diagnostic severity (error, warning, ...). Turbine only produces errors, non-error
   * diagnostics are only ever created by annotation processors.
   */
  public Diagnostic.Kind severity() {
    return severity;
  }

  boolean isError() {
    return severity.equals(Diagnostic.Kind.ERROR);
  }

  /** The diagnostic message. */
  public String diagnostic() {
    StringBuilder sb = new StringBuilder(path());
    if (line() != -1) {
      sb.append(':').append(line());
    }
    sb.append(": error: ");
    sb.append(message()).append(System.lineSeparator());
    if (line() != -1 && column() != -1) {
      requireNonNull(source); // line and column imply source is non-null
      sb.append(CharMatcher.breakingWhitespace().trimTrailingFrom(source.lineMap().line(position)))
          .append(System.lineSeparator());
      sb.append(Strings.repeat(" ", column() - 1)).append('^');
    }
    return sb.toString();
  }

  /** The diagnostic arguments. */
  public ImmutableList<Object> args() {
    return args;
  }

  private static TurbineDiagnostic create(
      Diagnostic.Kind severity,
      ErrorKind kind,
      ImmutableList<Object> args,
      SourceFile source,
      int position) {
    switch (kind) {
      case SYMBOL_NOT_FOUND:
        {
          checkArgument(
              args.size() == 1 && getOnlyElement(args) instanceof ClassSymbol,
              "diagnostic (%s) has invalid argument %s",
              kind,
              args);
          break;
        }
      default: // fall out
    }
    return new TurbineDiagnostic(severity, kind, args, source, position);
  }

  public static TurbineDiagnostic format(Diagnostic.Kind severity, ErrorKind kind, String message) {
    return create(severity, kind, ImmutableList.of(message), null, -1);
  }

  /**
   * Formats a diagnostic.
   *
   * @param source the current source file
   * @param kind the error kind
   * @param args format args
   */
  public static TurbineDiagnostic format(SourceFile source, ErrorKind kind, Object... args) {
    return create(Diagnostic.Kind.ERROR, kind, ImmutableList.copyOf(args), source, -1);
  }

  /**
   * Formats a diagnostic.
   *
   * @param position the diagnostic position
   * @param kind the error kind
   * @param args format args
   */
  public static TurbineDiagnostic format(
      Diagnostic.Kind severity, SourceFile source, int position, ErrorKind kind, Object... args) {
    return create(severity, kind, ImmutableList.copyOf(args), source, position);
  }

  @Override
  public int hashCode() {
    return Objects.hash(kind, source, position);
  }

  @Override
  public boolean equals(@Nullable Object obj) {
    if (!(obj instanceof TurbineDiagnostic)) {
      return false;
    }
    TurbineDiagnostic that = (TurbineDiagnostic) obj;
    return severity.equals(that.severity)
        && kind.equals(that.kind)
        && args.equals(that.args)
        && Objects.equals(source, that.source)
        && position == that.position;
  }

  public String path() {
    return source != null && source.path() != null ? source.path() : "<>";
  }

  @SuppressWarnings("nullness") // position != -1 implies source is non-null
  public int line() {
    return position != -1 ? source.lineMap().lineNumber(position) : -1;
  }

  @SuppressWarnings("nullness") // position != -1 implies source is non-null
  public int column() {
    return position != -1 ? source.lineMap().column(position) + 1 : -1;
  }

  public String message() {
    return kind.format(args.toArray());
  }
}