Repository navigation
Fix #15278 TemplateSimplifier: class templates nested in a namespace need one pass per level - #9082
tsobczynski wants to merge 1 commit into
Conversation
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
Regression: inside a namespace, a class template's use of its own member class template can now be resolved to a namespace-level template with the same name. In the code below, B<T> inside NS::A means NS::A::B. Base instantiates struct NS :: A<int> :: B<int> { int u ; }. With the PR, NS :: B<int> { int x ; } is instantiated instead and the member template B is left uninstantiated. rfind(" :: ") cuts the scope of the copied body from NS :: A down to NS, which drops the class scope. Before, the broken NS :: :: name matched nothing, so the usage was resolved correctly in the next pass, with the class scope included. The same wrong lookup already happens in base when there is no namespace, so the PR extends an existing bug to namespaces.
The fix itself works: the ticket's 11-level reproducer now reports the missing uninitvar, and the full testrunner passes.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | Member class template now resolved to a namespace-level template of the same name | AI review | lib/templatesimplifier.cpp:2304 |
🆕 New |
@tsobczynski, please fix, resolve or reply to the open and new comments: #1.
Testing
I built cppcheck and testrunner in pr/ and cppcheck in base/.
./testrunner TestSimplifyTemplate TestUninitVar TestTokenizerand the full./testrunner -q: 0 failures.- 11-level reproducer in the style of the ticket (
void f() { NS::A10<int> x; int y = x.m.m.m.m.m.m.m.m.m.m.t; }): base reports nothing; the PR reportserror: Uninitialized variable: x.m.m.m.m.m.m.m.m.m.m.t [uninitvar]. - Nested-scope variants (a nested class inside a class template in a namespace, a usage inside a member function, nested namespaces): same output with base and the PR.
Regression (scratch/t5.cpp):
namespace NS {
template<class T> struct B { T x; };
template<class T> struct A {
template<class U> struct B { U u; };
B<T> b;
};
}
int f() {
NS::A<int> a;
return a.b.u;
}Command: cppcheck --debug-normal t5.cpp
Base:
2: template < class T > struct B { T x@var1 ; } ;
3: struct NS :: A<int> {
4: struct B<int> ;
5: B<int> b@var5 ;
6: } ;
4: struct NS :: A<int> :: B<int> { int u@var6 ; } ;
PR:
2: struct B<int> ;
3: struct NS :: A<int> {
4: template < class U > struct B { U u@var4 ; } ;
5: B<int> b@var5 ;
6: } ;
2: struct NS :: B<int> { int x@var6 ; } ;
With the PR, b gets the namespace-level NS::B<int> (member x), so a.b.u no longer refers to a real member. If there is no namespace-level B (t6.cpp), both versions give the correct result. Without a namespace (t7.cpp), base already makes the same mistake. No checker diagnostic changed in this small example; the evidence is the simplified token output above.
Automated review of 01c5ec4 by Claude. The pull request was built and tested locally.
| // adjust for current scope | ||
| std::string token_scope = tok3->scopeInfo()->name; | ||
| const std::string::size_type end = token_scope.find_last_of(" :: "); | ||
| const std::string::size_type end = token_scope.rfind(" :: "); |
There was a problem hiding this comment.
AI review
With rfind, the scope of a usage in the copied body of NS::A becomes NS, which drops the class scope. A usage of a member class template is then looked up at namespace level:
namespace NS {
template<class T> struct B { T x; };
template<class T> struct A {
template<class U> struct B { U u; };
B<T> b;
};
}
NS::A<int> a;Base instantiates struct NS :: A<int> :: B<int> { int u ; }. The PR instantiates struct NS :: B<int> { int x ; } and leaves the member template B uninstantiated. Before, the broken NS :: :: name matched nothing, so the next pass resolved the usage with the class scope included. Without a namespace, base already gets this wrong, so this is an existing problem that the PR now extends to namespaces. The new scope should keep the instantiated class scope (NS :: A<int>) when looking up the name, or this case needs a test that shows it still works. Could you add such a test?
There was a problem hiding this comment.
Confirmed, thanks. Looking up from the class scope would not have been enough on its own: a template declared inside a class template is added to the template declarations only once the class template is instantiated, so NS :: A :: B is not there to be found while A is being expanded. Recording NS :: A<int> for every usage would bring #15278 back, because NS :: A1<int> :: A0 matches no declaration either.
The scope of a usage in the copied body is now resolved in this order:
- a template declared inside the class template being expanded, recorded in the scope of the new instantiation (
NS :: A<int>), where the copied member is found in the next pass; - the declaration found by the inner-to-outer lookup that
getTemplateInstantiationsuses, now shared asfindTemplateDeclaration; - the trimmed scope, as before.
Your example is template_namespace_15. The case without a namespace still goes wrong, as it does on main; I've left it out of this PR.
There was a problem hiding this comment.
The case without a namespace, which I left out of this PR, is now #15281.
|
Test results for commit 01c5ec4 (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9082/ Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong. |
01c5ec4 to
5e3f9f3
Compare
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
Regression: the fix in template_namespace_15 only holds while the class template is the only place that makes a B<int>. In NS::A, two uses that name different templates with the same arguments now lose the member template. When NS::A uses both its member B<T> and NS::B<T>, the PR creates only NS :: B<int> and leaves the member template B uninstantiated, so b gets the wrong type. Base got b right here. Likewise, B<T> b; Inner::B<T> c; no longer creates A<int>::Inner::B<int>, which base did create. Both seem to come from the new early lookup through findTemplateDeclaration: the namespace-level B<int> now exists first, and the member's B<int> from the next pass is then dropped.
The 11-level case from the ticket is fixed, and the earlier review point (C2) is fixed and has a test.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | Member class template resolved to a namespace-level template of the same name | AI review | lib/templatesimplifier.cpp:2304 |
✅ Fixed |
| 2 | Member template B left uninstantiated when NS::B or Inner::B is also used | AI review | lib/templatesimplifier.cpp:2366 |
🆕 New |
@tsobczynski, please fix, resolve or reply to the open and new comments: #2.
Testing
I built cppcheck and testrunner in pr/ and cppcheck in base/.
./testrunner TestSimplifyTemplateand the full./testrunner -q: 0 failures.- Variants with the same output in base and the PR:
- the member template used inside a member function body (
void f() { B<T> b; }) Inner::B<T>used alone, with and without a namespace-levelB- a
template<class U> friend struct B;declaration: both versions leaveBuninstantiated
- the member template used inside a member function body (
Regression 1: the member template is lost when NS::B<T> is also used (scratch/f6.cpp):
namespace NS {
template<class T> struct B { T x; };
template<class T> struct A {
template<class U> struct B { U u; };
B<T> b;
NS::B<T> c;
};
}
NS::A<int> a;Command: cppcheck --debug-normal f6.cpp
Base:
3: struct NS :: A<int> {
4: struct B<int> ;
5: B<int> b@var3 ;
6: NS :: B<int> c@var4 ;
7: } ;
4: struct NS :: A<int> :: B<int> { int u@var5 ; } ;
PR:
2: struct B<int> ;
3: struct NS :: A<int> {
4: template < class U > struct B { U u@var2 ; } ;
5: B<int> b@var3 ;
6: NS :: B<int> c@var4 ;
7: } ;
2: struct NS :: B<int> { int x@var5 ; } ;
Base gets b right and c wrong. The PR gets c right, but b now points to NS::B<int> and the member template is never instantiated.
Regression 2: a member template of a nested class is lost (scratch/f2.cpp):
namespace NS {
template<class T> struct B { T x; };
template<class T> struct A {
struct Inner { template<class U> struct B { U u; }; };
B<T> b;
Inner::B<T> c;
};
}
NS::A<int> a;- Base instantiates
struct NS :: A<int> :: Inner :: B<int> { int u ; }. - The PR instantiates only
struct NS :: B<int> { int x ; }and leavestemplate < class U > struct B { U u ; }insideInner, soc's type is never instantiated. - With only
Inner::B<T> c;and nob(f5.cpp), both versions are correct. - With checkers enabled (
g1.cpp, the same code withreturn a.c.u;), the PR addsuninitMemberVarNoCtorforb, which is now typed asNS::B<int>.
Automated review of 5e3f9f3 by Claude. The pull request was built and tested locally.
| // a member template of the new instantiation, which is declared in the next pass | ||
| const std::string instantiated = templateDeclaration.scope().empty() ? newName : (templateDeclaration.scope() + " :: " + newName); | ||
| scope = instantiated + memberScope.substr(generic.size()); | ||
| } else if (const TokenAndName *declaration = findTemplateDeclaration(tok3, scope, nullptr)) { |
There was a problem hiding this comment.
AI review
Resolving names through findTemplateDeclaration during the expansion creates the namespace-level B<int> in the same pass. The member's B<int>, which can only be resolved in the next pass, then appears to be dropped because the name is taken. So the template_namespace_15 case breaks as soon as the class template also uses NS::B<T>:
namespace NS {
template<class T> struct B { T x; };
template<class T> struct A {
template<class U> struct B { U u; };
B<T> b;
NS::B<T> c;
};
}
NS::A<int> a;- Base instantiates
struct NS :: A<int> :: B<int> { int u ; }, sobis right. - The PR instantiates only
struct NS :: B<int> { int x ; }and leavestemplate < class U > struct B { U u ; }uninstantiated insideA<int>.
The same happens with B<T> b; Inner::B<T> c; where Inner declares template<class U> struct B: base creates A<int> :: Inner :: B<int>, but the PR doesn't. Could you add these as tests?
There was a problem hiding this comment.
Confirmed, thanks. With this PR both examples create NS :: B<int> and not the other B<int>. On main it is the other way round, so neither creates both.
The other instantiation is lost in replaceTemplateUsage. When NS :: B<int> is expanded, the usage of the other template is recorded with a different full name, but the "fallback to just matching name" renames it anyway, and that template is then never instantiated. main avoids this only because it never instantiates NS::B in these examples. With that fallback removed, both instantiations are created in both examples and testrunner passes.
That fallback is Trac #15263, so I've kept its fix out of this PR. I've added your two examples as template_namespace_16 and template_namespace_17 with TODO_ASSERT_EQUALS, giving the output wanted once that ticket is fixed.
|
Test results for commit 5e3f9f3 (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9082/ Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong. |
…need one pass per level
cppcheck instantiates class templates in at most 10 passes. Inside a
namespace, each level of nesting cost a pass of its own, so from 11
levels the innermost template was never instantiated and bugs in it
went unreported.
When the simplifier copies a class template to make an instantiation
such as NS::A1<int>, it records each template the copy uses (A0<int>)
with the scope to find it in. It took that scope from the class's own
scope by dropping the last component with find_last_of(" :: "), which
finds the last space or colon character, not the separator. "NS :: A1"
became "NS ::", which names no scope, so the lookup failed and was
retried in the next pass.
rfind(" :: ") gives "NS". But the failed lookup had also hidden a case
that dropping the last component gets wrong, a member template with
the same name as a namespace template:
namespace NS {
template<class T> struct B { T x; };
template<class T> struct A {
template<class U> struct B { U u; };
B<T> b;
};
}
B<T> in A means the member A::B, but a lookup in NS finds NS::B. The
member can't be found during the expansion of A, because a template
nested in a class template is registered only once the class template
is instantiated. The retry in the next pass came after A<int>'s copy
of the member existed, which is why this used to come out right.
So the dropped-component scope is replaced by a lookup. A template
that the class template declares itself is looked up in the
instantiation (NS :: A<int>) in the next pass. Any other name is
resolved at once by the inner-to-outer search of
getTemplateInstantiations, now shared as findTemplateDeclaration. A
name with no declaration keeps the old scope.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5e3f9f3 to
fe7a1df
Compare
cppcheck instantiates class templates in at most 10 passes, so a template nested deeper than that is never analysed with concrete types, and bugs in it go unreported. Inside a namespace, each level of nesting costs a pass of its own, so 11 levels are enough:
A0<int>is never created, and the reproducer in #15278 misses anuninitvarthis way.The cause: when the simplifier copies a class template to make an instantiation such as
NS::A1<int>, it records each template the copy uses (A0<int>) with the scope to find it in. It takes that scope from the class's own scope by dropping the last component, usingfind_last_of(" :: "). That finds the last space or colon character, not the separator, soNS :: A1becomesNS ::. That names no scope, the lookup fails, and it is retried in the next pass: one pass per level.rfind(" :: ")givesNS, andA0<int>is found at once. But the failed lookup had also hidden a case that dropping the last component gets wrong, a member template with the same name as a namespace template:B<T>inAmeans the memberA::B, but a lookup inNSfindsNS::B. It came out right before only because the retry in the next pass happened afterA<int>'s copy of the member existed. The member can't be found any earlier, because the simplifier registers a template nested in a class template only once the class template is instantiated.So the PR replaces the dropped-component scope with a lookup:
NS :: A<int>), in the next pass;getTemplateInstantiations, whose search is moved intofindTemplateDeclaration);Tests:
template_namespace_14(the 11-level chain) andtemplate_namespace_15(the member template).🤖 Generated with Claude Code