SymbolDatabase: Improved ValueType in range for loop

This commit is contained in:
Daniel Marjamäki 2019-10-23 11:52:00 +02:00
parent 2500964b58
commit f131a99ae3
2 changed files with 21 additions and 18 deletions

View File

@ -5247,22 +5247,14 @@ void SymbolDatabase::setValueType(Token *tok, const ValueType &valuetype)
break;
}
const Token *containerElementType = typeStart;
while (Token::Match(containerElementType, "%name%|::"))
containerElementType = containerElementType->next();
// Try to determine type of "auto" token.
// TODO: Get type better
bool setType = false;
ValueType autovt;
const Type *templateArgType = nullptr; // container element type / smart pointer type
if (Token::Match(containerElementType, "< %type% *| *| >")) {
if (parsedecl(containerElementType->next(), &autovt, mDefaultSignedness, mSettings)) {
setType = true;
templateArgType = containerElementType->next()->type();
}
} else if (mSettings->library.isSmartPointer(containerElementType->next())) {
const Token *smartPointerTypeTok = containerElementType->next();
if (vt2->containerTypeToken) {
if (mSettings->library.isSmartPointer(vt2->containerTypeToken)) {
const Token *smartPointerTypeTok = vt2->containerTypeToken;
while (Token::Match(smartPointerTypeTok, "%name%|::"))
smartPointerTypeTok = smartPointerTypeTok->next();
if (Token::Match(smartPointerTypeTok, "< %name% > >") && smartPointerTypeTok->next()->type()) {
@ -5271,6 +5263,11 @@ void SymbolDatabase::setValueType(Token *tok, const ValueType &valuetype)
autovt.smartPointerType = templateArgType;
autovt.type = ValueType::Type::NONSTD;
}
} else if (parsedecl(vt2->containerTypeToken, &autovt, mDefaultSignedness, mSettings)) {
setType = true;
templateArgType = vt2->containerTypeToken->type();
}
}
if (setType) {

View File

@ -6607,12 +6607,18 @@ private:
// Container (vector)
Settings set;
Library::Container vector;
vector.startPattern = "Vector";
vector.startPattern = "Vector <";
vector.startPattern2 = "Vector !!::";
vector.type_templateArgNo = 0;
vector.arrayLike_indexOp = true;
set.library.containers["Vector"] = vector;
Library::Container string;
string.startPattern = "test :: string";
string.startPattern2 = "test :: string !!::";
string.arrayLike_indexOp = string.stdStringLike = true;
set.library.containers["test::string"] = string;
ASSERT_EQUALS("signed int", typeOf("Vector<int> v; v[0]=3;", "[", "test.cpp", &set));
ASSERT_EQUALS("container(test :: string)", typeOf("void foo(Vector<test::string> v) { for (auto s: v) { x=s+s; } }", "s", "test.cpp", &set));
}
// new