Skip to content

Commit

Permalink
Force the DOM parser to continue with errors (#808)
Browse files Browse the repository at this point in the history
* Force the DOM parser to continue with compilation errors
  • Loading branch information
testforstephen authored Sep 9, 2024
1 parent c4d1f11 commit cb3c54f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import org.eclipse.jdt.internal.core.util.BindingKeyParser;
import org.eclipse.jdt.internal.javac.JavacProblemConverter;
import org.eclipse.jdt.internal.javac.JavacUtils;
import org.eclipse.jdt.internal.javac.TolerantJavaCompiler;
import org.eclipse.jdt.internal.javac.UnusedProblemFactory;
import org.eclipse.jdt.internal.javac.UnusedTreeScanner;

Expand Down Expand Up @@ -479,6 +480,7 @@ private Map<org.eclipse.jdt.internal.compiler.env.ICompilationUnit, CompilationU
}
var compiler = ToolProvider.getSystemJavaCompiler();
Context context = new Context();
TolerantJavaCompiler.configureCompilerInstance(context);
Map<org.eclipse.jdt.internal.compiler.env.ICompilationUnit, CompilationUnit> result = new HashMap<>(sourceUnits.length, 1.f);
Map<JavaFileObject, CompilationUnit> filesToUnits = new HashMap<>();
final UnusedProblemFactory unusedProblemFactory = new UnusedProblemFactory(new DefaultProblemFactory(), compilerOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -46,12 +45,9 @@
import org.eclipse.jdt.internal.core.builder.SourceFile;

import com.sun.tools.javac.api.MultiTaskListener;
import com.sun.tools.javac.comp.*;
import com.sun.tools.javac.comp.CompileStates.CompileState;
import com.sun.tools.javac.main.JavaCompiler;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Pair;

public class JavacCompiler extends Compiler {
JavacConfig compilerConfig;
Expand Down Expand Up @@ -117,46 +113,7 @@ public void compile(ICompilationUnit[] sourceUnits) {
// Configure Javac to generate the class files in a mapped temporary location
var outputDir = JavacClassFile.getMappedTempOutput(outputSourceSet.getKey()).toFile();
JavacUtils.configureJavacContext(javacContext, this.compilerConfig, javaProject, outputDir, true);
JavaCompiler javac = new JavaCompiler(javacContext) {
boolean isInGeneration = false;

@Override
protected boolean shouldStop(CompileState cs) {
// Never stop
return false;
}

@Override
public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue, Queue<JavaFileObject> results) {
try {
this.isInGeneration = true;
super.generate(queue, results);
} catch (Throwable ex) {
// TODO error handling
} finally {
this.isInGeneration = false;
}
}

@Override
protected void desugar(Env<AttrContext> env, Queue<Pair<Env<AttrContext>, JCClassDecl>> results) {
try {
super.desugar(env, results);
} catch (Throwable ex) {
// TODO error handling
}
}

@Override
public int errorCount() {
// See JavaCompiler.genCode(Env<AttrContext> env, JCClassDecl cdef),
// it stops writeClass if errorCount is not zero.
// Force it to return 0 if we are in generation phase, and keeping
// generating class files for those files without errors.
return this.isInGeneration ? 0 : super.errorCount();
}
};
javacContext.put(JavaCompiler.compilerKey, javac);
JavaCompiler javac = TolerantJavaCompiler.configureCompilerInstance(javacContext);
javac.shouldStopPolicyIfError = CompileState.GENERATE;
try {
javac.compile(com.sun.tools.javac.util.List.from(outputSourceSet.getValue().stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*******************************************************************************
* Copyright (c) 2024 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Microsoft Corporation - initial API and implementation
*******************************************************************************/

package org.eclipse.jdt.internal.javac;

import java.util.Queue;

import javax.tools.JavaFileObject;

import com.sun.tools.javac.comp.AttrContext;
import com.sun.tools.javac.comp.CompileStates.CompileState;
import com.sun.tools.javac.comp.Env;
import com.sun.tools.javac.main.JavaCompiler;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Pair;

public class TolerantJavaCompiler extends JavaCompiler {
boolean isInGeneration = false;

public TolerantJavaCompiler(Context context) {
super(context);
}

@Override
protected boolean shouldStop(CompileState cs) {
// Never stop
return false;
}

@Override
public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue, Queue<JavaFileObject> results) {
try {
this.isInGeneration = true;
super.generate(queue, results);
} catch (Throwable ex) {
// TODO error handling
} finally {
this.isInGeneration = false;
}
}

@Override
protected void desugar(Env<AttrContext> env, Queue<Pair<Env<AttrContext>, JCClassDecl>> results) {
try {
super.desugar(env, results);
} catch (Throwable ex) {
// TODO error handling
}
}

@Override
public int errorCount() {
// See JavaCompiler.genCode(Env<AttrContext> env, JCClassDecl cdef),
// it stops writeClass if errorCount is not zero.
// Force it to return 0 if we are in generation phase, and keeping
// generating class files for those files without errors.
return this.isInGeneration ? 0 : super.errorCount();
}

public static JavaCompiler configureCompilerInstance(Context context) {
TolerantJavaCompiler javacCompiler = new TolerantJavaCompiler(context);
context.put(JavaCompiler.compilerKey, javacCompiler);
return javacCompiler;
}
}

0 comments on commit cb3c54f

Please sign in to comment.