Get type from iterator (#4613)

This commit is contained in:
chrchr-github 2022-12-07 09:38:36 +01:00 committed by GitHub
parent 8bb5ac0efd
commit 8713dabe58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 5 deletions

View File

@ -300,7 +300,7 @@ std::string astCanonicalType(const Token *expr)
{
if (!expr)
return "";
std::pair<const Token*, const Token*> decl = Token::typeDecl(expr);
std::pair<const Token*, const Token*> decl = Token::typeDecl(expr, /*pointedToType*/ true);
if (decl.first && decl.second) {
std::string ret;
for (const Token *type = decl.first; Token::Match(type,"%name%|::") && type != decl.second; type = type->next()) {

View File

@ -2245,7 +2245,7 @@ const ::Type* Token::typeOf(const Token* tok, const Token** typeTok)
return nullptr;
}
std::pair<const Token*, const Token*> Token::typeDecl(const Token * tok)
std::pair<const Token*, const Token*> Token::typeDecl(const Token* tok, bool pointedToType)
{
if (!tok)
return {};
@ -2264,7 +2264,7 @@ std::pair<const Token*, const Token*> Token::typeDecl(const Token * tok)
std::pair<const Token*, const Token*> r = typeDecl(tok2);
if (r.first)
return r;
if (tok2->astOperand1() && Token::simpleMatch(tok2, "new")) {
if (pointedToType && tok2->astOperand1() && Token::simpleMatch(tok2, "new")) {
if (Token::simpleMatch(tok2->astOperand1(), "("))
return { tok2->next(), tok2->astOperand1() };
const Token* declEnd = nextAfterAstRightmostLeaf(tok2->astOperand1());
@ -2278,11 +2278,16 @@ std::pair<const Token*, const Token*> Token::typeDecl(const Token * tok)
if (vt && vt->containerTypeToken)
return { vt->containerTypeToken, vt->containerTypeToken->linkAt(-1) };
}
if (astIsSmartPointer(var->nameToken())) {
if (pointedToType && astIsSmartPointer(var->nameToken())) {
const ValueType* vt = var->valueType();
if (vt && vt->smartPointerTypeToken)
return { vt->smartPointerTypeToken, vt->smartPointerTypeToken->linkAt(-1) };
}
if (pointedToType && astIsIterator(var->nameToken())) {
const ValueType* vt = var->valueType();
if (vt && vt->containerTypeToken)
return { vt->containerTypeToken, vt->containerTypeToken->linkAt(-1) };
}
}
return {var->typeStartToken(), var->typeEndToken()->next()};
} else if (Token::simpleMatch(tok, "return")) {

View File

@ -1078,7 +1078,11 @@ public:
static const ::Type* typeOf(const Token* tok, const Token** typeTok = nullptr);
static std::pair<const Token*, const Token*> typeDecl(const Token * tok);
/**
* @return the declaration associated with this token.
* @param pointedToType return the pointed-to type?
*/
static std::pair<const Token*, const Token*> typeDecl(const Token* tok, bool pointedToType = false);
static std::string typeStr(const Token* tok);

View File

@ -1922,6 +1922,12 @@ private:
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f(std::vector<std::vector<int>>& v) {\n"
" auto it = v.begin();\n"
" it->push_back(1);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
settings.severity = severity_old;
settings.checkLibrary = false;
}