Merge pull request #361 from Dmitry-Me/reduceVariablesLifetime
Bring variables declaration to where they are really needed.
This commit is contained in:
commit
4e9130061a
|
@ -389,15 +389,14 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken,
|
|||
|
||||
void CheckLeakAutoVar::functionCall(const Token *tok, VarInfo *varInfo, const int dealloc)
|
||||
{
|
||||
std::map<unsigned int, int> &alloctype = varInfo->alloctype;
|
||||
std::map<unsigned int, std::string> &possibleUsage = varInfo->possibleUsage;
|
||||
|
||||
// Ignore function call?
|
||||
const bool ignore = bool(_settings->library.leakignore.find(tok->str()) != _settings->library.leakignore.end());
|
||||
|
||||
if (ignore)
|
||||
return;
|
||||
|
||||
std::map<unsigned int, int> &alloctype = varInfo->alloctype;
|
||||
std::map<unsigned int, std::string> &possibleUsage = varInfo->possibleUsage;
|
||||
|
||||
for (const Token *arg = tok->tokAt(2); arg; arg = arg->nextArgument()) {
|
||||
if ((Token::Match(arg, "%var% [-,)]") && arg->varId() > 0) ||
|
||||
(Token::Match(arg, "& %var%") && arg->next()->varId() > 0)) {
|
||||
|
|
|
@ -2759,11 +2759,11 @@ void CheckOther::checkDuplicateExpression()
|
|||
|
||||
for (const Token *tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) {
|
||||
if (tok->isOp() && tok->astOperand1() && !Token::Match(tok, "+|*|<<|>>")) {
|
||||
bool assignment = tok->str() == "=";
|
||||
if (Token::Match(tok, "==|!=|-") && astIsFloat(tok->astOperand1(), true))
|
||||
continue;
|
||||
if (isSameExpression(tok->astOperand1(), tok->astOperand2(), _settings->library.functionpure)) {
|
||||
if (isWithoutSideEffects(_tokenizer, tok->astOperand1())) {
|
||||
const bool assignment = tok->str() == "=";
|
||||
if (assignment)
|
||||
selfAssignmentError(tok, tok->astOperand1()->expressionString());
|
||||
else
|
||||
|
@ -2949,13 +2949,13 @@ void CheckOther::checkComparisonFunctionIsAlwaysTrueOrFalse()
|
|||
const Scope * scope = symbolDatabase->functionScopes[i];
|
||||
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
|
||||
if (tok->isName() && Token::Match(tok, "isgreater|isless|islessgreater|isgreaterequal|islessequal ( %var% , %var% )")) {
|
||||
const std::string& functionName = tok->str(); // store function name
|
||||
const std::string& varNameLeft = tok->strAt(2); // get the left variable name
|
||||
const unsigned int varidLeft = tok->tokAt(2)->varId();// get the left varid
|
||||
const unsigned int varidRight = tok->tokAt(4)->varId();// get the right varid
|
||||
// compare varids: if they are not zero but equal
|
||||
// --> the comparison function is calles with the same variables
|
||||
if (varidLeft != 0 && varidLeft == varidRight) {
|
||||
const std::string& functionName = tok->str(); // store function name
|
||||
const std::string& varNameLeft = tok->strAt(2); // get the left variable name
|
||||
if (functionName == "isgreater" || functionName == "isless" || functionName == "islessgreater") {
|
||||
// e.g.: isgreater(x,x) --> (x)>(x) --> false
|
||||
checkComparisonFunctionIsAlwaysTrueOrFalseError(tok,functionName,varNameLeft,false);
|
||||
|
|
|
@ -2344,9 +2344,7 @@ AccessControl Scope::defaultAccess() const
|
|||
// Get variable list..
|
||||
void Scope::getVariableList()
|
||||
{
|
||||
AccessControl varaccess = defaultAccess();
|
||||
const Token *start;
|
||||
unsigned int level = 1;
|
||||
|
||||
if (classStart)
|
||||
start = classStart->next();
|
||||
|
@ -2359,6 +2357,8 @@ void Scope::getVariableList()
|
|||
else
|
||||
return;
|
||||
|
||||
AccessControl varaccess = defaultAccess();
|
||||
unsigned int level = 1;
|
||||
for (const Token *tok = start; tok; tok = tok->next()) {
|
||||
// end of scope?
|
||||
if (tok->str() == "}") {
|
||||
|
|
|
@ -5273,13 +5273,13 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, Token * tokEnd, bool only_k_r_
|
|||
//pattern: "%type% *| ... *| const| %var% ,|="
|
||||
if (Token::Match(tok2, "%type%") ||
|
||||
(tok2 && tok2->previous() && tok2->previous()->str() == ">")) {
|
||||
bool ispointer = false;
|
||||
Token *varName = tok2;
|
||||
if (!tok2->previous() || tok2->previous()->str() != ">")
|
||||
varName = varName->next();
|
||||
else
|
||||
--typelen;
|
||||
//skip all the pointer part
|
||||
bool ispointer = false;
|
||||
while (varName && varName->str() == "*") {
|
||||
ispointer = true;
|
||||
varName = varName->next();
|
||||
|
@ -6337,13 +6337,13 @@ bool Tokenizer::simplifyKnownVariables()
|
|||
}
|
||||
|
||||
// struct name..
|
||||
if (Token::Match(tok2, "%varid% = &| %varid%", tok2->varId()))
|
||||
continue;
|
||||
|
||||
const std::string structname = Token::Match(tok2->tokAt(-3), "[;{}] %var% .") ?
|
||||
std::string(tok2->strAt(-2) + " .") :
|
||||
std::string("");
|
||||
|
||||
if (Token::Match(tok2, "%varid% = &| %varid%", tok2->varId()))
|
||||
continue;
|
||||
|
||||
const Token * const valueToken = tok2->tokAt(2);
|
||||
|
||||
std::string value;
|
||||
|
@ -6396,13 +6396,14 @@ bool Tokenizer::simplifyKnownVariables()
|
|||
|
||||
else if (Token::Match(tok2, "strcpy|sprintf ( %var% , %str% ) ;")) {
|
||||
const unsigned int varid(tok2->tokAt(2)->varId());
|
||||
std::string::size_type n = std::string::npos;
|
||||
if (varid == 0)
|
||||
continue;
|
||||
|
||||
const std::string structname;
|
||||
const Token * const valueToken = tok2->tokAt(4);
|
||||
std::string value(valueToken->str());
|
||||
if (tok2->str() == "sprintf") {
|
||||
std::string::size_type n = std::string::npos;
|
||||
while ((n = value.find("%%",n+1)) != std::string::npos) {
|
||||
value.replace(n,2,"%");
|
||||
}
|
||||
|
|
|
@ -579,13 +579,13 @@ static void compilePrecedence2(Token *&tok, AST_state& state)
|
|||
bool opPrevTopSquare = !state.op.empty() && state.op.top() && state.op.top()->str() == "[";
|
||||
std::size_t oldOpSize = state.op.size();
|
||||
compileExpression(tok, state);
|
||||
bool operandInside = oldOpSize < state.op.size();
|
||||
tok = tok2;
|
||||
if ((tok->previous() && tok->previous()->isName() && (tok->strAt(-1) != "return" && (!state.cpp || !Token::Match(tok->previous(), "throw|delete"))))
|
||||
|| (tok->strAt(-1) == "]" && (!state.cpp || !Token::Match(tok->linkAt(-1)->previous(), "new|delete")))
|
||||
|| (tok->strAt(-1) == ">" && tok->linkAt(-1))
|
||||
|| (tok->strAt(-1) == ")" && !iscast(tok->linkAt(-1))) // Don't treat brackets to clarify precedence as function calls
|
||||
|| (tok->strAt(-1) == "}" && opPrevTopSquare)) {
|
||||
const bool operandInside = oldOpSize < state.op.size();
|
||||
if (operandInside)
|
||||
compileBinOp(tok, state, nullptr);
|
||||
else
|
||||
|
|
Loading…
Reference in New Issue