symbol database: improve function argument count and default argument count

This commit is contained in:
Robert Reif 2011-03-30 21:59:43 -04:00
parent d17a204096
commit faaff942d2
2 changed files with 30 additions and 18 deletions

View File

@ -651,7 +651,7 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti
if (func->type == Function::eConstructor) if (func->type == Function::eConstructor)
{ {
// check for no arguments: func ( ) // check for no arguments: func ( )
if (func->argDef->next() == func->argDef->link()) if (func->argCount() == 0)
{ {
hasDefaultConstructor = true; hasDefaultConstructor = true;
break; break;
@ -1195,14 +1195,12 @@ void SymbolDatabase::debugMessage(const Token *tok, const std::string &msg) cons
unsigned int Function::initializedArgCount() const unsigned int Function::initializedArgCount() const
{ {
unsigned int count = 0; unsigned int count = 0;
std::list<Variable>::const_iterator var;
if (argDef->link() != argDef->next()) for (var = argumentList.begin(); var != argumentList.end(); ++var)
{ {
for (const Token *tok = argDef->next(); tok && tok->next() && tok->next() != argDef->link(); tok = tok->next()) if (var->hasDefault())
{ ++count;
if (tok->str() == "=")
count++;
}
} }
return count; return count;
@ -1219,6 +1217,7 @@ void Function::addArguments(const SymbolDatabase *symbolDatabase, const Scope *s
const Token *nameTok; const Token *nameTok;
bool isConstVar; bool isConstVar;
bool isArrayVar; bool isArrayVar;
bool hasDefault;
const Token *tok = arg->next(); const Token *tok = arg->next();
for (;;) for (;;)
{ {
@ -1227,6 +1226,7 @@ void Function::addArguments(const SymbolDatabase *symbolDatabase, const Scope *s
nameTok = NULL; nameTok = NULL;
isConstVar = bool(tok->str() == "const"); isConstVar = bool(tok->str() == "const");
isArrayVar = false; isArrayVar = false;
hasDefault = false;
while (tok->str() != "," && tok->str() != ")" && tok->str() != "=") while (tok->str() != "," && tok->str() != ")" && tok->str() != "=")
{ {
@ -1270,15 +1270,17 @@ void Function::addArguments(const SymbolDatabase *symbolDatabase, const Scope *s
bool isClassVar = startTok == endTok && !startTok->isStandardType(); bool isClassVar = startTok == endTok && !startTok->isStandardType();
argumentList.push_back(Variable(nameTok, startTok, endTok, count++, Argument, false, false, isConstVar, isClassVar, argType, functionScope, isArrayVar));
// skip default values // skip default values
if (tok->str() == "=") if (tok->str() == "=")
{ {
hasDefault = true;
while (tok->str() != "," && tok->str() != ")") while (tok->str() != "," && tok->str() != ")")
tok = tok->next(); tok = tok->next();
} }
argumentList.push_back(Variable(nameTok, startTok, endTok, count++, Argument, false, false, isConstVar, isClassVar, argType, functionScope, isArrayVar, hasDefault));
if (tok->str() == ")") if (tok->str() == ")")
break; break;
@ -1365,8 +1367,7 @@ Scope::hasDefaultConstructor() const
for (func = functionList.begin(); func != functionList.end(); ++func) for (func = functionList.begin(); func != functionList.end(); ++func)
{ {
if (func->type == Function::eConstructor && if (func->type == Function::eConstructor && func->argCount() == 0)
func->argDef->link() == func->argDef->next())
return true; return true;
} }
} }

View File

@ -46,11 +46,12 @@ class Variable
/** @brief flags mask used to access specific bit. */ /** @brief flags mask used to access specific bit. */
enum enum
{ {
fIsMutable = (1 << 0), /** @brief mutable variable */ fIsMutable = (1 << 0), /** @brief mutable variable */
fIsStatic = (1 << 1), /** @brief static variable */ fIsStatic = (1 << 1), /** @brief static variable */
fIsConst = (1 << 2), /** @brief const variable */ fIsConst = (1 << 2), /** @brief const variable */
fIsClass = (1 << 3), /** @brief user defined type */ fIsClass = (1 << 3), /** @brief user defined type */
fIsArray = (1 << 4) /** @brief array variable */ fIsArray = (1 << 4), /** @brief array variable */
fHasDefault = (1 << 5) /** @brief function argument with default value */
}; };
/** /**
@ -77,7 +78,7 @@ public:
Variable(const Token *name_, const Token *start_, const Token *end_, Variable(const Token *name_, const Token *start_, const Token *end_,
std::size_t index_, AccessControl access_, bool mutable_, std::size_t index_, AccessControl access_, bool mutable_,
bool static_, bool const_, bool class_, const Scope *type_, bool static_, bool const_, bool class_, const Scope *type_,
const Scope *scope_, bool array_) const Scope *scope_, bool array_, bool default_)
: _name(name_), : _name(name_),
_start(start_), _start(start_),
_end(end_), _end(end_),
@ -92,6 +93,7 @@ public:
setFlag(fIsConst, const_); setFlag(fIsConst, const_);
setFlag(fIsClass, class_); setFlag(fIsClass, class_);
setFlag(fIsArray, array_); setFlag(fIsArray, array_);
setFlag(fHasDefault, default_);
} }
/** /**
@ -266,6 +268,15 @@ public:
return getFlag(fIsArray); return getFlag(fIsArray);
} }
/**
* Does variable have a default value.
* @return true if has a default falue, false if not
*/
bool hasDefault() const
{
return getFlag(fHasDefault);
}
/** /**
* Get Scope pointer of known type. * Get Scope pointer of known type.
* @return pointer to type if known, NULL if not known * @return pointer to type if known, NULL if not known
@ -445,7 +456,7 @@ public:
{ {
varlist.push_back(Variable(token_, start_, end_, varlist.size(), varlist.push_back(Variable(token_, start_, end_, varlist.size(),
access_, mutable_, static_, const_, class_, access_, mutable_, static_, const_, class_,
type_, scope_, array_)); type_, scope_, array_, false));
} }
/** @brief initialize varlist */ /** @brief initialize varlist */