8000 GRAPH-775 annotation processing fix (#732) · tjni/scip-java@8602980 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8602980

Browse files
GRAPH-775 annotation processing fix (sourcegraph#732)
* Fix exception reporting in javac plugin Without closing the print writer, empty message was produced. * (root cause) add unary tree to semanticdb and handle it * Allow regenerating a subset of snapshots * Add link to JLS
1 parent b8c11c4 commit 8602980

File tree

8 files changed

+190
-4
lines changed

8 files changed

+190
-4
lines changed

scip-semanticdb/src/main/java/com/sourcegraph/scip_semanticdb/SignatureFormatter.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,11 +468,39 @@ private String formatTree(Tree tree) {
468468
return formatTree(tree.getAssignTree().getLhs())
469469
+ " = "
470470
+ formatTree(tree.getAssignTree().getRhs());
471+
} else if (tree.hasUnaryopTree()) {
472+
return formatUnaryOperation(tree.getUnaryopTree());
471473
}
472474

473475
throw new IllegalArgumentException("tree was of unexpected type " + tree);
474476
}
475477

478+
private String formatUnaryOperation(UnaryOperatorTree tree) {
479+
String formattedValue = formatTree(tree.getTree());
480+
switch (tree.getOp()) {
481+
case UNARY_MINUS:
482+
return "-" + formattedValue;
483+
case UNARY_PLUS:
484+
return "-" + formattedValue;
485+
case UNARY_POSTFIX_INCREMENT:
486+
return formattedValue + "++";
487+
case UNARY_POSTFIX_DECREMENT:
488+
return formattedValue + "--";
489+
case UNARY_PREFIX_DECREMENT:
490+
return "--" + formattedValue;
491+
case UNARY_PREFIX_INCREMENT:
492+
return "++" + formattedValue;
493+
494+
case UNARY_BITWISE_COMPLEMENT:
495+
return "~" + formattedValue;
496+
case UNARY_LOGICAL_COMPLEMENT:
497+
return "!" + formattedValue;
498+
}
499+
500+
throw new IllegalArgumentException(
501+
"unary operation of unexpected type" + tree.getOp().toString());
502+
}
503+
476504
private String formatConstant(Constant constant) {
477505
if (constant.hasUnitConstant()) {
478506
return isScala ? "()" : "scala.Unit()";

semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbBuilders.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,15 @@ public static Semanticdb.BinaryOperatorTree binopTree(
146146
.build();
147147
}
148148

149+
public static Semanticdb.Tree tree(Semanticdb.UnaryOperatorTree unaryOperatorTree) {
150+
return Semanticdb.Tree.newBuilder().setUnaryopTree(unaryOperatorTree).build();
151+
}
152+
153+
public static Semanticdb.UnaryOperatorTree unaryOpTree(
154+
Semanticdb.UnaryOperator operator, Semanticdb.Tree rhs) {
155+
return Semanticdb.UnaryOperatorTree.newBuilder().setOp(operator).setTree(rhs).build();
156+
}
157+
149158
public static Semanticdb.Tree tree(Semanticdb.AssignTree assignTree) {
150159
return Semanticdb.Tree.newBuilder().setAssignTree(assignTree).build();
151160
}

semanticdb-java/src/main/protobuf/semanticdb.proto

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ message Tree {
297297
AnnotationTree annotation_tree = 9;
298298
AssignTree assign_tree = 10;
299299
BinaryOperatorTree binop_tree = 11;
300+
UnaryOperatorTree unaryop_tree = 12;
300301
// -- OUT OF SPEC -- //
301302
}
302303
}
@@ -378,6 +379,23 @@ enum BinaryOperator {
378379 2364
GREATER_THAN_EQUAL = 17;
379380
LESS_THAN_EQUAL = 18;
380381
}
382+
383+
// https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.15
384+
message UnaryOperatorTree {
385+
UnaryOperator op = 1;
F438
386+
Tree tree = 2;
387+
}
388+
389+
enum UnaryOperator {
390+
UNARY_MINUS = 0;
391+
UNARY_PLUS = 1;
392+
UNARY_POSTFIX_INCREMENT = 2;
393+
UNARY_POSTFIX_DECREMENT = 3;
394+
UNARY_PREFIX_DECREMENT = 4;
395+
UNARY_PREFIX_INCREMENT = 5;
396+
UNARY_BITWISE_COMPLEMENT = 6;
397+
UNARY_LOGICAL_COMPLEMENT = 7;
398+
}
381399
// -- OUT OF SPEC -- //
382400

383401
message Constant {

semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ public void finished(TaskEvent e) {
8383
// exception, it just prints the location with an empty message.
8484
private void reportException(Throwable exception, TaskEvent e) {
8585
ByteArrayOutputStream baos = new ByteArrayOutputStream();
86-
exception.printStackTrace(new PrintWriter(baos));
86+
PrintWriter pw = new PrintWriter(baos);
87+
exception.printStackTrace(pw);
88+
pw.close();
8789
reporter.error(baos.toString(), e.getCompilationUnit(), e.getCompilationUnit());
8890
}
8991

semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTrees.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.sun.source.tree.Tree;
99
import com.sun.source.tree.Tree.Kind;
1010
import com.sun.source.tree.BinaryTree;
11+
import com.sun.source.tree.UnaryTree;
1112
import com.sun.source.tree.AssignmentTree;
1213
import com.sun.source.tree.MemberSelectTree;
1314
import com.sun.source.tree.ClassTree;
@@ -78,7 +79,8 @@ public Semanticdb.AnnotationTree annotationBuilder(AnnotationTree annotation) {
7879
ArrayList<Semanticdb.Tree> params = new ArrayList<>(annotation.getArguments().size());
7980

8081
for (ExpressionTree param : annotation.getArguments()) {
81-
// anecdotally not always AssignmentTree in some situations when a compilation unit can't
82+
// anecdotally not always AssignmentTree in some situations when a compilation
83+
// unit can't
8284
// resolve symbols fully
8385
if (param instanceof AssignmentTree) {
8486
AssignmentTree assign = (AssignmentTree) param;
@@ -152,6 +154,12 @@ private Semanticdb.Tree annotationParameter(ExpressionTree expr) {
152154
annotationParameter(binExpr.getLeftOperand()),
153155
semanticdbBinaryOperator(expr.getKind()),
154156
annotationParameter(binExpr.getRightOperand())));
157+
} else if (expr instanceof UnaryTree) {
158+
UnaryTree unaryExpr = (UnaryTree) expr;
159+
return tree(
160+
unaryOpTree(
161+
semanticdbUnaryOperator(unaryExpr.getKind()),
162+
annotationParameter(unaryExpr.getExpression())));
155163
}
156164
throw new IllegalArgumentException(
157165
semanticdbUri
@@ -206,4 +214,36 @@ private Semanticdb.BinaryOperator semanticdbBinaryOperator(Tree.Kind kind) {
206214
semanticdbUri + ": unexpected binary expression operator kind " + kind);
207215
}
208216
}
217+
218+
private Semanticdb.UnaryOperator semanticdbUnaryOperator(Tree.Kind kind) {
219+
switch (kind) {
220+
case UNARY_MINUS:
221+
return Semanticdb.UnaryOperator.UNARY_MINUS;
222+
223+
case UNARY_PLUS:
224+
return Semanticdb.UnaryOperator.UNARY_PLUS;
225+
226+
case POSTFIX_INCREMENT:
227+
return Semanticdb.UnaryOperator.UNARY_POSTFIX_INCREMENT;
228+
229+
case POSTFIX_DECREMENT:
230+ 6377
return Semanticdb.UnaryOperator.UNARY_POSTFIX_DECREMENT;
231+
232+
case PREFIX_INCREMENT:
233+
return Semanticdb.UnaryOperator.UNARY_PREFIX_INCREMENT;
234+
235+
case PREFIX_DECREMENT:
236+
return Semanticdb.UnaryOperator.UNARY_PREFIX_DECREMENT;
237+
238+
case BITWISE_COMPLEMENT:
239+
return Semanticdb.UnaryOperator.UNARY_BITWISE_COMPLEMENT;
240+
241+
case LOGICAL_COMPLEMENT:
242+
return Semanticdb.UnaryOperator.UNARY_LOGICAL_COMPLEMENT;
243+
244+
default:
245+
throw new IllegalStateException(
246+
semanticdbUri + ": unexpected unary expression operator kind " + kind);
247+
}
248+
}
209249
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package minimized;
2+
3+
4+
@interface Bar {
5+
double value();
6+
}
7+
8+
@interface BarB {
9+
boolean value();
10+
}
11+
12+
interface Foo {
13+
@Bar(-1d)
14+
double test();
15+
16+
@Bar(~5)
17+
@SuppressWarnings(value = "unchecked")
18+
double test2();
19+
20+
@BarB(!true)
21+
double test3();
22+
}
23+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package minimized;
2+
3+
4+
@interface Bar {
5+
// ^^^ definition semanticdb maven . . minimized/Bar#
6+
// display_name Bar
7+
// signature_documentation java @interface Bar
8+
// kind Interface
9+
// relationship is_implementation semanticdb maven jdk 11 java/lang/annotation/Annotation#
10+
double value();
11+
}
12+
13+
@interface BarB {
14+
// ^^^^ definition semanticdb maven . . minimized/BarB#
15+
// display_name BarB
16+
// signature_documentation java @interface BarB
17+
// kind Interface
18+
// relationship is_implementation semanticdb maven jdk 11 java/lang/annotation/Annotation#
19+
boolean value();
20+
}
21+
22+
interface Foo {
23+
// ^^^ definition semanticdb maven . . minimized/Foo#
24+
// display_name Foo
25+
// signature_documentation java interface Foo
26+
// kind Interface
27+
@Bar(-1d)
28+
// ^^^ reference semanticdb maven . . minimized/Bar#
29+
double test();
30+
// ^^^^ definition semanticdb maven . . minimized/Foo#test().
31+
// display_name test
32+
// signature_documentation java @Bar(-1.0)\npublic abstract double test()
33+
// kind AbstractMethod
34+
35+
@Bar(~5)
36+
// ^^^ reference semanticdb maven . . minimized/Bar#
37+
@SuppressWarnings(value = "unchecked")
38+
// ^^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings#
39+
// ^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings#value().
40+
double test2();
41+
// ^^^^^ definition semanticdb maven . . minimized/Foo#test2().
42+
// display_name test2
43+
// signature_documentation java @Bar(~5)\n@SuppressWarnings("unchecked")\npublic abstract double test2()
44+
// kind AbstractMethod
45+
46+
@BarB(!true)
47+
// ^^^^ reference semanticdb maven . . minimized/BarB#
48+
double test3();
49+
// ^^^^^ definition semanticdb maven . . minimized/Foo#test3().
50+
// display_name test3
51+
// signature_documentation java @BarB(!true)\npublic abstract double test3()
52+
// kind AbstractMethod
53+
}
54+

tests/snapshots/src/main/scala/tests/SaveSnapshots.scala

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,19 @@ package tests
33
object SaveSnapshots {
44
def main(args: Array[String]): Unit = {
55
val expectDirectory = tests.snapshots.BuildInfo.snapshotDirectory.toPath
6-
SemanticdbJavacSnapshotGenerator
7-
.run(SnapshotContext(expectDirectory), new SaveSnapshotHandler)
6+
val mapping = Map(
7+
"minimized" -> new MinimizedSnapshotScipGenerator(),
8+
"library" -> new LibrarySnapshotGenerator()
9+
)
10+
11+
val enabledGenerators =
12+
if (args.isEmpty)
13+
mapping.values.toList
14+
else
15+
args.flatMap(mapping.get).toList
16+
17+
val generator = new AggregateSnapshotGenerator(enabledGenerators)
18+
19+
generator.run(SnapshotContext(expectDirectory), new SaveSnapshotHandler)
820
}
921
}

0 commit comments

Comments
 (0)
0