Refactorizations:

- Use const string references instead of const strings copies when possible
- Fixed cppcheck warning about postfix increment in CheckIO
- Use symbolDatabase to detect pointers in CheckOther::checkAssignBoolToPointer
This commit is contained in:
PKEuS 2012-05-25 03:09:41 -07:00
parent e2bab4b6a3
commit 9dc8123151
10 changed files with 18 additions and 18 deletions

View File

@ -55,7 +55,7 @@ void CheckAssignIf::assignIf()
if (tok2->str() == "(" || tok2->str() == "}" || tok2->str() == "=")
break;
if (Token::Match(tok2,"if ( %varid% %any% %num% &&|%oror%|)", varid)) {
const std::string op(tok2->strAt(3));
const std::string& op(tok2->strAt(3));
const MathLib::bigint num2 = MathLib::toLongNumber(tok2->strAt(4));
if (op == "==" && (num & num2) != ((bitop=='&') ? num2 : num))
assignIfError(tok2, false);
@ -105,7 +105,7 @@ void CheckAssignIf::comparison()
continue;
if ((num1 & num2) != num2) {
const std::string op(compareToken->str());
const std::string& op(compareToken->str());
comparisonError(tok, num1, op, num2, op=="==" ? false : true);
}
}

View File

@ -2046,7 +2046,7 @@ void CheckBufferOverrun::arrayIndexThenCheck()
return;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (Token::Match(tok, "%var% [ %var% ]")) {
const std::string indexName(tok->strAt(2));
const std::string& indexName(tok->strAt(2));
// skip array index..
tok = tok->tokAt(4);

View File

@ -1044,7 +1044,7 @@ void CheckClass::virtualDestructor()
const Scope *derivedFrom = scope->derivedFrom[j].scope;
// Name of base class..
const std::string baseName = derivedFrom->className;
const std::string& baseName = derivedFrom->className;
// Check for this pattern:
// 1. Base class pointer is given the address of derived class instance

View File

@ -35,7 +35,7 @@ void CheckInternal::checkTokenMatchPatterns()
if (!Token::simpleMatch(tok, "Token :: Match (") && !Token::simpleMatch(tok, "Token :: findmatch ("))
continue;
const std::string funcname = tok->strAt(2);
const std::string& funcname = tok->strAt(2);
// Get pattern string
const Token *pattern_tok = tok->tokAt(4)->nextArgument();
@ -64,7 +64,7 @@ void CheckInternal::checkTokenSimpleMatchPatterns()
if (!Token::simpleMatch(tok, "Token :: simpleMatch (") && !Token::simpleMatch(tok, "Token :: findsimplematch ("))
continue;
const std::string funcname = tok->strAt(2);
const std::string& funcname = tok->strAt(2);
// Get pattern string
const Token *pattern_tok = tok->tokAt(4)->nextArgument();
@ -136,7 +136,7 @@ void CheckInternal::checkMissingPercentCharacter()
if (!Token::simpleMatch(tok, "Token :: Match (") && !Token::simpleMatch(tok, "Token :: findmatch ("))
continue;
const std::string funcname = tok->strAt(2);
const std::string& funcname = tok->strAt(2);
// Get pattern string
const Token *pattern_tok = tok->tokAt(4)->nextArgument();

View File

@ -118,7 +118,7 @@ void CheckIO::checkFileUsage()
indent++;
else if (tok->str() == "}") {
indent--;
for (std::map<unsigned int, Filepointer>::iterator i = filepointers.begin(); i != filepointers.end(); i++) {
for (std::map<unsigned int, Filepointer>::iterator i = filepointers.begin(); i != filepointers.end(); ++i) {
if (indent < i->second.mode_indent) {
i->second.mode_indent = 0;
i->second.mode = UNKNOWN;

View File

@ -2710,7 +2710,7 @@ void CheckMemoryLeakNoVar::check()
if (tok3->str() == "(") {
// Is it a function call..
if (Token::Match(tok3->tokAt(-2), "[;{}] %var% (")) {
const std::string functionName = tok3->strAt(-1);
const std::string& functionName = tok3->strAt(-1);
if (functionName == "delete" ||
functionName == "free" ||
functionName == "fclose" ||

View File

@ -443,7 +443,7 @@ void CheckNullPointer::nullPointerAfterLoop()
continue;
// Get variable name for the loop variable
const std::string varname(tok->strAt(2));
const std::string& varname(tok->strAt(2));
// Locate the end of the while loop body..
const Token *tok2 = tok->next()->link()->next()->link();
@ -782,7 +782,7 @@ void CheckNullPointer::nullPointerByDeRefAndChec()
continue;
// Name of pointer
const std::string varname(vartok->str());
const std::string& varname(vartok->str());
const Variable* var = symbolDatabase->getVariableFromVarId(varid);
// Check that variable is a pointer..

View File

@ -2773,13 +2773,13 @@ void CheckOther::sizeofCalculationError(const Token *tok, bool inconclusive)
void CheckOther::checkAssignBoolToPointer()
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (Token::Match(tok, "[;{}] %var% = %bool% ;")) {
if (Token::Match(tok, "!!* %var% = %bool% ;")) {
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
const Variable *var1(symbolDatabase->getVariableFromVarId(tok->next()->varId()));
// Is variable a pointer?
if (var1 && var1->nameToken()->strAt(-1) == "*")
if (var1 && var1->isPointer())
assignBoolToPointerError(tok->next());
}
}

View File

@ -679,7 +679,7 @@ void CheckStl::stlBoundries()
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
// Declaring iterator..
if (tok->str() == "<" && Token::Match(tok->previous(), STL_CONTAINER_LIST)) {
const std::string container_name(tok->strAt(-1));
const std::string& container_name(tok->strAt(-1));
while (tok && tok->str() != ">")
tok = tok->next();
if (!tok)

View File

@ -3262,7 +3262,7 @@ bool Tokenizer::simplifyTokenList()
continue;
}
const std::string num = tok->strAt(4);
const std::string& num = tok->strAt(4);
int indent = 1;
for (Token *tok2 = tok->tokAt(6); tok2; tok2 = tok2->next()) {
if (tok2->str() == "{") {
@ -4226,8 +4226,8 @@ bool Tokenizer::simplifyConditions()
// Compare numbers
if (cmp == "==" || cmp == "!=") {
const std::string op1(tok->next()->str());
const std::string op2(tok->strAt(3));
const std::string& op1(tok->next()->str());
const std::string& op2(tok->strAt(3));
bool eq = false;
if (MathLib::isInt(op1) && MathLib::isInt(op2))
@ -5846,7 +5846,7 @@ bool Tokenizer::simplifyKnownVariablesGetData(unsigned int varid, Token **_tok2,
return false;
// no break => the value of the counter value is known after the for loop..
const std::string compareop = tok2->strAt(5);
const std::string& compareop = tok2->strAt(5);
if (compareop == "<") {
value = tok2->strAt(6);
valueVarId = tok2->tokAt(6)->varId();