diff options
author | Alon Zakai <azakai@google.com> | 2019-05-01 14:48:41 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-01 14:48:41 -0700 |
commit | 2bd3758a22131cfd6925b3fd995657b211095c90 (patch) | |
tree | 2a38a48ab68c00ed1b55e885f86014bbdda92ff2 /src/emscripten-optimizer | |
parent | 73709b4da08d285c2237c8c23a54ba53274c0c7f (diff) | |
download | binaryen-2bd3758a22131cfd6925b3fd995657b211095c90.tar.gz binaryen-2bd3758a22131cfd6925b3fd995657b211095c90.tar.bz2 binaryen-2bd3758a22131cfd6925b3fd995657b211095c90.zip |
clang-tidy braces changes (#2075)
Applies the changes in #2065, and temprarily disables the hook since it's too slow to run on a change this large. We should re-enable it in a later commit.
Diffstat (limited to 'src/emscripten-optimizer')
-rw-r--r-- | src/emscripten-optimizer/istring.h | 15 | ||||
-rw-r--r-- | src/emscripten-optimizer/optimizer-shared.cpp | 50 | ||||
-rw-r--r-- | src/emscripten-optimizer/optimizer.h | 3 | ||||
-rw-r--r-- | src/emscripten-optimizer/parser.h | 148 | ||||
-rw-r--r-- | src/emscripten-optimizer/simple_ast.cpp | 13 | ||||
-rw-r--r-- | src/emscripten-optimizer/simple_ast.h | 222 |
6 files changed, 285 insertions, 166 deletions
diff --git a/src/emscripten-optimizer/istring.h b/src/emscripten-optimizer/istring.h index 320a3e590..21f94fcb9 100644 --- a/src/emscripten-optimizer/istring.h +++ b/src/emscripten-optimizer/istring.h @@ -136,12 +136,15 @@ struct IString { const char* stripPrefix(const char* prefix) const { const char* ptr = str; while (true) { - if (*prefix == 0) + if (*prefix == 0) { return ptr; - if (*ptr == 0) + } + if (*ptr == 0) { return nullptr; - if (*ptr++ != *prefix++) + } + if (*ptr++ != *prefix++) { return nullptr; + } } } @@ -191,11 +194,13 @@ public: strncpy(curr, init, size); while (1) { char* end = strchr(curr, ' '); - if (end) + if (end) { *end = 0; + } insert(curr); - if (!end) + if (!end) { break; + } curr = end + 1; } } diff --git a/src/emscripten-optimizer/optimizer-shared.cpp b/src/emscripten-optimizer/optimizer-shared.cpp index 3ac3ca7ea..172b9f45f 100644 --- a/src/emscripten-optimizer/optimizer-shared.cpp +++ b/src/emscripten-optimizer/optimizer-shared.cpp @@ -61,27 +61,32 @@ AsmType detectType(Ref node, if (node->isString()) { if (asmData) { AsmType ret = asmData->getType(node->getCString()); - if (ret != ASM_NONE) + if (ret != ASM_NONE) { return ret; + } } if (!inVarDef) { - if (node == INF || node == NaN) + if (node == INF || node == NaN) { return ASM_DOUBLE; - if (node == TEMP_RET0) + } + if (node == TEMP_RET0) { return ASM_INT; + } return ASM_NONE; } // We are in a variable definition, where Math_fround(0) optimized into a // global constant becomes f0 = Math_fround(0) - if (ASM_FLOAT_ZERO.isNull()) + if (ASM_FLOAT_ZERO.isNull()) { ASM_FLOAT_ZERO = node->getIString(); - else + } else { assert(node == ASM_FLOAT_ZERO); + } return ASM_FLOAT; } if (node->isNumber()) { - if (!wasm::isInteger(node->getNumber())) + if (!wasm::isInteger(node->getNumber())) { return ASM_DOUBLE; + } return ASM_INT; } switch (node[0]->getCString()[0]) { @@ -105,20 +110,21 @@ AsmType detectType(Ref node, if (node[0] == CALL) { if (node[1]->isString()) { IString name = node[1]->getIString(); - if (name == MATH_FROUND || name == minifiedFround) + if (name == MATH_FROUND || name == minifiedFround) { return ASM_FLOAT; - else if (allowI64 && (name == INT64 || name == INT64_CONST)) + } else if (allowI64 && (name == INT64 || name == INT64_CONST)) { return ASM_INT64; - else if (name == SIMD_FLOAT32X4 || name == SIMD_FLOAT32X4_CHECK) + } else if (name == SIMD_FLOAT32X4 || name == SIMD_FLOAT32X4_CHECK) { return ASM_FLOAT32X4; - else if (name == SIMD_FLOAT64X2 || name == SIMD_FLOAT64X2_CHECK) + } else if (name == SIMD_FLOAT64X2 || name == SIMD_FLOAT64X2_CHECK) { return ASM_FLOAT64X2; - else if (name == SIMD_INT8X16 || name == SIMD_INT8X16_CHECK) + } else if (name == SIMD_INT8X16 || name == SIMD_INT8X16_CHECK) { return ASM_INT8X16; - else if (name == SIMD_INT16X8 || name == SIMD_INT16X8_CHECK) + } else if (name == SIMD_INT16X8 || name == SIMD_INT16X8_CHECK) { return ASM_INT16X8; - else if (name == SIMD_INT32X4 || name == SIMD_INT32X4_CHECK) + } else if (name == SIMD_INT32X4 || name == SIMD_INT32X4_CHECK) { return ASM_INT32X4; + } } return ASM_NONE; } else if (node[0] == CONDITIONAL) { @@ -155,8 +161,9 @@ AsmType detectType(Ref node, } else if (node[0] == SUB) { assert(node[1]->isString()); HeapInfo info = parseHeap(node[1][1]->getCString()); - if (info.valid) + if (info.valid) { return ASM_NONE; + } return info.floaty ? ASM_DOUBLE : ASM_INT; // XXX ASM_FLOAT? } break; @@ -179,12 +186,15 @@ AsmSign detectSign(Ref node, IString minifiedFround) { } if (node->isNumber()) { double value = node->getNumber(); - if (value < 0) + if (value < 0) { return ASM_SIGNED; - if (value > uint32_t(-1) || fmod(value, 1) != 0) + } + if (value > uint32_t(-1) || fmod(value, 1) != 0) { return ASM_NONSIGNED; - if (wasm::isSInteger32(value)) + } + if (wasm::isSInteger32(value)) { return ASM_FLEXIBLE; + } return ASM_UNSIGNED; } IString type = node[0]->getIString(); @@ -192,8 +202,9 @@ AsmSign detectSign(Ref node, IString minifiedFround) { IString op = node[1]->getIString(); switch (op.str[0]) { case '>': { - if (op == TRSHIFT) + if (op == TRSHIFT) { return ASM_UNSIGNED; + } } // fallthrough case '|': case '&': @@ -228,8 +239,9 @@ AsmSign detectSign(Ref node, IString minifiedFround) { return detectSign(node[2], minifiedFround); } else if (type == CALL) { if (node[1]->isString() && - (node[1] == MATH_FROUND || node[1] == minifiedFround)) + (node[1] == MATH_FROUND || node[1] == minifiedFround)) { return ASM_NONSIGNED; + } } else if (type == SEQ) { return detectSign(node[2], minifiedFround); } diff --git a/src/emscripten-optimizer/optimizer.h b/src/emscripten-optimizer/optimizer.h index 36a9322fd..e30347025 100644 --- a/src/emscripten-optimizer/optimizer.h +++ b/src/emscripten-optimizer/optimizer.h @@ -64,8 +64,9 @@ struct AsmData { AsmType getType(const cashew::IString& name) { auto ret = locals.find(name); - if (ret != locals.end()) + if (ret != locals.end()) { return ret->second.type; + } return ASM_NONE; } void setType(const cashew::IString& name, AsmType type) { diff --git a/src/emscripten-optimizer/parser.h b/src/emscripten-optimizer/parser.h index d180ea12c..1d4cda79b 100644 --- a/src/emscripten-optimizer/parser.h +++ b/src/emscripten-optimizer/parser.h @@ -160,16 +160,19 @@ template<class NodeRef, class Builder> class Parser { } if (curr[0] == '/' && curr[1] == '/') { curr += 2; - while (*curr && *curr != '\n') + while (*curr && *curr != '\n') { curr++; - if (*curr) + } + if (*curr) { curr++; + } continue; } if (curr[0] == '/' && curr[1] == '*') { curr += 2; - while (*curr && (curr[0] != '*' || curr[1] != '/')) + while (*curr && (curr[0] != '*' || curr[1] != '/')) { curr++; + } curr += 2; continue; } @@ -180,9 +183,11 @@ template<class NodeRef, class Builder> class Parser { static bool isDigit(char x) { return x >= '0' && x <= '9'; } static bool hasChar(const char* list, char x) { - while (*list) - if (*list++ == x) + while (*list) { + if (*list++ == x) { return true; + } + } return false; } @@ -247,8 +252,9 @@ template<class NodeRef, class Builder> class Parser { } else if (*src >= 'A' && *src <= 'F') { num *= 16; num += *src - 'A' + 10; - } else + } else { break; + } src++; } } else { @@ -402,12 +408,15 @@ template<class NodeRef, class Builder> class Parser { return parseExpression(parseFrag(frag), src, seps); } case SEPARATOR: { - if (frag.str == OPEN_PAREN) + if (frag.str == OPEN_PAREN) { return parseExpression(parseAfterParen(src), src, seps); - if (frag.str == OPEN_BRACE) + } + if (frag.str == OPEN_BRACE) { return parseExpression(parseAfterBrace(src), src, seps); - if (frag.str == OPEN_CURLY) + } + if (frag.str == OPEN_CURLY) { return parseExpression(parseAfterCurly(src), src, seps); + } abort(); } case OPERATOR: { @@ -439,30 +448,31 @@ template<class NodeRef, class Builder> class Parser { NodeRef parseAfterKeyword(Frag& frag, char*& src, const char* seps) { skipSpace(src); - if (frag.str == FUNCTION) + if (frag.str == FUNCTION) { return parseFunction(src, seps); - else if (frag.str == VAR) + } else if (frag.str == VAR) { return parseVar(src, seps, false); - else if (frag.str == CONST) + } else if (frag.str == CONST) { return parseVar(src, seps, true); - else if (frag.str == RETURN) + } else if (frag.str == RETURN) { return parseReturn(src, seps); - else if (frag.str == IF) + } else if (frag.str == IF) { return parseIf(src, seps); - else if (frag.str == DO) + } else if (frag.str == DO) { return parseDo(src, seps); - else if (frag.str == WHILE) + } else if (frag.str == WHILE) { return parseWhile(src, seps); - else if (frag.str == BREAK) + } else if (frag.str == BREAK) { return parseBreak(src, seps); - else if (frag.str == CONTINUE) + } else if (frag.str == CONTINUE) { return parseContinue(src, seps); - else if (frag.str == SWITCH) + } else if (frag.str == SWITCH) { return parseSwitch(src, seps); - else if (frag.str == NEW) + } else if (frag.str == NEW) { return parseNew(src, seps); - else if (frag.str == FOR) + } else if (frag.str == FOR) { return parseFor(src, seps); + } dump(frag.str.str, src); abort(); return nullptr; @@ -482,15 +492,17 @@ template<class NodeRef, class Builder> class Parser { src++; while (1) { skipSpace(src); - if (*src == ')') + if (*src == ')') { break; + } Frag arg(src); assert(arg.type == IDENT); src += arg.size; Builder::appendArgumentToFunction(ret, arg.str); skipSpace(src); - if (*src == ')') + if (*src == ')') { break; + } if (*src == ',') { src++; continue; @@ -507,8 +519,9 @@ template<class NodeRef, class Builder> class Parser { NodeRef ret = Builder::makeVar(is_const); while (1) { skipSpace(src); - if (*src == ';') + if (*src == ';') { break; + } Frag name(src); assert(name.type == IDENT); NodeRef value; @@ -521,8 +534,9 @@ template<class NodeRef, class Builder> class Parser { } Builder::appendToVar(ret, name.str, value); skipSpace(src); - if (*src == ';') + if (*src == ';') { break; + } if (*src == ',') { src++; continue; @@ -538,8 +552,9 @@ template<class NodeRef, class Builder> class Parser { NodeRef value = !hasChar(seps, *src) ? parseElement(src, seps) : nullptr; skipSpace(src); assert(hasChar(seps, *src)); - if (*src == ';') + if (*src == ';') { src++; + } return Builder::makeReturn(value); } @@ -597,16 +612,18 @@ template<class NodeRef, class Builder> class Parser { NodeRef parseBreak(char*& src, const char* seps) { skipSpace(src); Frag next(src); - if (next.type == IDENT) + if (next.type == IDENT) { src += next.size; + } return Builder::makeBreak(next.type == IDENT ? next.str : IString()); } NodeRef parseContinue(char*& src, const char* seps) { skipSpace(src); Frag next(src); - if (next.type == IDENT) + if (next.type == IDENT) { src += next.size; + } return Builder::makeContinue(next.type == IDENT ? next.str : IString()); } @@ -618,8 +635,9 @@ template<class NodeRef, class Builder> class Parser { while (1) { // find all cases and possibly a default skipSpace(src); - if (*src == '}') + if (*src == '}') { break; + } Frag next(src); if (next.type == KEYWORD) { if (next.str == CASE) { @@ -681,10 +699,12 @@ template<class NodeRef, class Builder> class Parser { NodeRef parseAfterIdent(Frag& frag, char*& src, const char* seps) { skipSpace(src); - if (*src == '(') + if (*src == '(') { return parseExpression(parseCall(parseFrag(frag), src), src, seps); - if (*src == '[') + } + if (*src == '[') { return parseExpression(parseIndexing(parseFrag(frag), src), src, seps); + } if (*src == ':' && expressionPartsStack.back().size() == 0) { src++; skipSpace(src); @@ -697,8 +717,9 @@ template<class NodeRef, class Builder> class Parser { } return Builder::makeLabel(frag.str, inner); } - if (*src == '.') + if (*src == '.') { return parseExpression(parseDotting(parseFrag(frag), src), src, seps); + } return parseExpression(parseFrag(frag), src, seps); } @@ -709,12 +730,14 @@ template<class NodeRef, class Builder> class Parser { NodeRef ret = Builder::makeCall(target); while (1) { skipSpace(src); - if (*src == ')') + if (*src == ')') { break; + } Builder::appendToCall(ret, parseElement(src, ",)")); skipSpace(src); - if (*src == ')') + if (*src == ')') { break; + } if (*src == ',') { src++; continue; @@ -767,13 +790,15 @@ template<class NodeRef, class Builder> class Parser { while (1) { skipSpace(src); assert(*src); - if (*src == ']') + if (*src == ']') { break; + } NodeRef element = parseElement(src, ",]"); Builder::appendToArray(ret, element); skipSpace(src); - if (*src == ']') + if (*src == ']') { break; + } if (*src == ',') { src++; continue; @@ -790,8 +815,9 @@ template<class NodeRef, class Builder> class Parser { while (1) { skipSpace(src); assert(*src); - if (*src == '}') + if (*src == '}') { break; + } Frag key(src); assert(key.type == IDENT || key.type == STRING); src += key.size; @@ -801,8 +827,9 @@ template<class NodeRef, class Builder> class Parser { NodeRef value = parseElement(src, ",}"); Builder::appendToObject(ret, key.str, value); skipSpace(src); - if (*src == '}') + if (*src == '}') { break; + } if (*src == ',') { src++; continue; @@ -868,8 +895,9 @@ template<class NodeRef, class Builder> class Parser { parts.push_back(initial); } NodeRef last = parseElement(src, seps); - if (!top) + if (!top) { return last; + } { // |parts| may have been invalidated by that call ExpressionParts& parts = expressionPartsStack.back(); @@ -880,11 +908,13 @@ template<class NodeRef, class Builder> class Parser { if (ops.rtl) { // right to left for (int i = parts.size() - 1; i >= 0; i--) { - if (parts[i].isNode) + if (parts[i].isNode) { continue; + } IString op = parts[i].getOp(); - if (!ops.ops.has(op)) + if (!ops.ops.has(op)) { continue; + } if (ops.type == OperatorClass::Binary && i > 0 && i < (int)parts.size() - 1) { parts[i] = @@ -893,20 +923,23 @@ template<class NodeRef, class Builder> class Parser { parts.erase(parts.begin() + i - 1); } else if (ops.type == OperatorClass::Prefix && i < (int)parts.size() - 1) { - if (i > 0 && parts[i - 1].isNode) + if (i > 0 && parts[i - 1].isNode) { // cannot apply prefix operator if it would join two nodes continue; + } parts[i] = Builder::makePrefix(op, parts[i + 1].getNode()); parts.erase(parts.begin() + i + 1); } else if (ops.type == OperatorClass::Tertiary) { // we must be at X ? Y : Z // ^ // dumpParts(parts, i); - if (op != COLON) + if (op != COLON) { continue; + } assert(i < (int)parts.size() - 1 && i >= 3); - if (parts[i - 2].getOp() != QUESTION) + if (parts[i - 2].getOp() != QUESTION) { continue; // e.g. x ? y ? 1 : 0 : 2 + } parts[i - 3] = Builder::makeConditional(parts[i - 3].getNode(), parts[i - 1].getNode(), parts[i + 1].getNode()); @@ -918,11 +951,13 @@ template<class NodeRef, class Builder> class Parser { } else { // left to right for (int i = 0; i < (int)parts.size(); i++) { - if (parts[i].isNode) + if (parts[i].isNode) { continue; + } IString op = parts[i].getOp(); - if (!ops.ops.has(op)) + if (!ops.ops.has(op)) { continue; + } if (ops.type == OperatorClass::Binary && i > 0 && i < (int)parts.size() - 1) { parts[i] = @@ -932,9 +967,10 @@ template<class NodeRef, class Builder> class Parser { i--; } else if (ops.type == OperatorClass::Prefix && i < (int)parts.size() - 1) { - if (i > 0 && parts[i - 1].isNode) + if (i > 0 && parts[i - 1].isNode) { // cannot apply prefix operator if it would join two nodes continue; + } parts[i] = Builder::makePrefix(op, parts[i + 1].getNode()); parts.erase(parts.begin() + i + 1); // allow a previous prefix operator to cascade @@ -960,23 +996,27 @@ template<class NodeRef, class Builder> class Parser { // dump("parseBlock", src); while (1) { skipSpace(src); - if (*src == 0) + if (*src == 0) { break; + } if (*src == ';') { src++; // skip a statement in this block continue; } - if (hasChar(seps, *src)) + if (hasChar(seps, *src)) { break; + } if (!!keywordSep1) { Frag next(src); - if (next.type == KEYWORD && next.str == keywordSep1) + if (next.type == KEYWORD && next.str == keywordSep1) { break; + } } if (!!keywordSep2) { Frag next(src); - if (next.type == KEYWORD && next.str == keywordSep2) + if (next.type == KEYWORD && next.str == keywordSep2) { break; + } } NodeRef element = parseElementOrStatement(src, seps); Builder::appendToBlock(block, element); @@ -1061,12 +1101,14 @@ template<class NodeRef, class Builder> class Parser { while (*curr) { if (*curr == '\n') { newlinesLeft--; - if (newlinesLeft == 0) + if (newlinesLeft == 0) { break; + } } charsLeft--; - if (charsLeft == 0) + if (charsLeft == 0) { break; + } fprintf(stderr, "%c", *curr++); } fprintf(stderr, "\n\n"); diff --git a/src/emscripten-optimizer/simple_ast.cpp b/src/emscripten-optimizer/simple_ast.cpp index 5853b3289..6afa0a66e 100644 --- a/src/emscripten-optimizer/simple_ast.cpp +++ b/src/emscripten-optimizer/simple_ast.cpp @@ -105,10 +105,11 @@ void Value::stringify(std::ostream& os, bool pretty) { } for (size_t i = 0; i < arr->size(); i++) { if (i > 0) { - if (pretty) + if (pretty) { os << "," << std::endl; - else + } else { os << ", "; + } } indentify(); (*arr)[i]->stringify(os, pretty); @@ -141,8 +142,9 @@ void Value::stringify(std::ostream& os, bool pretty) { first = false; } else { os << ", "; - if (pretty) + if (pretty) { os << std::endl; + } } indentify(); os << '"' << i.first.c_str() << "\": "; @@ -178,10 +180,11 @@ void Value::stringify(std::ostream& os, bool pretty) { void dump(const char* str, Ref node, bool pretty) { std::cerr << str << ": "; - if (!!node) + if (!!node) { node->stringify(std::cerr, pretty); - else + } else { std::cerr << "(nullptr)"; + } std::cerr << std::endl; } diff --git a/src/emscripten-optimizer/simple_ast.h b/src/emscripten-optimizer/simple_ast.h index e4f2c1f86..bcfce9712 100644 --- a/src/emscripten-optimizer/simple_ast.h +++ b/src/emscripten-optimizer/simple_ast.h @@ -153,8 +153,9 @@ struct Value { void free() { if (type == Array) { arr->clear(); - } else if (type == Object) + } else if (type == Object) { delete obj; + } type = Null; num = 0; } @@ -286,8 +287,9 @@ struct Value { } bool operator==(const Value& other) { - if (type != other.type) + if (type != other.type) { return false; + } switch (other.type) { case String: return str == other.str; @@ -334,8 +336,9 @@ struct Value { arr->push_back(temp); curr = temp->parse(curr); skip(); - if (*curr == ']') + if (*curr == ']') { break; + } assert(*curr == ','); curr++; skip(); @@ -377,8 +380,9 @@ struct Value { curr = value->parse(curr); (*obj)[key] = value; skip(); - if (*curr == '}') + if (*curr == '}') { break; + } assert(*curr == ','); curr++; skip(); @@ -411,8 +415,9 @@ struct Value { void setSize(size_t size) { assert(isArray()); auto old = arr->size(); - if (old != size) + if (old != size) { arr->resize(size); + } if (old < size) { for (auto i = old; i < size; i++) { (*arr)[i] = arena.alloc<Value>(); @@ -439,8 +444,9 @@ struct Value { Ref back() { assert(isArray()); - if (arr->size() == 0) + if (arr->size() == 0) { return nullptr; + } return arr->back(); } @@ -452,8 +458,9 @@ struct Value { int indexOf(Ref other) { assert(isArray()); for (size_t i = 0; i < arr->size(); i++) { - if (other == (*arr)[i]) + if (other == (*arr)[i]) { return i; + } } return -1; } @@ -474,8 +481,9 @@ struct Value { ret->setArray(); for (size_t i = 0; i < arr->size(); i++) { Ref curr = (*arr)[i]; - if (func(curr)) + if (func(curr)) { ret->push_back(curr); + } } return ret; } @@ -586,8 +594,9 @@ struct JSPrinter { void emit(char c) { maybeSpace(c); - if (!pretty && c == '}' && buffer[used - 1] == ';') + if (!pretty && c == '}' && buffer[used - 1] == ';') { used--; // optimize ;} into }, the ; is not separating anything + } ensure(1); buffer[used++] = c; } @@ -601,30 +610,35 @@ struct JSPrinter { } void newline() { - if (!pretty) + if (!pretty) { return; + } emit('\n'); - for (int i = 0; i < indent; i++) + for (int i = 0; i < indent; i++) { emit(' '); + } } void space() { - if (pretty) + if (pretty) { emit(' '); + } } void safeSpace() { - if (pretty) + if (pretty) { emit(' '); - else + } else { possibleSpace = true; + } } void maybeSpace(char s) { if (possibleSpace) { possibleSpace = false; - if (isIdentPart(s)) + if (isIdentPart(s)) { emit(' '); + } } } @@ -635,15 +649,18 @@ struct JSPrinter { bool isDefun(Ref node) { return node->isArray() && node[0] == DEFUN; } bool endsInBlock(Ref node) { - if (node->isArray() && node[0] == BLOCK) + if (node->isArray() && node[0] == BLOCK) { return true; + } // Check for a label on a block - if (node->isArray() && node[0] == LABEL && endsInBlock(node[2])) + if (node->isArray() && node[0] == LABEL && endsInBlock(node[2])) { return true; + } // Check for an if if (node->isArray() && node[0] == IF && - endsInBlock(ifHasElse(node) ? node[3] : node[2])) + endsInBlock(ifHasElse(node) ? node[3] : node[2])) { return true; + } return false; } @@ -670,119 +687,133 @@ struct JSPrinter { IString type = node[0]->getIString(); switch (type.str[0]) { case 'a': { - if (type == ARRAY) + if (type == ARRAY) { printArray(node); - else + } else { abort(); + } break; } case 'b': { - if (type == BINARY) + if (type == BINARY) { printBinary(node); - else if (type == BLOCK) + } else if (type == BLOCK) { printBlock(node); - else if (type == BREAK) + } else if (type == BREAK) { printBreak(node); - else + } else { abort(); + } break; } case 'c': { - if (type == CALL) + if (type == CALL) { printCall(node); - else if (type == CONDITIONAL) + } else if (type == CONDITIONAL) { printConditional(node); - else if (type == CONTINUE) + } else if (type == CONTINUE) { printContinue(node); - else + } else { abort(); + } break; } case 'd': { - if (type == DEFUN) + if (type == DEFUN) { printDefun(node); - else if (type == DO) + } else if (type == DO) { printDo(node); - else if (type == DOT) + } else if (type == DOT) { printDot(node); - else + } else { abort(); + } break; } case 'i': { - if (type == IF) + if (type == IF) { printIf(node); - else + } else { abort(); + } break; } case 'l': { - if (type == LABEL) + if (type == LABEL) { printLabel(node); - else + } else { abort(); + } break; } case 'n': { - if (type == NEW) + if (type == NEW) { printNew(node); - else + } else { abort(); + } break; } case 'o': { - if (type == OBJECT) + if (type == OBJECT) { printObject(node); + } break; } case 'r': { - if (type == RETURN) + if (type == RETURN) { printReturn(node); - else + } else { abort(); + } break; } case 's': { - if (type == SUB) + if (type == SUB) { printSub(node); - else if (type == SEQ) + } else if (type == SEQ) { printSeq(node); - else if (type == SWITCH) + } else if (type == SWITCH) { printSwitch(node); - else if (type == STRING) + } else if (type == STRING) { printString(node); - else + } else { abort(); + } break; } case 't': { - if (type == TOPLEVEL) + if (type == TOPLEVEL) { printToplevel(node); - else if (type == TRY) + } else if (type == TRY) { printTry(node); - else + } else { abort(); + } break; } case 'u': { - if (type == UNARY_PREFIX) + if (type == UNARY_PREFIX) { printUnaryPrefix(node); - else + } else { abort(); + } break; } case 'v': { - if (type == VAR) + if (type == VAR) { printVar(node); - else + } else { abort(); + } break; } case 'w': { - if (type == WHILE) + if (type == WHILE) { printWhile(node); - else + } else { abort(); + } break; } default: { @@ -796,8 +827,9 @@ struct JSPrinter { void print(Ref node, const char* otherwise) { auto last = used; print(node); - if (used == last) + if (used == last) { emit(otherwise); + } } void printStats(Ref stats) { @@ -805,10 +837,11 @@ struct JSPrinter { for (size_t i = 0; i < stats->size(); i++) { Ref curr = stats[i]; if (!isNothing(curr)) { - if (first) + if (first) { first = false; - else + } else { newline(); + } print(curr); if (!isDefun(curr) && !endsInBlock(curr) && !isIf(curr)) { emit(';'); @@ -843,8 +876,9 @@ struct JSPrinter { emit('('); Ref args = node[2]; for (size_t i = 0; i < args->size(); i++) { - if (i > 0) + if (i > 0) { (pretty ? emit(", ") : emit(',')); + } emit(args[i]->getCString()); } emit(')'); @@ -906,8 +940,9 @@ struct JSPrinter { } } bool neg = d < 0; - if (neg) + if (neg) { d = -d; + } // try to emit the fewest necessary characters bool integer = fmod(d, 1) == 0; #define BUFFERSIZE 1000 @@ -940,8 +975,9 @@ struct JSPrinter { sscanf(buffer, "%lf", &temp); // errv("%.18f, %.18e => %s => %.18f, %.18e (%d), ", d, d, // buffer, temp, temp, temp == d); - if (temp == d) + if (temp == d) { break; + } } } else { // integer @@ -973,8 +1009,9 @@ struct JSPrinter { if (dot) { // remove trailing zeros char* end = dot + 1; - while (*end >= '0' && *end <= '9') + while (*end >= '0' && *end <= '9') { end++; + } end--; while (*end == '0') { char* copy = end; @@ -999,8 +1036,9 @@ struct JSPrinter { char* test = end; // remove zeros, and also doubles can use at most 24 digits, we can // truncate any extras even if not zero - while ((*test == '0' || test - buffer > 24) && test > buffer) + while ((*test == '0' || test - buffer > 24) && test > buffer) { test--; + } int num = end - test; if (num >= 3) { test++; @@ -1092,10 +1130,12 @@ struct JSPrinter { int parentPrecedence = getPrecedence(parent, true); int childPrecedence = getPrecedence(child, false); - if (childPrecedence > parentPrecedence) + if (childPrecedence > parentPrecedence) { return true; // child is definitely a danger - if (childPrecedence < parentPrecedence) + } + if (childPrecedence < parentPrecedence) { return false; // definitely cool + } // equal precedence, so associativity (rtl/ltr) is what matters // (except for some exceptions, where multiple operators can combine into // confusion) @@ -1106,24 +1146,29 @@ struct JSPrinter { return true; } } - if (childPosition == 0) + if (childPosition == 0) { return true; // child could be anywhere, so always paren - if (childPrecedence < 0) + } + if (childPrecedence < 0) { return false; // both precedences are safe + } // check if child is on the dangerous side - if (OperatorClass::getRtl(parentPrecedence)) + if (OperatorClass::getRtl(parentPrecedence)) { return childPosition < 0; - else + } else { return childPosition > 0; + } } void printChild(Ref child, Ref parent, int childPosition = 0) { bool parens = needParens(parent, child, childPosition); - if (parens) + if (parens) { emit('('); + } print(child); - if (parens) + if (parens) { emit(')'); + } } void printBinary(Ref node) { @@ -1145,12 +1190,15 @@ struct JSPrinter { ensure(1); // we temporarily append a 0 char* curr = buffer + last; // ensure might invalidate buffer[used] = 0; - if (strstr(curr, "infinity")) + if (strstr(curr, "infinity")) { return; - if (strstr(curr, "nan")) + } + if (strstr(curr, "nan")) { return; - if (strchr(curr, '.')) + } + if (strchr(curr, '.')) { return; // already a decimal point, all good + } char* e = strchr(curr, 'e'); if (!e) { emit(".0"); @@ -1193,8 +1241,9 @@ struct JSPrinter { emit('('); Ref args = node[2]; for (size_t i = 0; i < args->size(); i++) { - if (i > 0) + if (i > 0) { (pretty ? emit(", ") : emit(',')); + } printChild(args[i], node, 0); } emit(')'); @@ -1238,10 +1287,11 @@ struct JSPrinter { auto curr = used; printStats(c[1]); indent--; - if (curr != used) + if (curr != used) { newline(); - else + } else { used--; // avoid the extra indentation we added tentatively + } } else { newline(); } @@ -1269,8 +1319,9 @@ struct JSPrinter { emit("var "); Ref args = node[1]; for (size_t i = 0; i < args->size(); i++) { - if (i > 0) + if (i > 0) { (pretty ? emit(", ") : emit(',')); + } emit(args[i][0]->getCString()); if (args[i]->size() > 1) { space(); @@ -1377,8 +1428,9 @@ struct JSPrinter { emit('['); Ref args = node[1]; for (size_t i = 0; i < args->size(); i++) { - if (i > 0) + if (i > 0) { (pretty ? emit(", ") : emit(',')); + } print(args[i]); } emit(']'); @@ -1413,11 +1465,13 @@ struct JSPrinter { } check++; } - if (needQuote) + if (needQuote) { emit('"'); + } emit(str); - if (needQuote) + if (needQuote) { emit('"'); + } emit(":"); space(); print(args[i][1]); @@ -1467,8 +1521,9 @@ public: target[1]->setArray(block[1]->getArray()); } else if (target[0] == DEFUN) { target[3]->setArray(block[1]->getArray()); - } else + } else { abort(); + } } static void appendToBlock(Ref block, Ref element) { @@ -1583,8 +1638,9 @@ public: static void appendToVar(Ref var, IString name, Ref value) { assert(var[0] == VAR); Ref array = &makeRawArray(1)->push_back(makeRawString(name)); - if (!!value) + if (!!value) { array->push_back(value); + } var[1]->push_back(array); } |