Skip to content
Open
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
6 changes: 2 additions & 4 deletions java/com/google/turbine/binder/Processing.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.google.auto.value.AutoValue;
import com.google.common.base.Joiner;
import com.google.common.base.Stopwatch;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -296,9 +295,8 @@ static SupportedAnnotationTypes create(Processor processor) {
private static void logProcessorCrash(TurbineLog log, Processor processor, Throwable t) {
log.diagnostic(
Diagnostic.Kind.ERROR,
String.format(
"An exception occurred in %s:\n%s",
processor.getClass().getCanonicalName(), Throwables.getStackTraceAsString(t)));
String.format("An exception occurred in %s:", processor.getClass().getCanonicalName()),
t);
}

/** Returns a map from annotations present in the compilation to the annotated elements. */
Expand Down
34 changes: 30 additions & 4 deletions java/com/google/turbine/diag/TurbineDiagnostic.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static java.util.Objects.requireNonNull;

import com.google.common.base.CharMatcher;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.turbine.binder.sym.ClassSymbol;
import com.google.turbine.diag.TurbineError.ErrorKind;
Expand All @@ -37,18 +38,21 @@ public class TurbineDiagnostic {
private final ImmutableList<Object> args;
private final @Nullable SourceFile source;
private final int position;
private final @Nullable Throwable cause;

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

/** The diagnostic kind. */
Expand Down Expand Up @@ -85,6 +89,9 @@ public String diagnostic() {
sb.append(": ").append(severity);
}
sb.append(": ").append(message());
if (cause != null) {
sb.append(System.lineSeparator()).append(Throwables.getStackTraceAsString(cause));
}
if (position != -1) {
sb.append(System.lineSeparator());
sb.append(lineSource());
Expand All @@ -104,7 +111,8 @@ private static TurbineDiagnostic create(
ErrorKind kind,
ImmutableList<Object> args,
SourceFile source,
int position) {
int position,
@Nullable Throwable cause) {
switch (kind) {
case SYMBOL_NOT_FOUND ->
checkArgument(
Expand All @@ -114,13 +122,31 @@ private static TurbineDiagnostic create(
args);
default -> {}
}
return new TurbineDiagnostic(severity, kind, args, source, position);
return new TurbineDiagnostic(severity, kind, args, source, position, cause);
}

private static TurbineDiagnostic create(
Diagnostic.Kind severity,
ErrorKind kind,
ImmutableList<Object> args,
SourceFile source,
int position) {
return create(severity, kind, args, source, position, null);
}

public @Nullable Throwable cause() {
return cause;
}

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

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

public static TurbineDiagnostic format(Diagnostic.Kind severity, ErrorKind kind) {
return create(severity, kind, ImmutableList.of(), null, -1);
}
Expand Down Expand Up @@ -149,7 +175,7 @@ public static TurbineDiagnostic format(
}

public TurbineDiagnostic withPosition(SourceFile source, int position) {
return new TurbineDiagnostic(severity, kind, args, source, position);
return new TurbineDiagnostic(severity, kind, args, source, position, cause);
}

@Override
Expand Down
7 changes: 7 additions & 0 deletions java/com/google/turbine/diag/TurbineLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ public void diagnostic(Diagnostic.Kind severity, String message) {
add(TurbineDiagnostic.format(severity, ErrorKind.PROC, message));
}

/**
* Reports an annotation processing diagnostic with no position information, retaining the cause.
*/
public void diagnostic(Diagnostic.Kind severity, String message, Throwable t) {
add(TurbineDiagnostic.format(severity, ErrorKind.PROC, message, t));
}

public void add(TurbineDiagnostic diagnostic) {
synchronized (lock) {
diagnostics.add(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ public void crash() throws IOException {
e.diagnostics().stream().map(TurbineDiagnostic::message).collect(toImmutableList());
assertThat(messages).hasSize(2);
assertThat(messages.getFirst()).contains("could not resolve NoSuch");
assertThat(messages.get(1)).contains("crash!");
assertThat(messages.get(1))
.contains(
"An exception occurred in"
+ " com.google.turbine.processing.ProcessingIntegrationTest.CrashingProcessor:");
assertThat(e.diagnostics().get(1).cause()).hasMessageThat().contains("crash!");
}

@SupportedAnnotationTypes("*")
Expand Down
Loading