From dfdc0f6b99956e5bc91cfdeae34f4e5b5110a558 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Mon, 26 Oct 2015 19:20:42 +0100 Subject: [PATCH] Support range-based for-loop in SymbolDatabase --- lib/symboldatabase.cpp | 9 ++++++--- test/testsymboldatabase.cpp | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 11ff790cf..313ebaa94 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -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; diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 07bf1ed93..0ef1609cf 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -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();