Add union support to template simplifier. (#1398)
This commit is contained in:
parent
2bbe73e04a
commit
33b74a04ab
|
@ -421,7 +421,7 @@ bool TemplateSimplifier::removeTemplate(Token *tok)
|
||||||
else if (indentlevel >= 2 && tok2->str() == ">")
|
else if (indentlevel >= 2 && tok2->str() == ">")
|
||||||
--indentlevel;
|
--indentlevel;
|
||||||
|
|
||||||
else if (Token::Match(tok2, "> class|struct %name% [,)]")) {
|
else if (Token::Match(tok2, "> class|struct|union %name% [,)]")) {
|
||||||
tok2 = tok2->next();
|
tok2 = tok2->next();
|
||||||
Token::eraseTokens(tok, tok2);
|
Token::eraseTokens(tok, tok2);
|
||||||
deleteToken(tok);
|
deleteToken(tok);
|
||||||
|
@ -515,7 +515,7 @@ static void setScopeInfo(const Token *tok, std::list<ScopeInfo2> *scopeInfo)
|
||||||
{
|
{
|
||||||
while (tok->str() == "}" && !scopeInfo->empty() && tok == scopeInfo->back().bodyEnd)
|
while (tok->str() == "}" && !scopeInfo->empty() && tok == scopeInfo->back().bodyEnd)
|
||||||
scopeInfo->pop_back();
|
scopeInfo->pop_back();
|
||||||
if (!Token::Match(tok, "namespace|class|struct %name% {|:|::"))
|
if (!Token::Match(tok, "namespace|class|struct|union %name% {|:|::"))
|
||||||
return;
|
return;
|
||||||
tok = tok->next();
|
tok = tok->next();
|
||||||
std::string classname = tok->str();
|
std::string classname = tok->str();
|
||||||
|
@ -537,7 +537,7 @@ std::list<TemplateSimplifier::TokenAndName> TemplateSimplifier::getTemplateDecla
|
||||||
std::list<ScopeInfo2> scopeInfo;
|
std::list<ScopeInfo2> scopeInfo;
|
||||||
std::list<TokenAndName> declarations;
|
std::list<TokenAndName> declarations;
|
||||||
for (Token *tok = mTokenList.front(); tok; tok = tok->next()) {
|
for (Token *tok = mTokenList.front(); tok; tok = tok->next()) {
|
||||||
if (Token::Match(tok, "}|namespace|class|struct")) {
|
if (Token::Match(tok, "}|namespace|class|struct|union")) {
|
||||||
setScopeInfo(tok, &scopeInfo);
|
setScopeInfo(tok, &scopeInfo);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -577,7 +577,7 @@ void TemplateSimplifier::getTemplateInstantiations()
|
||||||
std::list<ScopeInfo2> scopeList;
|
std::list<ScopeInfo2> scopeList;
|
||||||
|
|
||||||
for (Token *tok = mTokenList.front(); tok; tok = tok->next()) {
|
for (Token *tok = mTokenList.front(); tok; tok = tok->next()) {
|
||||||
if (Token::Match(tok, "}|namespace|class|struct")) {
|
if (Token::Match(tok, "}|namespace|class|struct|union")) {
|
||||||
setScopeInfo(tok, &scopeList);
|
setScopeInfo(tok, &scopeList);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -679,7 +679,7 @@ void TemplateSimplifier::useDefaultArgumentValues()
|
||||||
|
|
||||||
// end of template parameters?
|
// end of template parameters?
|
||||||
if (tok->str() == ">") {
|
if (tok->str() == ">") {
|
||||||
if (Token::Match(tok, "> class|struct %name%"))
|
if (Token::Match(tok, "> class|struct|union %name%"))
|
||||||
classname = tok->strAt(2);
|
classname = tok->strAt(2);
|
||||||
if (templateParmDepth<2)
|
if (templateParmDepth<2)
|
||||||
break;
|
break;
|
||||||
|
@ -955,7 +955,7 @@ int TemplateSimplifier::getTemplateNamePosition(const Token *tok)
|
||||||
{
|
{
|
||||||
// get the position of the template name
|
// get the position of the template name
|
||||||
int namepos = 0, starAmpPossiblePosition = 0;
|
int namepos = 0, starAmpPossiblePosition = 0;
|
||||||
if (Token::Match(tok, "> class|struct %type% {|:|<"))
|
if (Token::Match(tok, "> class|struct|union %type% {|:|<"))
|
||||||
namepos = 2;
|
namepos = 2;
|
||||||
else if (Token::Match(tok, "> %type% *|&| %type% ("))
|
else if (Token::Match(tok, "> %type% *|&| %type% ("))
|
||||||
namepos = 2;
|
namepos = 2;
|
||||||
|
@ -993,7 +993,7 @@ void TemplateSimplifier::expandTemplate(
|
||||||
const Token *endOfTemplateDefinition = nullptr;
|
const Token *endOfTemplateDefinition = nullptr;
|
||||||
const Token * const templateDeclarationNameToken = templateDeclarationToken->tokAt(getTemplateNamePosition(templateDeclarationToken));
|
const Token * const templateDeclarationNameToken = templateDeclarationToken->tokAt(getTemplateNamePosition(templateDeclarationToken));
|
||||||
for (const Token *tok3 = mTokenList.front(); tok3; tok3 = tok3 ? tok3->next() : nullptr) {
|
for (const Token *tok3 = mTokenList.front(); tok3; tok3 = tok3 ? tok3->next() : nullptr) {
|
||||||
if (Token::Match(tok3, "}|namespace|class|struct")) {
|
if (Token::Match(tok3, "}|namespace|class|struct|union")) {
|
||||||
setScopeInfo(tok3, &scopeInfo);
|
setScopeInfo(tok3, &scopeInfo);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -1647,7 +1647,7 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
|
||||||
typeStringsUsedInTemplateInstantiation.push_back(tok3->str());
|
typeStringsUsedInTemplateInstantiation.push_back(tok3->str());
|
||||||
}
|
}
|
||||||
// add additional type information
|
// add additional type information
|
||||||
if (!constconst && !Token::Match(tok3, "class|struct|enum")) {
|
if (!constconst && !Token::Match(tok3, "class|struct|union|enum")) {
|
||||||
if (tok3->isUnsigned())
|
if (tok3->isUnsigned())
|
||||||
typeForNewName += "unsigned";
|
typeForNewName += "unsigned";
|
||||||
else if (tok3->isSigned())
|
else if (tok3->isSigned())
|
||||||
|
@ -1708,7 +1708,7 @@ void TemplateSimplifier::replaceTemplateUsage(Token * const instantiationToken,
|
||||||
std::list<ScopeInfo2> scopeInfo;
|
std::list<ScopeInfo2> scopeInfo;
|
||||||
std::list< std::pair<Token *, Token *> > removeTokens;
|
std::list< std::pair<Token *, Token *> > removeTokens;
|
||||||
for (Token *nameTok = instantiationToken; nameTok; nameTok = nameTok->next()) {
|
for (Token *nameTok = instantiationToken; nameTok; nameTok = nameTok->next()) {
|
||||||
if (Token::Match(nameTok, "}|namespace|class|struct")) {
|
if (Token::Match(nameTok, "}|namespace|class|struct|union")) {
|
||||||
setScopeInfo(nameTok, &scopeInfo);
|
setScopeInfo(nameTok, &scopeInfo);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,6 +105,7 @@ private:
|
||||||
TEST_CASE(template65); // #8321
|
TEST_CASE(template65); // #8321
|
||||||
TEST_CASE(template66); // #8725
|
TEST_CASE(template66); // #8725
|
||||||
TEST_CASE(template67); // #8122
|
TEST_CASE(template67); // #8122
|
||||||
|
TEST_CASE(template68); // union
|
||||||
TEST_CASE(template_specialization_1); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
|
TEST_CASE(template_specialization_1); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
|
||||||
TEST_CASE(template_specialization_2); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
|
TEST_CASE(template_specialization_2); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
|
||||||
TEST_CASE(template_enum); // #6299 Syntax error in complex enum declaration (including template)
|
TEST_CASE(template_enum); // #6299 Syntax error in complex enum declaration (including template)
|
||||||
|
@ -1234,6 +1235,19 @@ private:
|
||||||
ASSERT_EQUALS(expected, tok(code));
|
ASSERT_EQUALS(expected, tok(code));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void template68() {
|
||||||
|
const char code[] = "template <class T> union Fred {\n"
|
||||||
|
" char dummy[sizeof(T)];\n"
|
||||||
|
" T value;\n"
|
||||||
|
"};\n"
|
||||||
|
"Fred<int> fred;";
|
||||||
|
const char exp [] = "Fred<int> fred ; union Fred<int> { "
|
||||||
|
"char dummy [ 4 ] ; "
|
||||||
|
"int value ; "
|
||||||
|
"} ;";
|
||||||
|
ASSERT_EQUALS(exp, tok(code));
|
||||||
|
}
|
||||||
|
|
||||||
void template_specialization_1() { // #7868 - template specialization template <typename T> struct S<C<T>> {..};
|
void template_specialization_1() { // #7868 - template specialization template <typename T> struct S<C<T>> {..};
|
||||||
const char code[] = "template <typename T> struct C {};\n"
|
const char code[] = "template <typename T> struct C {};\n"
|
||||||
"template <typename T> struct S {a};\n"
|
"template <typename T> struct S {a};\n"
|
||||||
|
|
Loading…
Reference in New Issue