comparison truffle/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/verify/VerifyTruffleProcessor.java @ 21951:9c8c0937da41

Moving all sources into truffle subdirectory
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 17 Jun 2015 10:58:08 +0200
parents graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/verify/VerifyTruffleProcessor.java@31fc2fce38f3
children dc83cc1f94f2
comparison
equal deleted inserted replaced
21950:2a5011c7e641 21951:9c8c0937da41
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 import com.oracle.truffle.api.nodes.Node.Child;
38 import com.oracle.truffle.dsl.processor.*;
39
40 @SupportedAnnotationTypes({"com.oracle.truffle.api.CompilerDirectives.TruffleBoundary", "com.oracle.truffle.api.nodes.Node.Child"})
41 public class VerifyTruffleProcessor extends AbstractProcessor {
42 @Override
43 public SourceVersion getSupportedSourceVersion() {
44 return SourceVersion.latest();
45 }
46
47 /**
48 * Node class currently being processed.
49 */
50 private Element scope;
51
52 public static boolean isEnclosedIn(Element e, Element scopeElement) {
53 List<Element> elementHierarchy = getElementHierarchy(e);
54 return elementHierarchy.contains(scopeElement);
55 }
56
57 void errorMessage(Element element, String format, Object... args) {
58 message(Kind.ERROR, element, format, args);
59 }
60
61 void message(Kind kind, Element element, String format, Object... args) {
62 if (scope != null && !isEnclosedIn(element, scope)) {
63 // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=428357#c1
64 List<Element> elementHierarchy = getElementHierarchy(element);
65 reverse(elementHierarchy);
66
67 StringBuilder str = new StringBuilder();
68 for (Element e : elementHierarchy) {
69 if (e.getKind() != ElementKind.PACKAGE) {
70 str.append(str.length() == 0 ? "" : ".");
71 str.append(e);
72 }
73 }
74 processingEnv.getMessager().printMessage(kind, String.format(str + ": " + format, args), scope);
75 } else {
76 processingEnv.getMessager().printMessage(kind, String.format(format, args), element);
77 }
78 }
79
80 /**
81 * Bugs in an annotation processor can cause silent failure so try to report any exception
82 * throws as errors.
83 */
84 private void reportException(Kind kind, Element element, Throwable t) {
85 StringWriter buf = new StringWriter();
86 t.printStackTrace(new PrintWriter(buf));
87 buf.toString();
88 message(kind, element, "Exception thrown during processing: %s", buf.toString());
89 }
90
91 @Override
92 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
93 if (roundEnv.processingOver()) {
94 return false;
95 }
96
97 TypeElement virtualFrameType = processingEnv.getElementUtils().getTypeElement("com.oracle.truffle.api.frame.VirtualFrame");
98
99 for (Element element : roundEnv.getElementsAnnotatedWith(TruffleBoundary.class)) {
100 scope = element;
101 try {
102 ExecutableElement method = (ExecutableElement) element;
103
104 for (VariableElement parameter : method.getParameters()) {
105 Element paramType = processingEnv.getTypeUtils().asElement(parameter.asType());
106 if (paramType != null && paramType.equals(virtualFrameType)) {
107 errorMessage(element, "Method %s cannot be annotated with @%s and have a parameter of type %s", method.getSimpleName(), TruffleBoundary.class.getSimpleName(),
108 paramType.getSimpleName());
109 }
110 }
111 } catch (Throwable t) {
112 reportException(isBug367599(t) ? Kind.NOTE : Kind.ERROR, element, t);
113 } finally {
114 scope = null;
115 }
116 }
117
118 for (Element e : roundEnv.getElementsAnnotatedWith(Child.class)) {
119 if (e.getModifiers().contains(Modifier.FINAL)) {
120 emitError("@Child field cannot be final", e);
121 continue;
122 }
123 assertNoErrorExpected(e);
124 }
125 return false;
126 }
127
128 void assertNoErrorExpected(Element element) {
129 ExpectError.assertNoErrorExpected(processingEnv, element);
130 }
131
132 void emitError(String message, Element element) {
133 if (ExpectError.isExpectedError(processingEnv, element, message)) {
134 return;
135 }
136 processingEnv.getMessager().printMessage(Kind.ERROR, message, element);
137 }
138
139 /**
140 * Determines if a given exception is (most likely) caused by <a
141 * href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=367599">Bug 367599</a>.
142 */
143 public static boolean isBug367599(Throwable t) {
144 if (t instanceof FilerException) {
145 for (StackTraceElement ste : t.getStackTrace()) {
146 if (ste.toString().contains("org.eclipse.jdt.internal.apt.pluggable.core.filer.IdeFilerImpl.create")) {
147 // See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=367599
148 return true;
149 }
150 }
151 }
152 if (t.getCause() != null) {
153 return isBug367599(t.getCause());
154 }
155 return false;
156 }
157 }