Skip to content

Commit d9c6baf

Browse files
committed
Add a helper method to emit processor errors
1 parent c17869a commit d9c6baf

File tree

4 files changed

+56
-72
lines changed

4 files changed

+56
-72
lines changed

src/processor/java/org/truffleruby/processor/BuildInformationProcessor.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,19 @@
2424
import java.util.regex.Matcher;
2525
import java.util.regex.Pattern;
2626

27-
import javax.annotation.processing.AbstractProcessor;
2827
import javax.annotation.processing.ProcessingEnvironment;
2928
import javax.annotation.processing.RoundEnvironment;
3029
import javax.annotation.processing.SupportedAnnotationTypes;
31-
import javax.lang.model.SourceVersion;
3230
import javax.lang.model.element.Element;
3331
import javax.lang.model.element.ExecutableElement;
3432
import javax.lang.model.element.PackageElement;
3533
import javax.lang.model.element.TypeElement;
36-
import javax.tools.Diagnostic.Kind;
3734
import javax.tools.JavaFileObject;
3835

3936
import org.truffleruby.PopulateBuildInformation;
4037

4138
@SupportedAnnotationTypes("org.truffleruby.PopulateBuildInformation")
42-
public class BuildInformationProcessor extends AbstractProcessor {
39+
public class BuildInformationProcessor extends TruffleRubyProcessor {
4340

4441
private static final String SUFFIX = "Impl";
4542

@@ -120,11 +117,6 @@ private String runCommand(String command) throws IOException, InterruptedExcepti
120117
return firstLine;
121118
}
122119

123-
@Override
124-
public SourceVersion getSupportedSourceVersion() {
125-
return SourceVersion.latest();
126-
}
127-
128120
@Override
129121
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment) {
130122
assert isInitialized();
@@ -133,7 +125,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
133125
try {
134126
processBuildInformation((TypeElement) element);
135127
} catch (Exception e) {
136-
processingEnv.getMessager().printMessage(Kind.ERROR, e.getClass() + " " + e.getMessage(), element);
128+
error(e.getClass() + " " + e.getMessage(), element);
137129
}
138130
}
139131
}

src/processor/java/org/truffleruby/processor/CoreModuleChecks.java

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import javax.lang.model.element.VariableElement;
1818
import javax.lang.model.type.TypeKind;
1919
import javax.lang.model.type.TypeMirror;
20-
import javax.tools.Diagnostic;
2120

2221
import com.oracle.truffle.api.dsl.CachedLanguage;
2322
import org.truffleruby.builtins.CoreMethod;
@@ -68,21 +67,15 @@ static void checks(
6867
}
6968

7069
if (lowerArgs == null) {
71-
processor.getProcessingEnvironment().getMessager().printMessage(
72-
Diagnostic.Kind.ERROR,
73-
"could not find specializations (lowerArgs == null)",
74-
klass);
70+
processor.error("could not find specializations (lowerArgs == null)", klass);
7571
return;
7672
}
7773

7874
// Verify against the lowerFixnum annotation
7975
for (int i = 0; i < lowerArgs.length; i++) {
8076
boolean shouldLower = lowerArgs[i] == 0b01; // int without long
8177
if (shouldLower && !contains(lowerFixnum, hasZeroArgument ? i : i + 1)) {
82-
processor.getProcessingEnvironment().getMessager().printMessage(
83-
Diagnostic.Kind.ERROR,
84-
"should use lowerFixnum for argument " + (hasZeroArgument ? i : i + 1),
85-
klass);
78+
processor.error("should use lowerFixnum for argument " + (hasZeroArgument ? i : i + 1), klass);
8679
}
8780
}
8881
}
@@ -153,10 +146,7 @@ private static void checkAmbiguousOptionalArguments(
153146

154147
if (coreMethod.needsBlock()) {
155148
if (n < 0) {
156-
processor.getProcessingEnvironment().getMessager().printMessage(
157-
Diagnostic.Kind.ERROR,
158-
"invalid block method parameter position for",
159-
specializationMethod);
149+
processor.error("invalid block method parameter position for", specializationMethod);
160150
return;
161151
}
162152
isParameterBlock(processor, parameters.get(n));
@@ -165,29 +155,20 @@ private static void checkAmbiguousOptionalArguments(
165155

166156
if (coreMethod.rest()) {
167157
if (n < 0) {
168-
processor.getProcessingEnvironment().getMessager().printMessage(
169-
Diagnostic.Kind.ERROR,
170-
"missing rest method parameter",
171-
specializationMethod);
158+
processor.error("missing rest method parameter", specializationMethod);
172159
return;
173160
}
174161

175162
if (parameters.get(n).asType().getKind() != TypeKind.ARRAY) {
176-
processor.getProcessingEnvironment().getMessager().printMessage(
177-
Diagnostic.Kind.ERROR,
178-
"rest method parameter is not array",
179-
parameters.get(n));
163+
processor.error("rest method parameter is not array", parameters.get(n));
180164
return;
181165
}
182166
n--; // ignore final Object[] argument
183167
}
184168

185169
for (int i = 0; i < coreMethod.optional(); i++, n--) {
186170
if (n < 0) {
187-
processor.getProcessingEnvironment().getMessager().printMessage(
188-
Diagnostic.Kind.ERROR,
189-
"invalid optional parameter count for",
190-
specializationMethod);
171+
processor.error("invalid optional parameter count for", specializationMethod);
191172
continue;
192173
}
193174
isParameterUnguarded(processor, specializationAnnotation, parameters.get(n));
@@ -216,8 +197,7 @@ private static void isParameterUnguarded(
216197
!name.startsWith("unused") &&
217198
!name.startsWith("maybe") &&
218199
!isGuarded(name, specializationAnnotation.guards())) {
219-
processor.getProcessingEnvironment().getMessager().printMessage(
220-
Diagnostic.Kind.ERROR,
200+
processor.error(
221201
"Since Object is the super type of NotProvided any optional parameter declaration of type Object " +
222202
"must have additional guards to check whether this specialization should be called, " +
223203
"or must make it clear in the parameter name (by using unused or maybe prefix) " +
@@ -244,10 +224,7 @@ private static void isParameterBlock(CoreModuleProcessor processor, VariableElem
244224
if (!(processor.isSameType(blockType, processor.nilType) ||
245225
processor.isSameType(blockType, processor.rubyProcType) ||
246226
processor.isSameType(blockType, processor.objectType))) {
247-
processor.getProcessingEnvironment().getMessager().printMessage(
248-
Diagnostic.Kind.ERROR,
249-
"A block parameter must be of type Nil, RubyProc or Object.",
250-
parameter);
227+
processor.error("A block parameter must be of type Nil, RubyProc or Object.", parameter);
251228
}
252229
}
253230
}

src/processor/java/org/truffleruby/processor/CoreModuleProcessor.java

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,15 @@
1919
import java.util.Set;
2020
import java.util.StringJoiner;
2121

22-
import javax.annotation.processing.AbstractProcessor;
23-
import javax.annotation.processing.ProcessingEnvironment;
2422
import javax.annotation.processing.RoundEnvironment;
2523
import javax.annotation.processing.SupportedAnnotationTypes;
26-
import javax.lang.model.SourceVersion;
2724
import javax.lang.model.element.Element;
2825
import javax.lang.model.element.ExecutableElement;
2926
import javax.lang.model.element.PackageElement;
3027
import javax.lang.model.element.TypeElement;
3128
import javax.lang.model.element.VariableElement;
3229
import javax.lang.model.type.TypeMirror;
3330
import javax.lang.model.util.Elements;
34-
import javax.tools.Diagnostic.Kind;
3531
import javax.tools.FileObject;
3632
import javax.tools.JavaFileObject;
3733
import javax.tools.StandardLocation;
@@ -43,11 +39,7 @@
4339
import com.oracle.truffle.api.dsl.Specialization;
4440

4541
@SupportedAnnotationTypes("org.truffleruby.builtins.CoreModule")
46-
public class CoreModuleProcessor extends AbstractProcessor {
47-
48-
ProcessingEnvironment getProcessingEnvironment() {
49-
return processingEnv;
50-
}
42+
public class CoreModuleProcessor extends TruffleRubyProcessor {
5143

5244
private static final String SUFFIX = "Builtins";
5345
private static final Set<String> KEYWORDS;
@@ -101,11 +93,6 @@ ProcessingEnvironment getProcessingEnvironment() {
10193
TypeMirror rubyNodeType;
10294
TypeMirror rubyBaseNodeType;
10395

104-
@Override
105-
public SourceVersion getSupportedSourceVersion() {
106-
return SourceVersion.latest();
107-
}
108-
10996
@Override
11097
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment) {
11198
Elements elementUtils = processingEnv.getElementUtils();
@@ -121,18 +108,14 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
121108
try {
122109
processCoreModule((TypeElement) element);
123110
} catch (IOException e) {
124-
processingEnv.getMessager().printMessage(Kind.ERROR, e.getClass() + " " + e.getMessage(), element);
111+
error(e.getClass() + " " + e.getMessage(), element);
125112
}
126113
}
127114
}
128115

129116
return true;
130117
}
131118

132-
boolean isSameType(TypeMirror type1, TypeMirror type2) {
133-
return processingEnv.getTypeUtils().isSameType(type1, type2);
134-
}
135-
136119
private void processCoreModule(TypeElement coreModuleElement) throws IOException {
137120
final CoreModule coreModule = coreModuleElement.getAnnotation(CoreModule.class);
138121

@@ -313,8 +296,7 @@ private void processCoreMethod(
313296
numberOfArguments);
314297

315298
if (argumentNames.isEmpty() && numberOfArguments > 0) {
316-
processingEnv.getMessager().printMessage(
317-
Kind.ERROR,
299+
error(
318300
"Did not find argument names. If the class has inherited Specializations use org.truffleruby.builtins.CoreMethod.argumentNames",
319301
klass);
320302

@@ -357,8 +339,7 @@ private void processCoreMethod(
357339
args.add("&" + argumentNames.get(index));
358340
}
359341
} catch (IndexOutOfBoundsException e) {
360-
processingEnv.getMessager().printMessage(
361-
Kind.ERROR,
342+
error(
362343
"Not enough arguments found compared to declared numbers, check required, optional etc. declarations",
363344
klass);
364345
}
@@ -385,10 +366,7 @@ private List<String> getArgumentNames(
385366
argumentNames = getArgumentNamesFromSpecializations(klass, hasSelfArgument);
386367
} else {
387368
if (argumentNamesFromAnnotation.length != numberOfArguments && numberOfArguments >= 0) {
388-
processingEnv.getMessager().printMessage(
389-
Kind.ERROR,
390-
"The size of argumentNames does not match declared number of arguments.",
391-
klass);
369+
error("The size of argumentNames does not match declared number of arguments.", klass);
392370
argumentNames = new ArrayList<>();
393371
} else {
394372
argumentNames = Arrays.asList(argumentNamesFromAnnotation);
@@ -452,11 +430,10 @@ private List<String> getArgumentNamesFromSpecializations(TypeElement klass, bool
452430
argumentElements.add(parameter);
453431
} else {
454432
if (!argumentNames.get(index).equals(name)) {
455-
processingEnv.getMessager().printMessage(
456-
Kind.ERROR,
433+
error(
457434
"The argument does not match with the first occurrence of this argument which was '" +
458-
argumentElements.get(index).getSimpleName() +
459-
"' (translated to Ruby as '" + argumentNames.get(index) + "').",
435+
argumentElements.get(index).getSimpleName() + "' (translated to Ruby as '" +
436+
argumentNames.get(index) + "').",
460437
parameter);
461438
}
462439
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. This
3+
* code is released under a tri EPL/GPL/LGPL license. You can use it,
4+
* redistribute it and/or modify it under the terms of the:
5+
*
6+
* Eclipse Public License version 2.0, or
7+
* GNU General Public License version 2, or
8+
* GNU Lesser General Public License version 2.1.
9+
*/
10+
package org.truffleruby.processor;
11+
12+
import javax.annotation.processing.AbstractProcessor;
13+
import javax.annotation.processing.ProcessingEnvironment;
14+
import javax.lang.model.SourceVersion;
15+
import javax.lang.model.element.Element;
16+
import javax.lang.model.type.TypeMirror;
17+
import javax.tools.Diagnostic;
18+
19+
public abstract class TruffleRubyProcessor extends AbstractProcessor {
20+
21+
ProcessingEnvironment getProcessingEnvironment() {
22+
return processingEnv;
23+
}
24+
25+
@Override
26+
public SourceVersion getSupportedSourceVersion() {
27+
return SourceVersion.latest();
28+
}
29+
30+
boolean isSameType(TypeMirror type1, TypeMirror type2) {
31+
return processingEnv.getTypeUtils().isSameType(type1, type2);
32+
}
33+
34+
void error(String message, Element element) {
35+
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, message, element);
36+
}
37+
38+
}

0 commit comments

Comments
 (0)