Skip to content

Commit 1625761

Browse files
committed
Revert "Fixed danmar#6183 (TemplateSimplifier: Does not handle methods) (danmar#1540)"
This reverts commit 7875555.
1 parent b3e6ecd commit 1625761

File tree

4 files changed

+39
-228
lines changed

4 files changed

+39
-228
lines changed

lib/templatesimplifier.cpp

Lines changed: 38 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,6 @@ bool TemplateSimplifier::getTemplateDeclarations()
497497
tok2 = tok2->link();
498498
else if (tok2->str() == ")")
499499
break;
500-
// skip decltype(...)
501-
else if (Token::Match(tok2, "decltype ("))
502-
tok2 = tok2->linkAt(1);
503500
// Declaration => add to mTemplateForwardDeclarations
504501
else if (tok2->str() == ";") {
505502
const int namepos = getTemplateNamePosition(parmEnd, true);
@@ -894,14 +891,7 @@ bool TemplateSimplifier::getTemplateNamePositionTemplateFunction(const Token *to
894891
while (tok && tok->next()) {
895892
if (Token::Match(tok->next(), ";|{"))
896893
return false;
897-
// skip decltype(...)
898-
else if (Token::Match(tok, "decltype (")) {
899-
const Token * end = tok->linkAt(1);
900-
while (tok && tok != end) {
901-
tok = tok->next();
902-
namepos++;
903-
}
904-
} else if (Token::Match(tok->next(), "%type% <")) {
894+
else if (Token::Match(tok->next(), "%type% <")) {
905895
const Token *closing = tok->tokAt(2)->findClosingBracket();
906896
if (closing) {
907897
if (closing->strAt(1) == "(" && mTokenizer->isFunctionHead(closing->tokAt(2), ";|{|:"))
@@ -994,19 +984,15 @@ void TemplateSimplifier::expandTemplate(
994984
const Token *endOfTemplateDefinition = nullptr;
995985
const Token * const templateDeclarationNameToken = templateDeclarationToken->tokAt(getTemplateNamePosition(templateDeclarationToken));
996986
const bool isClass = Token::Match(templateDeclarationToken->next(), "class|struct|union %name% <|{|:");
997-
const bool isFunction = templateDeclarationNameToken->strAt(1) == "(" ||
998-
(templateDeclarationNameToken->strAt(1) == "<" && templateDeclarationNameToken->next()->findClosingBracket()->strAt(1) == "(");
999-
const bool isSpecialization = Token::Match(templateDeclaration.token, "template < >");
987+
const bool isFunction = templateDeclarationNameToken->strAt(1) == "(";
1000988

1001989
// add forward declarations
1002990
if (copy && isClass) {
1003991
templateDeclaration.token->insertToken(templateDeclarationToken->strAt(1), "", true);
1004992
templateDeclaration.token->insertToken(newName, "", true);
1005993
templateDeclaration.token->insertToken(";", "", true);
1006-
} else if (isFunction && (copy || (!copy && isSpecialization))) {
994+
} else if (copy && isFunction) {
1007995
Token * dst = templateDeclaration.token;
1008-
bool isStatic = false;
1009-
std::string scope;
1010996
Token * start;
1011997
Token * end;
1012998
auto it = mTemplateForwardDeclarationsMap.find(dst);
@@ -1017,21 +1003,8 @@ void TemplateSimplifier::expandTemplate(
10171003
start = temp1->next();
10181004
end = temp2->linkAt(1)->next();
10191005
} else {
1020-
auto it2 = mTemplateUserSpecializationMap.find(dst);
1021-
if (it2 != mTemplateUserSpecializationMap.end()) {
1022-
dst = it2->second;
1023-
isStatic = dst->next()->findClosingBracket()->strAt(1) == "static";
1024-
const Token * temp = templateDeclarationNameToken;
1025-
while (Token::Match(temp->tokAt(-2), "%name% ::")) {
1026-
scope.insert(0, temp->strAt(-2) + " :: ");
1027-
temp = temp->tokAt(-2);
1028-
}
1029-
}
10301006
start = templateDeclarationToken->next();
1031-
if (templateDeclarationNameToken->strAt(1) == "(")
1032-
end = templateDeclarationNameToken->linkAt(1)->next();
1033-
else
1034-
end = templateDeclarationNameToken->next()->findClosingBracket()->linkAt(1)->next();
1007+
end = templateDeclarationNameToken->linkAt(1)->next();
10351008
}
10361009
unsigned int typeindentlevel = 0;
10371010
while (!(typeindentlevel == 0 && Token::Match(end, ";|{|:"))) {
@@ -1042,9 +1015,6 @@ void TemplateSimplifier::expandTemplate(
10421015
end = end->next();
10431016
}
10441017

1045-
if (isStatic)
1046-
dst->insertToken("static", "", true);
1047-
10481018
std::map<const Token *, Token *> links;
10491019
while (start && start != end) {
10501020
unsigned int itype = 0;
@@ -1071,11 +1041,7 @@ void TemplateSimplifier::expandTemplate(
10711041
dst->previous()->isLong(typetok->isLong());
10721042
}
10731043
} else {
1074-
if (isSpecialization && !copy && !scope.empty() && Token::Match(start, (scope + templateDeclarationNameToken->str()).c_str())) {
1075-
// skip scope
1076-
while (start->strAt(1) != templateDeclarationNameToken->str())
1077-
start = start->next();
1078-
} else if (start->str() == templateDeclarationNameToken->str()) {
1044+
if (start->str() == templateDeclarationNameToken->str()) {
10791045
dst->insertToken(newName, "", true);
10801046
if (start->strAt(1) == "<")
10811047
start = start->next()->findClosingBracket();
@@ -1167,8 +1133,6 @@ void TemplateSimplifier::expandTemplate(
11671133
// Start of template..
11681134
if (tok3 == templateDeclarationToken) {
11691135
tok3 = tok3->next();
1170-
if (tok3->str() == "static")
1171-
tok3 = tok3->next();
11721136
}
11731137

11741138
// member function implemented outside class definition
@@ -1943,12 +1907,23 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
19431907
return false;
19441908

19451909
// already simplified
1946-
if (!Token::Match(tok2, "%name% <"))
1910+
if (!Token::Match(startToken, "%name% <"))
19471911
return false;
19481912

1949-
if (!matchSpecialization(tok->tokAt(namepos), tok2, specializations))
1913+
if (templateDeclaration.scope.empty()) {
1914+
if (startToken->str() != templateDeclaration.name)
1915+
return false;
1916+
} else {
1917+
std::string name = templateDeclaration.scope + " :: " + startToken->str();
1918+
if (name != templateDeclaration.name)
1919+
return false;
1920+
}
1921+
1922+
if (!matchSpecialization(tok->tokAt(namepos), startToken, specializations))
19501923
return false;
19511924

1925+
tok2 = startToken;
1926+
19521927
// New type..
19531928
mTypesUsedInTemplateInstantiation.clear();
19541929
std::list<std::string> typeStringsUsedInTemplateInstantiation;
@@ -2076,61 +2051,6 @@ void TemplateSimplifier::replaceTemplateUsage(
20762051
}
20772052
}
20782053

2079-
static std::string getPathName(const TemplateSimplifier::TokenAndName & decl)
2080-
{
2081-
std::string name = decl.scope;
2082-
if (!name.empty())
2083-
name += " :: ";
2084-
if (decl.nameToken->strAt(-1) == "::") {
2085-
const Token * start = decl.nameToken;
2086-
2087-
while (Token::Match(start->tokAt(-2), "%name% ::"))
2088-
start = start->tokAt(-2);
2089-
2090-
while (start != decl.nameToken) {
2091-
name += (start->str() + " ");
2092-
start = start->next();
2093-
}
2094-
}
2095-
name += decl.name;
2096-
return name;
2097-
}
2098-
2099-
void TemplateSimplifier::getUserDefinedSpecializations()
2100-
{
2101-
// try to locate a matching declaration for each user defined specialization
2102-
for (auto & spec : mTemplateDeclarations) {
2103-
if (Token::Match(spec.token, "template < >")) {
2104-
std::string specName = getPathName(spec);
2105-
2106-
bool found = false;
2107-
for (auto & decl : mTemplateDeclarations) {
2108-
if (Token::Match(decl.token, "template < >"))
2109-
continue;
2110-
std::string declName = getPathName(decl);
2111-
2112-
// make sure the scopes and names match
2113-
if (specName == declName) {
2114-
// @todo make sure function parameters also match
2115-
mTemplateUserSpecializationMap[spec.token] = decl.token;
2116-
}
2117-
}
2118-
2119-
if (!found) {
2120-
for (auto & decl : mTemplateForwardDeclarations) {
2121-
std::string declName = getPathName(decl);
2122-
2123-
// make sure the scopes and names match
2124-
if (specName == declName) {
2125-
// @todo make sure function parameters also match
2126-
mTemplateUserSpecializationMap[spec.token] = decl.token;
2127-
}
2128-
}
2129-
}
2130-
}
2131-
}
2132-
}
2133-
21342054
void TemplateSimplifier::fixForwardDeclaredDefaultArgumentValues()
21352055
{
21362056
// try to locate a matching declaration for each forward declaration
@@ -2146,8 +2066,26 @@ void TemplateSimplifier::fixForwardDeclaredDefaultArgumentValues()
21462066

21472067
// make sure the number of arguments match
21482068
if (params1.size() == params2.size()) {
2149-
std::string declName = getPathName(decl);
2150-
std::string forwardDeclName = getPathName(forwardDecl);
2069+
std::string declName = decl.scope;
2070+
if (!declName.empty())
2071+
declName += " :: ";
2072+
if (decl.nameToken->strAt(-1) == "::") {
2073+
const Token * start = decl.nameToken;
2074+
2075+
while (Token::Match(start->tokAt(-2), "%name% ::"))
2076+
start = start->tokAt(-2);
2077+
2078+
while (start != decl.nameToken) {
2079+
declName += (start->str() + " ");
2080+
start = start->next();
2081+
}
2082+
}
2083+
declName += decl.name;
2084+
2085+
std::string forwardDeclName = forwardDecl.scope;
2086+
if (!forwardDeclName.empty())
2087+
forwardDeclName += " :: ";
2088+
forwardDeclName += forwardDecl.name;
21512089

21522090
// make sure the scopes and names match
21532091
if (forwardDeclName == declName) {
@@ -2186,7 +2124,6 @@ void TemplateSimplifier::simplifyTemplates(
21862124
mTemplateDeclarations.clear();
21872125
mTemplateForwardDeclarations.clear();
21882126
mTemplateForwardDeclarationsMap.clear();
2189-
mTemplateUserSpecializationMap.clear();
21902127
mTemplateInstantiations.clear();
21912128
mInstantiatedTemplates.clear();
21922129
mExplicitInstantiationsToDelete.clear();
@@ -2220,9 +2157,6 @@ void TemplateSimplifier::simplifyTemplates(
22202157
// Copy default argument values from forward declaration to declaration
22212158
fixForwardDeclaredDefaultArgumentValues();
22222159

2223-
// Locate user defined specializations.
2224-
getUserDefinedSpecializations();
2225-
22262160
// Locate possible instantiations of templates..
22272161
getTemplateInstantiations();
22282162

lib/templatesimplifier.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,6 @@ class CPPCHECKLIB TemplateSimplifier {
156156
*/
157157
void useDefaultArgumentValues();
158158

159-
/**
160-
* Try to locate a matching declaration for each user defined
161-
* specialization.
162-
*/
163-
void getUserDefinedSpecializations();
164-
165159
/**
166160
* simplify template aliases
167161
*/
@@ -284,7 +278,6 @@ class CPPCHECKLIB TemplateSimplifier {
284278
std::list<TokenAndName> mTemplateDeclarations;
285279
std::list<TokenAndName> mTemplateForwardDeclarations;
286280
std::map<Token *, Token *> mTemplateForwardDeclarationsMap;
287-
std::map<Token *, Token *> mTemplateUserSpecializationMap;
288281
std::list<TokenAndName> mTemplateInstantiations;
289282
std::list<TokenAndName> mInstantiatedTemplates;
290283
std::list<TokenAndName> mMemberFunctionsToDelete;

test/testsimplifytemplate.cpp

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,6 @@ class TestSimplifyTemplate : public TestFixture {
125125
TEST_CASE(template85); // #8902 crash
126126
TEST_CASE(template86); // crash
127127
TEST_CASE(template87);
128-
TEST_CASE(template88); // #6183
129-
TEST_CASE(template89); // #8917
130-
TEST_CASE(template90); // crash
131128
TEST_CASE(template_specialization_1); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
132129
TEST_CASE(template_specialization_2); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
133130
TEST_CASE(template_enum); // #6299 Syntax error in complex enum declaration (including template)
@@ -482,8 +479,7 @@ class TestSimplifyTemplate : public TestFixture {
482479
"}\n";
483480

484481
// The expected result..
485-
const char expected[] = "void foo<int*> ( ) ; "
486-
"void foo<int*> ( ) "
482+
const char expected[] = "void foo<int*> ( ) "
487483
"{ x ( ) ; } "
488484
"int main ( ) "
489485
"{ foo<int*> ( ) ; }";
@@ -509,7 +505,6 @@ class TestSimplifyTemplate : public TestFixture {
509505
// The expected result..
510506
const char expected[] = "void a<2> ( ) ; "
511507
"void a<1> ( ) ; "
512-
"void a<0> ( ) ; "
513508
"void a<0> ( ) { } "
514509
"int main ( ) "
515510
"{ a<2> ( ) ; return 0 ; } "
@@ -1669,92 +1664,6 @@ class TestSimplifyTemplate : public TestFixture {
16691664
ASSERT_EQUALS(exp, tok(code));
16701665
}
16711666

1672-
void template88() { // #6183.cpp
1673-
const char code[] = "class CTest {\n"
1674-
"public:\n"
1675-
" template <typename T>\n"
1676-
" static void Greeting(T val) {\n"
1677-
" std::cout << val << std::endl;\n"
1678-
" }\n"
1679-
"private:\n"
1680-
" static void SayHello() {\n"
1681-
" std::cout << \"Hello World!\" << std::endl;\n"
1682-
" }\n"
1683-
"};\n"
1684-
"template<>\n"
1685-
"void CTest::Greeting(bool) {\n"
1686-
" CTest::SayHello();\n"
1687-
"}\n"
1688-
"int main() {\n"
1689-
" CTest::Greeting<bool>(true);\n"
1690-
" return 0;\n"
1691-
"}";
1692-
const char exp[] = "class CTest { "
1693-
"public: "
1694-
"static void Greeting<bool> ( bool ) ; "
1695-
"template < typename T > "
1696-
"static void Greeting ( T val ) { "
1697-
"std :: cout << val << std :: endl ; "
1698-
"} "
1699-
"private: "
1700-
"static void SayHello ( ) { "
1701-
"std :: cout << \"Hello World!\" << std :: endl ; "
1702-
"} "
1703-
"} ; "
1704-
"void CTest :: Greeting<bool> ( bool ) { "
1705-
"CTest :: SayHello ( ) ; "
1706-
"} "
1707-
"int main ( ) { "
1708-
"CTest :: Greeting<bool> ( true ) ; "
1709-
"return 0 ; "
1710-
"}";
1711-
ASSERT_EQUALS(exp, tok(code));
1712-
}
1713-
1714-
void template89() { // #8917
1715-
const char code[] = "struct Fred {\n"
1716-
" template <typename T> static void foo() { }\n"
1717-
"};\n"
1718-
"template void Fred::foo<char>();\n"
1719-
"template void Fred::foo<float>();\n"
1720-
"template <> void Fred::foo<bool>() { }\n"
1721-
"template <> void Fred::foo<int>() { }";
1722-
const char exp[] = "struct Fred { "
1723-
"static void foo<char> ( ) ; "
1724-
"static void foo<float> ( ) ; "
1725-
"static void foo<bool> ( ) ; "
1726-
"static void foo<int> ( ) ; "
1727-
"} ; "
1728-
"void Fred :: foo<char> ( ) { } "
1729-
"void Fred :: foo<float> ( ) { } "
1730-
"void Fred :: foo<bool> ( ) { } "
1731-
"void Fred :: foo<int> ( ) { }";
1732-
const char act[] = "struct Fred { "
1733-
"static void foo<char> ( ) ; "
1734-
"static void foo<float> ( ) ; "
1735-
"} ; "
1736-
"void Fred :: foo<bool> ( ) ; "
1737-
"void Fred :: foo<bool> ( ) { } "
1738-
"void Fred :: foo<int> ( ) ; "
1739-
"void Fred :: foo<int> ( ) { } "
1740-
"void Fred :: foo<char> ( ) { } "
1741-
"void Fred :: foo<float> ( ) { }";
1742-
TODO_ASSERT_EQUALS(exp, act, tok(code));
1743-
}
1744-
1745-
void template90() { // crash
1746-
const char code[] = "template <typename T> struct S1 {};\n"
1747-
"void f(S1<double>) {}\n"
1748-
"template <typename T>\n"
1749-
"decltype(S1<T>().~S1<T>()) fun1() {};";
1750-
const char exp[] = "struct S1<double> ; "
1751-
"void f ( S1<double> ) { } "
1752-
"template < typename T > "
1753-
"decltype ( S1 < T > ( ) . ~ S1 < T > ( ) ) fun1 ( ) { } ; "
1754-
"struct S1<double> { } ;";
1755-
ASSERT_EQUALS(exp, tok(code));
1756-
}
1757-
17581667
void template_specialization_1() { // #7868 - template specialization template <typename T> struct S<C<T>> {..};
17591668
const char code[] = "template <typename T> struct C {};\n"
17601669
"template <typename T> struct S {a};\n"

0 commit comments

Comments
 (0)