From 53a0760fdf47a9a2532e74da8c7192fb1989a5a0 Mon Sep 17 00:00:00 2001 From: shaneasd Date: Tue, 15 Sep 2020 00:44:50 +0800 Subject: [PATCH] Improve ast generation for templated function parameters (#2803) --- lib/checkunusedvar.cpp | 3 +++ lib/tokenlist.cpp | 15 +++++++++++++-- test/testtokenize.cpp | 6 ++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 6f4b3281f..b29b58756 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -1229,6 +1229,9 @@ void CheckUnusedVar::checkFunctionVariableUsage() const Token *expr = varDecl ? varDecl : tok->astOperand1(); + if (isInitialization) + expr = tok->previous(); + // Is variable in lhs a union member? if (tok->previous() && tok->previous()->variable() && tok->previous()->variable()->nameToken()->scope()->type == Scope::eUnion) continue; diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 64330324e..927da2c00 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -796,8 +796,19 @@ static void compileTerm(Token *&tok, AST_state& state) } } else if (!state.cpp || !Token::Match(tok, "new|delete %name%|*|&|::|(|[")) { tok = skipDecl(tok); - while (tok->next() && tok->next()->isName()) - tok = tok->next(); + bool repeat = true; + while (repeat) + { + repeat = false; + if (Token::Match(tok->next(), "%name%")) { + tok = tok->next(); + repeat = true; + } + if (Token::simpleMatch(tok->next(), "<") && Token::Match(tok->linkAt(1), "> %name%")) { + tok = tok->next()->link()->next(); + repeat = true; + } + } state.op.push(tok); if (Token::Match(tok, "%name% <") && tok->linkAt(1)) tok = tok->linkAt(1); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index bc7ceefa3..1575dcb25 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -494,6 +494,8 @@ private: TEST_CASE(noCrash1); TEST_CASE(noCrash2); + TEST_CASE(noCrash3); + // --check-config TEST_CASE(checkConfiguration); @@ -8519,6 +8521,10 @@ private: "template <> d::d(const d &) {}\n")); } + void noCrash3() { + ASSERT_NO_THROW(tokenizeAndStringify("void a(X x, typename Y1::Y2 y, Z z = []{});")); + } + void checkConfig(const char code[]) { errout.str("");