astyle formatting

This commit is contained in:
Daniel Marjamäki 2018-10-18 20:08:32 +02:00
parent 58b21e3071
commit 4983a6a5dc
4 changed files with 106 additions and 126 deletions

View File

@ -1771,10 +1771,10 @@ void CheckOther::misusedScopeObjectError(const Token *tok, const std::string& va
static const Token * getSingleExpressionInBlock(const Token * tok) static const Token * getSingleExpressionInBlock(const Token * tok)
{ {
if(!tok) if (!tok)
return nullptr; return nullptr;
const Token * top = tok->astTop(); const Token * top = tok->astTop();
if(!top) if (!top)
return nullptr; return nullptr;
const Token * nextExpression = nextAfterAstRightmostLeaf(top); const Token * nextExpression = nextAfterAstRightmostLeaf(top);
if (!Token::simpleMatch(nextExpression, "; }")) if (!Token::simpleMatch(nextExpression, "; }"))
@ -1840,9 +1840,9 @@ void CheckOther::checkDuplicateBranch()
const Token * branchTop2 = getSingleExpressionInBlock(scope.bodyEnd->tokAt(3)); const Token * branchTop2 = getSingleExpressionInBlock(scope.bodyEnd->tokAt(3));
if (!branchTop1 || !branchTop2) if (!branchTop1 || !branchTop2)
continue; continue;
if(branchTop1->str() != branchTop2->str()) if (branchTop1->str() != branchTop2->str())
continue; 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)) isSameExpression(mTokenizer->isCPP(), false, branchTop1->astOperand2(), branchTop2->astOperand2(), mSettings->library, true, true, &errorPath))
duplicateBranchError(scope.classDef, scope.bodyEnd->next(), errorPath); duplicateBranchError(scope.classDef, scope.bodyEnd->next(), errorPath);
} }
@ -2027,7 +2027,7 @@ void CheckOther::checkDuplicateExpression()
} }
if (!assigned && !isUniqueExpression(tok->astOperand2())) if (!assigned && !isUniqueExpression(tok->astOperand2()))
duplicateAssignExpressionError(var1, var2, false); duplicateAssignExpressionError(var1, var2, false);
else if(mSettings->inconclusive) else if (mSettings->inconclusive)
duplicateAssignExpressionError(var1, var2, true); duplicateAssignExpressionError(var1, var2, true);
} }
} }

View File

@ -264,8 +264,7 @@ static std::string getContainerName(const Token *containerToken)
return ret; return ret;
} }
enum OperandPosition enum OperandPosition {
{
Left, Left,
Right Right
}; };
@ -273,18 +272,13 @@ enum OperandPosition
static const Token* findIteratorContainer(const Token* start, const Token* end, unsigned int id) static const Token* findIteratorContainer(const Token* start, const Token* end, unsigned int id)
{ {
const Token* containerToken = nullptr; const Token* containerToken = nullptr;
for(const Token* tok = start; tok != end; tok = tok->next()) for (const Token* tok = start; tok != end; tok = tok->next()) {
{ if (Token::Match(tok, "%varid% = %name% . %name% (", id)) {
if (Token::Match(tok, "%varid% = %name% . %name% (", id))
{
// Iterator is assigned to value // 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); 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 // Prevent FP: iterator is assigned to something
// TODO: Fix it in future // TODO: Fix it in future
containerToken = nullptr; containerToken = nullptr;
@ -478,13 +472,10 @@ bool CheckStl::compareIteratorAgainstDifferentContainer(const Token* operatorTok
const Token *otherOperand = nullptr; const Token *otherOperand = nullptr;
OperandPosition operandPosition; OperandPosition operandPosition;
if (operatorTok->astOperand1()->varId() == iteratorId) if (operatorTok->astOperand1()->varId() == iteratorId) {
{
otherOperand = operatorTok->astOperand2(); otherOperand = operatorTok->astOperand2();
operandPosition = OperandPosition::Right; operandPosition = OperandPosition::Right;
} } else if (operatorTok->astOperand2()->varId() == iteratorId) {
else if (operatorTok->astOperand2()->varId() == iteratorId)
{
otherOperand = operatorTok->astOperand1(); otherOperand = operatorTok->astOperand1();
operandPosition = OperandPosition::Left; operandPosition = OperandPosition::Left;
} }
@ -493,43 +484,32 @@ bool CheckStl::compareIteratorAgainstDifferentContainer(const Token* operatorTok
return false; return false;
const Token * const otherExprPart = otherOperand->tokAt(-3); 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& firstContainerName = getContainerName(containerTok);
const std::string& secondContainerName = getContainerName(otherExprPart); const std::string& secondContainerName = getContainerName(otherExprPart);
if (firstContainerName != secondContainerName) if (firstContainerName != secondContainerName) {
{
if (operandPosition == OperandPosition::Right) if (operandPosition == OperandPosition::Right)
iteratorsError(operatorTok, containerTok, firstContainerName, secondContainerName); iteratorsError(operatorTok, containerTok, firstContainerName, secondContainerName);
else else
iteratorsError(operatorTok, containerTok, secondContainerName, firstContainerName); iteratorsError(operatorTok, containerTok, secondContainerName, firstContainerName);
} } else {
else
{
iteratorsError(operatorTok, containerTok, firstContainerName); iteratorsError(operatorTok, containerTok, firstContainerName);
} }
return true; return true;
} } else {
else
{
const unsigned int otherId = otherOperand->varId(); const unsigned int otherId = otherOperand->varId();
auto it = iteratorScopeBeginInfo.find(otherId); auto it = iteratorScopeBeginInfo.find(otherId);
if (it != iteratorScopeBeginInfo.end()) if (it != iteratorScopeBeginInfo.end()) {
{
const Token* otherContainerToken = findIteratorContainer(it->second, operatorTok->astOperand1(), otherId); 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& firstContainerName = getContainerName(containerTok);
const std::string& secondContainerName = getContainerName(otherContainerToken); const std::string& secondContainerName = getContainerName(otherContainerToken);
if (firstContainerName != secondContainerName) if (firstContainerName != secondContainerName) {
{
if (operandPosition == OperandPosition::Right) if (operandPosition == OperandPosition::Right)
iteratorsCmpError(operatorTok, containerTok, otherContainerToken, firstContainerName, secondContainerName); iteratorsCmpError(operatorTok, containerTok, otherContainerToken, firstContainerName, secondContainerName);
else else
iteratorsCmpError(operatorTok, containerTok, otherContainerToken, secondContainerName, firstContainerName); iteratorsCmpError(operatorTok, containerTok, otherContainerToken, secondContainerName, firstContainerName);
} } else {
else
{
iteratorsCmpError(operatorTok, containerTok, otherContainerToken, firstContainerName); iteratorsCmpError(operatorTok, containerTok, otherContainerToken, firstContainerName);
} }
return true; return true;

View File

@ -198,7 +198,7 @@ private:
void iteratorsError(const Token* tok, const std::string& containerName1, const std::string& containerName2); 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& containerName1, const std::string& containerName2);
void iteratorsError(const Token* tok, const Token* containerTok, const std::string& containerName); 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 iteratorsCmpError(const Token* cmpOperatorTok, const Token* containerTok1, const Token* containerTok2, const std::string& containerName);
void mismatchingContainersError(const Token* tok); void mismatchingContainersError(const Token* tok);
void mismatchingContainerExpressionError(const Token *tok1, const Token *tok2); void mismatchingContainerExpressionError(const Token *tok1, const Token *tok2);

View File

@ -748,99 +748,99 @@ private:
} }
void iterator18() { void iterator18() {
check("void foo()\n" check("void foo()\n"
"{\n" "{\n"
" std::list<int> l1;\n" " std::list<int> l1;\n"
" std::list<int> l2;\n" " std::list<int> l2;\n"
" std::list<int>::iterator it1 = l1.begin();\n" " std::list<int>::iterator it1 = l1.begin();\n"
" std::list<int>::iterator it2 = l1.end();\n" " std::list<int>::iterator it2 = l1.end();\n"
" while (++it1 != --it2)\n" " while (++it1 != --it2)\n"
" {\n" " {\n"
" }\n" " }\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("void foo()\n" check("void foo()\n"
"{\n" "{\n"
" std::list<int> l1;\n" " std::list<int> l1;\n"
" std::list<int> l2;\n" " std::list<int> l2;\n"
" std::list<int>::iterator it1 = l1.begin();\n" " std::list<int>::iterator it1 = l1.begin();\n"
" std::list<int>::iterator it2 = l1.end();\n" " std::list<int>::iterator it2 = l1.end();\n"
" while (it1++ != --it2)\n" " while (it1++ != --it2)\n"
" {\n" " {\n"
" }\n" " }\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("void foo()\n" check("void foo()\n"
"{\n" "{\n"
" std::list<int> l1;\n" " std::list<int> l1;\n"
" std::list<int> l2;\n" " std::list<int> l2;\n"
" std::list<int>::iterator it1 = l1.begin();\n" " std::list<int>::iterator it1 = l1.begin();\n"
" std::list<int>::iterator it2 = l1.end();\n" " std::list<int>::iterator it2 = l1.end();\n"
" if (--it2 > it1++)\n" " if (--it2 > it1++)\n"
" {\n" " {\n"
" }\n" " }\n"
"}"); "}");
TODO_ASSERT_EQUALS("", "[test.cpp:7]: (error) Dangerous comparison using operator< on iterator.\n", errout.str()); TODO_ASSERT_EQUALS("", "[test.cpp:7]: (error) Dangerous comparison using operator< on iterator.\n", errout.str());
} }
void iterator19() { void iterator19() {
check("void foo()\n" check("void foo()\n"
"{\n" "{\n"
" std::list<int> l1;\n" " std::list<int> l1;\n"
" std::list<int>::iterator it1 = l1.begin();\n" " std::list<int>::iterator it1 = l1.begin();\n"
" {\n" " {\n"
" std::list<int> l1;\n" " std::list<int> l1;\n"
" if (it1 != l1.end())\n" " if (it1 != l1.end())\n"
" {\n" " {\n"
" }\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()); 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" check("void foo()\n"
"{\n" "{\n"
" std::list<int> l1;\n" " std::list<int> l1;\n"
" std::list<int>::iterator it1 = l1.begin();\n" " std::list<int>::iterator it1 = l1.begin();\n"
" {\n" " {\n"
" std::list<int> l1;\n" " std::list<int> l1;\n"
" if (l1.end() > it1)\n" " if (l1.end() > it1)\n"
" {\n" " {\n"
" }\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", 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", "[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()); errout.str());
check("void foo()\n" check("void foo()\n"
"{\n" "{\n"
" std::list<int> l1;\n" " std::list<int> l1;\n"
" std::list<int>::iterator it1 = l1.begin();\n" " std::list<int>::iterator it1 = l1.begin();\n"
" {\n" " {\n"
" std::list<int> l1;\n" " std::list<int> l1;\n"
" std::list<int>::iterator it2 = l1.begin();\n" " std::list<int>::iterator it2 = l1.begin();\n"
" if (it1 != it2)\n" " if (it1 != it2)\n"
" {\n" " {\n"
" }\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()); 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" check("void foo()\n"
"{\n" "{\n"
" std::list<int> l1;\n" " std::list<int> l1;\n"
" std::list<int>::iterator it1 = l1.begin();\n" " std::list<int>::iterator it1 = l1.begin();\n"
" {\n" " {\n"
" std::list<int> l1;\n" " std::list<int> l1;\n"
" std::list<int>::iterator it2 = l1.begin();\n" " std::list<int>::iterator it2 = l1.begin();\n"
" if (it2 != it1)\n" " if (it2 != it1)\n"
" {\n" " {\n"
" }\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()); 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());
} }