diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/CompilationErrorHandler.java b/compiler/backend/src/org/jetbrains/jet/codegen/CompilationErrorHandler.java index ccd782a7c12..a3b2acf3846 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/CompilationErrorHandler.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/CompilationErrorHandler.java @@ -24,6 +24,9 @@ public interface CompilationErrorHandler { CompilationErrorHandler THROW_EXCEPTION = new CompilationErrorHandler() { @Override public void reportException(Throwable exception, String fileUrl) { + if (exception instanceof RuntimeException) { + throw (RuntimeException)exception; + } throw new IllegalStateException(exception); } }; diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/CompilationException.java b/compiler/backend/src/org/jetbrains/jet/codegen/CompilationException.java index 4c7627c8ed6..f60a56148b9 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/CompilationException.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/CompilationException.java @@ -23,20 +23,22 @@ import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils; /** * @author alex.tkachman +* @author abreslav */ public class CompilationException extends RuntimeException { - private PsiElement element; + private final PsiElement element; CompilationException(@NotNull String message, @Nullable Throwable cause, @NotNull PsiElement element) { super(message, cause); this.element = element; } - @Override - public String toString() { - return getMessage(); + @NotNull + public PsiElement getElement() { + return element; } - + + private String where() { Throwable cause = getCause(); Throwable throwable = cause != null ? cause : this; @@ -56,7 +58,7 @@ public class CompilationException extends RuntimeException { message.append("Cause: ").append(causeMessage == null ? cause.toString() : causeMessage).append("\n"); } message.append("File being compiled and position: ").append(DiagnosticUtils.atLocation(element)).append("\n"); - message.append("The root cause was thrown from: ").append(where()); + message.append("The root cause was thrown at: ").append(where()); return message.toString(); } diff --git a/compiler/cli/src/org/jetbrains/jet/cli/KotlinCompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/KotlinCompiler.java index 32a175995e9..bf87036dfb1 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/KotlinCompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/KotlinCompiler.java @@ -25,11 +25,9 @@ import com.intellij.openapi.util.Disposer; import com.sampullara.cli.Args; import jet.modules.Module; import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.codegen.CompilationException; import org.jetbrains.jet.compiler.*; -import org.jetbrains.jet.compiler.messages.CompilerMessageLocation; -import org.jetbrains.jet.compiler.messages.CompilerMessageSeverity; -import org.jetbrains.jet.compiler.messages.MessageCollector; -import org.jetbrains.jet.compiler.messages.MessageRenderer; +import org.jetbrains.jet.compiler.messages.*; import org.jetbrains.jet.lang.resolve.java.CompilerDependencies; import org.jetbrains.jet.lang.resolve.java.CompilerSpecialMode; import org.jetbrains.jet.utils.PathUtil; @@ -147,7 +145,8 @@ public class KotlinCompiler { JetCoreEnvironment environment = new JetCoreEnvironment(rootDisposable, dependencies); CompileEnvironmentConfiguration configuration = new CompileEnvironmentConfiguration(environment, dependencies, messageCollector); - configuration.getMessageCollector().report(CompilerMessageSeverity.LOGGING, "Configuring the compilation environment", CompilerMessageLocation.NO_LOCATION); + messageCollector.report(CompilerMessageSeverity.LOGGING, "Configuring the compilation environment", + CompilerMessageLocation.NO_LOCATION); try { configureEnvironment(configuration, arguments); @@ -172,8 +171,13 @@ public class KotlinCompiler { } return noErrors ? OK : COMPILATION_ERROR; } + catch (CompilationException e) { + messageCollector.report(CompilerMessageSeverity.EXCEPTION, MessageRenderer.PLAIN.renderException(e), + MessageUtil.psiElementToMessageLocation(e.getElement())); + return INTERNAL_ERROR; + } catch (Throwable t) { - errStream.println(messageRenderer.renderException(t)); + messageCollector.report(CompilerMessageSeverity.EXCEPTION, MessageRenderer.PLAIN.renderException(t), CompilerMessageLocation.NO_LOCATION); return INTERNAL_ERROR; } finally { diff --git a/compiler/cli/src/org/jetbrains/jet/compiler/messages/MessageUtil.java b/compiler/cli/src/org/jetbrains/jet/compiler/messages/MessageUtil.java new file mode 100644 index 00000000000..6ad95bc1e3d --- /dev/null +++ b/compiler/cli/src/org/jetbrains/jet/compiler/messages/MessageUtil.java @@ -0,0 +1,32 @@ +/* + * Copyright 2010-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.compiler.messages; + +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; +import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils; + +/** + * @author abreslav + */ +public class MessageUtil { + public static CompilerMessageLocation psiElementToMessageLocation(PsiElement element) { + PsiFile file = element.getContainingFile(); + DiagnosticUtils.LineAndColumn lineAndColumn = DiagnosticUtils.getLineAndColumnInPsiFile(file, element.getTextRange()); + return CompilerMessageLocation.create(file.getVirtualFile().getPath(), lineAndColumn.getLine(), lineAndColumn.getColumn()); + } +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/DiagnosticUtils.java b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/DiagnosticUtils.java index 67ab02f1d5f..be456c83593 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/DiagnosticUtils.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/DiagnosticUtils.java @@ -80,11 +80,16 @@ public class DiagnosticUtils { @NotNull public static LineAndColumn getLineAndColumn(@NotNull Diagnostic diagnostic) { PsiFile file = diagnostic.getPsiFile(); - Document document = file.getViewProvider().getDocument(); List textRanges = diagnostic.getTextRanges(); if (textRanges.isEmpty()) return LineAndColumn.NONE; TextRange firstRange = textRanges.iterator().next(); - return offsetToLineAndColumn(document, firstRange.getStartOffset()); + return getLineAndColumnInPsiFile(file, firstRange); + } + + @NotNull + public static LineAndColumn getLineAndColumnInPsiFile(PsiFile file, TextRange range) { + Document document = file.getViewProvider().getDocument(); + return offsetToLineAndColumn(document, range.getStartOffset()); } @NotNull