Skip to content

Commit 0e2deb0

Browse files
committed
Fix #13593 Template not simplified with global scope operator
1 parent 54a9ba4 commit 0e2deb0

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

lib/templatesimplifier.cpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,19 @@ static bool areAllParamsTypes(const std::vector<const Token *> &params)
774774
});
775775
}
776776

777+
static bool isTemplateInstantion(const Token* tok)
778+
{
779+
if (!tok->isName())
780+
return false;
781+
if (Token::Match(tok->tokAt(-1), "%type% %name% ::|<"))
782+
return true;
783+
if (Token::Match(tok->tokAt(-2), "[,:] private|protected|public %name% ::|<"))
784+
return true;
785+
if (Token::Match(tok->tokAt(-1), "(|{|}|;|=|>|<<|:|.|*|&|return|<|,|!|[ %name% ::|<|("))
786+
return true;
787+
return Token::Match(tok->tokAt(-2), "(|{|}|;|=|<<|:|.|*|&|return|<|,|!|[ :: %name% ::|<|(");
788+
}
789+
777790
void TemplateSimplifier::getTemplateInstantiations()
778791
{
779792
std::multimap<std::string, const TokenAndName *> functionNameMap;
@@ -830,9 +843,7 @@ void TemplateSimplifier::getTemplateInstantiations()
830843
Token *tok2 = Token::findsimplematch(tok->tokAt(2), ";");
831844
if (tok2)
832845
tok = tok2;
833-
} else if (Token::Match(tok->previous(), "(|{|}|;|=|>|<<|:|.|*|&|return|<|,|!|[ %name% ::|<|(") ||
834-
Token::Match(tok->previous(), "%type% %name% ::|<") ||
835-
Token::Match(tok->tokAt(-2), "[,:] private|protected|public %name% ::|<")) {
846+
} else if (isTemplateInstantion(tok)) {
836847
if (!tok->scopeInfo())
837848
syntaxError(tok);
838849
std::string scopeName = tok->scopeInfo()->name;

test/testsimplifytemplate.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ class TestSimplifyTemplate : public TestFixture {
241241
TEST_CASE(template_namespace_10);
242242
TEST_CASE(template_namespace_11); // #7145
243243
TEST_CASE(template_namespace_12);
244+
TEST_CASE(template_namespace_13);
244245
TEST_CASE(template_pointer_type);
245246
TEST_CASE(template_array_type);
246247

@@ -5307,6 +5308,22 @@ class TestSimplifyTemplate : public TestFixture {
53075308
tok(code));
53085309
}
53095310

5311+
void template_namespace_13() {
5312+
const char code[] = "namespace N {\n" // #13593
5313+
" template<typename T>\n"
5314+
" struct S {\n"
5315+
" using U = T*;\n"
5316+
" };\n"
5317+
"}\n"
5318+
"::N::S<int>::U u;\n";
5319+
ASSERT_EQUALS("namespace N { "
5320+
"struct S<int> ; "
5321+
"} "
5322+
"int * u ; "
5323+
"struct N :: S<int> { } ;",
5324+
tok(code));
5325+
}
5326+
53105327
void template_pointer_type() {
53115328
const char code[] = "template<class T> void foo(const T x) {}\n"
53125329
"void bar() { foo<int*>(0); }";

0 commit comments

Comments
 (0)