From 1dee3b04b9e8af2cf5da29b1e0c2304e54adf252 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Mon, 14 May 2012 12:47:02 -0700 Subject: [PATCH] Bugfixes in SymbolDatabase: - Constant pointers are now detected as variable declarations - Probably fixed #3802 --- lib/symboldatabase.cpp | 12 +++++++++--- test/testsymboldatabase.cpp | 32 ++++++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index b00efefa9..006d7a2e8 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -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; diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 681554318..5c87fc445 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -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() {