Skip to content

Commit

Permalink
Fix scoping of temporary variables in Darwin codegen.
Browse files Browse the repository at this point in the history
When converting a C++ DecodableList into an Objective C NSArray, we
declare some temporary variables.  These were not scoped in their own
scope, so if multiple lists were decoded at the same nesting level
(e.g. two list-typed members of the same struct), the names would
collide and produce compile errors.

The fix is to just scope the entire conversion.
  • Loading branch information
bzbarsky-apple committed Feb 12, 2022
1 parent 6a291ca commit 1898991
Show file tree
Hide file tree
Showing 3 changed files with 5,274 additions and 5,263 deletions.
20 changes: 10 additions & 10 deletions src/darwin/Framework/CHIP/templates/partials/decode_value.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@
{{>decode_value target=target source=(concat source ".Value()") cluster=cluster errorCode=errorCode depth=(incrementDepth depth) isNullable=false}}
}
{{else if isArray}}
auto * array_{{depth}} = [NSMutableArray new];
auto iter_{{depth}} = {{source}}.begin();
while (iter_{{depth}}.Next()) {
auto & entry_{{depth}} = iter_{{depth}}.GetValue();
{{asObjectiveCClass type cluster forceNotList=true}} * newElement_{{depth}};
{{>decode_value target=(concat "newElement_" depth) source=(concat "entry_" depth) cluster=cluster errorCode=errorCode depth=(incrementDepth depth) isArray=false}}
[array_{{depth}} addObject:newElement_{{depth}}];
}
{ // Scope for the error so we will know what it's named
{ // Scope for our temporary variables
auto * array_{{depth}} = [NSMutableArray new];
auto iter_{{depth}} = {{source}}.begin();
while (iter_{{depth}}.Next()) {
auto & entry_{{depth}} = iter_{{depth}}.GetValue();
{{asObjectiveCClass type cluster forceNotList=true}} * newElement_{{depth}};
{{>decode_value target=(concat "newElement_" depth) source=(concat "entry_" depth) cluster=cluster errorCode=errorCode depth=(incrementDepth depth) isArray=false}}
[array_{{depth}} addObject:newElement_{{depth}}];
}
CHIP_ERROR err = iter_{{depth}}.GetStatus();
if (err != CHIP_NO_ERROR) {
{{errorCode}}
}
{{target}} = array_{{depth}};
}
{{target}} = array_{{depth}};
{{else}}
{{#if_is_struct type}}
{{target}} = [{{asObjectiveCClass type cluster forceNotList=true}} new];
Expand Down
Loading

0 comments on commit 1898991

Please sign in to comment.