Skip to content

Commit b4cb250

Browse files
committed
SymbolDatabase: Add Variable::valueType(). First step to reuse ValueType handling in Variable
1 parent cfddc8f commit b4cb250

3 files changed

Lines changed: 85 additions & 77 deletions

File tree

lib/symboldatabase.cpp

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
365365
scope->definedTypesMap[new_type->name()] = new_type;
366366
}
367367

368-
scope->addVariable(varNameTok, tok, tok, access[scope], new_scope->definedType, scope, &mSettings->library);
368+
scope->addVariable(varNameTok, tok, tok, access[scope], new_scope->definedType, scope, mSettings);
369369

370370
const Token *tok2 = tok->next();
371371

@@ -637,9 +637,9 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
637637
scope->nestedList.push_back(&scopeList.back());
638638
scope = &scopeList.back();
639639
if (scope->type == Scope::eFor)
640-
scope->checkVariable(tok->tokAt(2), Local, &mSettings->library); // check for variable declaration and add it to new scope if found
640+
scope->checkVariable(tok->tokAt(2), Local, mSettings); // check for variable declaration and add it to new scope if found
641641
else if (scope->type == Scope::eCatch)
642-
scope->checkVariable(tok->tokAt(2), Throw, &mSettings->library); // check for variable declaration and add it to new scope if found
642+
scope->checkVariable(tok->tokAt(2), Throw, mSettings); // check for variable declaration and add it to new scope if found
643643
tok = scopeStartTok;
644644
} else if (tok->str() == "{") {
645645
if (tok->previous()->varId())
@@ -717,7 +717,7 @@ void SymbolDatabase::createSymbolDatabaseVariableInfo()
717717
// fill in variable info
718718
for (std::list<Scope>::iterator it = scopeList.begin(); it != scopeList.end(); ++it) {
719719
// find variables
720-
it->getVariableList(&mSettings->library);
720+
it->getVariableList(mSettings);
721721
}
722722

723723
// fill in function arguments
@@ -1598,6 +1598,11 @@ void SymbolDatabase::validate() const
15981598
//validateVariables();
15991599
}
16001600

1601+
Variable::~Variable()
1602+
{
1603+
delete mValueType;
1604+
}
1605+
16011606
bool Variable::isPointerArray() const
16021607
{
16031608
return isArray() && nameToken() && nameToken()->previous() && (nameToken()->previous()->str() == "*");
@@ -1614,14 +1619,16 @@ const Token * Variable::declEndToken() const
16141619
return declEnd;
16151620
}
16161621

1617-
void Variable::evaluate(const Library* lib)
1622+
void Variable::evaluate(const Settings* settings)
16181623
{
1619-
unsigned int pointer = 0;
1620-
mConstness = 0;
1624+
const Library * const lib = settings ? &settings->library : nullptr;
16211625

16221626
if (mNameToken)
16231627
setFlag(fIsArray, arrayDimensions(lib));
16241628

1629+
if (mTypeStartToken)
1630+
setValueType(ValueType::parseDecl(mTypeStartToken,settings));
1631+
16251632
const Token* tok = mTypeStartToken;
16261633
while (tok && tok->previous() && tok->previous()->isName())
16271634
tok = tok->previous();
@@ -1637,13 +1644,11 @@ void Variable::evaluate(const Library* lib)
16371644
setFlag(fIsVolatile, true);
16381645
else if (tok->str() == "mutable")
16391646
setFlag(fIsMutable, true);
1640-
else if (tok->str() == "const") {
1647+
else if (tok->str() == "const")
16411648
setFlag(fIsConst, true);
1642-
mConstness |= 1 << pointer;
1643-
} else if (tok->str() == "*") {
1649+
else if (tok->str() == "*") {
16441650
setFlag(fIsPointer, !isArray() || Token::Match(tok->previous(), "( * %name% )"));
16451651
setFlag(fIsConst, false); // Points to const, isn't necessarily const itself
1646-
++pointer;
16471652
} else if (tok->str() == "&") {
16481653
if (isReference())
16491654
setFlag(fIsRValueRef, true);
@@ -1708,6 +1713,15 @@ void Variable::evaluate(const Library* lib)
17081713
}
17091714
}
17101715

1716+
void Variable::setValueType(const ValueType &valueType)
1717+
{
1718+
delete mValueType;
1719+
mValueType = new ValueType(valueType);
1720+
if ((mValueType->pointer > 0) && (!isArray() || Token::Match(mNameToken->previous(), "( * %name% )")))
1721+
setFlag(fIsPointer, true);
1722+
setFlag(fIsConst, mValueType->constness & (1U << mValueType->pointer));
1723+
}
1724+
17111725
Function::Function(const Tokenizer *mTokenizer, const Token *tok, const Scope *scope, const Token *tokDef, const Token *tokArgDef)
17121726
: tokenDef(tokDef),
17131727
argDef(tokArgDef),
@@ -2503,15 +2517,6 @@ bool Variable::arrayDimensions(const Library* lib)
25032517
return arr;
25042518
}
25052519

2506-
void Variable::setFlags(const ValueType &valuetype)
2507-
{
2508-
if (valuetype.constness)
2509-
setFlag(fIsConst,true);
2510-
if (valuetype.pointer)
2511-
setFlag(fIsPointer,true);
2512-
}
2513-
2514-
25152520
static std::ostream & operator << (std::ostream & s, Scope::ScopeType type)
25162521
{
25172522
s << (type == Scope::eGlobal ? "Global" :
@@ -3006,7 +3011,6 @@ void SymbolDatabase::printXml(std::ostream &out) const
30063011
out << " isPointer=\"" << var->isPointer() << '\"';
30073012
out << " isReference=\"" << var->isReference() << '\"';
30083013
out << " isStatic=\"" << var->isStatic() << '\"';
3009-
out << " constness=\"" << var->constness() << '\"';
30103014
out << "/>" << std::endl;
30113015
}
30123016
out << " </variables>" << std::endl;
@@ -3123,7 +3127,7 @@ void Function::addArguments(const SymbolDatabase *symbolDatabase, const Scope *s
31233127
while (Token::Match(startTok, "enum|struct|const|volatile"))
31243128
startTok = startTok->next();
31253129

3126-
argumentList.emplace_back(nameTok, startTok, endTok, count++, Argument, argType, functionScope, &symbolDatabase->mSettings->library);
3130+
argumentList.emplace_back(nameTok, startTok, endTok, count++, Argument, argType, functionScope, symbolDatabase->mSettings);
31273131

31283132
if (tok->str() == ")") {
31293133
// check for a variadic function
@@ -3325,7 +3329,7 @@ AccessControl Scope::defaultAccess() const
33253329
}
33263330

33273331
// Get variable list..
3328-
void Scope::getVariableList(const Library* lib)
3332+
void Scope::getVariableList(const Settings* settings)
33293333
{
33303334
const Token *start;
33313335

@@ -3426,14 +3430,14 @@ void Scope::getVariableList(const Library* lib)
34263430
else if (tok->str() == ";")
34273431
continue;
34283432

3429-
tok = checkVariable(tok, varaccess, lib);
3433+
tok = checkVariable(tok, varaccess, settings);
34303434

34313435
if (!tok)
34323436
break;
34333437
}
34343438
}
34353439

3436-
const Token *Scope::checkVariable(const Token *tok, AccessControl varaccess, const Library* lib)
3440+
const Token *Scope::checkVariable(const Token *tok, AccessControl varaccess, const Settings* settings)
34373441
{
34383442
// Is it a throw..?
34393443
if (Token::Match(tok, "throw %any% (") &&
@@ -3494,7 +3498,7 @@ const Token *Scope::checkVariable(const Token *tok, AccessControl varaccess, con
34943498
if (Token::Match(typestart, "enum|struct"))
34953499
typestart = typestart->next();
34963500

3497-
addVariable(vartok, typestart, vartok->previous(), varaccess, vType, this, lib);
3501+
addVariable(vartok, typestart, vartok->previous(), varaccess, vType, this, settings);
34983502
}
34993503

35003504
return tok;
@@ -4872,7 +4876,7 @@ void SymbolDatabase::setValueType(Token *tok, const ValueType &valuetype)
48724876
setValueType(parent->previous(), *vt2);
48734877
Variable *var = const_cast<Variable *>(parent->previous()->variable());
48744878
if (var) {
4875-
var->setFlags(*vt2);
4879+
var->setValueType(*vt2);
48764880
if (vt2->typeScope && vt2->typeScope->definedType) {
48774881
var->type(vt2->typeScope->definedType);
48784882
if (autoTok->valueType()->pointer == 0)
@@ -4956,7 +4960,7 @@ void SymbolDatabase::setValueType(Token *tok, const ValueType &valuetype)
49564960
setValueType(parent->previous(), varvt);
49574961
Variable *var = const_cast<Variable *>(parent->previous()->variable());
49584962
if (var) {
4959-
var->setFlags(varvt);
4963+
var->setValueType(varvt);
49604964
if (vt2->typeScope && vt2->typeScope->definedType) {
49614965
var->type(vt2->typeScope->definedType);
49624966
autoToken->type(vt2->typeScope->definedType);
@@ -4985,7 +4989,7 @@ void SymbolDatabase::setValueType(Token *tok, const ValueType &valuetype)
49854989
setValueType(parent->previous(), varvt);
49864990
Variable * var = const_cast<Variable *>(parent->previous()->variable());
49874991
if (var) {
4988-
var->setFlags(varvt);
4992+
var->setValueType(varvt);
49894993
const Type * type = typeStart->tokAt(4)->type();
49904994
if (type && type->classScope && type->classScope->definedType) {
49914995
autoToken->type(type->classScope->definedType);

lib/symboldatabase.h

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ class CPPCHECKLIB Variable {
221221
public:
222222
Variable(const Token *name_, const Token *start_, const Token *end_,
223223
std::size_t index_, AccessControl access_, const Type *type_,
224-
const Scope *scope_, const Library* lib)
224+
const Scope *scope_, const Settings* settings)
225225
: mNameToken(name_),
226226
mTypeStartToken(start_),
227227
mTypeEndToken(end_),
@@ -230,10 +230,13 @@ class CPPCHECKLIB Variable {
230230
mFlags(0),
231231
mConstness(0),
232232
mType(type_),
233-
mScope(scope_) {
234-
evaluate(lib);
233+
mScope(scope_),
234+
mValueType(nullptr) {
235+
evaluate(settings);
235236
}
236237

238+
~Variable();
239+
237240
/**
238241
* Get name token.
239242
* @return name token
@@ -597,12 +600,12 @@ class CPPCHECKLIB Variable {
597600
return type() && type()->isEnumType();
598601
}
599602

600-
void setFlags(const ValueType &valuetype);
601-
602-
unsigned int constness() const {
603-
return mConstness;
603+
const ValueType *valueType() const {
604+
return mValueType;
604605
}
605606

607+
void setValueType(const ValueType &valueType);
608+
606609
private:
607610
// only symbol database can change the type
608611
friend class SymbolDatabase;
@@ -642,11 +645,13 @@ class CPPCHECKLIB Variable {
642645
/** @brief pointer to scope this variable is in */
643646
const Scope *mScope;
644647

648+
ValueType *mValueType;
649+
645650
/** @brief array dimensions */
646651
std::vector<Dimension> mDimensions;
647652

648653
/** @brief fill in information, depending on Tokens given at instantiation */
649-
void evaluate(const Library* lib);
654+
void evaluate(const Settings* settings);
650655
};
651656

652657
class CPPCHECKLIB Function {
@@ -1008,14 +1013,14 @@ class CPPCHECKLIB Scope {
10081013

10091014
void addVariable(const Token *token_, const Token *start_,
10101015
const Token *end_, AccessControl access_, const Type *type_,
1011-
const Scope *scope_, const Library* lib) {
1016+
const Scope *scope_, const Settings* settings) {
10121017
varlist.emplace_back(token_, start_, end_, varlist.size(),
10131018
access_,
1014-
type_, scope_, lib);
1019+
type_, scope_, settings);
10151020
}
10161021

10171022
/** @brief initialize varlist */
1018-
void getVariableList(const Library* lib);
1023+
void getVariableList(const Settings* settings);
10191024

10201025
const Function *getDestructor() const;
10211026

@@ -1035,10 +1040,10 @@ class CPPCHECKLIB Scope {
10351040
* @brief check if statement is variable declaration and add it if it is
10361041
* @param tok pointer to start of statement
10371042
* @param varaccess access control of statement
1038-
* @param lib Library instance
1043+
* @param settings Settings
10391044
* @return pointer to last token
10401045
*/
1041-
const Token *checkVariable(const Token *tok, AccessControl varaccess, const Library* lib);
1046+
const Token *checkVariable(const Token *tok, AccessControl varaccess, const Settings* settings);
10421047

10431048
/**
10441049
* @brief get variable from name

0 commit comments

Comments
 (0)