From 4dd38356178fe273042f3cccf0358c1766092874 Mon Sep 17 00:00:00 2001 From: Slava Semushin Date: Sun, 14 Jun 2009 12:33:46 +0700 Subject: [PATCH 1/7] Embed errout.str() into ASSERT_EQUALS() call. Get rid of useless variables. No functional change. --- test/testbufferoverrun.cpp | 6 ++---- test/testconstructors.cpp | 16 +++++----------- test/testmemleak.cpp | 18 ++++++------------ 3 files changed, 13 insertions(+), 27 deletions(-) diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 03236147c..4ee0694b4 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -414,8 +414,7 @@ private: "{\n" " str[10] = 0;\n" "}\n"); - std::string err(errout.str()); - ASSERT_EQUALS("[test.cpp:10]: (all) Array index out of bounds\n", err); + ASSERT_EQUALS("[test.cpp:10]: (all) Array index out of bounds\n", errout.str()); } @@ -456,8 +455,7 @@ private: " for (i = 0; i <= 10; ++i)\n" " a[i] = 0;\n" "}\n"); - std::string err(errout.str()); - ASSERT_EQUALS("[test.cpp:7]: (all) Buffer overrun\n", err); + ASSERT_EQUALS("[test.cpp:7]: (all) Buffer overrun\n", errout.str()); } diff --git a/test/testconstructors.cpp b/test/testconstructors.cpp index ef0859505..653a29ab1 100644 --- a/test/testconstructors.cpp +++ b/test/testconstructors.cpp @@ -86,9 +86,7 @@ private: "public:\n" " int i;\n" "};\n"); - std::string actual(errout.str()); - std::string expected("[test.cpp:1]: (style) The class 'Fred' has no constructor\n"); - ASSERT_EQUALS(expected, actual); + ASSERT_EQUALS("[test.cpp:1]: (style) The class 'Fred' has no constructor\n", errout.str()); } @@ -186,8 +184,7 @@ private: " { i = fred.i; return *this; }\n" "};\n"); - std::string err(errout.str()); - ASSERT_EQUALS("", err); + ASSERT_EQUALS("", errout.str()); } @@ -239,8 +236,7 @@ private: " };\n" "}\n"); - std::string err(errout.str()); - ASSERT_EQUALS("", err); + ASSERT_EQUALS("", errout.str()); } void initvar_chained_assign() @@ -260,8 +256,7 @@ private: " m_iMyInt1 = m_iMyInt2 = 0;\n" "}\n"); - std::string err(errout.str()); - ASSERT_EQUALS("", err); + ASSERT_EQUALS("", errout.str()); } @@ -295,8 +290,7 @@ private: " m_iMyInt = 0;\n" "}\n"); - std::string err(errout.str()); - ASSERT_EQUALS("", err); + ASSERT_EQUALS("", errout.str()); } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index ffb0d4915..a4ed8f0c4 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -1222,8 +1222,8 @@ private: " char *p = new char[100];\n" " foo(p);\n" "}\n", true); - std::string err(errout.str()); - ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:3]: (error) Mismatching allocation and deallocation: str\n", err); + ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:3]: (error) Mismatching allocation and deallocation: str\n", + errout.str()); } @@ -1239,8 +1239,7 @@ private: " char *p = new char[100];\n" " foo(p);\n" "}\n"); - std::string err(errout.str()); - ASSERT_EQUALS("[test.cpp:10]: (error) Memory leak: p\n", err); + ASSERT_EQUALS("[test.cpp:10]: (error) Memory leak: p\n", errout.str()); } @@ -1258,8 +1257,7 @@ private: " char *p = new char[100];\n" " foo(p);\n" "}\n"); - std::string err(errout.str()); - TODO_ASSERT_EQUALS("[test.cpp:11]: (error) Memory leak: p\n", err); + TODO_ASSERT_EQUALS("[test.cpp:11]: (error) Memory leak: p\n", errout.str()); } @@ -1471,9 +1469,7 @@ private: " return;\n" "}\n"); - std::string err(errout.str()); - - ASSERT_EQUALS("[test.cpp:12]: (error) Memory leak: s2\n", err); + ASSERT_EQUALS("[test.cpp:12]: (error) Memory leak: s2\n", errout.str()); } @@ -1995,9 +1991,7 @@ private: " memset(&(out[0]), 0, 1);\n" "}\n"); - std::string err(errout.str()); - - ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: out\n", err); + ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: out\n", errout.str()); } void strndup_function() From b3841dda967fa940e60dce264b2e97ac2f755478 Mon Sep 17 00:00:00 2001 From: Slava Semushin Date: Sun, 14 Jun 2009 13:09:02 +0700 Subject: [PATCH 2/7] Replace tok->next()->next()->next()->next() call to tok->tokAt(4). Done by command: git grep -l 'next()->next()->next()->next(),' | xargs sed -i 's|next()->next()->next()->next()|tokAt(4)|' No functional change. --- src/checkmemoryleak.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index c7472aeeb..9162f343d 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -1152,7 +1152,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) // erase "loop {" Token::eraseTokens(tok2, tok2->tokAt(3)); // erase "if break|continue ; }" - tok2 = tok2->next()->next()->next()->next(); + tok2 = tok2->tokAt(4); Token::eraseTokens(tok2, tok2->tokAt(5)); done = false; } From ab0a1973e82d79b81e6c0e21898eaa1838908f16 Mon Sep 17 00:00:00 2001 From: Slava Semushin Date: Sun, 14 Jun 2009 13:12:04 +0700 Subject: [PATCH 3/7] Replace tok->next()->next()->next() call to tok->tokAt(3). Done by command: git grep -l 'next()->next()->next(),' | xargs sed -i 's|next()->next()->next()|tokAt(3)|' No functional change. --- src/checkmemoryleak.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index 9162f343d..837359e45 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -941,7 +941,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) if (Token::Match(tok2->next(), "{ %var% %var% ; }")) { Token::eraseTokens(tok2, tok2->tokAt(2)); - Token::eraseTokens(tok2->next()->next()->next(), tok2->tokAt(5)); + Token::eraseTokens(tok2->tokAt(3), tok2->tokAt(5)); done = false; } @@ -1125,7 +1125,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) // Remove the "if break|continue ;" that follows "dealloc ; alloc ;" if (! _settings->_showAll && Token::Match(tok2, "dealloc ; alloc ; if break|continue ;")) { - tok2 = tok2->next()->next()->next(); + tok2 = tok2->tokAt(3); Token::eraseTokens(tok2, tok2->tokAt(3)); done = false; } @@ -1217,7 +1217,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) // Reduce "[;{}] return use ; %var%" => "[;{}] return use ;" if (Token::Match(tok2, "[;{}] return use ; %var%")) { - Token::eraseTokens(tok2->next()->next()->next(), tok2->tokAt(5)); + Token::eraseTokens(tok2->tokAt(3), tok2->tokAt(5)); done = false; } From 4245047674c3afef7c0093e4c097c8a960c82629 Mon Sep 17 00:00:00 2001 From: Slava Semushin Date: Sun, 14 Jun 2009 13:20:51 +0700 Subject: [PATCH 4/7] Replace tok->next()->next() call to tok->tokAt(2). Done by command: git grep -l 'next()->next()' | xargs sed -i 's|next()->next()|tokAt(2)|' No functional change. --- src/checkmemoryleak.cpp | 10 +++++----- src/tokenize.cpp | 10 +++++----- test/testtoken.cpp | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index 837359e45..ee545daf3 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -935,7 +935,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) if (Token::Match(tok2->next(), "{ %var% ; }")) { Token::eraseTokens(tok2, tok2->tokAt(2)); - Token::eraseTokens(tok2->next()->next(), tok2->tokAt(4)); + Token::eraseTokens(tok2->tokAt(2), tok2->tokAt(4)); done = false; } if (Token::Match(tok2->next(), "{ %var% %var% ; }")) @@ -1135,7 +1135,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) if (Token::simpleMatch(tok2->next(), "do { alloc ; }")) { Token::eraseTokens(tok2, tok2->tokAt(3)); - Token::eraseTokens(tok2->next()->next(), tok2->tokAt(4)); + Token::eraseTokens(tok2->tokAt(2), tok2->tokAt(4)); done = false; } @@ -1161,7 +1161,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) if (Token::Match(tok2->next(), "loop { %var% ; break ; }")) { Token::eraseTokens(tok2, tok2->tokAt(3)); - Token::eraseTokens(tok2->next()->next(), tok2->tokAt(6)); + Token::eraseTokens(tok2->tokAt(2), tok2->tokAt(6)); done = false; } @@ -1210,7 +1210,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) // Reduce "[;{}] return ; %var%" => "[;{}] return ;" if (Token::Match(tok2, "[;{}] return ; %var%")) { - Token::eraseTokens(tok2->next()->next(), tok2->tokAt(4)); + Token::eraseTokens(tok2->tokAt(2), tok2->tokAt(4)); done = false; } @@ -1342,7 +1342,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) if (Token::simpleMatch(tok2, "break ;")) { tok2->str(";"); - tok2 = tok2->next()->next(); + tok2 = tok2->tokAt(2); } } } diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 2ceca1264..0131c2acf 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -831,7 +831,7 @@ void Tokenizer::setVarId() for (Token *tok2 = tok; tok2; tok2 = tok2->next()) { if (tok2->varId() == tok->varId() && Token::simpleMatch(tok2->next(), pattern.c_str())) - tok2->next()->next()->varId(_varId); + tok2->tokAt(2)->varId(_varId); } } } @@ -1462,7 +1462,7 @@ void Tokenizer::simplifyTokenList() for (Token *tok = _tokens; tok; tok = tok->next()) { if (Token::Match(tok, "case %any% : %var%")) - tok->next()->next()->insertToken(";"); + tok->tokAt(2)->insertToken(";"); if (Token::Match(tok, "default : %var%")) tok->next()->insertToken(";"); } @@ -1775,7 +1775,7 @@ bool Tokenizer::simplifyConditions() if (Token::Match(tok, "if|while ( %num%") && (tok->tokAt(3)->str() == ")" || tok->tokAt(3)->str() == "||" || tok->tokAt(3)->str() == "&&")) { - tok->next()->next()->str((tok->tokAt(2)->str() != "0") ? "true" : "false"); + tok->tokAt(2)->str((tok->tokAt(2)->str() != "0") ? "true" : "false"); ret = true; } Token *tok2 = tok->tokAt(2); @@ -2149,7 +2149,7 @@ bool Tokenizer::simplifyVarDecl() else if (Token::Match(tok2, "%type% * %var% ,|=")) { - if (tok2->next()->next()->str() != "operator") + if (tok2->tokAt(2)->str() != "operator") tok2 = tok2->tokAt(3); // The ',' token else tok2 = NULL; @@ -2359,7 +2359,7 @@ bool Tokenizer::simplifyNot() for (Token *tok = _tokens; tok; tok = tok->next()) { if (Token::Match(tok, "if|while ( not %var%")) - tok->next()->next()->str("!"); + tok->tokAt(2)->str("!"); if (Token::Match(tok, "&& not %var%")) tok->next()->str("!"); if (Token::Match(tok, "|| not %var%")) diff --git a/test/testtoken.cpp b/test/testtoken.cpp index 75706db76..1ab04b5ba 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -42,10 +42,10 @@ private: token->str("1"); token->insertToken("2"); token->next()->insertToken("3"); - Token *last = token->next()->next(); + Token *last = token->tokAt(2); ASSERT_EQUALS(token->str(), "1"); ASSERT_EQUALS(token->next()->str(), "2"); - ASSERT_EQUALS(token->next()->next()->str(), "3"); + ASSERT_EQUALS(token->tokAt(2)->str(), "3"); if (last->next()) ASSERT_EQUALS("Null was expected", ""); From 57f1a6d2b727228fb81867315a0fd2c4b88d8ba9 Mon Sep 17 00:00:00 2001 From: Slava Semushin Date: Sun, 14 Jun 2009 13:24:13 +0700 Subject: [PATCH 5/7] Replace tok->previous()->previous()->previous() call to tok->tokAt(-3). Done by command: git grep -l 'previous()->previous()->previous()' | xargs sed -i 's|previous()->previous()->previous()|tokAt(-3)|' No functional change. --- src/checkmemoryleak.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index ee545daf3..c251fa3ab 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -656,7 +656,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::listnext() && tok->next()->link() && - Token::simpleMatch(tok->next()->link()->previous()->previous()->previous(), std::string("&& ! " + varnameStr).c_str())) + Token::simpleMatch(tok->next()->link()->tokAt(-3), std::string("&& ! " + varnameStr).c_str())) { addtoken("if(!var)"); } From bc32d04efa4298db32ec1a5a476fee185a62c479 Mon Sep 17 00:00:00 2001 From: Slava Semushin Date: Sun, 14 Jun 2009 13:26:20 +0700 Subject: [PATCH 6/7] Replace tok->previous()->previous() call to tok->tokAt(-2). Done by command: git grep -l 'previous()->previous()' | xargs sed -i 's|previous()->previous()|tokAt(-2)|' No functional change. --- src/tokenize.cpp | 8 ++++---- test/testtoken.cpp | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 0131c2acf..ee31c4f34 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -1865,10 +1865,10 @@ bool Tokenizer::simplifyQuestionMark() if (tok->str() != "?") continue; - if (!tok->previous() || !tok->previous()->previous()) + if (!tok->previous() || !tok->tokAt(-2)) continue; - if (!Token::Match(tok->previous()->previous(), "[=,(]")) + if (!Token::Match(tok->tokAt(-2), "[=,(]")) continue; if (!Token::Match(tok->previous(), "%bool%") && @@ -1884,7 +1884,7 @@ bool Tokenizer::simplifyQuestionMark() continue; end = end->next(); - tok = tok->previous()->previous(); + tok = tok->tokAt(-2); while (tok->next() != end) { tok->deleteNext(); @@ -2564,7 +2564,7 @@ bool Tokenizer::simplifyRedundantParanthesis() // We have "(( *something* ))", remove the inner // paranthesis tok->deleteNext(); - tok->link()->previous()->previous()->deleteNext(); + tok->link()->tokAt(-2)->deleteNext(); ret = true; } diff --git a/test/testtoken.cpp b/test/testtoken.cpp index 1ab04b5ba..8b2292a37 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -51,7 +51,7 @@ private: ASSERT_EQUALS(last->str(), "3"); ASSERT_EQUALS(last->previous()->str(), "2"); - ASSERT_EQUALS(last->previous()->previous()->str(), "1"); + ASSERT_EQUALS(last->tokAt(-2)->str(), "1"); if (token->previous()) ASSERT_EQUALS("Null was expected", ""); From 4286fdbabb64014fd6ef968e163257a977400d3e Mon Sep 17 00:00:00 2001 From: Slava Semushin Date: Sun, 14 Jun 2009 13:55:23 +0700 Subject: [PATCH 7/7] Token: introduce str(const std::string &) method. Get rid of useless std::string.c_str() calls. No functional change. --- src/token.cpp | 7 ++++++- src/token.h | 2 ++ src/tokenize.cpp | 12 ++++++------ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/token.cpp b/src/token.cpp index 51635a31d..ec9e9df33 100644 --- a/src/token.cpp +++ b/src/token.cpp @@ -44,7 +44,7 @@ Token::~Token() } -void Token::str(const char s[]) +void Token::str(const std::string &s) { _str = s; _isName = bool(_str[0] == '_' || std::isalpha(_str[0])); @@ -57,6 +57,11 @@ void Token::str(const char s[]) _varId = 0; } +void Token::str(const char s[]) +{ + str(std::string(s)); +} + void Token::concatStr(std::string const& b) { _str.erase(_str.length() - 1); diff --git a/src/token.h b/src/token.h index 0a3995175..86934048d 100644 --- a/src/token.h +++ b/src/token.h @@ -26,6 +26,8 @@ class Token public: Token(); ~Token(); + + void str(const std::string &s); void str(const char s[]); void concatStr(std::string const& b); diff --git a/src/tokenize.cpp b/src/tokenize.cpp index ee31c4f34..a01dd8940 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -104,7 +104,7 @@ void Tokenizer::addtoken(const char str[], const unsigned int lineno, const unsi { _tokens = new Token; _tokensBack = _tokens; - _tokensBack->str(str2.str().c_str()); + _tokensBack->str(str2.str()); } _tokensBack->linenr(lineno); @@ -1257,7 +1257,7 @@ void Tokenizer::simplifyTokenList() { std::ostringstream str; str << SizeOfType(tok->strAt(2)); - tok->str(str.str().c_str()); + tok->str(str.str()); for (int i = 0; i < 3; i++) { @@ -1279,7 +1279,7 @@ void Tokenizer::simplifyTokenList() { std::ostringstream str; str << size; - tok->str(str.str().c_str()); + tok->str(str.str()); for (int i = 0; i < 3; i++) { tok->deleteNext(); @@ -1307,7 +1307,7 @@ void Tokenizer::simplifyTokenList() { std::ostringstream ostr; ostr << sz; - tok->str(ostr.str().c_str()); + tok->str(ostr.str()); while (tok->next()->str() != ")") tok->deleteNext(); tok->deleteNext(); @@ -1356,7 +1356,7 @@ void Tokenizer::simplifyTokenList() { std::ostringstream str; str << total_size; - tok2->str(str.str().c_str()); + tok2->str(str.str()); // Delete the other tokens.. for (int i = 0; i < 3; i++) { @@ -2334,7 +2334,7 @@ bool Tokenizer::simplifyIfNot() if (Token::Match(tok, "%var% == 0")) { tok->deleteNext(); - tok->next()->str(tok->str().c_str()); + tok->next()->str(tok->str()); tok->str("!"); ret = true; }