Skip to content

Commit

Permalink
Supports multiple --dead_code_report flags in j2objc, creates report …
Browse files Browse the repository at this point in the history
…from entry_classes.

PiperOrigin-RevId: 358889283
  • Loading branch information
tomball authored and copybara-github committed Feb 22, 2021
1 parent baa0256 commit becab65
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public static Parser createParser(Options options) {
return parser;
}

private static CodeReferenceMap loadDeadCodeMap() {
return ProGuardUsageParser.parseDeadCodeFile(Options.getProGuardUsageFile());
private static CodeReferenceMap loadDeadCodeMap(Options options) {
return ProGuardUsageParser.parseDeadCodeFile(options.getProGuardUsageFile());
}

/**
Expand Down Expand Up @@ -123,7 +123,7 @@ public static void run(List<String> fileArgs, Options options) {

options.getHeaderMap().loadMappings();
TranslationProcessor translationProcessor =
new TranslationProcessor(parser, loadDeadCodeMap());
new TranslationProcessor(parser, loadDeadCodeMap(options));
translationProcessor.processInputs(inputs);
if (ErrorUtil.errorCount() > 0) {
return;
Expand Down
24 changes: 20 additions & 4 deletions translator/src/main/java/com/google/devtools/j2objc/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

package com.google.devtools.j2objc;

import static com.google.common.io.FileWriteMode.APPEND;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
Expand Down Expand Up @@ -104,7 +107,7 @@ public class Options {

private SourceVersion sourceVersion = null;

private static File proGuardUsageFile = null;
private File proGuardUsageFile = null;

private static String fileHeader;
private static final String FILE_HEADER_KEY = "file-header";
Expand Down Expand Up @@ -364,7 +367,7 @@ private void processArg(Iterator<String> args) throws IOException {
} else if (arg.equals("--output-header-mapping")) {
headerMap.setOutputMappingFile(new File(getArgValue(args, arg)));
} else if (arg.equals("--dead-code-report")) {
proGuardUsageFile = new File(getArgValue(args, arg));
addDeadCodeReport(getArgValue(args, arg));
} else if (arg.equals("--prefix")) {
addPrefixOption(getArgValue(args, arg));
} else if (arg.equals("--prefixes")) {
Expand Down Expand Up @@ -810,14 +813,27 @@ public static String getFileHeader() {
return fileHeader;
}

public static void setProGuardUsageFile(File newProGuardUsageFile) {
public void setProGuardUsageFile(File newProGuardUsageFile) {
proGuardUsageFile = newProGuardUsageFile;
}

public static File getProGuardUsageFile() {
public File getProGuardUsageFile() {
return proGuardUsageFile;
}

/**
* Appends a dead code report to the proGuardUsageFile. If that file
* doesn't exist, it's created first.
*/
public void addDeadCodeReport(String path) throws IOException {
if (proGuardUsageFile == null) {
proGuardUsageFile = File.createTempFile("dead_code_report", "cfg");
}
File f = new File(path);
String newReport = Files.asCharSource(f, UTF_8).read();
Files.asCharSink(proGuardUsageFile, UTF_8, APPEND).write(newReport);
}

public List<String> getBootClasspath() {
return getPathArgument(bootclasspath, false, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

package com.google.devtools.j2objc;

import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.io.Files;
import com.google.devtools.j2objc.util.SourceVersion;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -89,4 +92,21 @@ public void testFlagsIntermixedWithSources() throws IOException {
assertEquals(SourceVersion.JAVA_8, options.getSourceVersion());
assertTrue(options.fileUtil().getClassPathEntries().contains(tmpDir.getPath()));
}

public void testMultipleDeadCodeReports() throws IOException {
File first = new File(getTempDir(), "first.cfg");
Files.write("first line\n", first, UTF_8);
options.addDeadCodeReport(first.getPath());

File second = new File(getTempDir(), "second.cfg");
Files.write("second line\n", second, UTF_8);
options.addDeadCodeReport(second.getPath());

File third = new File(getTempDir(), "third.cfg");
Files.write("third line\n", third, UTF_8);
options.addDeadCodeReport(third.getPath());

String result = Files.asCharSource(options.getProGuardUsageFile(), UTF_8).read();
assertEquals("first line\nsecond line\nthird line\n", result);
}
}

0 comments on commit becab65

Please sign in to comment.