Add Variable::isStlType function

This commit is contained in:
Lucas Manuel Rodriguez 2014-01-28 11:44:56 -03:00
parent 16afbbac8e
commit 2048313915
4 changed files with 82 additions and 3 deletions

View File

@ -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) {

View File

@ -25,6 +25,7 @@
#include <list>
#include <vector>
#include <set>
#include <algorithm>
#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 <std::size_t array_length>
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<Dimension> _dimensions;
/** @brief true if variable is of STL type */
bool _stlType;
/** @brief fill in information, depending on Tokens given at instantiation */
void evaluate();
};

View File

@ -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<int> 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"

View File

@ -44,4 +44,4 @@ public:
}
};
#endif//ndef TestUtilsH
#endif // TestUtilsH