Skip to content

Commit

Permalink
Extracted Parser interface from JdtParser, added private front-end fl…
Browse files Browse the repository at this point in the history
…ags.

	Change on 2016/08/03 by tball <tball@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=129238699
  • Loading branch information
tomball committed Sep 7, 2016
1 parent 9632a06 commit 79e3761
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 103 deletions.
Expand Up @@ -18,13 +18,12 @@
import com.google.common.io.Files;
import com.google.devtools.j2objc.ast.CompilationUnit;
import com.google.devtools.j2objc.file.RegularInputFile;
import com.google.devtools.j2objc.jdt.TreeConverter;
import com.google.devtools.j2objc.pipeline.J2ObjCIncompatibleStripper;
import com.google.devtools.j2objc.translate.OuterReferenceResolver;
import com.google.devtools.j2objc.util.ErrorUtil;
import com.google.devtools.j2objc.util.FileUtil;
import com.google.devtools.j2objc.util.JdtParser;

import com.google.devtools.j2objc.util.Parser;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
Expand Down Expand Up @@ -57,8 +56,8 @@ public CycleFinder(Options options) throws IOException {
});
}

private static JdtParser createParser(Options options) {
JdtParser parser = new JdtParser();
private static Parser createParser(Options options) {
Parser parser = new JdtParser();
parser.addSourcepathEntries(Strings.nullToEmpty(options.getSourcepath()));
parser.addClasspathEntries(Strings.nullToEmpty(options.getBootclasspath()));
parser.addClasspathEntries(Strings.nullToEmpty(options.getClasspath()));
Expand Down Expand Up @@ -95,7 +94,7 @@ private NameList getBlacklist() throws IOException {
}

private File stripIncompatible(
List<String> sourceFileNames, JdtParser parser) throws IOException {
List<String> sourceFileNames, Parser parser) throws IOException {
File strippedDir = null;
for (int i = 0; i < sourceFileNames.size(); i++) {
String fileName = sourceFileNames.get(i);
Expand All @@ -122,24 +121,15 @@ private File stripIncompatible(

public List<List<Edge>> findCycles() throws IOException {
final TypeCollector typeCollector = new TypeCollector();
JdtParser parser = createParser(options);
Parser parser = createParser(options);
final OuterReferenceResolver outerResolver = new OuterReferenceResolver();

List<String> sourceFiles = options.getSourceFiles();
File strippedDir = stripIncompatible(sourceFiles, parser);

JdtParser.Handler handler = new JdtParser.Handler() {
Parser.Handler handler = new Parser.Handler() {
@Override
public void handleParsedUnit(String path, org.eclipse.jdt.core.dom.CompilationUnit jdtUnit) {
String source = "";
RegularInputFile file = new RegularInputFile(path);
try {
source = FileUtil.readFile(file);
} catch (IOException e) {
ErrorUtil.error("Error reading file " + path + ": " + e.getMessage());
}
CompilationUnit unit = TreeConverter.convertCompilationUnit(
jdtUnit, path, FileUtil.getMainTypeName(file), source, null);
public void handleParsedUnit(String path, CompilationUnit unit) {
typeCollector.visitAST(unit);
outerResolver.run(unit);
}
Expand Down
1 change: 1 addition & 0 deletions translator/Makefile
Expand Up @@ -290,6 +290,7 @@ JAVA_SOURCES = \
util/JdtParser.java \
util/NameTable.java \
util/PackagePrefixes.java \
util/Parser.java \
util/ProGuardUsageParser.java \
util/SourceVersion.java \
util/TimeTracker.java \
Expand Down
Expand Up @@ -28,7 +28,7 @@
import com.google.devtools.j2objc.util.DeadCodeMap;
import com.google.devtools.j2objc.util.ErrorUtil;
import com.google.devtools.j2objc.util.FileUtil;
import com.google.devtools.j2objc.util.JdtParser;
import com.google.devtools.j2objc.util.Parser;
import com.google.devtools.j2objc.util.ProGuardUsageParser;
import com.google.devtools.j2objc.util.UnicodeUtils;
import java.io.File;
Expand Down Expand Up @@ -71,8 +71,8 @@ private static void checkErrors() {
}

@VisibleForTesting
public static JdtParser createParser() {
JdtParser parser = new JdtParser();
public static Parser createParser() {
Parser parser = Options.newParser();
parser.addClasspathEntries(Options.getClassPathEntries());
parser.addClasspathEntries(Options.getBootClasspath());
parser.addSourcepathEntries(Options.getSourcePathEntries());
Expand Down Expand Up @@ -102,7 +102,7 @@ public static void run(List<String> fileArgs) {
File preProcessorTempDir = null;
File strippedSourcesDir = null;
try {
JdtParser parser = createParser();
Parser parser = createParser();

List<ProcessingContext> inputs = Lists.newArrayList();
GenerationBatch batch = new GenerationBatch();
Expand Down
21 changes: 21 additions & 0 deletions translator/src/main/java/com/google/devtools/j2objc/Options.java
Expand Up @@ -26,6 +26,7 @@
import com.google.devtools.j2objc.util.FileUtil;
import com.google.devtools.j2objc.util.HeaderMap;
import com.google.devtools.j2objc.util.PackagePrefixes;
import com.google.devtools.j2objc.util.Parser;
import com.google.devtools.j2objc.util.SourceVersion;
import com.google.devtools.j2objc.util.Version;

Expand Down Expand Up @@ -96,6 +97,9 @@ public class Options {
private boolean includeGeneratedSources = false;
private TimingLevel timingLevel = TimingLevel.NONE;

// TODO(tball): remove after front-end conversion is complete.
private FrontEnd javaFrontEnd = FrontEnd.JDT;

private PackagePrefixes packagePrefixes = new PackagePrefixes();

// TODO(kirbs): Uncomment following lines and lines in OptionsTest when we enable automatic
Expand Down Expand Up @@ -195,6 +199,11 @@ public String suffix() {
}
}

// TODO(tball): remove after front-end conversion is complete.
private static enum FrontEnd {
JDT, JAVAC
}

/**
* Xlint options and their associated JDT parser warnings.
*/
Expand Down Expand Up @@ -514,6 +523,10 @@ private String[] loadInternal(String[] args) throws IOException {
nullability = true;
} else if (arg.startsWith("-Xlint")) {
lintOptions = LintOption.parse(arg);
} else if (arg.equals("-Xuse-jdt")) {
javaFrontEnd = FrontEnd.JDT;
} else if (arg.equals("-Xuse-javac")) {
javaFrontEnd = FrontEnd.JAVAC;
} else if (arg.equals("-version")) {
version();
} else if (arg.startsWith("-h") || arg.equals("--help")) {
Expand Down Expand Up @@ -1012,4 +1025,12 @@ public static boolean includeGeneratedSources() {
public static TimingLevel timingLevel() {
return instance.timingLevel;
}

// TODO(tball): remove after front-end conversion is complete.
public static Parser newParser() {
if (instance.javaFrontEnd == FrontEnd.JDT) {
return new com.google.devtools.j2objc.util.JdtParser();
}
throw new AssertionError("javac front-end not implemented");
}
}
Expand Up @@ -19,17 +19,13 @@
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.devtools.j2objc.Options;
import com.google.devtools.j2objc.ast.CompilationUnit;
import com.google.devtools.j2objc.file.InputFile;
import com.google.devtools.j2objc.jdt.BindingConverter;
import com.google.devtools.j2objc.jdt.TreeConverter;
import com.google.devtools.j2objc.util.ErrorUtil;
import com.google.devtools.j2objc.util.FileUtil;
import com.google.devtools.j2objc.util.JdtParser;
import com.google.devtools.j2objc.util.NameTable;

import org.eclipse.jdt.core.dom.CompilationUnit;

import java.io.IOException;
import com.google.devtools.j2objc.util.Parser;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -44,17 +40,16 @@ abstract class FileProcessor {

private static final Logger logger = Logger.getLogger(FileProcessor.class.getName());

private final JdtParser parser;
private final Parser parser;
protected final BuildClosureQueue closureQueue;
private final NameTable.Factory nameTableFactory = NameTable.newFactory();

private final int batchSize = Options.batchTranslateMaximum();
private final Set<ProcessingContext> batchInputs =
Sets.newLinkedHashSetWithExpectedSize(batchSize);

private final boolean doBatching = batchSize > 0;

public FileProcessor(JdtParser parser) {
public FileProcessor(Parser parser) {
this.parser = Preconditions.checkNotNull(parser);
if (Options.buildClosure()) {
// Should be an error if the user specifies this with --build-closure
Expand Down Expand Up @@ -101,21 +96,13 @@ private void processInput(ProcessingContext input) {

logger.finest("parsing " + file);

String source = null;
CompilationUnit compilationUnit = null;
try {
source = FileUtil.readFile(file);
compilationUnit = parser.parseWithBindings(file.getUnitName(), source);
} catch (IOException e) {
ErrorUtil.error(e.getMessage());
}

CompilationUnit compilationUnit = parser.parse(file);
if (compilationUnit == null) {
handleError(input);
return;
}

processCompiledSource(input, source, compilationUnit);
processCompiledSource(input, compilationUnit);
}

protected boolean isBatchable(InputFile file) {
Expand All @@ -136,17 +123,12 @@ private void processBatch() {
inputMap.put(path, input);
}

JdtParser.Handler handler = new JdtParser.Handler() {
JdtParser.Handler handler = new Parser.Handler() {
@Override
public void handleParsedUnit(String path, CompilationUnit unit) {
ProcessingContext input = inputMap.get(path);
try {
String source = FileUtil.readFile(input.getFile());
processCompiledSource(input, source, unit);
batchInputs.remove(input);
} catch (IOException e) {
ErrorUtil.error(e.getMessage());
}
processCompiledSource(input, unit);
batchInputs.remove(input);
}
};
logger.finest("Processing batch of size " + batchInputs.size());
Expand All @@ -160,17 +142,14 @@ public void handleParsedUnit(String path, CompilationUnit unit) {
batchInputs.clear();
}

private void processCompiledSource(ProcessingContext input, String source, CompilationUnit unit) {
private void processCompiledSource(ProcessingContext input,
com.google.devtools.j2objc.ast.CompilationUnit unit) {
InputFile file = input.getFile();
if (closureQueue != null) {
closureQueue.addProcessedName(FileUtil.getQualifiedMainTypeName(file, unit));
}
try {
com.google.devtools.j2objc.ast.CompilationUnit convertedUnit =
TreeConverter.convertCompilationUnit(
unit, input.getOriginalSourcePath(), FileUtil.getMainTypeName(file), source,
nameTableFactory);
processConvertedTree(input, convertedUnit);
processConvertedTree(input, unit);
} catch (Throwable t) {
// Report any uncaught exceptions.
ErrorUtil.fatalError(t, input.getOriginalSourcePath());
Expand Down
Expand Up @@ -16,17 +16,15 @@

import com.google.common.io.Files;
import com.google.devtools.j2objc.Options;
import com.google.devtools.j2objc.ast.Annotation;
import com.google.devtools.j2objc.ast.CompilationUnit;
import com.google.devtools.j2objc.ast.SingleMemberAnnotation;
import com.google.devtools.j2objc.file.InputFile;
import com.google.devtools.j2objc.file.RegularInputFile;
import com.google.devtools.j2objc.util.ErrorUtil;
import com.google.devtools.j2objc.util.FileUtil;
import com.google.devtools.j2objc.util.JdtParser;
import com.google.devtools.j2objc.util.Parser;
import com.google.j2objc.annotations.ObjectiveCName;

import org.eclipse.jdt.core.dom.Annotation;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.SingleMemberAnnotation;

import java.io.File;
import java.io.IOException;
import java.util.List;
Expand All @@ -39,10 +37,10 @@ public class InputFilePreprocessor {

private static final Logger logger = Logger.getLogger(InputFilePreprocessor.class.getName());

private final JdtParser parser;
private final Parser parser;
private File strippedSourcesDir;

public InputFilePreprocessor(JdtParser parser) {
public InputFilePreprocessor(Parser parser) {
this.parser = parser;
}

Expand Down Expand Up @@ -84,9 +82,10 @@ private void processRegularSource(ProcessingContext input) throws IOException {
// No need to parse.
return;
}
CompilationUnit compilationUnit = parser.parseWithoutBindings(file.getUnitName(), source);
org.eclipse.jdt.core.dom.CompilationUnit compilationUnit =
parser.parseWithoutBindings(file.getUnitName(), source);
if (compilationUnit == null) {
// An error occured, reported by the JdtParser.
// The parser found and reported one or more errors.
return;
}
String qualifiedName = FileUtil.getQualifiedMainTypeName(file, compilationUnit);
Expand All @@ -107,7 +106,8 @@ private void processRegularSource(ProcessingContext input) throws IOException {
private void processPackageInfoSource(ProcessingContext input) throws IOException {
InputFile file = input.getFile();
String source = FileUtil.readFile(file);
CompilationUnit compilationUnit = parser.parseWithBindings(file.getUnitName(), source);
CompilationUnit compilationUnit =
parser.parse(FileUtil.getMainTypeName(file), file.getUnitName(), source);
if (compilationUnit != null) {
extractPackagePrefix(file, compilationUnit);
}
Expand All @@ -116,18 +116,16 @@ private void processPackageInfoSource(ProcessingContext input) throws IOExceptio
private void extractPackagePrefix(InputFile file, CompilationUnit unit) {
// We should only reach here if it's a package-info.java file.
assert file.getUnitName().endsWith("package-info.java");
@SuppressWarnings("unchecked")
List<Annotation> annotations = (List<Annotation>) unit.getPackage().annotations();
List<Annotation> annotations = (List<Annotation>) unit.getPackage().getAnnotations();
for (Annotation annotation : annotations) {
// getFullyQualifiedName() might not actually return a fully qualified name.
String name = annotation.getTypeName().getFullyQualifiedName();
if (name.endsWith("ObjectiveCName")) {
// Per Eclipse docs, binding resolution can be a resource hog.
if (annotation.resolveAnnotationBinding().getAnnotationType().getQualifiedName().equals(
if (annotation.getAnnotationBinding().getAnnotationType().getQualifiedName().equals(
ObjectiveCName.class.getCanonicalName())) {
String key = unit.getPackage().getName().getFullyQualifiedName();
String val = (String) ((SingleMemberAnnotation) annotation).getValue()
.resolveConstantExpressionValue();
String val = (String) ((SingleMemberAnnotation) annotation).getValue().getConstantValue();
Options.addPackagePrefix(key, val);
}
}
Expand Down
Expand Up @@ -64,7 +64,7 @@
import com.google.devtools.j2objc.types.Import;
import com.google.devtools.j2objc.util.DeadCodeMap;
import com.google.devtools.j2objc.util.ErrorUtil;
import com.google.devtools.j2objc.util.JdtParser;
import com.google.devtools.j2objc.util.Parser;
import com.google.devtools.j2objc.util.TimeTracker;

import java.util.Set;
Expand All @@ -85,7 +85,7 @@ public class TranslationProcessor extends FileProcessor {

private int processedCount = 0;

public TranslationProcessor(JdtParser parser, DeadCodeMap deadCodeMap) {
public TranslationProcessor(Parser parser, DeadCodeMap deadCodeMap) {
super(parser);
this.deadCodeMap = deadCodeMap;
}
Expand Down
Expand Up @@ -16,12 +16,12 @@
import com.google.common.io.CharStreams;
import com.google.devtools.j2objc.J2ObjC;
import com.google.devtools.j2objc.Options;
import com.google.devtools.j2objc.ast.CompilationUnit;
import com.google.devtools.j2objc.ast.PackageDeclaration;
import com.google.devtools.j2objc.file.InputFile;
import com.google.devtools.j2objc.file.JarredInputFile;
import com.google.devtools.j2objc.file.RegularInputFile;

import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.PackageDeclaration;

import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -52,6 +52,18 @@ public static String getMainTypeName(InputFile file) {
return basename;
}

// TODO(tball): remove when Parser extraction is complete.
public static String getQualifiedMainTypeName(InputFile file,
org.eclipse.jdt.core.dom.CompilationUnit unit) {
String qualifiedName = getMainTypeName(file);
org.eclipse.jdt.core.dom.PackageDeclaration packageDecl = unit.getPackage();
if (packageDecl != null) {
String packageName = packageDecl.getName().getFullyQualifiedName();
qualifiedName = packageName + "." + qualifiedName;
}
return qualifiedName;
}

public static String getQualifiedMainTypeName(InputFile file, CompilationUnit unit) {
String qualifiedName = getMainTypeName(file);
PackageDeclaration packageDecl = unit.getPackage();
Expand Down

0 comments on commit 79e3761

Please sign in to comment.