1From 580fcef90ab970ad37ea9f7059373f773b3e55d2 Mon Sep 17 00:00:00 2001
2From: Jakub Pawlowski <jpawlowski@google.com>
3Date: Fri, 03 Aug 2018 08:46:10 +0000
4Subject: [PATCH] Fix data_types_definition.tmpl
5
6This template can generate code that does not compile, if there are
7multiple fields with different versions where at least one has version 0
8and end up being laid out in a way that does not match the ordinal order
9
10Sample Mojo file:
11// Describes ARC package.
12struct ArcPackageInfo {
13  string package_name;
14  int32 package_version;
15  int64 last_backup_android_id;
16  int64 last_backup_time;
17  bool sync;  // true if package installation should be synced
18  [MinVersion=11] bool system;  // true if package is system package.
19  // true if package registers VPNService intent.
20  [MinVersion=25] bool vpn_provider;
21};
22
23Sample badly generated code (no closing "}" for last if):
24
25    @SuppressWarnings("unchecked")
26    public static ArcPackageInfo decode(org.chromium.mojo.bindings.Decoder decoder0) {
27        if (decoder0 == null) {
28            return null;
29        }
30        decoder0.increaseStackDepth();
31        ArcPackageInfo result;
32        try {
33            org.chromium.mojo.bindings.DataHeader mainDataHeader = decoder0.readAndValidateDataHeader(VERSION_ARRAY);
34            final int elementsOrVersion = mainDataHeader.elementsOrVersion;
35            result = new ArcPackageInfo(elementsOrVersion);
36                {
37
38                result.packageName = decoder0.readString(8, false);
39                }
40                {
41
42                result.packageVersion = decoder0.readInt(16);
43                }
44                {
45
46                result.sync = decoder0.readBoolean(20, 0);
47                }
48            if (elementsOrVersion >= 11) {
49                {
50
51                result.system = decoder0.readBoolean(20, 1);
52                }
53            }
54            if (elementsOrVersion >= 25) {
55                {
56
57                result.vpnProvider = decoder0.readBoolean(20, 2);
58                }
59            }
60            if (elementsOrVersion >= 0) {
61                {
62
63                result.lastBackupAndroidId = decoder0.readLong(24);
64                }
65                {
66
67                result.lastBackupTime = decoder0.readLong(32);
68                }
69        } finally {
70            decoder0.decreaseStackDepth();
71        }
72        return result;
73    }
74
75Change-Id: I4c1b573a71b20cc6a0828a2cceff6bbfbb4ac5bc
76Reviewed-on: https://chromium-review.googlesource.com/1158702
77Reviewed-by: Luis Hector Chavez <lhchavez@chromium.org>
78Reviewed-by: Ken Rockot <rockot@chromium.org>
79Commit-Queue: Jakub x Jakub Pawlowski <jpawlowski@google.com>
80Cr-Commit-Position: refs/heads/master@{#580480}
81---
82
83diff --git a/mojo/public/tools/bindings/generators/java_templates/data_types_definition.tmpl b/mojo/public/tools/bindings/generators/java_templates/data_types_definition.tmpl
84index 59c6fee..7af57bd 100644
85--- a/mojo/public/tools/bindings/generators/java_templates/data_types_definition.tmpl
86+++ b/mojo/public/tools/bindings/generators/java_templates/data_types_definition.tmpl
87@@ -175,6 +175,7 @@
88             org.chromium.mojo.bindings.DataHeader mainDataHeader = decoder0.readAndValidateDataHeader(VERSION_ARRAY);
89             final int elementsOrVersion = mainDataHeader.elementsOrVersion;
90             result = new {{struct|name}}(elementsOrVersion);
91+
92 {%- set prev_ver = [0] %}
93 {%- for byte in struct.bytes %}
94 {%-   for packed_field in byte.packed_fields %}
95@@ -183,7 +184,9 @@
96             }
97 {%-       endif %}
98 {%-       set _ = prev_ver.append(packed_field.min_version) %}
99+{%-       if prev_ver[-1] != 0 %}
100             if (elementsOrVersion >= {{packed_field.min_version}}) {
101+{%-       endif %}
102 {%-     endif %}
103                 {
104                     {{decode('result.' ~ packed_field.field|name, packed_field.field.kind, 8+packed_field.offset, packed_field.bit)|indent(16)}}
105@@ -193,6 +196,7 @@
106 {%- if prev_ver[-1] != 0 %}
107             }
108 {%- endif %}
109+
110         } finally {
111             decoder0.decreaseStackDepth();
112         }
113