Support range-based for-loop in SymbolDatabase

This commit is contained in:
PKEuS 2015-10-26 19:20:42 +01:00
parent 5add1d8901
commit dfdc0f6b99
2 changed files with 23 additions and 3 deletions

View File

@ -3038,16 +3038,19 @@ 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")
if (!localVarTok)
return false;
if (localVarTok->str() == "const")
localVarTok = localVarTok->next();
if (Token::Match(localVarTok, "%name% ;|=")) {
if (Token::Match(localVarTok, "%name% ;|=") || (localVarTok->varId() && localVarTok->strAt(1) == ":")) {
vartok = localVarTok;
typetok = localTypeTok;
} else if (Token::Match(localVarTok, "%name% )|[") && localVarTok->str() != "operator") {
vartok = localVarTok;
typetok = localTypeTok;
} else if (localVarTok && localVarTok->varId() && Token::Match(localVarTok, "%name% (|{") &&
} else if (localVarTok->varId() && Token::Match(localVarTok, "%name% (|{") &&
Token::Match(localVarTok->next()->link(), ")|} ;")) {
vartok = localVarTok;
typetok = localTypeTok;

View File

@ -129,6 +129,8 @@ private:
TEST_CASE(isVariableDeclarationRValueRef);
TEST_CASE(isVariableStlType);
TEST_CASE(rangeBasedFor);
TEST_CASE(arrayMemberVar1);
TEST_CASE(arrayMemberVar2);
TEST_CASE(arrayMemberVar3);
@ -674,6 +676,21 @@ private:
ASSERT(var.tokens()->tokAt(2)->scope() != 0);
}
void rangeBasedFor() {
GET_SYMBOL_DB("void reset() {\n"
" for(auto& e : array)\n"
" foo(e);\n"
"}");
ASSERT(db != nullptr);
if (!db)
return;
ASSERT(db->scopeList.back().type == Scope::eFor);
ASSERT_EQUALS(2, db->getVariableListSize());
const Variable* e = db->getVariableFromVarId(1);
ASSERT(e && e->isReference() && e->isLocal());
}
void isVariableStlType() {
{
reset();