Skip to content

Commit 9354546

Browse files
committed
Do not increase pointer after last enum constant
1 parent d324540 commit 9354546

3 files changed

Lines changed: 13 additions & 13 deletions

File tree

.idea/misc.xml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

translator/src/main/java/com/google/devtools/j2objc/translate/EnumRewriter.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,16 +208,17 @@ private void addNonArcInitialization(EnumDeclaration node) {
208208
} else {
209209
baseTypeCount++;
210210
}
211-
212-
initStatements.add(new ExpressionStatement(
213-
new CommaExpression(
214-
new CastExpression(voidType, new ParenthesizedExpression(
215-
new Assignment(new SimpleName(varElement),
216-
new Assignment(new SimpleName(localEnum),
211+
Expression initExpr = new CastExpression(voidType, new ParenthesizedExpression(
212+
new Assignment(new SimpleName(varElement),
213+
new Assignment(new SimpleName(localEnum),
217214
new NativeExpression(
218215
UnicodeUtils.format("objc_constructInstance(%s, (void *)ptr)", classExpr),
219-
type.asType()))))),
220-
new NativeExpression("ptr += " + sizeName, voidType))));
216+
type.asType())))));
217+
if (i < node.getEnumConstants().size() - 1) {
218+
initExpr = new CommaExpression(initExpr,
219+
new NativeExpression("ptr += " + sizeName, voidType));
220+
}
221+
initStatements.add(new ExpressionStatement(initExpr));
221222
String initName = nameTable.getFullFunctionName(methodElement);
222223
FunctionElement initElement = new FunctionElement(initName, voidType, valueType)
223224
.addParameters(valueType.asType())

translator/src/test/java/com/google/devtools/j2objc/translate/EnumRewriterTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public void testGenericEnumConstructor() throws IOException {
3333
"void Test_initWithId_withNSString_withInt_("
3434
+ "Test *self, id t, NSString *__name, int32_t __ordinal) {");
3535
assertTranslatedLines(translation,
36-
"((void) (JreEnum(Test, A) = e = "
37-
+ "objc_constructInstance(self, (void *)ptr)), ptr += objSize);",
36+
"(void) (JreEnum(Test, A) = e = "
37+
+ "objc_constructInstance(self, (void *)ptr));",
3838
"Test_initWithId_withNSString_withInt_(e, @\"foo\", @\"A\", 0);");
3939
}
4040

@@ -160,8 +160,8 @@ public void testEnumAllocationCode() throws Exception {
160160
"((void) (JreEnum(Test, B) = e = objc_constructInstance([Test_1 class],"
161161
+ " (void *)ptr)), ptr += objSize_B);",
162162
"Test_1_initWithNSString_withInt_(e, @\"B\", 1);",
163-
"((void) (JreEnum(Test, C) = e = "
164-
+ "objc_constructInstance(self, (void *)ptr)), ptr += objSize);",
163+
"(void) (JreEnum(Test, C) = e = "
164+
+ "objc_constructInstance(self, (void *)ptr));",
165165
"Test_initWithNSString_withInt_(e, @\"C\", 2);");
166166
}
167167

0 commit comments

Comments
 (0)