Add support for auto variables in regular for loop.

This commit is contained in:
Robert Reif 2017-04-21 15:25:02 -04:00
parent 5c7b1c7ff2
commit 78ad9825af
2 changed files with 31 additions and 5 deletions

View File

@ -4621,9 +4621,9 @@ void SymbolDatabase::setValueType(Token *tok, const ValueType &valuetype)
Token::Match(parent->tokAt(-1), "%var% ="))) { Token::Match(parent->tokAt(-1), "%var% ="))) {
Token *var1Tok = parent->strAt(-2) == ";" ? parent->tokAt(-3) : parent->tokAt(-1); Token *var1Tok = parent->strAt(-2) == ";" ? parent->tokAt(-3) : parent->tokAt(-1);
Token *autoTok = nullptr; Token *autoTok = nullptr;
if (Token::Match(var1Tok->tokAt(-2), ";|{|}|const auto")) if (Token::Match(var1Tok->tokAt(-2), ";|{|}|(|const auto"))
autoTok = var1Tok->previous(); autoTok = var1Tok->previous();
else if (Token::Match(var1Tok->tokAt(-3), ";|{|}|const auto *")) else if (Token::Match(var1Tok->tokAt(-3), ";|{|}|(|const auto *"))
autoTok = var1Tok->tokAt(-2); autoTok = var1Tok->tokAt(-2);
if (autoTok) { if (autoTok) {
ValueType vt(*vt2); ValueType vt(*vt2);
@ -4635,11 +4635,11 @@ void SymbolDatabase::setValueType(Token *tok, const ValueType &valuetype)
setAutoTokenProperties(autoTok); setAutoTokenProperties(autoTok);
setValueType(var1Tok, *vt2); setValueType(var1Tok, *vt2);
setValueType(parent->previous(), *vt2); setValueType(parent->previous(), *vt2);
const Variable *var = parent->previous()->variable(); Variable *var = const_cast<Variable *>(parent->previous()->variable());
if (var) { if (var) {
const_cast<Variable *>(var)->setFlags(*vt2); var->setFlags(*vt2);
if (vt2->typeScope && vt2->typeScope->definedType) { if (vt2->typeScope && vt2->typeScope->definedType) {
const_cast<Variable *>(var)->type(vt2->typeScope->definedType); var->type(vt2->typeScope->definedType);
if (autoTok->valueType()->pointer == 0) if (autoTok->valueType()->pointer == 0)
autoTok->type(vt2->typeScope->definedType); autoTok->type(vt2->typeScope->definedType);
} }

View File

@ -328,6 +328,7 @@ private:
TEST_CASE(auto5); TEST_CASE(auto5);
TEST_CASE(auto6); // #7963 (segmentation fault) TEST_CASE(auto6); // #7963 (segmentation fault)
TEST_CASE(auto7); TEST_CASE(auto7);
TEST_CASE(auto8);
} }
void array() { void array() {
@ -5111,6 +5112,31 @@ private:
} }
} }
void auto8() {
GET_SYMBOL_DB("std::vector<int> vec;\n"
"void foo() {\n"
" for (auto it = vec.begin(); it != vec.end(); ++it) { }\n"
"}");
const Token *autotok = Token::findsimplematch(tokenizer.tokens(), "auto it");
ASSERT(db && autotok && autotok->valueType());
if (db && autotok && autotok->valueType()) {
ASSERT_EQUALS(0, autotok->valueType()->constness);
ASSERT_EQUALS(0, autotok->valueType()->pointer);
ASSERT_EQUALS(ValueType::UNKNOWN_SIGN, autotok->valueType()->sign);
ASSERT_EQUALS(ValueType::ITERATOR, autotok->valueType()->type);
}
vartok = Token::findsimplematch(autotok, "it =");
ASSERT(db && vartok && vartok->valueType());
if (db && vartok && vartok->valueType()) {
ASSERT_EQUALS(0, vartok->valueType()->constness);
ASSERT_EQUALS(0, vartok->valueType()->pointer);
ASSERT_EQUALS(ValueType::UNKNOWN_SIGN, vartok->valueType()->sign);
ASSERT_EQUALS(ValueType::ITERATOR, vartok->valueType()->type);
}
}
}; };
REGISTER_TEST(TestSymbolDatabase) REGISTER_TEST(TestSymbolDatabase)