Skip to content

Commit

Permalink
Make sure the last option is used when multiple
Browse files Browse the repository at this point in the history
-Xecj_problem_severity_preferences are specified

Also allow a later -Xecj_problem_severity_preferences to turn off an
earlier one
  • Loading branch information
guw committed Aug 23, 2023
1 parent 301e78f commit d4e7980
Showing 1 changed file with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ static class BlazeEclipseBatchCompiler extends Main {
public BlazeEclipseBatchCompiler(PrintWriter outWriter, PrintWriter errWriter,
ImmutableList<BlazeJavaCompilerPlugin> plugins, String sandboxPathPrefix,
Map<Path, Path> sourceFilesByAbsoluteOrCanonicalPath,
UsedDependencyCollectionMode usedDependencyCollectionMode,
Path problemSeverityPreferences) {
UsedDependencyCollectionMode usedDependencyCollectionMode, Path problemSeverityPreferences) {
super(outWriter, errWriter, false /* systemExitWhenFinished */, null /* customDefaultOptions */,
null /* compilationProgress */);
this.usedDependencyCollectionMode = usedDependencyCollectionMode;
Expand All @@ -121,7 +120,7 @@ public BlazeEclipseBatchCompiler(PrintWriter outWriter, PrintWriter errWriter,
this.directDependenciesMap = dependencyModule.getExplicitDependenciesMap();
this.noneDirectDependenciesMap = dependencyModule.getImplicitDependenciesMap();

if(problemSeverityPreferences != null) {
if (problemSeverityPreferences != null) {
this.options.putAll(loadProblemSeverityPreferences(problemSeverityPreferences));
}

Expand Down Expand Up @@ -298,11 +297,15 @@ public static BlazeJavacResult compile(BlazeJavacArguments arguments) {

// note, all -Xecj... are "blaze" specific javac options
String collectUsedDepsOption = getJavacOptionValue(arguments.blazeJavacOptions(), "-Xecj_collect_used_deps");
String problemSeverityPreferences = getJavacOptionValue(arguments.blazeJavacOptions(), "-Xecj_problem_severity_preferences");
String problemSeverityPreferences = getJavacOptionValue(arguments.blazeJavacOptions(),
"-Xecj_problem_severity_preferences");

BlazeEclipseBatchCompiler compiler = new BlazeEclipseBatchCompiler(errWriter, errWriter, arguments.plugins(),
sandboxPathPrefix, sourceFilesByAbsoluteOrCanonicalPath, UsedDependencyCollectionMode
.fromOptionValue(collectUsedDepsOption), problemSeverityPreferences != null ? Path.of(problemSeverityPreferences) : null);
sandboxPathPrefix, sourceFilesByAbsoluteOrCanonicalPath,
UsedDependencyCollectionMode.fromOptionValue(collectUsedDepsOption),
problemSeverityPreferences != null && !isDisabled(problemSeverityPreferences)
? Path.of(problemSeverityPreferences)
: null);

List<String> ecjArguments = new ArrayList<>();
setLocations(ecjArguments, arguments, compiler.dependencyModule);
Expand Down Expand Up @@ -364,27 +367,43 @@ public static BlazeJavacResult compile(BlazeJavacArguments arguments) {
builder.build());
}

private static boolean isDisabled(String problemSeverityPreferences) {
Set<String> turnOffSettings = Set.of("off", "none", "disabled", "");
return turnOffSettings.contains(problemSeverityPreferences);
}

/**
* Go through the list of javac options and collect the value of the <b>last</b> option found.
* <p>
* The reason we go with last is that we assume it's the most specific.
* </p>
* @param javacOptions
* @param optionName
* @return
*/
private static String getJavacOptionValue(List<String> javacOptions, String optionName) {
String value = null; // last one wins
for (int i = 0; i < javacOptions.size(); i++) {
String option = javacOptions.get(i);
if (option.startsWith(optionName)) {
int separatorPos = option.indexOf('=');
if (separatorPos == -1 && javacOptions.size() > i + 1) {
return javacOptions.get(i + 1);
value = javacOptions.get(i + 1);
} else {
return option.substring(separatorPos + 1).trim();
value = option.substring(separatorPos + 1).trim();
}
}
}
return null;
return value;
}

static Map<String, String> loadProblemSeverityPreferences(Path compilerPreferencesFile) {
final Properties properties = new Properties();
try (InputStream is = new BufferedInputStream(newInputStream(compilerPreferencesFile))) {
properties.load(is);
} catch (IOException e) {
throw new IllegalStateException(format("Error loading problem severity preferences '%s': %s", compilerPreferencesFile, e.getMessage()), e);
throw new IllegalStateException(format("Error loading problem severity preferences '%s': %s",
compilerPreferencesFile, e.getMessage()), e);
}

Set<String> warningOptions = Set.of(CompilerOptions.warningOptionNames());
Expand Down

0 comments on commit d4e7980

Please sign in to comment.