handle dangerous sub-ifs aside from the final child, when deciding which if braces can be removed

This commit is contained in:
Alon Zakai 2015-01-16 11:31:59 -08:00
Родитель 9e3d84710a
Коммит 5d66b59df4
3 изменённых файлов: 42 добавлений и 7 удалений

Просмотреть файл

@ -6,5 +6,18 @@ function test() {
if (HEAP32[i5 >> 2] | 0) {
if ((_fread(i4 | 0, 1, 257, HEAP32[i5 >> 2] | 0) | 0) != 257) if ((HEAPU8[i4 >> 0] | 0 | 0) != (HEAP32[84328] | 0)) if (_memcmp(i4 + 1 | 0, HEAP32[i20 >> 2] | 0, 256) | 0) if ((_fread(HEAP32[i16 >> 2] | 0, 256, 256, HEAP32[i5 >> 2] | 0) | 0) != 256) i31 = 9;
} else i31 = 9;
moar();
if ((c[21902] | 0) != 0 & (Vb | 0) == 29) {
if ((Ib | 0) == 60) if (!(cq(39080, 448, c[F >> 2] | 0, c[M >> 2] | 0, 0, 0) | 0)) x = 373; else {
Mb = 52;
Nb = 0;
Ob = 0;
return;
}
} else x = 373;
yet();
if (a) if (b) {
if (c) d();
} else e(); else f();
}

Просмотреть файл

@ -32,6 +32,31 @@ function test() {
} else {
i31 = 9;
}
moar();
if ((c[21902] | 0) != 0 & (Vb | 0) == 29) {
if ((Ib | 0) == 60) {
if (!(cq(39080, 448, c[F >> 2] | 0, c[M >> 2] | 0, 0, 0) | 0)) {
x = 373;
} else {
Mb = 52;
Nb = 0;
Ob = 0;
return;
}
}
} else x = 373;
yet();
if (a) {
if (b) {
if (c) {
d();
}
} else {
e();
}
} else {
f();
}
}
// EMSCRIPTEN_GENERATED_FUNCTIONS: ["test"]

Просмотреть файл

@ -1121,21 +1121,18 @@ struct JSPrinter {
// also need to recurse for if () { if () { } else { if () } else
// (note that this is only a problem if the if body has a single element in it, not a block or such, as then
// the block would be braced)
// this analysis is a little conservative - it assumes any child if could be confused with us, which implies
// all other braces vanished (the worst case for us, we are not saved by other braces).
bool needBraces = false;
bool hasElse = ifHasElse(node);
if (hasElse) {
Ref child = node[2];
while (child[0] == IF) {
Ref last = ifHasElse(child) ? child[3] : child[2];
if (last[0] == IF) {
child = last;
continue;
}
// we are at the top, the one dangerous to be confused with us
if (!ifHasElse(child)) {
needBraces = true;
break;
}
break;
child = child[3]; // continue into the else
}
}
if (needBraces) {