From 2048313915ab1812d8acd83b7bb4bbd385322893 Mon Sep 17 00:00:00 2001 From: Lucas Manuel Rodriguez Date: Tue, 28 Jan 2014 11:44:56 -0300 Subject: [PATCH] Add Variable::isStlType function --- lib/symboldatabase.cpp | 4 +++- lib/symboldatabase.h | 34 +++++++++++++++++++++++++++- test/testsymboldatabase.cpp | 45 +++++++++++++++++++++++++++++++++++++ test/testutils.h | 2 +- 4 files changed, 82 insertions(+), 3 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index b459ea849..2bab7df7e 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -1064,8 +1064,10 @@ void Variable::evaluate() if (_name) setFlag(fIsArray, arrayDimensions(_dimensions, _name->next())); - if (_start) + if (_start) { setFlag(fIsClass, !_start->isStandardType() && !isPointer() && !isReference()); + _stlType = Token::Match(_start, "std ::"); + } if (_access == Argument) { tok = _name; if (!tok) { diff --git a/lib/symboldatabase.h b/lib/symboldatabase.h index 2a2cfd7ff..d9fb534b3 100644 --- a/lib/symboldatabase.h +++ b/lib/symboldatabase.h @@ -25,6 +25,7 @@ #include #include #include +#include #include "config.h" #include "token.h" @@ -160,7 +161,8 @@ public: _access(access_), _flags(0), _type(type_), - _scope(scope_) { + _scope(scope_), + _stlType(false) { evaluate(); } @@ -430,6 +432,33 @@ public: return _dimensions[index_].known; } + /** + * Checks if the variable is an STL type ('std::') + * E.g.: + * std::string s; + * ... + * sVar->isStlType() == true + * @return true if it is an stl type and its type matches any of the types in 'stlTypes' + */ + bool isStlType() const { + return _stlType; + } + + /** + * Checks if the variable is of any of the STL types passed as arguments ('std::') + * E.g.: + * std::string s; + * ... + * const char *str[] = {"string", "wstring"}; + * sVar->isStlType(str) == true + * @param stlTypes array of stl types in alphabetical order + * @return true if it is an stl type and its type matches any of the types in 'stlTypes' + */ + template + bool isStlType(const char* (&stlTypes)[array_length]) const { + return _stlType && std::binary_search(stlTypes, stlTypes + array_length, typeStartToken()->strAt(2)); + } + private: // only symbol database can change the type friend class SymbolDatabase; @@ -469,6 +498,9 @@ private: /** @brief array dimensions */ std::vector _dimensions; + /** @brief true if variable is of STL type */ + bool _stlType; + /** @brief fill in information, depending on Tokens given at instantiation */ void evaluate(); }; diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 17e4071ef..144f2efd4 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -119,6 +119,7 @@ private: TEST_CASE(isVariableDeclarationDoesNotIdentifyTemplateClass); TEST_CASE(isVariableDeclarationPointerConst); TEST_CASE(isVariableDeclarationRValueRef); + TEST_CASE(isVariableStlType); TEST_CASE(arrayMemberVar1); TEST_CASE(arrayMemberVar2); @@ -556,6 +557,50 @@ private: ASSERT(var.tokens()->tokAt(2)->scope() != 0); } + void isVariableStlType() { + { + reset(); + std::istringstream code("std::string s;"); + TokenList list(NULL); + list.createTokens(code, "test.cpp"); + bool result = si.isVariableDeclaration(list.front(), vartok, typetok); + ASSERT_EQUALS(true, result); + Variable v(vartok, list.front(), list.back(), 0, Public, 0, 0); + const char* types[] = { "string", "wstring" }; + const char* no_types[] = { "set" }; + ASSERT_EQUALS(true, v.isStlType()); + ASSERT_EQUALS(true, v.isStlType(types)); + ASSERT_EQUALS(false, v.isStlType(no_types)); + } + { + reset(); + std::istringstream code("std::vector v;"); + TokenList list(NULL); + list.createTokens(code, "test.cpp"); + list.front()->tokAt(3)->link(list.front()->tokAt(5)); + bool result = si.isVariableDeclaration(list.front(), vartok, typetok); + ASSERT_EQUALS(true, result); + Variable v(vartok, list.front(), list.back(), 0, Public, 0, 0); + const char* types[] = { "bitset", "set", "vector", "wstring" }; + const char* no_types[] = { "bitset", "map", "set" }; + ASSERT_EQUALS(true, v.isStlType()); + ASSERT_EQUALS(true, v.isStlType(types)); + ASSERT_EQUALS(false, v.isStlType(no_types)); + } + { + reset(); + std::istringstream code("SomeClass s;"); + TokenList list(NULL); + list.createTokens(code, "test.cpp"); + bool result = si.isVariableDeclaration(list.front(), vartok, typetok); + ASSERT_EQUALS(true, result); + Variable v(vartok, list.front(), list.back(), 0, Public, 0, 0); + const char* types[] = { "bitset", "set", "vector" }; + ASSERT_EQUALS(false, v.isStlType()); + ASSERT_EQUALS(false, v.isStlType(types)); + } + } + void arrayMemberVar1() { const char code[] = "struct Foo {\n" " int x;\n" diff --git a/test/testutils.h b/test/testutils.h index e61febfe8..d7401978e 100644 --- a/test/testutils.h +++ b/test/testutils.h @@ -44,4 +44,4 @@ public: } }; -#endif//ndef TestUtilsH +#endif // TestUtilsH