Refactoring CheckClass::checkConst. Use continue.

This commit is contained in:
Daniel Marjamäki 2016-09-04 15:38:56 +02:00
parent 03a6282ab3
commit 530a05e40e
1 changed files with 63 additions and 58 deletions

View File

@ -1692,71 +1692,76 @@ void CheckClass::checkConst()
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
// does the function have a body? // does the function have a body?
if (func->type == Function::eFunction && func->hasBody() && !func->isFriend() && !func->isStatic() && !func->isVirtual()) { if (func->type != Function::eFunction || !func->hasBody())
// get last token of return type continue;
const Token *previous = func->tokenDef->previous();
// does the function return a pointer or reference? // don't warn for friend/static/virtual methods
if (Token::Match(previous, "*|&")) { if (func->isFriend() || func->isStatic() || func->isVirtual())
if (func->retDef->str() != "const") continue;
continue;
} else if (Token::Match(previous->previous(), "*|& >")) {
const Token *temp = previous->previous();
bool foundConst = false; // get last token of return type
while (!Token::Match(temp->previous(), ";|}|{|public:|protected:|private:")) { const Token *previous = func->tokenDef->previous();
temp = temp->previous();
if (temp->str() == "const") {
foundConst = true;
break;
}
}
if (!foundConst) // does the function return a pointer or reference?
continue; if (Token::Match(previous, "*|&")) {
} else if (func->isOperator() && Token::Match(previous, ";|{|}|public:|private:|protected:")) { // Operator without return type: conversion operator if (func->retDef->str() != "const")
const std::string& opName = func->tokenDef->str(); continue;
if (opName.compare(8, 5, "const") != 0 && opName.back() == '&') } else if (Token::Match(previous->previous(), "*|& >")) {
continue; const Token *temp = previous->previous();
} else {
// don't warn for unknown types..
// LPVOID, HDC, etc
if (previous->str().size() > 2 && !previous->type() && previous->isUpperCaseName())
continue;
}
// check if base class function is virtual bool foundConst = false;
if (!scope->definedType->derivedFrom.empty()) { while (!Token::Match(temp->previous(), ";|}|{|public:|protected:|private:")) {
if (func->isImplicitlyVirtual(true)) temp = temp->previous();
continue; if (temp->str() == "const") {
} foundConst = true;
break;
bool memberAccessed = false;
// if nothing non-const was found. write error..
if (checkConstFunc(scope, &*func, memberAccessed)) {
std::string classname = scope->className;
const Scope *nest = scope->nestedIn;
while (nest && nest->type != Scope::eGlobal) {
classname = std::string(nest->className + "::" + classname);
nest = nest->nestedIn;
}
// get function name
std::string functionName = (func->tokenDef->isName() ? "" : "operator") + func->tokenDef->str();
if (func->tokenDef->str() == "(")
functionName += ")";
else if (func->tokenDef->str() == "[")
functionName += "]";
if (!func->isConst() || (!memberAccessed && !func->isOperator())) {
if (func->isInline())
checkConstError(func->token, classname, functionName, !memberAccessed && !func->isOperator());
else // not inline
checkConstError2(func->token, func->tokenDef, classname, functionName, !memberAccessed && !func->isOperator());
} }
} }
if (!foundConst)
continue;
} else if (func->isOperator() && Token::Match(previous, ";|{|}|public:|private:|protected:")) { // Operator without return type: conversion operator
const std::string& opName = func->tokenDef->str();
if (opName.compare(8, 5, "const") != 0 && opName.back() == '&')
continue;
} else {
// don't warn for unknown types..
// LPVOID, HDC, etc
if (previous->str().size() > 2 && !previous->type() && previous->isUpperCaseName())
continue;
} }
// check if base class function is virtual
if (!scope->definedType->derivedFrom.empty() && !func->isImplicitlyVirtual(true))
continue;
bool memberAccessed = false;
// if nothing non-const was found. write error..
if (!checkConstFunc(scope, &*func, memberAccessed))
continue;
if (func->isConst() && (memberAccessed || func->isOperator()))
continue;
std::string classname = scope->className;
const Scope *nest = scope->nestedIn;
while (nest && nest->type != Scope::eGlobal) {
classname = std::string(nest->className + "::" + classname);
nest = nest->nestedIn;
}
// get function name
std::string functionName = (func->tokenDef->isName() ? "" : "operator") + func->tokenDef->str();
if (func->tokenDef->str() == "(")
functionName += ")";
else if (func->tokenDef->str() == "[")
functionName += "]";
if (func->isInline())
checkConstError(func->token, classname, functionName, !memberAccessed && !func->isOperator());
else // not inline
checkConstError2(func->token, func->tokenDef, classname, functionName, !memberAccessed && !func->isOperator());
} }
} }
} }