Parser; fixed ast and auto type deduction for c++17 braced init lists

This commit is contained in:
Daniel Marjamäki 2021-04-25 10:38:33 +02:00
parent 9ad7ab4263
commit 4f43dbf954
4 changed files with 24 additions and 0 deletions

View File

@ -5770,6 +5770,15 @@ void SymbolDatabase::setValueType(Token *tok, const ValueType &valuetype)
return;
}
// c++17 auto type deduction of braced init list
if (mIsCpp && mSettings->standards.cpp >= Standards::CPP17 && vt2 && Token::Match(parent->tokAt(-2), "auto %var% {")) {
Token *autoTok = parent->tokAt(-2);
setValueType(autoTok, *vt2);
setAutoTokenProperties(autoTok);
const_cast<Variable *>(parent->previous()->variable())->setValueType(*vt2);
return;
}
if (!vt1)
return;
if (parent->astOperand2() && !vt2)

View File

@ -1593,6 +1593,7 @@ static Token * createAstAtToken(Token *tok, bool cpp)
(cpp && tok->str() == "throw") ||
!tok->previous() ||
Token::Match(tok, "%name% %op%|(|[|.|::|<|?|;") ||
(cpp && Token::Match(tok, "%name% {") && iscpp11init(tok->next())) ||
Token::Match(tok->previous(), "[;{}] %cop%|++|--|( !!{") ||
Token::Match(tok->previous(), "[;{}] %num%|%str%|%char%")) {
if (cpp && (Token::Match(tok->tokAt(-2), "[;{}] new|delete %name%") || Token::Match(tok->tokAt(-3), "[;{}] :: new|delete %name%")))

View File

@ -468,6 +468,7 @@ private:
TEST_CASE(auto12); // #8993 - const std::string &x; auto y = x; if (y.empty()) ..
TEST_CASE(auto13);
TEST_CASE(auto14);
TEST_CASE(auto15); // C++17 auto deduction from braced-init-list
TEST_CASE(unionWithConstructor);
@ -8147,6 +8148,18 @@ private:
ASSERT(tok && !tok->valueType());
}
void auto15() {
GET_SYMBOL_DB("auto var1{3};\n"
"auto var2{4.0};");
ASSERT_EQUALS(3, db->variableList().size());
const Variable *var1 = db->variableList()[1];
ASSERT(var1->valueType());
ASSERT_EQUALS(ValueType::Type::INT, var1->valueType()->type);
const Variable *var2 = db->variableList()[2];
ASSERT(var2->valueType());
ASSERT_EQUALS(ValueType::Type::DOUBLE, var2->valueType()->type);
}
void unionWithConstructor() {
GET_SYMBOL_DB("union Fred {\n"
" Fred(int x) : i(x) { }\n"

View File

@ -5698,6 +5698,7 @@ private:
ASSERT_EQUALS("stdvector::", testAst("std::vector<std::vector<int>>{{},{}}"));
ASSERT_EQUALS("abR{{,P(,((", testAst("a(b(R{},{},P()));"));
ASSERT_EQUALS("f1{2{,3{,{x,(", testAst("f({{1},{2},{3}},x);"));
ASSERT_EQUALS("a1{ b2{", testAst("auto a{1}; auto b{2};"));
}
void astbrackets() { // []