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 jet.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.psi.JetIdeTemplate;
034 import org.jetbrains.jet.lang.resolve.AnalyzingUtils;
035 import org.jetbrains.jet.lang.resolve.BindingContext;
036 import org.jetbrains.jet.lang.resolve.BindingContextUtils;
037 import org.jetbrains.jet.lang.resolve.DescriptorUtils;
038 import org.jetbrains.jet.lang.resolve.java.JavaBindingContext;
039 import org.jetbrains.jet.lang.resolve.java.JvmAbi;
040 import org.jetbrains.jet.lang.resolve.java.resolver.TraceBasedErrorReporter;
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 @NotNull
066 private static final DiagnosticFactory0<JetIdeTemplate> UNRESOLVED_IDE_TEMPLATE_ERROR_FACTORY
067 = DiagnosticFactory0.create(Severity.ERROR);
068
069 private boolean hasErrors = false;
070 @NotNull
071 private final MessageCollector messageCollectorWrapper;
072 @Nullable
073 private AnalyzeExhaust analyzeExhaust = null;
074
075 public AnalyzerWithCompilerReport(@NotNull final MessageCollector collector) {
076 messageCollectorWrapper = new MessageCollector() {
077 @Override
078 public void report(@NotNull CompilerMessageSeverity severity,
079 @NotNull String message,
080 @NotNull CompilerMessageLocation location) {
081 if (CompilerMessageSeverity.ERRORS.contains(severity)) {
082 hasErrors = true;
083 }
084 collector.report(severity, message, location);
085 }
086 };
087 }
088
089 private static boolean reportDiagnostic(@NotNull Diagnostic diagnostic, @NotNull MessageCollector messageCollector) {
090 if (!diagnostic.isValid()) return false;
091 DiagnosticUtils.LineAndColumn lineAndColumn = DiagnosticUtils.getLineAndColumn(diagnostic);
092 String render;
093 if (diagnostic instanceof MyDiagnostic) {
094 render = ((MyDiagnostic)diagnostic).message;
095 }
096 else {
097 render = DefaultErrorMessages.RENDERER.render(diagnostic);
098 }
099 messageCollector.report(convertSeverity(diagnostic.getSeverity()), render,
100 MessageUtil.psiFileToMessageLocation(diagnostic.getPsiFile(), null, lineAndColumn.getLine(), lineAndColumn.getColumn()));
101 return diagnostic.getSeverity() == Severity.ERROR;
102 }
103
104 private void reportIncompleteHierarchies() {
105 assert analyzeExhaust != null;
106 Collection<ClassDescriptor> incompletes = analyzeExhaust.getBindingContext().getKeys(BindingContext.INCOMPLETE_HIERARCHY);
107 if (!incompletes.isEmpty()) {
108 StringBuilder message = new StringBuilder("The following classes have incomplete hierarchies:\n");
109 for (ClassDescriptor incomplete : incompletes) {
110 String fqName = DescriptorUtils.getFQName(incomplete).asString();
111 message.append(" ").append(fqName).append("\n");
112 }
113 messageCollectorWrapper.report(CompilerMessageSeverity.ERROR, message.toString(), CompilerMessageLocation.NO_LOCATION);
114 }
115 }
116
117 private void reportAlternativeSignatureErrors() {
118 assert analyzeExhaust != null;
119 BindingContext bc = analyzeExhaust.getBindingContext();
120 Collection<DeclarationDescriptor> descriptorsWithErrors = bc.getKeys(JavaBindingContext.LOAD_FROM_JAVA_SIGNATURE_ERRORS);
121 if (!descriptorsWithErrors.isEmpty()) {
122 StringBuilder message = new StringBuilder("The following Java entities have annotations with wrong Kotlin signatures:\n");
123 for (DeclarationDescriptor descriptor : descriptorsWithErrors) {
124 PsiElement declaration = BindingContextUtils.descriptorToDeclaration(bc, descriptor);
125 assert declaration instanceof PsiModifierListOwner;
126
127 List<String> errors = bc.get(JavaBindingContext.LOAD_FROM_JAVA_SIGNATURE_ERRORS, descriptor);
128 assert errors != null && !errors.isEmpty();
129
130 String externalName = PsiFormatUtil.getExternalName((PsiModifierListOwner) declaration);
131 message.append(externalName).append(":\n");
132
133 for (String error : errors) {
134 message.append(" ").append(error).append("\n");
135 }
136 }
137 messageCollectorWrapper.report(CompilerMessageSeverity.ERROR,
138 message.toString(), CompilerMessageLocation.NO_LOCATION);
139 }
140 }
141
142 private void reportAbiVersionErrors() {
143 assert analyzeExhaust != null;
144 BindingContext bindingContext = analyzeExhaust.getBindingContext();
145
146 Collection<TraceBasedErrorReporter.AbiVersionErrorLocation> errorLocations =
147 bindingContext.getKeys(TraceBasedErrorReporter.ABI_VERSION_ERRORS);
148 for (TraceBasedErrorReporter.AbiVersionErrorLocation abiVersionErrorLocation : errorLocations) {
149 Integer abiVersion = bindingContext.get(TraceBasedErrorReporter.ABI_VERSION_ERRORS, abiVersionErrorLocation);
150 messageCollectorWrapper.report(CompilerMessageSeverity.ERROR,
151 "Class '" + abiVersionErrorLocation.getClassFqName().asString() +
152 "' was compiled with an incompatible version of Kotlin. " +
153 "Its ABI version is " + abiVersion + ", expected ABI version is " + JvmAbi.VERSION,
154 CompilerMessageLocation.create(toSystemDependentName(abiVersionErrorLocation.getPath()), 0, 0));
155 }
156 }
157
158 public static boolean reportDiagnostics(@NotNull BindingContext bindingContext, @NotNull MessageCollector messageCollector) {
159 boolean hasErrors = false;
160 for (Diagnostic diagnostic : sortedDiagnostics(bindingContext.getDiagnostics())) {
161 hasErrors |= reportDiagnostic(diagnostic, messageCollector);
162 }
163 return hasErrors;
164 }
165
166 private void reportSyntaxErrors(@NotNull Collection<JetFile> files) {
167 for (JetFile file : files) {
168 reportSyntaxErrors(file, messageCollectorWrapper);
169 }
170 }
171
172 public static class SyntaxErrorReport {
173 private final boolean hasErrors;
174 private final boolean onlyErrorAtEof;
175
176 public SyntaxErrorReport(boolean hasErrors, boolean onlyErrorAtEof) {
177 this.hasErrors = hasErrors;
178 this.onlyErrorAtEof = onlyErrorAtEof;
179 }
180
181 public boolean isHasErrors() {
182 return hasErrors;
183 }
184
185 public boolean isOnlyErrorAtEof() {
186 return onlyErrorAtEof;
187 }
188 }
189
190 public static SyntaxErrorReport reportSyntaxErrors(@NotNull final PsiElement file, @NotNull final MessageCollector messageCollector) {
191 class ErrorReportingVisitor extends AnalyzingUtils.PsiErrorElementVisitor {
192 boolean hasErrors = false;
193 boolean onlyErrorAtEof = false;
194
195 private <E extends PsiElement> void reportDiagnostic(E element, DiagnosticFactory0<E> factory, String message) {
196 MyDiagnostic<?> diagnostic = new MyDiagnostic<E>(element, factory, message);
197 AnalyzerWithCompilerReport.reportDiagnostic(diagnostic, messageCollector);
198 if (element.getTextRange().getStartOffset() == file.getTextRange().getEndOffset()) {
199 onlyErrorAtEof = !hasErrors;
200 }
201 hasErrors = true;
202 }
203
204 @Override
205 public void visitIdeTemplate(JetIdeTemplate expression) {
206 String placeholderText = expression.getPlaceholderText();
207 reportDiagnostic(expression, UNRESOLVED_IDE_TEMPLATE_ERROR_FACTORY,
208 "Unresolved IDE template" + (StringUtil.isEmpty(placeholderText) ? "" : ": " + placeholderText));
209 }
210
211 @Override
212 public void visitErrorElement(PsiErrorElement element) {
213 String description = element.getErrorDescription();
214 reportDiagnostic(element, SYNTAX_ERROR_FACTORY, StringUtil.isEmpty(description) ? "Syntax error" : description);
215 }
216 }
217 ErrorReportingVisitor visitor = new ErrorReportingVisitor();
218
219 file.accept(visitor);
220
221 return new SyntaxErrorReport(visitor.hasErrors, visitor.onlyErrorAtEof);
222 }
223
224 @Nullable
225 public AnalyzeExhaust getAnalyzeExhaust() {
226 return analyzeExhaust;
227 }
228
229 public boolean hasErrors() {
230 return hasErrors;
231 }
232
233 public void analyzeAndReport(@NotNull Function0<AnalyzeExhaust> analyzer, @NotNull Collection<JetFile> files) {
234 reportSyntaxErrors(files);
235 analyzeExhaust = analyzer.invoke();
236 reportDiagnostics(analyzeExhaust.getBindingContext(), messageCollectorWrapper);
237 reportIncompleteHierarchies();
238 reportAlternativeSignatureErrors();
239 reportAbiVersionErrors();
240 }
241
242
243 private static class MyDiagnostic<E extends PsiElement> extends SimpleDiagnostic<E> {
244 private String message;
245
246 public MyDiagnostic(@NotNull E psiElement, @NotNull DiagnosticFactory0<E> factory, String message) {
247 super(psiElement, factory, Severity.ERROR);
248 this.message = message;
249 }
250
251 @Override
252 public boolean isValid() {
253 return true;
254 }
255 }
256 }