comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/verify/VerifyTruffleProcessor.java @ 18977:8fd7354353b7

add annotation processor to verify TruffleBoundary/VirtualFrame usages
author Lukas Stadler <lukas.stadler@oracle.com>
date Tue, 27 Jan 2015 23:11:11 +0100
parents
children fb17e716b03c
comparison
equal deleted inserted replaced
18976:3faa4f98d5c8 18977:8fd7354353b7
1 /*
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.oracle.truffle.dsl.processor.verify;
24
25 import static com.oracle.truffle.dsl.processor.java.ElementUtils.*;
26 import static java.util.Collections.*;
27
28 import java.io.*;
29 import java.util.*;
30
31 import javax.annotation.processing.*;
32 import javax.lang.model.*;
33 import javax.lang.model.element.*;
34 import javax.tools.Diagnostic.Kind;
35
36 import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
37
38 @SupportedSourceVersion(SourceVersion.RELEASE_7)
39 @SupportedAnnotationTypes({"com.oracle.truffle.api.CompilerDirectives.TruffleBoundary"})
40 public class VerifyTruffleProcessor extends AbstractProcessor {
41 @Override
42 public SourceVersion getSupportedSourceVersion() {
43 return SourceVersion.latest();
44 }
45
46 /**
47 * Node class currently being processed.
48 */
49 private Element scope;
50
51 public static boolean isEnclosedIn(Element e, Element scopeElement) {
52 List<Element> elementHierarchy = getElementHierarchy(e);
53 return elementHierarchy.contains(scopeElement);
54 }
55
56 void errorMessage(Element element, String format, Object... args) {
57 message(Kind.ERROR, element, format, args);
58 }
59
60 void message(Kind kind, Element element, String format, Object... args) {
61 if (scope != null && !isEnclosedIn(element, scope)) {
62 // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=428357#c1
63 List<Element> elementHierarchy = getElementHierarchy(element);
64 reverse(elementHierarchy);
65
66 StringBuilder str = new StringBuilder();
67 for (Element e : elementHierarchy) {
68 if (e.getKind() != ElementKind.PACKAGE) {
69 str.append(str.length() == 0 ? "" : ".");
70 str.append(e);
71 }
72 }
73 processingEnv.getMessager().printMessage(kind, String.format(str + ": " + format, args), scope);
74 } else {
75 processingEnv.getMessager().printMessage(kind, String.format(format, args), element);
76 }
77 }
78
79 /**
80 * Bugs in an annotation processor can cause silent failure so try to report any exception
81 * throws as errors.
82 */
83 private void reportException(Kind kind, Element element, Throwable t) {
84 StringWriter buf = new StringWriter();
85 t.printStackTrace(new PrintWriter(buf));
86 buf.toString();
87 message(kind, element, "Exception thrown during processing: %s", buf.toString());
88 }
89
90 @Override
91 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
92 if (roundEnv.processingOver()) {
93 return false;
94 }
95
96 TypeElement virtualFrameType = processingEnv.getElementUtils().getTypeElement("com.oracle.truffle.api.frame.VirtualFrame");
97
98 for (Element element : roundEnv.getElementsAnnotatedWith(TruffleBoundary.class)) {
99 scope = element;
100 try {
101 ExecutableElement method = (ExecutableElement) element;
102
103 for (VariableElement parameter : method.getParameters()) {
104 Element paramType = processingEnv.getTypeUtils().asElement(parameter.asType());
105 if (paramType != null && paramType.equals(virtualFrameType)) {
106 errorMessage(element, "Method %s cannot be annotated with @%s and have a parameter of type %s", method.getSimpleName(), TruffleBoundary.class.getSimpleName(),
107 paramType.getSimpleName());
108 }
109 }
110 } catch (Throwable t) {
111 reportException(isBug367599(t) ? Kind.NOTE : Kind.ERROR, element, t);
112 } finally {
113 scope = null;
114 }
115 }
116 return false;
117 }
118
119 /**
120 * Determines if a given exception is (most likely) caused by <a
121 * href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=367599">Bug 367599</a>.
122 */
123 public static boolean isBug367599(Throwable t) {
124 if (t instanceof FilerException) {
125 for (StackTraceElement ste : t.getStackTrace()) {
126 if (ste.toString().contains("org.eclipse.jdt.internal.apt.pluggable.core.filer.IdeFilerImpl.create")) {
127 // See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=367599
128 return true;
129 }
130 }
131 }
132 if (t.getCause() != null) {
133 return isBug367599(t.getCause());
134 }
135 return false;
136 }
137 }