Fixed #1563 (false positive: function can be const (assignment to static))

This commit is contained in:
Robert Reif 2010-04-02 08:02:47 +02:00 committed by Daniel Marjamäki
parent 1dcbf02bd8
commit 3507b06e0b
3 changed files with 28 additions and 8 deletions

View File

@ -100,10 +100,6 @@ CheckClass::Var *CheckClass::getVarList(const Token *tok1, bool withClasses, boo
if (next->str().find(":") != std::string::npos) if (next->str().find(":") != std::string::npos)
continue; continue;
// Variable declarations that start with "static" shall be ignored..
if (next->str() == "static")
continue;
// Borland C++: Ignore properties.. // Borland C++: Ignore properties..
if (next->str() == "__property") if (next->str() == "__property")
continue; continue;
@ -112,6 +108,14 @@ CheckClass::Var *CheckClass::getVarList(const Token *tok1, bool withClasses, boo
if (next->str() == "typedef") if (next->str() == "typedef")
continue; continue;
// Is it a static variable?
bool isStatic = false;
if (next->str() == "static")
{
isStatic = true;
next = next->next();
}
// Is it a mutable variable? // Is it a mutable variable?
bool isMutable = false; bool isMutable = false;
if (next->str() == "mutable") if (next->str() == "mutable")
@ -185,7 +189,7 @@ CheckClass::Var *CheckClass::getVarList(const Token *tok1, bool withClasses, boo
// If the varname was set in one of the two if-block above, create a entry for this variable.. // If the varname was set in one of the two if-block above, create a entry for this variable..
if (!varname.empty() && varname != "operator") if (!varname.empty() && varname != "operator")
{ {
Var *var = new Var(varname, false, priv, isMutable, varlist); Var *var = new Var(varname, false, priv, isMutable, isStatic, varlist);
varlist = var; varlist = var;
} }
} }
@ -507,7 +511,7 @@ void CheckClass::constructors()
// If there is a private variable, there should be a constructor.. // If there is a private variable, there should be a constructor..
for (const Var *var = varlist; var; var = var->next) for (const Var *var = varlist; var; var = var->next)
{ {
if (var->priv) if (var->priv && !var->isStatic)
{ {
noConstructorError(tok1, classNameToken->str(), isStruct); noConstructorError(tok1, classNameToken->str(), isStruct);
break; break;
@ -586,7 +590,7 @@ void CheckClass::checkConstructors(const Token *tok1, const std::string &funcnam
if (classNameUsed) if (classNameUsed)
operatorEqVarError(constructor_token, className, var->name); operatorEqVarError(constructor_token, className, var->name);
} }
else else if (!var->isStatic)
uninitVarError(constructor_token, className, var->name, hasPrivateConstructor); uninitVarError(constructor_token, className, var->name, hasPrivateConstructor);
} }

View File

@ -113,11 +113,12 @@ private:
class Var class Var
{ {
public: public:
Var(const std::string &name_, bool init_ = false, bool priv_ = false, bool mutable_ = false, Var *next_ = 0) Var(const std::string &name_, bool init_ = false, bool priv_ = false, bool mutable_ = false, bool static_ = false, Var *next_ = 0)
: name(name_), : name(name_),
init(init_), init(init_),
priv(priv_), priv(priv_),
isMutable(mutable_), isMutable(mutable_),
isStatic(static_),
next(next_) next(next_)
{ {
} }
@ -134,6 +135,9 @@ private:
/** @brief is this variable mutable? */ /** @brief is this variable mutable? */
bool isMutable; bool isMutable;
/** @brief is this variable static? */
bool isStatic;
/** @brief next Var item */ /** @brief next Var item */
Var *next; Var *next;
}; };

View File

@ -110,6 +110,7 @@ private:
TEST_CASE(const15); TEST_CASE(const15);
TEST_CASE(const16); // ticket #1551 TEST_CASE(const16); // ticket #1551
TEST_CASE(const17); // ticket #1552 TEST_CASE(const17); // ticket #1552
TEST_CASE(const18); // ticket #1563
TEST_CASE(constoperator); // operator< can often be const TEST_CASE(constoperator); // operator< can often be const
TEST_CASE(constincdec); // increment/decrement => non-const TEST_CASE(constincdec); // increment/decrement => non-const
TEST_CASE(constReturnReference); TEST_CASE(constReturnReference);
@ -2817,6 +2818,17 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void const18()
{
// ticket #1563
checkConst("class Fred {\n"
"static int x;\n"
"public:\n"
" void set(int i) { x = i; }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
// increment/decrement => not const // increment/decrement => not const
void constincdec() void constincdec()
{ {