astyle formatting

This commit is contained in:
Daniel Marjamäki 2010-10-14 19:59:10 +02:00
parent 96e99d347e
commit e11d9f1628
2 changed files with 267 additions and 257 deletions

View File

@ -17,7 +17,7 @@
*/
//---------------------------------------------------------------------------
// You should use ++ and -- as prefix whenever possible as these are more
// You should use ++ and -- as prefix whenever possible as these are more
// efficient than postfix operators
//---------------------------------------------------------------------------
@ -40,37 +40,47 @@ void CheckPostfixOperator::postfixOperator()
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
bool result = false;
if ( Token::Match(tok, "++|--") ) {
if ( Token::Match(tok->previous()->previous(), ";|{|}") && Token::Match(tok->next(), ";|)|,") ) {
result = true;
}
else if (tok->strAt(-2) == ",") {
int i(1);
while(tok->strAt(i) != ")") {
if(tok->strAt(i) == ";") {
result = true;
break;
}
++i;
}
if (Token::Match(tok, "++|--"))
{
if (Token::Match(tok->previous()->previous(), ";|{|}") && Token::Match(tok->next(), ";|)|,"))
{
result = true;
}
else if (tok->strAt(-2) == ",")
{
int i(1);
while (tok->strAt(i) != ")")
{
if (tok->strAt(i) == ";")
{
result = true;
break;
}
++i;
}
}
else if (tok->strAt(-2) == "<<" && tok->strAt(1) == "<<")
{
result = true;
}
else if (tok->strAt(-2) == "<<" && tok->strAt(1) == "<<") {
result = true;
}
}
if(result) {
const Token *decltok = Token::findmatch(_tokenizer->tokens(), "%varid%", tok->previous()->varId());
if (decltok && Token::Match(decltok->previous(), "iterator|const_iterator|reverse_iterator|const_reverse_iterator")) {
// the variable is an iterator
postfixOperatorError(tok);
}
else {
const std::string classDef = std::string("class ") + std::string(decltok->previous()->strAt(0));
if (Token::findmatch(_tokenizer->tokens(), classDef.c_str())) {
// the variable is an instance of class
postfixOperatorError(tok);
}
if (result)
{
const Token *decltok = Token::findmatch(_tokenizer->tokens(), "%varid%", tok->previous()->varId());
if (decltok && Token::Match(decltok->previous(), "iterator|const_iterator|reverse_iterator|const_reverse_iterator"))
{
// the variable is an iterator
postfixOperatorError(tok);
}
else
{
const std::string classDef = std::string("class ") + std::string(decltok->previous()->strAt(0));
if (Token::findmatch(_tokenizer->tokens(), classDef.c_str()))
{
// the variable is an instance of class
postfixOperatorError(tok);
}
}
}
}

View File

@ -72,105 +72,105 @@ private:
void testsimple()
{
check("\n"
"#include <iostream>\n"
"int main(int argc, char *argv[])\n"
"{\n"
" unsigned int k(0);\n"
" std::cout << k << std::endl;\n"
" k++;\n"
" std::cout << k << std::endl;\n"
" if(k) {\n"
" k++;\n"
" }\n"
" std::cout << k << std::endl;\n"
" k--;\n"
" std::cout << k << std::endl;\n"
" return 0;\n"
"}\n");
"#include <iostream>\n"
"int main(int argc, char *argv[])\n"
"{\n"
" unsigned int k(0);\n"
" std::cout << k << std::endl;\n"
" k++;\n"
" std::cout << k << std::endl;\n"
" if(k) {\n"
" k++;\n"
" }\n"
" std::cout << k << std::endl;\n"
" k--;\n"
" std::cout << k << std::endl;\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("\n"
"#include <iostream>\n"
"class K {};"
"int main(int argc, char *argv[])\n"
"{\n"
" K k(0);\n"
" std::cout << k << std::endl;\n"
" k++;\n"
" std::cout << k << std::endl;\n"
" return 0;\n"
"}\n");
"#include <iostream>\n"
"class K {};"
"int main(int argc, char *argv[])\n"
"{\n"
" K k(0);\n"
" std::cout << k << std::endl;\n"
" k++;\n"
" std::cout << k << std::endl;\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7]: (style) You should use ++ and -- as prefix whenever possible as these are more efficient than postfix operators\n", errout.str());
check("\n"
"#include <iostream>\n"
"class K {};"
"int main(int argc, char *argv[])\n"
"{\n"
" K k(1);\n"
" std::cout << k << std::endl;\n"
" if(k) {\n"
" k++;\n"
" }\n"
" std::cout << k << std::endl;\n"
" return 0;\n"
"}\n");
"#include <iostream>\n"
"class K {};"
"int main(int argc, char *argv[])\n"
"{\n"
" K k(1);\n"
" std::cout << k << std::endl;\n"
" if(k) {\n"
" k++;\n"
" }\n"
" std::cout << k << std::endl;\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:8]: (style) You should use ++ and -- as prefix whenever possible as these are more efficient than postfix operators\n", errout.str());
check("\n"
"#include <iostream>\n"
"class K {};"
"int main(int argc, char *argv[])\n"
"{\n"
" K k(1);\n"
" std::cout << k << std::endl;\n"
" if(k) {\n"
" ++k;\n"
" }\n"
" k++;\n"
" std::cout << k << std::endl;\n"
" return 0;\n"
"}\n");
"#include <iostream>\n"
"class K {};"
"int main(int argc, char *argv[])\n"
"{\n"
" K k(1);\n"
" std::cout << k << std::endl;\n"
" if(k) {\n"
" ++k;\n"
" }\n"
" k++;\n"
" std::cout << k << std::endl;\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:10]: (style) You should use ++ and -- as prefix whenever possible as these are more efficient than postfix operators\n", errout.str());
check("\n"
"#include <iostream>\n"
"class K {};"
"int main(int argc, char *argv[])\n"
"{\n"
" K k(0);\n"
" std::cout << k << std::endl;\n"
" k--;\n"
" std::cout << k << std::endl;\n"
" return 0;\n"
"}\n");
"#include <iostream>\n"
"class K {};"
"int main(int argc, char *argv[])\n"
"{\n"
" K k(0);\n"
" std::cout << k << std::endl;\n"
" k--;\n"
" std::cout << k << std::endl;\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7]: (style) You should use ++ and -- as prefix whenever possible as these are more efficient than postfix operators\n", errout.str());
check("\n"
"#include <iostream>\n"
"class K {};"
"int main(int argc, char *argv[])\n"
"{\n"
" K k(0);\n"
" std::cout << k << std::endl;\n"
" ++k;\n"
" std::cout << k << std::endl;\n"
" return 0;\n"
"}\n");
"#include <iostream>\n"
"class K {};"
"int main(int argc, char *argv[])\n"
"{\n"
" K k(0);\n"
" std::cout << k << std::endl;\n"
" ++k;\n"
" std::cout << k << std::endl;\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("\n"
"#include <iostream>\n"
"class K {};"
"int main(int argc, char *argv[])\n"
"{\n"
" K k(0);\n"
" std::cout << k << std::endl;\n"
" --k;\n"
" std::cout << k << std::endl;\n"
" return 0;\n"
"}\n");
"#include <iostream>\n"
"class K {};"
"int main(int argc, char *argv[])\n"
"{\n"
" K k(0);\n"
" std::cout << k << std::endl;\n"
" --k;\n"
" std::cout << k << std::endl;\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
@ -178,191 +178,191 @@ private:
void testfor()
{
check("\n"
"#include <iostream>\n"
"int main(int argc, char *argv[])\n"
"{\n"
" for ( unsigned int i=0; i <= 10; i++) {\n"
" std::cout << i << std::endl;\n"
" }\n"
" return 0;\n"
"}\n");
"#include <iostream>\n"
"int main(int argc, char *argv[])\n"
"{\n"
" for ( unsigned int i=0; i <= 10; i++) {\n"
" std::cout << i << std::endl;\n"
" }\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("\n"
"#include <iostream>\n"
"class K {};\n"
"int main(int argc, char *argv[])\n"
"{\n"
" for ( K i(0); i <= 10; i++) {\n"
" std::cout << i << std::endl;\n"
" }\n"
" return 0;\n"
"}\n");
"#include <iostream>\n"
"class K {};\n"
"int main(int argc, char *argv[])\n"
"{\n"
" for ( K i(0); i <= 10; i++) {\n"
" std::cout << i << std::endl;\n"
" }\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6]: (style) You should use ++ and -- as prefix whenever possible as these are more efficient than postfix operators\n", errout.str());
check("\n"
"#include <iostream>\n"
"class K {};\n"
"int main(int argc, char *argv[])\n"
"{\n"
" for ( K i(0); i <= 10; ++i) {\n"
" std::cout << i << std::endl;\n"
" }\n"
" return 0;\n"
"}\n");
"#include <iostream>\n"
"class K {};\n"
"int main(int argc, char *argv[])\n"
"{\n"
" for ( K i(0); i <= 10; ++i) {\n"
" std::cout << i << std::endl;\n"
" }\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("\n"
"#include <iostream>\n"
"class K {};\n"
"int main(int argc, char *argv[])\n"
"{\n"
" for ( K i(10); i > 1; i--) {\n"
" std::cout << i << std::endl;\n"
" }\n"
" return 0;\n"
"}\n");
"#include <iostream>\n"
"class K {};\n"
"int main(int argc, char *argv[])\n"
"{\n"
" for ( K i(10); i > 1; i--) {\n"
" std::cout << i << std::endl;\n"
" }\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6]: (style) You should use ++ and -- as prefix whenever possible as these are more efficient than postfix operators\n", errout.str());
check("\n"
"#include <iostream>\n"
"class K {};\n"
"int main(int argc, char *argv[])\n"
"{\n"
" for ( K i=10; i > 1; --i) {\n"
" std::cout << i << std::endl;\n"
" }\n"
" return 0;\n"
"}\n");
"#include <iostream>\n"
"class K {};\n"
"int main(int argc, char *argv[])\n"
"{\n"
" for ( K i=10; i > 1; --i) {\n"
" std::cout << i << std::endl;\n"
" }\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
}
void teststream()
{
check("\n"
"#include <iostream>\n"
"int main(int argc, char *argv[])\n"
"{\n"
" int k(0);\n"
" std::cout << k << std::endl;\n"
" std::cout << k++ << std::endl;\n"
" std::cout << k-- << std::endl;\n"
" return 0;\n"
"}\n");
"#include <iostream>\n"
"int main(int argc, char *argv[])\n"
"{\n"
" int k(0);\n"
" std::cout << k << std::endl;\n"
" std::cout << k++ << std::endl;\n"
" std::cout << k-- << std::endl;\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("\n"
"#include <iostream>\n"
"class K {};\n"
"int main(int argc, char *argv[])\n"
"{\n"
" K k(0);\n"
" std::cout << k << std::endl;\n"
" std::cout << k-- << std::endl;\n"
" return 0;\n"
"}\n");
"#include <iostream>\n"
"class K {};\n"
"int main(int argc, char *argv[])\n"
"{\n"
" K k(0);\n"
" std::cout << k << std::endl;\n"
" std::cout << k-- << std::endl;\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:8]: (style) You should use ++ and -- as prefix whenever possible as these are more efficient than postfix operators\n", errout.str());
check("\n"
"#include <iostream>\n"
"class K {};\n"
"int main(int argc, char *argv[])\n"
"{\n"
" K k(0);\n"
" std::cout << k << std::endl;\n"
" std::cout << ++k << std::endl;\n"
" std::cout << --k << std::endl;\n"
" return 0;\n"
"}\n");
"#include <iostream>\n"
"class K {};\n"
"int main(int argc, char *argv[])\n"
"{\n"
" K k(0);\n"
" std::cout << k << std::endl;\n"
" std::cout << ++k << std::endl;\n"
" std::cout << --k << std::endl;\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void testvolatile()
{
check("\n"
"#include <iostream>\n"
"class K {};\n"
"int main(int argc, char *argv[])\n"
"{\n"
" volatile K k(0);\n"
" std::cout << k << std::endl;\n"
" k++;\n"
" std::cout << k << std::endl;\n"
" return 0;\n"
"}\n");
"#include <iostream>\n"
"class K {};\n"
"int main(int argc, char *argv[])\n"
"{\n"
" volatile K k(0);\n"
" std::cout << k << std::endl;\n"
" k++;\n"
" std::cout << k << std::endl;\n"
" return 0;\n"
"}\n");
TODO_ASSERT_EQUALS("", errout.str());
}
}
void testiterator()
{
check("\n"
"#include <vector>\n"
"class Base {};\n"
"int main() {\n"
" std::vector<Base*> v;\n"
" v.push_back(new Base());\n"
" v.push_back(new Base());\n"
" for (std::vector<Base*>::iterator i=v.begin(); i!=v.end(); i++) {\n"
" ;;\n"
" }\n"
" v.clear();\n"
" return 0;\n"
"}\n");
void testiterator()
{
check("\n"
"#include <vector>\n"
"class Base {};\n"
"int main() {\n"
" std::vector<Base*> v;\n"
" v.push_back(new Base());\n"
" v.push_back(new Base());\n"
" for (std::vector<Base*>::iterator i=v.begin(); i!=v.end(); i++) {\n"
" ;;\n"
" }\n"
" v.clear();\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:8]: (style) You should use ++ and -- as prefix whenever possible as these are more efficient than postfix operators\n", errout.str());
check("\n"
"#include <iostream>\n"
"#include <vector>\n"
"int main() {\n"
" std::vector<int> v;\n"
" std::vector<int>::iterator it;\n"
" for( int i=0; i < 10; ++i ) v.push_back(i);\n"
" unsigned int total = 0;\n"
" it = v.begin();\n"
" while( it != v.end() ) {\n"
" total += *it;\n"
" it++;\n"
" }\n"
" return 0;\n"
"}\n");
check("\n"
"#include <iostream>\n"
"#include <vector>\n"
"int main() {\n"
" std::vector<int> v;\n"
" std::vector<int>::iterator it;\n"
" for( int i=0; i < 10; ++i ) v.push_back(i);\n"
" unsigned int total = 0;\n"
" it = v.begin();\n"
" while( it != v.end() ) {\n"
" total += *it;\n"
" it++;\n"
" }\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:12]: (style) You should use ++ and -- as prefix whenever possible as these are more efficient than postfix operators\n", errout.str());
check("\n"
"#include <iostream>\n"
"#include <vector>\n"
"int main() {\n"
" std::vector<int> v;\n"
" std::vector<int>::const_iterator it;\n"
" for( int i=0; i < 10; ++i ) v.push_back(i);\n"
" unsigned int total = 0;\n"
" it = v.begin();\n"
" while( it != v.end() ) {\n"
" it++;\n"
" }\n"
" return 0;\n"
"}\n");
check("\n"
"#include <iostream>\n"
"#include <vector>\n"
"int main() {\n"
" std::vector<int> v;\n"
" std::vector<int>::const_iterator it;\n"
" for( int i=0; i < 10; ++i ) v.push_back(i);\n"
" unsigned int total = 0;\n"
" it = v.begin();\n"
" while( it != v.end() ) {\n"
" it++;\n"
" }\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:11]: (style) You should use ++ and -- as prefix whenever possible as these are more efficient than postfix operators\n", errout.str());
check("\n"
"#include <iostream>\n"
"#include <vector>\n"
"int main() {\n"
" std::vector<int> v;\n"
" std::vector<int>::iterator it;\n"
" for( int i=0; i < 10; ++i ) v.push_back(i);\n"
" unsigned int total = 0;\n"
" std::vector<int>::reverse_iterator rit;\n"
" rit= v.rend();\n"
" while( rit != v.rbegin() ) {\n"
" rit--;\n"
" }\n"
" return 0;\n"
"}\n");
check("\n"
"#include <iostream>\n"
"#include <vector>\n"
"int main() {\n"
" std::vector<int> v;\n"
" std::vector<int>::iterator it;\n"
" for( int i=0; i < 10; ++i ) v.push_back(i);\n"
" unsigned int total = 0;\n"
" std::vector<int>::reverse_iterator rit;\n"
" rit= v.rend();\n"
" while( rit != v.rbegin() ) {\n"
" rit--;\n"
" }\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:12]: (style) You should use ++ and -- as prefix whenever possible as these are more efficient than postfix operators\n", errout.str());
}
}
};