2009-12-14 20:30:31 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2012-01-01 00:05:37 +01:00
|
|
|
* Copyright (C) 2007-2012 Daniel Marjamäki and Cppcheck team.
|
2009-12-14 20:30:31 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "executionpath.h"
|
|
|
|
#include "token.h"
|
|
|
|
#include <memory>
|
2010-04-25 11:55:57 +02:00
|
|
|
#include <set>
|
2010-04-25 12:10:50 +02:00
|
|
|
#include <iterator>
|
2010-04-28 21:33:11 +02:00
|
|
|
#include <iostream>
|
2009-12-14 20:30:31 +01:00
|
|
|
|
|
|
|
|
2010-06-05 09:45:35 +02:00
|
|
|
|
2009-12-25 19:45:21 +01:00
|
|
|
// default : bail out if the condition is has variable handling
|
2009-12-25 20:50:23 +01:00
|
|
|
bool ExecutionPath::parseCondition(const Token &tok, std::list<ExecutionPath *> & checks)
|
2009-12-25 19:45:21 +01:00
|
|
|
{
|
|
|
|
unsigned int parlevel = 0;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (const Token *tok2 = &tok; tok2; tok2 = tok2->next()) {
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "(")
|
2009-12-25 19:45:21 +01:00
|
|
|
++parlevel;
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (tok2->str() == ")") {
|
2010-04-02 07:30:58 +02:00
|
|
|
if (parlevel == 0)
|
2009-12-25 19:45:21 +01:00
|
|
|
break;
|
|
|
|
--parlevel;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (Token::Match(tok2, "[;{}]"))
|
2009-12-25 19:45:21 +01:00
|
|
|
break;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok2->varId() != 0) {
|
2010-04-25 11:55:57 +02:00
|
|
|
bailOutVar(checks, tok2->varId());
|
2009-12-25 19:45:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (std::list<ExecutionPath *>::iterator it = checks.begin(); it != checks.end();) {
|
|
|
|
if ((*it)->varId > 0 && (*it)->numberOfIf >= 1) {
|
2010-07-06 13:18:28 +02:00
|
|
|
delete *it;
|
|
|
|
checks.erase(it++);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2010-07-06 13:18:28 +02:00
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-25 19:45:21 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-28 21:33:11 +02:00
|
|
|
void ExecutionPath::print() const
|
|
|
|
{
|
|
|
|
std::cout << " varId=" << varId
|
|
|
|
<< " numberOfIf=" << numberOfIf
|
|
|
|
<< "\n";
|
|
|
|
}
|
|
|
|
|
2010-05-18 20:58:11 +02:00
|
|
|
// I use this function when debugging ExecutionPaths with GDB
|
2010-09-03 07:18:01 +02:00
|
|
|
/*
|
2010-04-28 21:33:11 +02:00
|
|
|
static void printchecks(const std::list<ExecutionPath *> &checks)
|
|
|
|
{
|
|
|
|
for (std::list<ExecutionPath *>::const_iterator it = checks.begin(); it != checks.end(); ++it)
|
|
|
|
(*it)->print();
|
|
|
|
}
|
2010-09-03 07:18:01 +02:00
|
|
|
*/
|
2010-04-28 21:33:11 +02:00
|
|
|
|
|
|
|
|
2010-06-05 09:45:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Parse If/Switch body recursively.
|
|
|
|
* @param tok First token in body.
|
|
|
|
* @param checks The current checks
|
|
|
|
* @param newchecks new checks
|
|
|
|
* @param countif The countif set - count number of if for each execution path
|
|
|
|
*/
|
|
|
|
static void parseIfSwitchBody(const Token * const tok,
|
|
|
|
const std::list<ExecutionPath *> &checks,
|
|
|
|
std::list<ExecutionPath *> &newchecks,
|
|
|
|
std::set<unsigned int> &countif)
|
|
|
|
{
|
|
|
|
std::set<unsigned int> countif2;
|
|
|
|
std::list<ExecutionPath *> c;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!checks.empty()) {
|
2010-06-05 09:45:35 +02:00
|
|
|
std::list<ExecutionPath *>::const_iterator it;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (it = checks.begin(); it != checks.end(); ++it) {
|
2010-07-06 13:18:28 +02:00
|
|
|
if ((*it)->numberOfIf == 0)
|
|
|
|
c.push_back((*it)->copy());
|
2010-06-05 09:45:35 +02:00
|
|
|
if ((*it)->varId != 0)
|
|
|
|
countif2.insert((*it)->varId);
|
|
|
|
}
|
|
|
|
}
|
2010-10-05 20:54:13 +02:00
|
|
|
ExecutionPath::checkScope(tok, c);
|
2011-10-13 20:53:06 +02:00
|
|
|
while (!c.empty()) {
|
|
|
|
if (c.back()->varId == 0) {
|
2011-05-19 19:31:51 +02:00
|
|
|
delete c.back();
|
2010-06-05 09:45:35 +02:00
|
|
|
c.pop_back();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool duplicate = false;
|
|
|
|
std::list<ExecutionPath *>::const_iterator it;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (it = checks.begin(); it != checks.end(); ++it) {
|
|
|
|
if (*(*it) == *c.back() && (*it)->numberOfIf == c.back()->numberOfIf) {
|
2010-06-05 09:45:35 +02:00
|
|
|
duplicate = true;
|
|
|
|
countif2.erase((*it)->varId);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!duplicate)
|
|
|
|
newchecks.push_back(c.back());
|
2011-05-19 19:31:51 +02:00
|
|
|
else
|
|
|
|
delete c.back();
|
2010-06-05 09:45:35 +02:00
|
|
|
c.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add countif2 ids to countif.. countif.
|
|
|
|
countif.insert(countif2.begin(), countif2.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-05 20:54:13 +02:00
|
|
|
void ExecutionPath::checkScope(const Token *tok, std::list<ExecutionPath *> &checks)
|
2009-12-14 20:30:31 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok || tok->str() == "}" || checks.empty())
|
2010-04-04 11:24:52 +02:00
|
|
|
return;
|
2010-01-08 21:54:24 +01:00
|
|
|
|
2009-12-14 20:30:31 +01:00
|
|
|
const std::auto_ptr<ExecutionPath> check(checks.front()->copy());
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (; tok; tok = tok->next()) {
|
2010-09-17 19:31:45 +02:00
|
|
|
// might be a noreturn function..
|
|
|
|
if (Token::simpleMatch(tok->tokAt(-2), ") ; }") &&
|
2011-11-20 14:22:39 +01:00
|
|
|
Token::Match(tok->linkAt(-2)->tokAt(-2), "[;{}] %var% (") &&
|
|
|
|
tok->linkAt(-2)->previous()->varId() == 0) {
|
2010-09-17 19:31:45 +02:00
|
|
|
ExecutionPath::bailOut(checks);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-25 16:35:30 +02:00
|
|
|
if (tok->str() == "}")
|
2010-04-04 11:24:52 +02:00
|
|
|
return;
|
2009-12-14 20:30:31 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok->str() == "break") {
|
2011-07-25 16:35:30 +02:00
|
|
|
ExecutionPath::bailOut(checks);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Token::simpleMatch(tok, "while (")) {
|
2010-01-03 13:30:20 +01:00
|
|
|
// parse condition
|
2011-10-13 20:53:06 +02:00
|
|
|
if (checks.size() > 10 || check->parseCondition(*tok->tokAt(2), checks)) {
|
2010-01-03 13:30:20 +01:00
|
|
|
ExecutionPath::bailOut(checks);
|
2010-04-04 11:24:52 +02:00
|
|
|
return;
|
2010-01-03 13:30:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// skip "while (fgets()!=NULL)"
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Token::simpleMatch(tok, "while ( fgets (")) {
|
2011-11-20 14:22:39 +01:00
|
|
|
const Token *tok2 = tok->linkAt(3);
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Token::simpleMatch(tok2, ") ) {")) {
|
2011-11-20 14:22:39 +01:00
|
|
|
tok = tok2->linkAt(2);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok)
|
2010-01-03 13:30:20 +01:00
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-03 07:18:01 +02:00
|
|
|
// goto/setjmp/longjmp => bailout
|
2011-12-17 11:26:20 +01:00
|
|
|
else if (Token::Match(tok, "goto|setjmp|longjmp")) {
|
2010-04-02 08:35:05 +02:00
|
|
|
ExecutionPath::bailOut(checks);
|
2010-04-04 11:24:52 +02:00
|
|
|
return;
|
2010-04-02 08:35:05 +02:00
|
|
|
}
|
|
|
|
|
2010-07-08 11:16:49 +02:00
|
|
|
// ?: => bailout
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok->str() == "?") {
|
|
|
|
for (const Token *tok2 = tok; tok2 && tok2->str() != ";"; tok2 = tok2->next()) {
|
2010-09-15 20:04:50 +02:00
|
|
|
if (tok2->varId() > 0)
|
|
|
|
ExecutionPath::bailOutVar(checks, tok2->varId());
|
|
|
|
}
|
2010-07-08 11:16:49 +02:00
|
|
|
}
|
|
|
|
|
2011-12-17 11:26:20 +01:00
|
|
|
// for/while/switch/do .. bail out
|
|
|
|
else if (Token::Match(tok, "for|while|switch|do")) {
|
|
|
|
// goto {
|
|
|
|
const Token *tok2 = tok->next();
|
|
|
|
if (tok2 && tok2->str() == "(")
|
|
|
|
tok2 = tok2->link();
|
|
|
|
if (tok2 && tok2->str() == ")")
|
|
|
|
tok2 = tok2->next();
|
|
|
|
if (!tok2 || tok2->str() != "{") {
|
2010-07-18 13:19:37 +02:00
|
|
|
ExecutionPath::bailOut(checks);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-17 11:26:20 +01:00
|
|
|
if (tok->str() == "switch") {
|
|
|
|
// parse condition
|
|
|
|
if (checks.size() > 10 || check->parseCondition(*tok->next(), checks)) {
|
|
|
|
ExecutionPath::bailOut(checks);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-06-03 20:02:58 +02:00
|
|
|
// what variable ids should the if be counted for?
|
|
|
|
std::set<unsigned int> countif;
|
|
|
|
|
|
|
|
std::list<ExecutionPath *> newchecks;
|
|
|
|
|
2011-12-17 11:26:20 +01:00
|
|
|
for (const Token* tok3 = tok2->next(); tok3; tok3 = tok3->next()) {
|
|
|
|
if (tok3->str() == "{")
|
|
|
|
tok3 = tok3->link();
|
|
|
|
else if (tok3->str() == "}")
|
2010-06-03 20:02:58 +02:00
|
|
|
break;
|
2011-12-17 11:26:20 +01:00
|
|
|
else if (tok3->str() == "case" &&
|
|
|
|
!Token::Match(tok3, "case %num% : ; case")) {
|
|
|
|
parseIfSwitchBody(tok3, checks, newchecks, countif);
|
2010-06-03 20:02:58 +02:00
|
|
|
}
|
2010-06-05 09:45:35 +02:00
|
|
|
}
|
2010-06-03 20:02:58 +02:00
|
|
|
|
2010-06-05 09:45:35 +02:00
|
|
|
// Add newchecks to checks..
|
|
|
|
std::copy(newchecks.begin(), newchecks.end(), std::back_inserter(checks));
|
2010-06-03 20:02:58 +02:00
|
|
|
|
2010-06-05 09:45:35 +02:00
|
|
|
// Increase numberOfIf
|
|
|
|
std::list<ExecutionPath *>::iterator it;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (it = checks.begin(); it != checks.end(); ++it) {
|
2010-06-05 09:45:35 +02:00
|
|
|
if (countif.find((*it)->varId) != countif.end())
|
|
|
|
(*it)->numberOfIf++;
|
2010-06-03 20:02:58 +02:00
|
|
|
}
|
|
|
|
}
|
2011-12-17 11:26:20 +01:00
|
|
|
// no switch
|
|
|
|
else {
|
2011-10-13 20:53:06 +02:00
|
|
|
for (const Token *tok3 = tok; tok3 && tok3 != tok2; tok3 = tok3->next()) {
|
2010-10-24 19:14:40 +02:00
|
|
|
if (tok3->varId())
|
|
|
|
ExecutionPath::bailOutVar(checks, tok3->varId());
|
|
|
|
}
|
2010-12-26 13:38:16 +01:00
|
|
|
|
|
|
|
// it is not certain that a for/while will be executed:
|
2011-10-13 20:53:06 +02:00
|
|
|
for (std::list<ExecutionPath *>::iterator it = checks.begin(); it != checks.end();) {
|
2010-12-26 13:38:16 +01:00
|
|
|
if ((*it)->numberOfIf > 0)
|
|
|
|
checks.erase(it++);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
2011-02-19 20:19:46 +01:00
|
|
|
// #2231 - loop body only contains a conditional initialization..
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Token::simpleMatch(tok2->next(), "if (")) {
|
2011-02-19 20:19:46 +01:00
|
|
|
// Start { for the if block
|
2011-11-20 14:22:39 +01:00
|
|
|
const Token *tok3 = tok2->linkAt(2);
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Token::simpleMatch(tok3,") {")) {
|
2011-02-19 20:19:46 +01:00
|
|
|
tok3 = tok3->next();
|
|
|
|
|
|
|
|
// End } for the if block
|
|
|
|
const Token *tok4 = tok3->link();
|
|
|
|
if (Token::Match(tok3, "{ %var% =") &&
|
|
|
|
Token::simpleMatch(tok4, "} }") &&
|
2011-10-13 20:53:06 +02:00
|
|
|
Token::simpleMatch(tok4->tokAt(-2), "break ;")) {
|
2011-02-19 20:19:46 +01:00
|
|
|
// Is there a assignment and then a break?
|
2011-10-27 10:54:50 +02:00
|
|
|
const Token *t = Token::findsimplematch(tok3, ";");
|
2011-10-13 20:53:06 +02:00
|
|
|
if (t && t->tokAt(3) == tok4) {
|
|
|
|
for (std::list<ExecutionPath *>::iterator it = checks.begin(); it != checks.end(); ++it) {
|
|
|
|
if ((*it)->varId == tok3->next()->varId()) {
|
2011-02-19 20:19:46 +01:00
|
|
|
(*it)->numberOfIf++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tok = tok2->link();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-26 13:38:16 +01:00
|
|
|
// parse loop bodies
|
2010-10-24 18:12:48 +02:00
|
|
|
check->parseLoopBody(tok2->next(), checks);
|
|
|
|
}
|
|
|
|
|
2010-01-10 22:05:51 +01:00
|
|
|
// skip { .. }
|
|
|
|
tok2 = tok2->link();
|
|
|
|
|
|
|
|
// if "do { .. } while ( .." , goto end of while..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok, "do {") && Token::simpleMatch(tok2, "} while ("))
|
2011-11-20 14:22:39 +01:00
|
|
|
tok2 = tok2->linkAt(2);
|
2010-01-10 22:05:51 +01:00
|
|
|
|
2010-02-17 18:10:50 +01:00
|
|
|
// bail out all variables if the scope contains a "return"
|
2010-01-10 22:05:51 +01:00
|
|
|
// bail out all variables used in this for/while/switch/do
|
2011-10-13 20:53:06 +02:00
|
|
|
for (; tok && tok != tok2; tok = tok->next()) {
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "return")
|
2010-02-17 18:10:50 +01:00
|
|
|
ExecutionPath::bailOut(checks);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->varId())
|
2010-01-10 22:05:51 +01:00
|
|
|
ExecutionPath::bailOutVar(checks, tok->varId());
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
2009-12-14 20:30:31 +01:00
|
|
|
}
|
|
|
|
|
2010-05-30 08:26:44 +02:00
|
|
|
// bailout used variables in '; FOREACH ( .. ) { .. }'
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (tok->str() != "if" && Token::Match(tok->previous(), "[;{}] %var% (")) {
|
2010-05-30 08:26:44 +02:00
|
|
|
// goto {
|
2010-05-30 09:00:18 +02:00
|
|
|
const Token *tok2 = tok->next()->link();
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok2 && tok2->str() == ")") {
|
2010-05-30 09:00:18 +02:00
|
|
|
tok2 = tok2->next();
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok2 && tok2->str() == "{") {
|
2010-05-30 09:00:18 +02:00
|
|
|
// goto "}"
|
|
|
|
tok2 = tok2->link();
|
|
|
|
|
|
|
|
// bail out all variables used in "{ .. }"
|
2011-10-13 20:53:06 +02:00
|
|
|
for (; tok && tok != tok2; tok = tok->next()) {
|
2010-05-30 09:00:18 +02:00
|
|
|
if (tok->varId())
|
|
|
|
ExecutionPath::bailOutVar(checks, tok->varId());
|
|
|
|
}
|
2010-05-30 08:26:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-08 21:24:48 +01:00
|
|
|
// .. ) { ... } => bail out
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Token::simpleMatch(tok, ") {")) {
|
2010-01-08 21:24:48 +01:00
|
|
|
ExecutionPath::bailOut(checks);
|
2010-04-04 11:24:52 +02:00
|
|
|
return;
|
2010-01-08 21:24:48 +01:00
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Token::Match(tok, "abort|exit (")) {
|
2010-01-03 15:35:32 +01:00
|
|
|
ExecutionPath::bailOut(checks);
|
2010-04-04 11:24:52 +02:00
|
|
|
return;
|
2010-01-03 15:35:32 +01:00
|
|
|
}
|
|
|
|
|
2009-12-21 18:17:35 +01:00
|
|
|
// don't parse into "struct type { .."
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Token::Match(tok, "struct|union|class %type% {|:")) {
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok && tok->str() != "{" && tok->str() != ";")
|
2010-02-08 18:06:28 +01:00
|
|
|
tok = tok->next();
|
|
|
|
tok = tok ? tok->link() : 0;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!tok) {
|
2010-12-31 20:55:28 +01:00
|
|
|
ExecutionPath::bailOut(checks);
|
|
|
|
return;
|
|
|
|
}
|
2010-02-08 18:06:28 +01:00
|
|
|
}
|
2009-12-21 18:17:35 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Token::simpleMatch(tok, "= {")) {
|
2010-03-28 10:42:37 +02:00
|
|
|
// GCC struct initialization.. bail out
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Token::Match(tok->tokAt(2), ". %var% =")) {
|
2010-03-28 10:42:37 +02:00
|
|
|
ExecutionPath::bailOut(checks);
|
2010-04-04 11:24:52 +02:00
|
|
|
return;
|
2010-03-28 10:42:37 +02:00
|
|
|
}
|
|
|
|
|
2009-12-14 20:30:31 +01:00
|
|
|
tok = tok->next()->link();
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!tok) {
|
2009-12-14 20:30:31 +01:00
|
|
|
ExecutionPath::bailOut(checks);
|
2010-04-04 11:24:52 +02:00
|
|
|
return;
|
2009-12-14 20:30:31 +01:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-01-08 21:24:48 +01:00
|
|
|
// ; { ... }
|
2011-12-17 11:26:20 +01:00
|
|
|
if (Token::Match(tok->previous(), "[;{}:] {")) {
|
2010-10-05 20:54:13 +02:00
|
|
|
ExecutionPath::checkScope(tok->next(), checks);
|
2010-01-08 21:24:48 +01:00
|
|
|
tok = tok->link();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok->str() == "if") {
|
2010-04-25 11:55:57 +02:00
|
|
|
// what variable ids should the numberOfIf be counted for?
|
|
|
|
std::set<unsigned int> countif;
|
|
|
|
|
2009-12-14 20:30:31 +01:00
|
|
|
std::list<ExecutionPath *> newchecks;
|
2011-10-13 20:53:06 +02:00
|
|
|
while (tok->str() == "if") {
|
2009-12-14 20:30:31 +01:00
|
|
|
// goto "("
|
|
|
|
tok = tok->next();
|
|
|
|
|
2009-12-25 19:45:21 +01:00
|
|
|
// parse condition
|
2011-10-13 20:53:06 +02:00
|
|
|
if (checks.size() > 10 || check->parseCondition(*tok->next(), checks)) {
|
2009-12-29 09:17:07 +01:00
|
|
|
ExecutionPath::bailOut(checks);
|
|
|
|
ExecutionPath::bailOut(newchecks);
|
2010-04-04 11:24:52 +02:00
|
|
|
return;
|
2009-12-14 20:30:31 +01:00
|
|
|
}
|
|
|
|
|
2009-12-25 19:45:21 +01:00
|
|
|
// goto ")"
|
|
|
|
tok = tok ? tok->link() : 0;
|
|
|
|
|
2009-12-14 20:30:31 +01:00
|
|
|
// goto "{"
|
|
|
|
tok = tok ? tok->next() : 0;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!Token::simpleMatch(tok, "{")) {
|
2009-12-29 09:17:07 +01:00
|
|
|
ExecutionPath::bailOut(checks);
|
|
|
|
ExecutionPath::bailOut(newchecks);
|
2010-04-04 11:24:52 +02:00
|
|
|
return;
|
2009-12-28 19:48:30 +01:00
|
|
|
}
|
2009-12-14 20:30:31 +01:00
|
|
|
|
|
|
|
// Recursively check into the if ..
|
2010-06-05 09:45:35 +02:00
|
|
|
parseIfSwitchBody(tok->next(), checks, newchecks, countif);
|
2009-12-14 20:30:31 +01:00
|
|
|
|
|
|
|
// goto "}"
|
|
|
|
tok = tok->link();
|
|
|
|
|
|
|
|
// there is no else => break out
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "} !!else"))
|
2009-12-14 20:30:31 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
// parse next "if"..
|
|
|
|
tok = tok->tokAt(2);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "if")
|
2009-12-14 20:30:31 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// there is no "if"..
|
2010-10-05 20:54:13 +02:00
|
|
|
ExecutionPath::checkScope(tok->next(), checks);
|
2009-12-14 20:30:31 +01:00
|
|
|
tok = tok->link();
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!tok) {
|
2009-12-29 09:17:07 +01:00
|
|
|
ExecutionPath::bailOut(newchecks);
|
2010-04-04 11:24:52 +02:00
|
|
|
return;
|
2009-12-28 19:48:30 +01:00
|
|
|
}
|
2009-12-14 20:30:31 +01:00
|
|
|
}
|
|
|
|
|
2010-04-25 11:55:57 +02:00
|
|
|
// Add newchecks to checks..
|
|
|
|
std::copy(newchecks.begin(), newchecks.end(), std::back_inserter(checks));
|
|
|
|
|
|
|
|
// Increase numberOfIf
|
2009-12-14 20:30:31 +01:00
|
|
|
std::list<ExecutionPath *>::iterator it;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (it = checks.begin(); it != checks.end(); ++it) {
|
2010-04-25 11:55:57 +02:00
|
|
|
if (countif.find((*it)->varId) != countif.end())
|
|
|
|
(*it)->numberOfIf++;
|
|
|
|
}
|
2010-11-15 20:35:01 +01:00
|
|
|
|
|
|
|
// Delete checks that have numberOfIf >= 2
|
2011-10-13 20:53:06 +02:00
|
|
|
for (it = checks.begin(); it != checks.end();) {
|
|
|
|
if ((*it)->varId > 0 && (*it)->numberOfIf >= 2) {
|
2010-11-15 20:35:01 +01:00
|
|
|
delete *it;
|
|
|
|
checks.erase(it++);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2010-11-15 20:35:01 +01:00
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
2009-12-14 20:30:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
2010-04-04 11:24:52 +02:00
|
|
|
tok = check->parse(*tok, checks);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (checks.empty())
|
2010-04-04 11:24:52 +02:00
|
|
|
return;
|
2009-12-14 20:30:31 +01:00
|
|
|
}
|
|
|
|
|
2010-01-01 20:12:39 +01:00
|
|
|
// return/throw ends all execution paths
|
2010-09-18 14:56:07 +02:00
|
|
|
if (tok->str() == "return" ||
|
|
|
|
tok->str() == "throw" ||
|
|
|
|
tok->str() == "continue" ||
|
2011-10-13 20:53:06 +02:00
|
|
|
tok->str() == "break") {
|
2009-12-14 20:30:31 +01:00
|
|
|
ExecutionPath::bailOut(checks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-25 20:12:06 +01:00
|
|
|
void checkExecutionPaths(const Token *tok, ExecutionPath *c)
|
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
for (; tok; tok = tok->next()) {
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() != ")")
|
2009-12-25 20:12:06 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Start of implementation..
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Token::Match(tok, ") const| {")) {
|
2009-12-25 20:12:06 +01:00
|
|
|
// goto the "{"
|
|
|
|
tok = tok->next();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "const")
|
2009-12-25 20:12:06 +01:00
|
|
|
tok = tok->next();
|
|
|
|
|
|
|
|
std::list<ExecutionPath *> checks;
|
|
|
|
checks.push_back(c->copy());
|
2010-10-05 20:54:13 +02:00
|
|
|
ExecutionPath::checkScope(tok, checks);
|
2009-12-25 20:12:06 +01:00
|
|
|
|
|
|
|
c->end(checks, tok->link());
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
while (!checks.empty()) {
|
2009-12-25 20:12:06 +01:00
|
|
|
delete checks.back();
|
|
|
|
checks.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|