From 5b8840c6b02bd648dc2b75e60145474ae12f8e67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 27 Apr 2012 18:02:07 +0200 Subject: [PATCH] Fixed #3764 (Tokenizer::setVarId: no varid for templated variables with const/struct/union types) --- lib/templatesimplifier.cpp | 8 ++++++-- test/testsimplifytokens.cpp | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 22d7db9b2..817f46967 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -180,6 +180,10 @@ unsigned int TemplateSimplifier::templateParameters(const Token *tok) if (level == 0) ++numberOfParameters; + // skip struct/union + if (Token::Match(tok, "struct|union|const %any%")) + tok = tok->next(); + // skip std:: if (tok->str() == "::") tok = tok->next(); @@ -195,8 +199,8 @@ unsigned int TemplateSimplifier::templateParameters(const Token *tok) if (!tok) return 0; - // optional "*" - if (tok->str() == "*") + // * / const + while (Token::Match(tok, "*|const")) tok = tok->next(); // Function pointer.. diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index d719b0b0c..95081e678 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -22,6 +22,7 @@ #include "tokenize.h" #include "token.h" #include "settings.h" +#include "templatesimplifier.h" #include #include @@ -128,6 +129,9 @@ private: TEST_CASE(template_typename); TEST_CASE(template_constructor); // #3152 - template constructor is removed + // Test TemplateSimplifier::templateParameters + TEST_CASE(templateParameters); + TEST_CASE(namespaces); // Assignment in condition.. @@ -2271,6 +2275,25 @@ private: ASSERT_EQUALS("class Fred { Fred ( T t ) { } }", tok(code2)); } + unsigned int templateParameters(const char code[]) { + Settings settings; + Tokenizer tokenizer(&settings, this); + + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + + return TemplateSimplifier::templateParameters(tokenizer.tokens()); + } + + void templateParameters() { + // Test that the function TemplateSimplifier::templateParameters works + ASSERT_EQUALS(1U, templateParameters(" x;")); + ASSERT_EQUALS(1U, templateParameters(" x;")); + ASSERT_EQUALS(1U, templateParameters(" x;")); + ASSERT_EQUALS(1U, templateParameters(" x;")); + } + + void namespaces() { { const char code[] = "using namespace std; namespace a{ namespace b{ void f(){} } }";