diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 0a87ba0ab..6ab7972f8 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -300,7 +300,7 @@ std::string astCanonicalType(const Token *expr) { if (!expr) return ""; - std::pair decl = Token::typeDecl(expr); + std::pair 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()) { diff --git a/lib/token.cpp b/lib/token.cpp index 3a0e4987b..8fb104c1c 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -2245,7 +2245,7 @@ const ::Type* Token::typeOf(const Token* tok, const Token** typeTok) return nullptr; } -std::pair Token::typeDecl(const Token * tok) +std::pair Token::typeDecl(const Token* tok, bool pointedToType) { if (!tok) return {}; @@ -2264,7 +2264,7 @@ std::pair Token::typeDecl(const Token * tok) std::pair 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 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")) { diff --git a/lib/token.h b/lib/token.h index f3a470edb..9db81a3db 100644 --- a/lib/token.h +++ b/lib/token.h @@ -1078,7 +1078,11 @@ public: static const ::Type* typeOf(const Token* tok, const Token** typeTok = nullptr); - static std::pair typeDecl(const Token * tok); + /** + * @return the declaration associated with this token. + * @param pointedToType return the pointed-to type? + */ + static std::pair typeDecl(const Token* tok, bool pointedToType = false); static std::string typeStr(const Token* tok); diff --git a/test/testfunctions.cpp b/test/testfunctions.cpp index c39663371..3eb35510d 100644 --- a/test/testfunctions.cpp +++ b/test/testfunctions.cpp @@ -1922,6 +1922,12 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + check("void f(std::vector>& v) {\n" + " auto it = v.begin();\n" + " it->push_back(1);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + settings.severity = severity_old; settings.checkLibrary = false; }