001 /*
002 * Copyright 2010-2013 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.jet.cli.common.messages;
018
019 import com.intellij.openapi.util.text.StringUtil;
020 import com.intellij.psi.PsiElement;
021 import com.intellij.psi.PsiErrorElement;
022 import com.intellij.psi.PsiModifierListOwner;
023 import com.intellij.psi.util.PsiFormatUtil;
024 import kotlin.Function0;
025 import org.jetbrains.annotations.NotNull;
026 import org.jetbrains.annotations.Nullable;
027 import org.jetbrains.jet.analyzer.AnalyzeExhaust;
028 import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
029 import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
030 import org.jetbrains.jet.lang.diagnostics.*;
031 import org.jetbrains.jet.lang.diagnostics.rendering.DefaultErrorMessages;
032 import org.jetbrains.jet.lang.psi.JetFile;
033 import org.jetbrains.jet.lang.resolve.AnalyzingUtils;
034 import org.jetbrains.jet.lang.resolve.BindingContext;
035 import org.jetbrains.jet.lang.resolve.BindingContextUtils;
036 import org.jetbrains.jet.lang.resolve.DescriptorUtils;
037 import org.jetbrains.jet.lang.resolve.java.JavaBindingContext;
038 import org.jetbrains.jet.lang.resolve.java.JvmAbi;
039 import org.jetbrains.jet.lang.resolve.java.resolver.TraceBasedErrorReporter;
040 import org.jetbrains.jet.lang.resolve.kotlin.VirtualFileKotlinClass;
041
042 import java.util.Collection;
043 import java.util.List;
044
045 import static com.intellij.openapi.util.io.FileUtil.toSystemDependentName;
046 import static org.jetbrains.jet.lang.diagnostics.DiagnosticUtils.sortedDiagnostics;
047
048 public final class AnalyzerWithCompilerReport {
049
050 @NotNull
051 private static CompilerMessageSeverity convertSeverity(@NotNull Severity severity) {
052 switch (severity) {
053 case INFO:
054 return CompilerMessageSeverity.INFO;
055 case ERROR:
056 return CompilerMessageSeverity.ERROR;
057 case WARNING:
058 return CompilerMessageSeverity.WARNING;
059 }
060 throw new IllegalStateException("Unknown severity: " + severity);
061 }
062
063 @NotNull
064 private static final DiagnosticFactory0<PsiErrorElement> SYNTAX_ERROR_FACTORY = DiagnosticFactory0.create(Severity.ERROR);
065
066 private boolean hasErrors = false;
067 @NotNull
068 private final MessageCollector messageCollectorWrapper;
069 @Nullable
070 private AnalyzeExhaust analyzeExhaust = null;
071
072 public AnalyzerWithCompilerReport(@NotNull final MessageCollector collector) {
073 messageCollectorWrapper = new MessageCollector() {
074 @Override
075 public void report(@NotNull CompilerMessageSeverity severity,
076 @NotNull String message,
077 @NotNull CompilerMessageLocation location) {
078 if (CompilerMessageSeverity.ERRORS.contains(severity)) {
079 hasErrors = true;
080 }
081 collector.report(severity, message, location);
082 }
083 };
084 }
085
086 private static boolean reportDiagnostic(@NotNull Diagnostic diagnostic, @NotNull MessageCollector messageCollector) {
087 if (!diagnostic.isValid()) return false;
088 DiagnosticUtils.LineAndColumn lineAndColumn = DiagnosticUtils.getLineAndColumn(diagnostic);
089 String render;
090 if (diagnostic instanceof MyDiagnostic) {
091 render = ((MyDiagnostic)diagnostic).message;
092 }
093 else {
094 render = DefaultErrorMessages.RENDERER.render(diagnostic);
095 }
096 messageCollector.report(convertSeverity(diagnostic.getSeverity()), render,
097 MessageUtil.psiFileToMessageLocation(diagnostic.getPsiFile(), null, lineAndColumn.getLine(), lineAndColumn.getColumn()));
098 return diagnostic.getSeverity() == Severity.ERROR;
099 }
100
101 private void reportIncompleteHierarchies() {
102 assert analyzeExhaust != null;
103 Collection<ClassDescriptor> incompletes = analyzeExhaust.getBindingContext().getKeys(BindingContext.INCOMPLETE_HIERARCHY);
104 if (!incompletes.isEmpty()) {
105 StringBuilder message = new StringBuilder("The following classes have incomplete hierarchies:\n");
106 for (ClassDescriptor incomplete : incompletes) {
107 String fqName = DescriptorUtils.getFqName(incomplete).asString();
108 message.append(" ").append(fqName).append("\n");
109 }
110 messageCollectorWrapper.report(CompilerMessageSeverity.ERROR, message.toString(), CompilerMessageLocation.NO_LOCATION);
111 }
112 }
113
114 private void reportAlternativeSignatureErrors() {
115 assert analyzeExhaust != null;
116 BindingContext bc = analyzeExhaust.getBindingContext();
117 Collection<DeclarationDescriptor> descriptorsWithErrors = bc.getKeys(JavaBindingContext.LOAD_FROM_JAVA_SIGNATURE_ERRORS);
118 if (!descriptorsWithErrors.isEmpty()) {
119 StringBuilder message = new StringBuilder("The following Java entities have annotations with wrong Kotlin signatures:\n");
120 for (DeclarationDescriptor descriptor : descriptorsWithErrors) {
121 PsiElement declaration = BindingContextUtils.descriptorToDeclaration(bc, descriptor);
122 assert declaration instanceof PsiModifierListOwner;
123
124 List<String> errors = bc.get(JavaBindingContext.LOAD_FROM_JAVA_SIGNATURE_ERRORS, descriptor);
125 assert errors != null && !errors.isEmpty();
126
127 String externalName = PsiFormatUtil.getExternalName((PsiModifierListOwner) declaration);
128 message.append(externalName).append(":\n");
129
130 for (String error : errors) {
131 message.append(" ").append(error).append("\n");
132 }
133 }
134 messageCollectorWrapper.report(CompilerMessageSeverity.ERROR,
135 message.toString(), CompilerMessageLocation.NO_LOCATION);
136 }
137 }
138
139 private void reportAbiVersionErrors() {
140 assert analyzeExhaust != null;
141 BindingContext bindingContext = analyzeExhaust.getBindingContext();
142
143 Collection<VirtualFileKotlinClass> errorClasses = bindingContext.getKeys(TraceBasedErrorReporter.ABI_VERSION_ERRORS);
144 for (VirtualFileKotlinClass kotlinClass : errorClasses) {
145 Integer abiVersion = bindingContext.get(TraceBasedErrorReporter.ABI_VERSION_ERRORS, kotlinClass);
146 String path = toSystemDependentName(kotlinClass.getFile().getPath());
147 messageCollectorWrapper.report(CompilerMessageSeverity.ERROR,
148 "Class '" + kotlinClass.getClassName() +
149 "' was compiled with an incompatible version of Kotlin. " +
150 "Its ABI version is " + abiVersion + ", expected ABI version is " + JvmAbi.VERSION,
151 CompilerMessageLocation.create(path, 0, 0));
152 }
153 }
154
155 public static boolean reportDiagnostics(@NotNull BindingContext bindingContext, @NotNull MessageCollector messageCollector) {
156 boolean hasErrors = false;
157 for (Diagnostic diagnostic : sortedDiagnostics(bindingContext.getDiagnostics().all())) {
158 hasErrors |= reportDiagnostic(diagnostic, messageCollector);
159 }
160 return hasErrors;
161 }
162
163 private void reportSyntaxErrors(@NotNull Collection<JetFile> files) {
164 for (JetFile file : files) {
165 reportSyntaxErrors(file, messageCollectorWrapper);
166 }
167 }
168
169 public static class SyntaxErrorReport {
170 private final boolean hasErrors;
171 private final boolean onlyErrorAtEof;
172
173 public SyntaxErrorReport(boolean hasErrors, boolean onlyErrorAtEof) {
174 this.hasErrors = hasErrors;
175 this.onlyErrorAtEof = onlyErrorAtEof;
176 }
177
178 public boolean isHasErrors() {
179 return hasErrors;
180 }
181
182 public boolean isOnlyErrorAtEof() {
183 return onlyErrorAtEof;
184 }
185 }
186
187 public static SyntaxErrorReport reportSyntaxErrors(@NotNull final PsiElement file, @NotNull final MessageCollector messageCollector) {
188 class ErrorReportingVisitor extends AnalyzingUtils.PsiErrorElementVisitor {
189 boolean hasErrors = false;
190 boolean onlyErrorAtEof = false;
191
192 private <E extends PsiElement> void reportDiagnostic(E element, DiagnosticFactory0<E> factory, String message) {
193 MyDiagnostic<?> diagnostic = new MyDiagnostic<E>(element, factory, message);
194 AnalyzerWithCompilerReport.reportDiagnostic(diagnostic, messageCollector);
195 if (element.getTextRange().getStartOffset() == file.getTextRange().getEndOffset()) {
196 onlyErrorAtEof = !hasErrors;
197 }
198 hasErrors = true;
199 }
200
201 @Override
202 public void visitErrorElement(PsiErrorElement element) {
203 String description = element.getErrorDescription();
204 reportDiagnostic(element, SYNTAX_ERROR_FACTORY, StringUtil.isEmpty(description) ? "Syntax error" : description);
205 }
206 }
207 ErrorReportingVisitor visitor = new ErrorReportingVisitor();
208
209 file.accept(visitor);
210
211 return new SyntaxErrorReport(visitor.hasErrors, visitor.onlyErrorAtEof);
212 }
213
214 @Nullable
215 public AnalyzeExhaust getAnalyzeExhaust() {
216 return analyzeExhaust;
217 }
218
219 public boolean hasErrors() {
220 return hasErrors;
221 }
222
223 public void analyzeAndReport(@NotNull Function0<AnalyzeExhaust> analyzer, @NotNull Collection<JetFile> files) {
224 reportSyntaxErrors(files);
225 analyzeExhaust = analyzer.invoke();
226 reportDiagnostics(analyzeExhaust.getBindingContext(), messageCollectorWrapper);
227 reportIncompleteHierarchies();
228 reportAlternativeSignatureErrors();
229 reportAbiVersionErrors();
230 }
231
232
233 private static class MyDiagnostic<E extends PsiElement> extends SimpleDiagnostic<E> {
234 private String message;
235
236 public MyDiagnostic(@NotNull E psiElement, @NotNull DiagnosticFactory0<E> factory, String message) {
237 super(psiElement, factory, Severity.ERROR);
238 this.message = message;
239 }
240
241 @Override
242 public boolean isValid() {
243 return true;
244 }
245 }
246 }