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. |
…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>
01c5ec4 to
5e3f9f3
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