Skip to content

Commit

Permalink
SymbolDatabase: Update some properties for auto variables
Browse files Browse the repository at this point in the history
  • Loading branch information
danmar committed Feb 27, 2017
1 parent 994f6b6 commit beaf29c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2418,6 +2418,15 @@ bool Variable::arrayDimensions(const Library* lib)
return arr;
}

void Variable::setFlags(const ValueType &valuetype)
{
if (valuetype.constness)
setFlag(fIsConst,true);
if (valuetype.pointer)
setFlag(fIsPointer,true);
}


static std::ostream & operator << (std::ostream & s, Scope::ScopeType type)
{
s << (type == Scope::eGlobal ? "Global" :
Expand Down Expand Up @@ -4404,6 +4413,13 @@ static void setValueType(Token *tok, const Enumerator &enumerator, bool cpp, Val
}
}

static void setAutoTokenProperties(Token * const autoTok)
{
const ValueType *valuetype = autoTok->valueType();
if (valuetype->isIntegral() || valuetype->isFloat())
autoTok->isStandardType(true);
}

static void setValueType(Token *tok, const ValueType &valuetype, bool cpp, ValueType::Sign defaultSignedness, const Settings* settings)
{
tok->setValueType(new ValueType(valuetype));
Expand Down Expand Up @@ -4434,8 +4450,10 @@ static void setValueType(Token *tok, const ValueType &valuetype, bool cpp, Value
autoTok = var1Tok->tokAt(-2);
if (autoTok) {
setValueType(autoTok, *vt2, cpp, defaultSignedness, settings);
setAutoTokenProperties(autoTok);
setValueType(var1Tok, *vt2, cpp, defaultSignedness, settings);
setValueType(parent->previous(), *vt2, cpp, defaultSignedness, settings);
const_cast<Variable *>(parent->previous()->variable())->setFlags(*vt2);
}
}
return;
Expand Down Expand Up @@ -4500,7 +4518,9 @@ static void setValueType(Token *tok, const ValueType &valuetype, bool cpp, Value
if (isconst)
vt.constness |= 1;
setValueType(autoToken, vt, cpp, defaultSignedness, settings);
setAutoTokenProperties(autoToken);
setValueType(parent->previous(), vt, cpp, defaultSignedness, settings);
const_cast<Variable *>(parent->previous()->variable())->setFlags(vt);
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/symboldatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,8 @@ class CPPCHECKLIB Variable {
return type() && type()->isEnumType();
}

void setFlags(const ValueType &valuetype);

private:
// only symbol database can change the type
friend class SymbolDatabase;
Expand Down
10 changes: 10 additions & 0 deletions test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,8 @@ class TestSymbolDatabase: public TestFixture {
TEST_CASE(variadic3); // #7387

TEST_CASE(noReturnType);

TEST_CASE(auto1);
}

void array() {
Expand Down Expand Up @@ -4347,6 +4349,14 @@ class TestSymbolDatabase: public TestFixture {
}
}
}

void auto1() {
GET_SYMBOL_DB("; auto x = \"abc\";");
const Token *autotok = tokenizer.tokens()->next();
ASSERT(autotok && autotok->isStandardType());
const Variable *var = db ? db->getVariableFromVarId(1) : nullptr;
ASSERT(var && var->isPointer() && var->isConst());
}
};

REGISTER_TEST(TestSymbolDatabase)

0 comments on commit beaf29c

Please sign in to comment.