Fix build non-determinism in J2ObjC code generator. When transpiling Java code to Objective-C via J2ObjC (rule J2objcTranspile), build output could differ across runs due to two issues: 1. Unordered HashSet instances were used for collecting types, forward declarations, and variables in HeaderImportCollector, ObjectiveCImplementationGenerator, ObjectiveCHeaderGenerator, and VariableRenamer. Using HashSet on TypeElement, VariableElement, TypeMirror, and Import resulted in non-deterministic iteration order based on identity hash codes. 2. When relative line directives (-g:relative) were enabled, temporary extraction directory paths (/tmp/<random_temp_dir>/...) were emitted into #line directives because /tmp/ paths failed startsWith(cwdPath). This CL replaces HashSet with LinkedHashSet across these generator passes and updates TypeImplementationGenerator.getSourceFilePath() to strip temporary directory prefixes when -g:relative is set. PiperOrigin-RevId: 951131076
diff --git a/translator/src/main/java/com/google/devtools/j2objc/gen/ObjectiveCHeaderGenerator.java b/translator/src/main/java/com/google/devtools/j2objc/gen/ObjectiveCHeaderGenerator.java index d35466e..85baeaa 100644 --- a/translator/src/main/java/com/google/devtools/j2objc/gen/ObjectiveCHeaderGenerator.java +++ b/translator/src/main/java/com/google/devtools/j2objc/gen/ObjectiveCHeaderGenerator.java
@@ -107,9 +107,9 @@ pushIgnoreDeprecatedDeclarationsPragma(); pushIgnoreNullabilityPragmas(); - Set<String> seenTypes = Sets.newHashSet(); + Set<String> seenTypes = Sets.newLinkedHashSet(); Set<String> includeFiles = Sets.newTreeSet(); - Set<Import> forwardDeclarations = Sets.newHashSet(); + Set<Import> forwardDeclarations = Sets.newLinkedHashSet(); includeFiles.add("J2ObjC_header.h");
diff --git a/translator/src/main/java/com/google/devtools/j2objc/gen/ObjectiveCImplementationGenerator.java b/translator/src/main/java/com/google/devtools/j2objc/gen/ObjectiveCImplementationGenerator.java index a90e434..07830d6 100644 --- a/translator/src/main/java/com/google/devtools/j2objc/gen/ObjectiveCImplementationGenerator.java +++ b/translator/src/main/java/com/google/devtools/j2objc/gen/ObjectiveCImplementationGenerator.java
@@ -149,8 +149,8 @@ print(code); } - Set<String> seenTypes = Sets.newHashSet(); - Set<Import> forwardDecls = Sets.newHashSet(); + Set<String> seenTypes = Sets.newLinkedHashSet(); + Set<Import> forwardDecls = Sets.newLinkedHashSet(); for (GeneratedType generatedType : getOrderedTypes()) { String name = generatedType.getTypeName(); seenTypes.add(name);
diff --git a/translator/src/main/java/com/google/devtools/j2objc/gen/TypeImplementationGenerator.java b/translator/src/main/java/com/google/devtools/j2objc/gen/TypeImplementationGenerator.java index 92d8deb..5c7ae33 100644 --- a/translator/src/main/java/com/google/devtools/j2objc/gen/TypeImplementationGenerator.java +++ b/translator/src/main/java/com/google/devtools/j2objc/gen/TypeImplementationGenerator.java
@@ -90,11 +90,21 @@ Path sourceFilePath = toNormalizedSourcePath(sourceFilePathString); Path cwdPath = toNormalizedSourcePath("."); - if (!sourceFilePath.startsWith(cwdPath)) { - return sourceFilePathString; + if (sourceFilePath.startsWith(cwdPath)) { + return cwdPath.relativize(sourceFilePath).toString(); } - return cwdPath.relativize(sourceFilePath).toString(); + Path tempDir = + Paths.get(System.getProperty("java.io.tmpdir", "/tmp")).normalize().toAbsolutePath(); + if (sourceFilePath.startsWith(tempDir)) { + Path relativeToTemp = tempDir.relativize(sourceFilePath); + if (relativeToTemp.getNameCount() > 1) { + return relativeToTemp.subpath(1, relativeToTemp.getNameCount()).toString(); + } + return relativeToTemp.toString(); + } + + return sourceFilePathString; } protected void generate() {
diff --git a/translator/src/main/java/com/google/devtools/j2objc/translate/VariableRenamer.java b/translator/src/main/java/com/google/devtools/j2objc/translate/VariableRenamer.java index 3b1bef3..b90d8a4 100644 --- a/translator/src/main/java/com/google/devtools/j2objc/translate/VariableRenamer.java +++ b/translator/src/main/java/com/google/devtools/j2objc/translate/VariableRenamer.java
@@ -36,7 +36,7 @@ import com.google.errorprone.annotations.CanIgnoreReturnValue; import java.util.ArrayDeque; import java.util.Deque; -import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.Set; import javax.lang.model.element.Element; import javax.lang.model.element.ExecutableElement; @@ -52,15 +52,15 @@ public class VariableRenamer extends UnitTreeVisitor { private final Deque<Set<String>> fieldNameStack = new ArrayDeque<>(); - private final Set<TypeElement> renamedTypes = new HashSet<>(); + private final Set<TypeElement> renamedTypes = new LinkedHashSet<>(); // Keep track of the variables and names in a scope so we can rename variables in a // scope-aware manner; start with a sentinel empty scope. private final Deque<Scope> scopes = new ArrayDeque<>(ImmutableList.of(new Scope())); // Keep track of variables that are in scope and the names that have already been used. private static class Scope { - private final Set<VariableElement> variables = new HashSet<>(); - private final Set<String> usedNames = new HashSet<>(); + private final Set<VariableElement> variables = new LinkedHashSet<>(); + private final Set<String> usedNames = new LinkedHashSet<>(); private Scope(Scope enclosingScope) { this.variables.addAll(enclosingScope.variables); @@ -94,12 +94,12 @@ collectAndRenameFields(ElementUtil.getSuperclass(type), fields); if (!renamedTypes.contains(type)) { renamedTypes.add(type); - Set<String> superFieldNames = new HashSet<>(); + Set<String> superFieldNames = new LinkedHashSet<>(); for (VariableElement superField : fields) { superFieldNames.add(superField.getSimpleName().toString()); } // Look for methods that might conflict with a static variable when functionized. - Set<String> staticMethodNames = new HashSet<>(); + Set<String> staticMethodNames = new LinkedHashSet<>(); for (ExecutableElement method : ElementUtil.getExecutables(type)) { if (method.getParameters().size() == 0) { staticMethodNames.add(nameTable.getFunctionName(method)); @@ -135,9 +135,9 @@ } private void pushType(TypeElement type) { - Set<VariableElement> fields = new HashSet<>(); + Set<VariableElement> fields = new LinkedHashSet<>(); collectAndRenameFields(type, fields); - Set<String> fullFieldNames = new HashSet<>(); + Set<String> fullFieldNames = new LinkedHashSet<>(); for (VariableElement field : fields) { fullFieldNames.add(nameTable.getVariableShortName(field)); } @@ -173,7 +173,8 @@ } if (var.getKind().isField()) { // Make sure fields for the declaring type are renamed. - collectAndRenameFields(ElementUtil.getDeclaringClass(var), new HashSet<VariableElement>()); + collectAndRenameFields( + ElementUtil.getDeclaringClass(var), new LinkedHashSet<VariableElement>()); } else { // Local variable or parameter. handleVariable(var);
diff --git a/translator/src/main/java/com/google/devtools/j2objc/types/HeaderImportCollector.java b/translator/src/main/java/com/google/devtools/j2objc/types/HeaderImportCollector.java index 78072b1..e5cf56f 100644 --- a/translator/src/main/java/com/google/devtools/j2objc/types/HeaderImportCollector.java +++ b/translator/src/main/java/com/google/devtools/j2objc/types/HeaderImportCollector.java
@@ -34,7 +34,6 @@ import com.google.devtools.j2objc.util.ElementUtil; import com.google.devtools.j2objc.util.TranslationUtil; import com.google.devtools.j2objc.util.TypeUtil; -import java.util.HashSet; import java.util.LinkedHashSet; import java.util.Set; import javax.lang.model.element.Element; @@ -80,7 +79,7 @@ // Supertypes of the below declared types that haven't been seen by this collector. private Set<Import> superTypes = new LinkedHashSet<>(); // Declared types seen by this collector. - private Set<Import> declaredTypes = new HashSet<>(); + private Set<Import> declaredTypes = new LinkedHashSet<>(); // The current type declarations is annotated for ObjC generics. private boolean wantsGenerateObjectiveCGenerics = false; private final boolean includeInnerTypes; @@ -152,10 +151,10 @@ private Set<TypeMirror> objCForwardDeclaredGenericParameters(TypeMirror type) { // Parameters only needed with ObjC generics are on. if (!unit.getEnv().options().asObjCGenericDecl() && !wantsGenerateObjectiveCGenerics) { - return new HashSet<>(); + return new LinkedHashSet<>(); } - HashSet<TypeMirror> vistedTypes = new HashSet<>(); + Set<TypeMirror> vistedTypes = new LinkedHashSet<>(); vistedTypes.add(type); for (TypeElement bound : unit.getEnv().typeUtil().getObjcUpperBounds(type)) { vistedTypes.add(bound.asType());