Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1395 dump on error fix #2

Closed
wants to merge 37 commits into from
Closed

1395 dump on error fix #2

wants to merge 37 commits into from

Conversation

aditya3434
Copy link
Owner

Fix for issue typetools#1395 (dump-on-error). Compound checkers such as nullness and index checkers were not giving adequate stack trace to pin-point where the error in the code had occurred. This was because in BaseTypeChecker, errors due to checkers with subcheckers were stored in CheckerMessage but those without were printed right away giving a correct stack trace. This storage caused the stack trace to unwind and take a different path. Since, all errors were stored in the same place, they gave the same stack trace when the -doe option is used. To fix this problem, I added a CheckerMessage constructor to store the stack trace when the error occurred. Then, I added a method to print this stored stack trace for all the stored errors. The code gives the correct stack trace when used with the '-AdumpOnErrors' compiler option.

Copy link

@wmdietl wmdietl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this issue!

@@ -565,10 +565,13 @@ protected void warnUnneededSuppressions() {
protected void printOrStoreMessage(
Diagnostic.Kind kind, String message, Tree source, CompilationUnitTree root) {
assert this.currentRoot == root;
StackTraceElement[] trace = new RuntimeException().getStackTrace();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Thread.getStackTrace() https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getStackTrace() instead of creating that exception object.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have used Thread.currentThread().getStackTrace() to acquire the stack trace.

if (messageStore == null) {
super.printOrStoreMessage(kind, message, source, root);
this.printStackTrace(trace);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need the explicit this in these calls.

*/
private void printStackTrace(StackTraceElement[] trace) {
boolean dumpOnErrors =
(processingEnv != null
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have used getBooleanOption instead of the previous logic, however, in this one, I have to provide a boolean value to the command line option for the flag to function.
Ex : javac -AdumpOnErrors=true -processor org.checkerframework.checker.nullness.NullnessChecker -implicit:class Test.java

&& processingEnv.getOptions() != null
&& processingEnv.getOptions().containsKey("dumpOnErrors"));
if (dumpOnErrors) {
System.err.println(new RuntimeException().toString());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't want this output, as it's a stacktrace in this same location for every error.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed this statement in my latest commit

if (dumpOnErrors) {
System.err.println(new RuntimeException().toString());
for (StackTraceElement elem : trace) {
System.err.println("\tat " + elem);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use System.err for any compiler output, always use the proper messager.
I would suggest using https://github.com/typetools/checker-framework/blob/master/framework/src/main/java/org/checkerframework/framework/source/SourceChecker.java#L1058 with diagnostic kind NOTE.
As you also have the diagnostic source, you could also use that location for the warning. How does doe output the message?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have used the given function to print the stack trace. I observed the -doe outputs using a printWriter in the Log.java file.

@@ -600,6 +623,17 @@ private void printStoredMessages(CompilationUnitTree unit) {
*/
final BaseTypeChecker checker;

/** Stores the stack trace till the point the checker encounters an error. */
StackTraceElement[] trace;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make final.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The field can be null, right? So it should be StackTraceElement @Nullable [] trace.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made the changes in my latest commit

* @param message error message that needs to be printed
* @param source tree element causing the error
* @param checker the type-checker in use
*/
private CheckerMessage(
Diagnostic.Kind kind, String message, Tree source, BaseTypeChecker checker) {
this.kind = kind;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This constructor should call the other constructor, providing null for the trace.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made the changes in my latest commit

String message,
Tree source,
BaseTypeChecker checker,
StackTraceElement[] trace) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also nullable.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made the changes in my latest commit

@@ -323,6 +323,10 @@
// org.checkerframework.framework.util.typeinference.DefaultTypeArgumentInference
"showInferenceSteps",

// Output stack trace when checker encounters errors
// org.checkerframework.common.basetype.BaseTypeChecker.printStackTrace()
"dumpOnErrors",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also add documentation to the manual.
There is already a comment on doe https://github.com/typetools/checker-framework/blob/d11aeaedaf4c039beb648ee0e3c177f894c060df/docs/manual/introduction.tex#L835
There you should mention this option.

You also need to document the feature itself, here in the introduction (find the appropriate place there) and in the corresponding section.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the documentation in the introduction.tex file in the debugging section.

@aditya3434 aditya3434 self-assigned this Jun 21, 2020
Copy link

@wmdietl wmdietl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still assigned to you, so I wasn't sure whether it's ready for another review. Find a few comments. Please move to typetools to not drop the ball on this.

@@ -792,6 +792,10 @@
Print information about the git repository from which the Checker Framework
was compiled.

\item
\<-AdumpOnErrors>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge this with the amount of detail section earlier in this document and then add the details in \ref{creating-debugging-options-detail}.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added the documentation in their corresponding sections in my latest commit

@@ -792,6 +792,10 @@
Print information about the git repository from which the Checker Framework
was compiled.

\item
\<-AdumpOnErrors>
Prints the stack trace when the checker framework encounters an error.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checker framework should be Checker Framework to be consistent.
Also output a stack trace when errors or warnings are reported.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made the changes in my latest commit

}

/**
* Prints given stack trace of runtime exception if -AdumpOnErrors flag is passed as compilation
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Output the given stack trace if the "dumpOnErrors" option is enabled.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made the changes in my latest commit

@@ -323,6 +323,10 @@
// org.checkerframework.framework.util.typeinference.DefaultTypeArgumentInference
"showInferenceSteps",

// Output stack trace when checker encounters errors
// org.checkerframework.common.basetype.BaseTypeChecker.printStackTrace()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the option be handled in this class, in method printOrStoreMessage, too?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we had to mention the place where the dumpOnErrors option was being checked. Since I was checking for the option in the printStackTrace() method, I wrote its path. The option is not used at all in the printOrStoreMessage (only the printStackTrace() method is called). Currently, I have not made any changes. Please let me know what you think I should do.

@wmdietl
Copy link

wmdietl commented Jul 21, 2020

Now that there is typetools#3406 don't you want to close this PR?

@aditya3434 aditya3434 closed this Jul 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants