Fixed #3764 (Tokenizer::setVarId: no varid for templated variables with const/struct/union types)

This commit is contained in:
Daniel Marjamäki 2012-04-27 18:02:07 +02:00
parent f1511dd795
commit 5b8840c6b0
2 changed files with 29 additions and 2 deletions

View File

@ -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..

View File

@ -22,6 +22,7 @@
#include "tokenize.h"
#include "token.h"
#include "settings.h"
#include "templatesimplifier.h"
#include <sstream>
#include <list>
@ -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("<struct C> x;"));
ASSERT_EQUALS(1U, templateParameters("<union C> x;"));
ASSERT_EQUALS(1U, templateParameters("<const int> x;"));
ASSERT_EQUALS(1U, templateParameters("<int const *> x;"));
}
void namespaces() {
{
const char code[] = "using namespace std; namespace a{ namespace b{ void f(){} } }";