Skip to content

Commit

Permalink
Add dump-on-error option (Fixes #1395) (#3406)
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya3434 authored Jul 31, 2020
1 parent 844034d commit e8be209
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
2 changes: 2 additions & 0 deletions docs/manual/creating-a-checker.tex
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,8 @@
already reported the bug, and you want to continue using the checker on a
large codebase without being inundated in stack traces.

\item \code{-AdumpOnErrors}: Outputs a stack trace when reporting errors or warnings.

\end{itemize}


Expand Down
1 change: 1 addition & 0 deletions docs/manual/introduction.tex
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,7 @@
\<-AprintAllQualifiers>,
\<-AprintVerboseGenerics>,
\<-Anomsgtext>
\<-AdumpOnErrors>
Amount of detail in messages; see Section~\ref{creating-debugging-options-detail}.

\item
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,10 +568,11 @@ protected void warnUnneededSuppressions() {
protected void printOrStoreMessage(
Diagnostic.Kind kind, String message, Tree source, CompilationUnitTree root) {
assert this.currentRoot == root;
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
if (messageStore == null) {
super.printOrStoreMessage(kind, message, source, root);
super.printOrStoreMessage(kind, message, source, root, trace);
} else {
CheckerMessage checkerMessage = new CheckerMessage(kind, message, source, this);
CheckerMessage checkerMessage = new CheckerMessage(kind, message, source, this, trace);
messageStore.add(checkerMessage);
}
}
Expand All @@ -586,7 +587,7 @@ protected void printOrStoreMessage(
private void printStoredMessages(CompilationUnitTree unit) {
if (messageStore != null) {
for (CheckerMessage msg : messageStore) {
super.printOrStoreMessage(msg.kind, msg.message, msg.source, unit);
super.printOrStoreMessage(msg.kind, msg.message, msg.source, unit, msg.trace);
}
}
}
Expand All @@ -599,6 +600,8 @@ private static class CheckerMessage {
final String message;
/** The source code that the message is about. */
final @InternedDistinct Tree source;
/** Stores the stack trace when the message is created. */
final StackTraceElement[] trace;

/**
* The checker that issued this message. The compound checker that depends on this checker
Expand All @@ -609,20 +612,23 @@ private static class CheckerMessage {
/**
* Create a new CheckerMessage.
*
* @param kind the severity of the message
* @param message the text of the message
* @param source the source code that the message is about
* @param checker the checker that issued the message.
* @param kind kind of diagnostic, for example, error or warning
* @param message error message that needs to be printed
* @param source tree element causing the error
* @param checker the type-checker in use
* @param trace the stack trace when the message is created
*/
private CheckerMessage(
Diagnostic.Kind kind,
String message,
@FindDistinct Tree source,
@FindDistinct BaseTypeChecker checker) {
@FindDistinct BaseTypeChecker checker,
StackTraceElement[] trace) {
this.kind = kind;
this.message = message;
this.source = source;
this.checker = checker;
this.trace = trace;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.tools.Diagnostic;
import javax.tools.Diagnostic.Kind;
import org.checkerframework.checker.compilermsgs.qual.CompilerMessageKey;
import org.checkerframework.checker.interning.qual.InternedDistinct;
Expand Down Expand Up @@ -317,6 +318,10 @@
// org.checkerframework.framework.util.typeinference.DefaultTypeArgumentInference
"showInferenceSteps",

// Output a stack trace when reporting errors or warnings
// org.checkerframework.common.basetype.SourceChecker.printStackTrace()
"dumpOnErrors",

/// Visualizing the CFG

// Implemented in the wrapper rather than this file, but worth noting here.
Expand Down Expand Up @@ -1073,15 +1078,52 @@ private void printMessage(String msg) {
*
* @param kind the kind of message to print
* @param message the message text
* @param source the souce code position of the diagnostic message
* @param source the source code position of the diagnostic message
* @param root the compilation unit
*/
protected void printOrStoreMessage(
javax.tools.Diagnostic.Kind kind,
String message,
Tree source,
CompilationUnitTree root) {
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
printOrStoreMessage(kind, message, source, root, trace);
}

/**
* Stores all messages and sorts them by location before outputting them for compound checkers.
* This method is overloaded with an additional stack trace argument. The stack trace is printed
* when the dumpOnErrors option is enabled.
*
* @param kind the kind of message to print
* @param message the message text
* @param source the source code position of the diagnostic message
* @param root the compilation unit
* @param trace the stack trace where the checker encountered an error
*/
protected void printOrStoreMessage(
javax.tools.Diagnostic.Kind kind,
String message,
Tree source,
CompilationUnitTree root,
StackTraceElement[] trace) {
Trees.instance(processingEnv).printMessage(kind, message, source, root);
printStackTrace(trace);
}

/**
* Output the given stack trace if the "dumpOnErrors" option is enabled.
*
* @param trace stack trace when the checker encountered a warning/error
*/
private void printStackTrace(StackTraceElement[] trace) {
if (hasOption("dumpOnErrors")) {
StringBuilder msg = new StringBuilder();
for (StackTraceElement elem : trace) {
msg.append("\tat " + elem + "\n");
}
message(Diagnostic.Kind.NOTE, msg.toString());
}
}

///////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit e8be209

Please sign in to comment.