2009-12-14 20:30:31 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2013-01-01 17:29:08 +01:00
|
|
|
* Copyright (C) 2007-2013 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/>.
|
|
|
|
*/
|
|
|
|
|
2013-09-04 20:59:49 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2009-12-14 20:30:31 +01:00
|
|
|
#ifndef executionpathH
|
|
|
|
#define executionpathH
|
2013-09-04 20:59:49 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2009-12-14 20:30:31 +01:00
|
|
|
|
|
|
|
#include <list>
|
2012-06-10 14:19:09 +02:00
|
|
|
#include "config.h"
|
2009-12-14 20:30:31 +01:00
|
|
|
|
|
|
|
class Token;
|
2009-12-21 18:17:35 +01:00
|
|
|
class Check;
|
2012-01-26 16:50:59 +01:00
|
|
|
class SymbolDatabase;
|
2009-12-14 20:30:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for Execution Paths checking
|
|
|
|
* An execution path is a linear list of statements. There are no "if"/.. to worry about.
|
|
|
|
**/
|
2012-06-10 14:19:09 +02:00
|
|
|
class CPPCHECKLIB ExecutionPath {
|
2009-12-14 20:30:31 +01:00
|
|
|
private:
|
2009-12-22 22:44:21 +01:00
|
|
|
/** No implementation */
|
|
|
|
void operator=(const ExecutionPath &);
|
|
|
|
|
2009-12-20 19:44:32 +01:00
|
|
|
protected:
|
2009-12-21 18:17:35 +01:00
|
|
|
Check * const owner;
|
2009-12-14 20:30:31 +01:00
|
|
|
|
2010-04-25 11:55:57 +02:00
|
|
|
/** Are two execution paths equal? */
|
|
|
|
virtual bool is_equal(const ExecutionPath *) const = 0;
|
|
|
|
|
2009-12-14 20:30:31 +01:00
|
|
|
public:
|
2013-08-07 16:27:37 +02:00
|
|
|
ExecutionPath(Check *c, unsigned int id) : owner(c), numberOfIf(0), varId(id) {
|
|
|
|
}
|
2009-12-14 20:30:31 +01:00
|
|
|
|
2013-08-07 16:27:37 +02:00
|
|
|
virtual ~ExecutionPath() {
|
|
|
|
}
|
2009-12-14 20:30:31 +01:00
|
|
|
|
2009-12-25 20:50:23 +01:00
|
|
|
/** Implement this in each derived class. This function must create a copy of the current instance */
|
2009-12-14 20:30:31 +01:00
|
|
|
virtual ExecutionPath *copy() = 0;
|
|
|
|
|
2010-04-28 21:33:11 +02:00
|
|
|
/** print checkdata */
|
|
|
|
void print() const;
|
|
|
|
|
2010-04-25 11:55:57 +02:00
|
|
|
/** number of if blocks */
|
|
|
|
unsigned int numberOfIf;
|
|
|
|
|
|
|
|
const unsigned int varId;
|
2009-12-25 20:50:23 +01:00
|
|
|
|
2009-12-14 20:30:31 +01:00
|
|
|
/**
|
|
|
|
* bail out all execution paths
|
|
|
|
* @param checks the execution paths to bail out on
|
|
|
|
**/
|
2011-10-13 20:53:06 +02:00
|
|
|
static void bailOut(std::list<ExecutionPath *> &checks) {
|
|
|
|
while (!checks.empty()) {
|
2009-12-29 09:17:07 +01:00
|
|
|
delete checks.back();
|
|
|
|
checks.pop_back();
|
|
|
|
}
|
2009-12-14 20:30:31 +01:00
|
|
|
}
|
|
|
|
|
2009-12-21 18:17:35 +01:00
|
|
|
/**
|
|
|
|
* bail out execution paths with given variable id
|
|
|
|
* @param checks the execution paths to bail out on
|
|
|
|
* @param varid the specific variable id
|
|
|
|
**/
|
2011-10-13 20:53:06 +02:00
|
|
|
static void bailOutVar(std::list<ExecutionPath *> &checks, const unsigned int varid) {
|
2010-04-02 07:30:58 +02:00
|
|
|
if (varid == 0)
|
2009-12-29 09:17:07 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
std::list<ExecutionPath *>::iterator it = checks.begin();
|
2011-10-13 20:53:06 +02:00
|
|
|
while (it != checks.end()) {
|
|
|
|
if ((*it)->varId == varid) {
|
2009-12-29 09:17:07 +01:00
|
|
|
delete *it;
|
|
|
|
checks.erase(it++);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2009-12-29 09:17:07 +01:00
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
2009-12-21 18:17:35 +01:00
|
|
|
}
|
|
|
|
|
2009-12-14 20:30:31 +01:00
|
|
|
/**
|
|
|
|
* Parse tokens at given location
|
|
|
|
* @param tok token to parse
|
|
|
|
* @param checks The execution paths. All execution paths in the list are executed in the current scope.
|
2009-12-25 19:45:21 +01:00
|
|
|
* @return the token before the "next" token.
|
2009-12-14 20:30:31 +01:00
|
|
|
**/
|
2010-04-04 11:24:52 +02:00
|
|
|
virtual const Token *parse(const Token &tok, std::list<ExecutionPath *> &checks) const = 0;
|
2009-12-25 19:45:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse condition
|
|
|
|
* @param tok first token in condition.
|
|
|
|
* @param checks The execution paths. All execution paths in the list are executed in the current scope
|
|
|
|
* @return true => bail out all checking
|
|
|
|
**/
|
2009-12-25 20:50:23 +01:00
|
|
|
virtual bool parseCondition(const Token &tok, std::list<ExecutionPath *> &checks);
|
2009-12-25 20:12:06 +01:00
|
|
|
|
2010-10-24 18:12:48 +02:00
|
|
|
/**
|
|
|
|
* Parse loop body
|
|
|
|
* @param tok the first token in the loop body (the token after the {)
|
|
|
|
* @param checks The execution paths
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
virtual void parseLoopBody(const Token *tok, std::list<ExecutionPath *> &checks) const {
|
2010-10-24 18:12:48 +02:00
|
|
|
(void)tok;
|
|
|
|
(void)checks;
|
|
|
|
}
|
|
|
|
|
2009-12-25 20:12:06 +01:00
|
|
|
/** going out of scope - all execution paths end */
|
2013-08-07 16:27:37 +02:00
|
|
|
virtual void end(const std::list<ExecutionPath *> & /*checks*/, const Token * /*tok*/) const {
|
|
|
|
}
|
2010-04-25 11:55:57 +02:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
bool operator==(const ExecutionPath &e) const {
|
2010-04-25 11:55:57 +02:00
|
|
|
return bool(varId == e.varId && is_equal(&e));
|
|
|
|
}
|
2010-10-05 20:54:13 +02:00
|
|
|
|
|
|
|
static void checkScope(const Token *tok, std::list<ExecutionPath *> &checks);
|
2009-12-14 20:30:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-01-26 16:50:59 +01:00
|
|
|
void checkExecutionPaths(const SymbolDatabase *symbolDatabase, ExecutionPath *c);
|
2009-12-14 20:30:31 +01:00
|
|
|
|
2013-09-04 20:59:49 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#endif // executionpathH
|