Bugfixes in SymbolDatabase:

- Constant pointers are now detected as variable declarations
- Probably fixed #3802
This commit is contained in:
PKEuS 2012-05-14 12:47:02 -07:00
parent eacf74be8d
commit 1dee3b04b9
2 changed files with 35 additions and 9 deletions

View File

@ -811,9 +811,7 @@ void Variable::evaluate()
const Token* tok = _start;
if (tok && tok->previous() && tok->previous()->isName())
tok = tok->previous();
for (; tok != _name; tok = tok->next()) {
if (tok->str() == "<")
tok->findClosingBracket(tok);
for (const Token* const end = _name?_name:_end; tok != end;) {
if (tok->str() == "static")
setFlag(fIsStatic, true);
else if (tok->str() == "mutable")
@ -825,6 +823,11 @@ void Variable::evaluate()
setFlag(fIsConst, false); // Points to const, isn't necessarily const itself
} else if (tok->str() == "&")
setFlag(fIsReference, true);
if (tok->str() == "<")
tok->findClosingBracket(tok);
else
tok = tok->next();
}
if (_name)
@ -1995,6 +1998,9 @@ bool Scope::isVariableDeclaration(const Token* tok, const Token*& vartok, const
localVarTok = skipPointers(localTypeTok->strAt(1)=="const"?localTypeTok->tokAt(2):localTypeTok->next());
}
if (localVarTok && localVarTok->str() == "const")
localVarTok = localVarTok->next();
if (Token::Match(localVarTok, "%var% ;|=")) {
vartok = localVarTok;
typetok = localTypeTok;

View File

@ -222,14 +222,34 @@ private:
void test_isVariableDeclarationIdentifiesPointers() {
reset();
givenACodeSampleToTokenize pointer("int* p;");
bool result = si.isVariableDeclaration(pointer.tokens(), vartok, typetok);
ASSERT_EQUALS(true, result);
bool result1 = si.isVariableDeclaration(pointer.tokens(), vartok, typetok);
ASSERT_EQUALS(true, result1);
ASSERT_EQUALS("p", vartok->str());
ASSERT_EQUALS("int", typetok->str());
Variable v(vartok, typetok, vartok->previous(), 0, Public, 0, 0);
ASSERT(false == v.isArray());
ASSERT(true == v.isPointer());
ASSERT(false == v.isReference());
Variable v1(vartok, typetok, vartok->previous(), 0, Public, 0, 0);
ASSERT(false == v1.isArray());
ASSERT(true == v1.isPointer());
ASSERT(false == v1.isReference());
reset();
givenACodeSampleToTokenize constpointer("const int* p;");
Variable v2(constpointer.tokens()->tokAt(3), constpointer.tokens()->next(), constpointer.tokens()->tokAt(2), 0, Public, 0, 0);
ASSERT(false == v2.isArray());
ASSERT(true == v2.isPointer());
ASSERT(false == v2.isConst());
ASSERT(false == v2.isReference());
reset();
givenACodeSampleToTokenize pointerconst("int* const p;");
bool result2 = si.isVariableDeclaration(pointerconst.tokens(), vartok, typetok);
ASSERT_EQUALS(true, result2);
ASSERT_EQUALS("p", vartok->str());
ASSERT_EQUALS("int", typetok->str());
Variable v3(vartok, typetok, vartok->previous(), 0, Public, 0, 0);
ASSERT(false == v3.isArray());
ASSERT(true == v3.isPointer());
ASSERT(true == v3.isConst());
ASSERT(false == v3.isReference());
}
void test_isVariableDeclarationDoesNotIdentifyConstness() {