diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index c7472aeeb..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)"); } @@ -935,13 +935,13 @@ 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% ; }")) { 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; } @@ -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; } @@ -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; } @@ -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,14 +1210,14 @@ 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; } // 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; } @@ -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/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 e1e8fd34b..3256083cc 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); @@ -841,7 +841,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); } } } @@ -1267,7 +1267,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++) { @@ -1289,7 +1289,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(); @@ -1317,7 +1317,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(); @@ -1366,7 +1366,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++) { @@ -1472,7 +1472,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(";"); } @@ -1785,7 +1785,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); @@ -1875,10 +1875,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%") && @@ -1894,7 +1894,7 @@ bool Tokenizer::simplifyQuestionMark() continue; end = end->next(); - tok = tok->previous()->previous(); + tok = tok->tokAt(-2); while (tok->next() != end) { tok->deleteNext(); @@ -2159,7 +2159,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; @@ -2344,7 +2344,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; } @@ -2369,7 +2369,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%")) @@ -2574,7 +2574,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/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() diff --git a/test/testtoken.cpp b/test/testtoken.cpp index 75706db76..8b2292a37 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -42,16 +42,16 @@ 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", ""); 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", "");