From 4983a6a5dcd305527657d9b67455714ac1759c72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 18 Oct 2018 20:08:32 +0200 Subject: [PATCH] astyle formatting --- lib/checkother.cpp | 10 +-- lib/checkstl.cpp | 50 ++++--------- lib/checkstl.h | 2 +- test/teststl.cpp | 170 ++++++++++++++++++++++----------------------- 4 files changed, 106 insertions(+), 126 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 21c54f201..664c5dc7b 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1771,10 +1771,10 @@ void CheckOther::misusedScopeObjectError(const Token *tok, const std::string& va static const Token * getSingleExpressionInBlock(const Token * tok) { - if(!tok) + if (!tok) return nullptr; const Token * top = tok->astTop(); - if(!top) + if (!top) return nullptr; const Token * nextExpression = nextAfterAstRightmostLeaf(top); if (!Token::simpleMatch(nextExpression, "; }")) @@ -1840,9 +1840,9 @@ void CheckOther::checkDuplicateBranch() const Token * branchTop2 = getSingleExpressionInBlock(scope.bodyEnd->tokAt(3)); if (!branchTop1 || !branchTop2) continue; - if(branchTop1->str() != branchTop2->str()) + if (branchTop1->str() != branchTop2->str()) continue; - if(isSameExpression(mTokenizer->isCPP(), false, branchTop1->astOperand1(), branchTop2->astOperand1(), mSettings->library, true, true, &errorPath) && + if (isSameExpression(mTokenizer->isCPP(), false, branchTop1->astOperand1(), branchTop2->astOperand1(), mSettings->library, true, true, &errorPath) && isSameExpression(mTokenizer->isCPP(), false, branchTop1->astOperand2(), branchTop2->astOperand2(), mSettings->library, true, true, &errorPath)) duplicateBranchError(scope.classDef, scope.bodyEnd->next(), errorPath); } @@ -2027,7 +2027,7 @@ void CheckOther::checkDuplicateExpression() } if (!assigned && !isUniqueExpression(tok->astOperand2())) duplicateAssignExpressionError(var1, var2, false); - else if(mSettings->inconclusive) + else if (mSettings->inconclusive) duplicateAssignExpressionError(var1, var2, true); } } diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 6269e5898..0db94bc7e 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -264,8 +264,7 @@ static std::string getContainerName(const Token *containerToken) return ret; } -enum OperandPosition -{ +enum OperandPosition { Left, Right }; @@ -273,18 +272,13 @@ enum OperandPosition static const Token* findIteratorContainer(const Token* start, const Token* end, unsigned int id) { const Token* containerToken = nullptr; - for(const Token* tok = start; tok != end; tok = tok->next()) - { - if (Token::Match(tok, "%varid% = %name% . %name% (", id)) - { + for (const Token* tok = start; tok != end; tok = tok->next()) { + if (Token::Match(tok, "%varid% = %name% . %name% (", id)) { // Iterator is assigned to value - if (tok->tokAt(5)->valueType() && tok->tokAt(5)->valueType()->type == ValueType::Type::ITERATOR) - { + if (tok->tokAt(5)->valueType() && tok->tokAt(5)->valueType()->type == ValueType::Type::ITERATOR) { containerToken = tok->tokAt(2); } - } - else if (Token::Match(tok, "%varid% = %name% (", id)) - { + } else if (Token::Match(tok, "%varid% = %name% (", id)) { // Prevent FP: iterator is assigned to something // TODO: Fix it in future containerToken = nullptr; @@ -478,13 +472,10 @@ bool CheckStl::compareIteratorAgainstDifferentContainer(const Token* operatorTok const Token *otherOperand = nullptr; OperandPosition operandPosition; - if (operatorTok->astOperand1()->varId() == iteratorId) - { + if (operatorTok->astOperand1()->varId() == iteratorId) { otherOperand = operatorTok->astOperand2(); operandPosition = OperandPosition::Right; - } - else if (operatorTok->astOperand2()->varId() == iteratorId) - { + } else if (operatorTok->astOperand2()->varId() == iteratorId) { otherOperand = operatorTok->astOperand1(); operandPosition = OperandPosition::Left; } @@ -493,43 +484,32 @@ bool CheckStl::compareIteratorAgainstDifferentContainer(const Token* operatorTok return false; const Token * const otherExprPart = otherOperand->tokAt(-3); - if (Token::Match(otherExprPart, "%name% . end|rend|cend|crend ( )") && otherExprPart->varId() != containerTok->varId()) - { + if (Token::Match(otherExprPart, "%name% . end|rend|cend|crend ( )") && otherExprPart->varId() != containerTok->varId()) { const std::string& firstContainerName = getContainerName(containerTok); const std::string& secondContainerName = getContainerName(otherExprPart); - if (firstContainerName != secondContainerName) - { + if (firstContainerName != secondContainerName) { if (operandPosition == OperandPosition::Right) iteratorsError(operatorTok, containerTok, firstContainerName, secondContainerName); else iteratorsError(operatorTok, containerTok, secondContainerName, firstContainerName); - } - else - { + } else { iteratorsError(operatorTok, containerTok, firstContainerName); } return true; - } - else - { + } else { const unsigned int otherId = otherOperand->varId(); auto it = iteratorScopeBeginInfo.find(otherId); - if (it != iteratorScopeBeginInfo.end()) - { + if (it != iteratorScopeBeginInfo.end()) { const Token* otherContainerToken = findIteratorContainer(it->second, operatorTok->astOperand1(), otherId); - if (otherContainerToken && otherContainerToken->varId() != containerTok->varId()) - { + if (otherContainerToken && otherContainerToken->varId() != containerTok->varId()) { const std::string& firstContainerName = getContainerName(containerTok); const std::string& secondContainerName = getContainerName(otherContainerToken); - if (firstContainerName != secondContainerName) - { + if (firstContainerName != secondContainerName) { if (operandPosition == OperandPosition::Right) iteratorsCmpError(operatorTok, containerTok, otherContainerToken, firstContainerName, secondContainerName); else iteratorsCmpError(operatorTok, containerTok, otherContainerToken, secondContainerName, firstContainerName); - } - else - { + } else { iteratorsCmpError(operatorTok, containerTok, otherContainerToken, firstContainerName); } return true; diff --git a/lib/checkstl.h b/lib/checkstl.h index 8ea598ef5..7d80a53d5 100644 --- a/lib/checkstl.h +++ b/lib/checkstl.h @@ -198,7 +198,7 @@ private: void iteratorsError(const Token* tok, const std::string& containerName1, const std::string& containerName2); void iteratorsError(const Token* tok, const Token* containerTok, const std::string& containerName1, const std::string& containerName2); void iteratorsError(const Token* tok, const Token* containerTok, const std::string& containerName); - void iteratorsCmpError(const Token* cmpOperatorTok, const Token* containerTok1, const Token* containerTok2, const std::string& containerName1, const std::string& containerName2); + void iteratorsCmpError(const Token* cmpOperatorTok, const Token* containerTok1, const Token* containerTok2, const std::string& containerName1, const std::string& containerName2); void iteratorsCmpError(const Token* cmpOperatorTok, const Token* containerTok1, const Token* containerTok2, const std::string& containerName); void mismatchingContainersError(const Token* tok); void mismatchingContainerExpressionError(const Token *tok1, const Token *tok2); diff --git a/test/teststl.cpp b/test/teststl.cpp index 3fa762891..fe5ef4291 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -748,99 +748,99 @@ private: } void iterator18() { - check("void foo()\n" - "{\n" - " std::list l1;\n" - " std::list l2;\n" - " std::list::iterator it1 = l1.begin();\n" - " std::list::iterator it2 = l1.end();\n" - " while (++it1 != --it2)\n" - " {\n" - " }\n" - "}"); - ASSERT_EQUALS("", errout.str()); + check("void foo()\n" + "{\n" + " std::list l1;\n" + " std::list l2;\n" + " std::list::iterator it1 = l1.begin();\n" + " std::list::iterator it2 = l1.end();\n" + " while (++it1 != --it2)\n" + " {\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); - check("void foo()\n" - "{\n" - " std::list l1;\n" - " std::list l2;\n" - " std::list::iterator it1 = l1.begin();\n" - " std::list::iterator it2 = l1.end();\n" - " while (it1++ != --it2)\n" - " {\n" - " }\n" - "}"); - ASSERT_EQUALS("", errout.str()); + check("void foo()\n" + "{\n" + " std::list l1;\n" + " std::list l2;\n" + " std::list::iterator it1 = l1.begin();\n" + " std::list::iterator it2 = l1.end();\n" + " while (it1++ != --it2)\n" + " {\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); - check("void foo()\n" - "{\n" - " std::list l1;\n" - " std::list l2;\n" - " std::list::iterator it1 = l1.begin();\n" - " std::list::iterator it2 = l1.end();\n" - " if (--it2 > it1++)\n" - " {\n" - " }\n" - "}"); - TODO_ASSERT_EQUALS("", "[test.cpp:7]: (error) Dangerous comparison using operator< on iterator.\n", errout.str()); + check("void foo()\n" + "{\n" + " std::list l1;\n" + " std::list l2;\n" + " std::list::iterator it1 = l1.begin();\n" + " std::list::iterator it2 = l1.end();\n" + " if (--it2 > it1++)\n" + " {\n" + " }\n" + "}"); + TODO_ASSERT_EQUALS("", "[test.cpp:7]: (error) Dangerous comparison using operator< on iterator.\n", errout.str()); } void iterator19() { - check("void foo()\n" - "{\n" - " std::list l1;\n" - " std::list::iterator it1 = l1.begin();\n" - " {\n" - " std::list l1;\n" - " if (it1 != l1.end())\n" - " {\n" - " }\n" - " }\n" - "}"); - ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:4]: (error) Same iterator is used with containers 'l1' that are defined in different scopes.\n", errout.str()); + check("void foo()\n" + "{\n" + " std::list l1;\n" + " std::list::iterator it1 = l1.begin();\n" + " {\n" + " std::list l1;\n" + " if (it1 != l1.end())\n" + " {\n" + " }\n" + " }\n" + "}"); + ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:4]: (error) Same iterator is used with containers 'l1' that are defined in different scopes.\n", errout.str()); - check("void foo()\n" - "{\n" - " std::list l1;\n" - " std::list::iterator it1 = l1.begin();\n" - " {\n" - " std::list l1;\n" - " if (l1.end() > it1)\n" - " {\n" - " }\n" - " }\n" - "}"); - TODO_ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:4]: (error) Same iterator is used with containers 'l1' that are defined in different scopes.\n", - "[test.cpp:7] -> [test.cpp:4]: (error) Same iterator is used with containers 'l1' that are defined in different scopes.\n[test.cpp:7]: (error) Dangerous comparison using operator< on iterator.\n", - errout.str()); + check("void foo()\n" + "{\n" + " std::list l1;\n" + " std::list::iterator it1 = l1.begin();\n" + " {\n" + " std::list l1;\n" + " if (l1.end() > it1)\n" + " {\n" + " }\n" + " }\n" + "}"); + TODO_ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:4]: (error) Same iterator is used with containers 'l1' that are defined in different scopes.\n", + "[test.cpp:7] -> [test.cpp:4]: (error) Same iterator is used with containers 'l1' that are defined in different scopes.\n[test.cpp:7]: (error) Dangerous comparison using operator< on iterator.\n", + errout.str()); - check("void foo()\n" - "{\n" - " std::list l1;\n" - " std::list::iterator it1 = l1.begin();\n" - " {\n" - " std::list l1;\n" - " std::list::iterator it2 = l1.begin();\n" - " if (it1 != it2)\n" - " {\n" - " }\n" - " }\n" - "}"); - ASSERT_EQUALS("[test.cpp:8] -> [test.cpp:4] -> [test.cpp:7]: (error) Comparison of iterators from containers 'l1' that are defined in different scopes.\n", errout.str()); + check("void foo()\n" + "{\n" + " std::list l1;\n" + " std::list::iterator it1 = l1.begin();\n" + " {\n" + " std::list l1;\n" + " std::list::iterator it2 = l1.begin();\n" + " if (it1 != it2)\n" + " {\n" + " }\n" + " }\n" + "}"); + ASSERT_EQUALS("[test.cpp:8] -> [test.cpp:4] -> [test.cpp:7]: (error) Comparison of iterators from containers 'l1' that are defined in different scopes.\n", errout.str()); - check("void foo()\n" - "{\n" - " std::list l1;\n" - " std::list::iterator it1 = l1.begin();\n" - " {\n" - " std::list l1;\n" - " std::list::iterator it2 = l1.begin();\n" - " if (it2 != it1)\n" - " {\n" - " }\n" - " }\n" - "}"); - ASSERT_EQUALS("[test.cpp:8] -> [test.cpp:4] -> [test.cpp:7]: (error) Comparison of iterators from containers 'l1' that are defined in different scopes.\n", errout.str()); + check("void foo()\n" + "{\n" + " std::list l1;\n" + " std::list::iterator it1 = l1.begin();\n" + " {\n" + " std::list l1;\n" + " std::list::iterator it2 = l1.begin();\n" + " if (it2 != it1)\n" + " {\n" + " }\n" + " }\n" + "}"); + ASSERT_EQUALS("[test.cpp:8] -> [test.cpp:4] -> [test.cpp:7]: (error) Comparison of iterators from containers 'l1' that are defined in different scopes.\n", errout.str()); }