Skip to content

Commit 6db418b

Browse files
committed
Fix FP
1 parent d65d1b8 commit 6db418b

2 files changed

Lines changed: 96 additions & 2 deletions

File tree

‎lib/templatesimplifier.cpp‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,28 @@ namespace {
11381138
};
11391139
} // namespace
11401140

1141+
// The class scope that a member call "obj . f ( x )" looks the member name up in: the
1142+
// class of the object expression, or of the pointee for "ptr -> f ( x )". nullptr when the
1143+
// type of the object is not known (unknown type, container, ...).
1144+
static const Scope* memberCallObjectScope(const Token* dotTok)
1145+
{
1146+
const Token* objectTok = dotTok->astOperand1();
1147+
const ValueType* objectType = objectTok ? objectTok->valueType() : nullptr;
1148+
if (!objectType)
1149+
return nullptr;
1150+
const bool isArrow = dotTok->originalName() == "->";
1151+
if (objectType->smartPointerType) {
1152+
if (!isArrow)
1153+
return nullptr; // a member of the smart pointer class itself
1154+
const Scope* classScope = objectType->smartPointerType->classScope;
1155+
return (classScope && classScope->isClassOrStructOrUnion()) ? classScope : nullptr;
1156+
}
1157+
if (objectType->pointer != (isArrow ? 1 : 0))
1158+
return nullptr;
1159+
const Scope* typeScope = objectType->typeScope;
1160+
return (typeScope && typeScope->isClassOrStructOrUnion()) ? typeScope : nullptr;
1161+
}
1162+
11411163
// Insert the tokens of a deduced type after tok
11421164
static void insertDeducedType(Token* tok, const DeducedType& deduced)
11431165
{
@@ -1239,8 +1261,16 @@ std::string TemplateSimplifier::deduceFunctionTemplateArguments(
12391261
std::vector<const Token*> instantiationArgs;
12401262
getFunctionArguments(tok, instantiationArgs);
12411263

1264+
// a member call "obj . f ( x )" or "obj . A :: f ( x )": the name is a member of the
1265+
// class of the object expression, not a name visible at the call site
1266+
const Token* beforeName = tok;
1267+
while (Token::Match(beforeName->tokAt(-2), "%name% ::"))
1268+
beforeName = beforeName->tokAt(-2);
1269+
beforeName = beforeName->previous();
1270+
const bool isMemberCall = beforeName && beforeName->str() == ".";
1271+
12421272
// deduction from literal arguments - works without type information
1243-
for (auto pos = range.first; pos != range.second; ++pos) {
1273+
for (auto pos = range.first; !isMemberCall && pos != range.second; ++pos) {
12441274
// look for declaration with same qualification or constructor with same qualification
12451275
if (pos->second->fullName() == fullName ||
12461276
(pos->second->scope() == fullName && tok->str() == pos->second->name())) {
@@ -1363,7 +1393,13 @@ std::string TemplateSimplifier::deduceFunctionTemplateArguments(
13631393
CallLookupLevels lookup;
13641394
if (!qualification.empty())
13651395
lookup.levels.push_back(LookupLevel{qualification, nullptr, false});
1366-
else {
1396+
else if (isMemberCall) {
1397+
// the class of the object expression and its base classes
1398+
const Scope* objectScope = memberCallObjectScope(beforeName);
1399+
if (!objectScope)
1400+
return "";
1401+
lookup.addClassLevels(objectScope);
1402+
} else {
13671403
// use the symbol database scopes
13681404
for (const Scope* scope = tok->scope(); scope; scope = scope->nestedIn) {
13691405
if (scope->functionOf && scope->functionOf->isClassOrStructOrUnion()) {

‎test/testsimplifytemplate.cpp‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ class TestSimplifyTemplate : public TestFixture {
325325
TEST_CASE(templateTypeDeductionArray); // array arguments: only the decayed dimension is a pointer
326326
TEST_CASE(templateTypeDeductionConstPointer); // "const T *": the parameter provides the pointee const
327327
TEST_CASE(templateTypeDeductionFunctionPointer); // pointers to functions and to members are not deduced
328+
TEST_CASE(templateTypeDeductionMemberCall); // "obj . f ( x )": lookup in the class of the object, not at the call site
328329

329330
TEST_CASE(simplifyTemplateArgs1);
330331
TEST_CASE(simplifyTemplateArgs2);
@@ -7868,6 +7869,63 @@ class TestSimplifyTemplate : public TestFixture {
78687869
ASSERT_EQUALS("", errout_str());
78697870
}
78707871

7872+
void templateTypeDeductionMemberCall()
7873+
{
7874+
{ // a free template with the same name and arity is not what "r . check ( p )" calls
7875+
const char code[] = "template<class T> void check(T* x) { (void)x; }\n"
7876+
"struct Range { template<class T> bool check(T x) { return x != 1000; } };\n"
7877+
"bool g(Range& r, unsigned char* p) { return r.check(p); }";
7878+
const char exp[] = "template < class T > void check ( T * x ) { ( void ) x ; } "
7879+
"struct Range { bool check<unsignedchar*> ( unsigned char * x ) ; } ; "
7880+
"bool g ( Range & r , unsigned char * p ) { return r . check<unsignedchar*> ( p ) ; } "
7881+
"bool Range :: check<unsignedchar*> ( unsigned char * x ) { return x != 1000 ; }";
7882+
ASSERT_EQUALS(exp, tok(code));
7883+
ASSERT_EQUALS("", errout_str());
7884+
}
7885+
{ // a literal argument: the free template is not considered either
7886+
const char code[] = "template<class T> void check(T* x) { (void)x; }\n"
7887+
"struct Range { template<class T> bool check(T x) { return x != 1000; } };\n"
7888+
"bool g(Range& r) { return r.check(5); }";
7889+
const char exp[] = "template < class T > void check ( T * x ) { ( void ) x ; } "
7890+
"struct Range { bool check<int> ( int x ) ; } ; "
7891+
"bool g ( Range & r ) { return r . check<int> ( 5 ) ; } "
7892+
"bool Range :: check<int> ( int x ) { return x != 1000 ; }";
7893+
ASSERT_EQUALS(exp, tok(code));
7894+
ASSERT_EQUALS("", errout_str());
7895+
}
7896+
{ // the type of the object is unknown: nothing is deduced, the call is not rewritten
7897+
const char code[] = "template<class T> T f(T x) { return 10 / x; }\n"
7898+
"int g(Unknown& o) { int a = 0; return o.f(a); }";
7899+
const char exp[] = "template < class T > T f ( T x ) { return 10 / x ; } "
7900+
"int g ( Unknown & o ) { int a ; a = 0 ; return o . f ( a ) ; }";
7901+
ASSERT_EQUALS(exp, tok(code));
7902+
ASSERT_EQUALS("", errout_str());
7903+
}
7904+
{ // object, pointer, dereferenced pointer, smart pointer, derived object: the member template is found
7905+
const char code[] = "struct P { template<class T> int f(T x) { return x; } };\n"
7906+
"struct Q : P {};\n"
7907+
"int g(P& p, P* q, std::shared_ptr<P> sp, Q& d, int* ip) {\n"
7908+
" return p.f(ip) + q->f(ip) + (*q).f(ip) + sp->f(ip) + d.f(ip);\n"
7909+
"}";
7910+
const char exp[] = "struct P { int f<int*> ( int * x ) ; } ; "
7911+
"struct Q : P { } ; "
7912+
"int g ( P & p , P * q , std :: shared_ptr < P > sp , Q & d , int * ip ) { "
7913+
"return p . f<int*> ( ip ) + q . f<int*> ( ip ) + ( * q ) . f<int*> ( ip ) + sp . f<int*> ( ip ) + d . P :: f<int*> ( ip ) ; "
7914+
"} "
7915+
"int P :: f<int*> ( int * x ) { return x ; }";
7916+
ASSERT_EQUALS(exp, tok(code));
7917+
ASSERT_EQUALS("", errout_str());
7918+
}
7919+
{ // a container is not a class with member templates; a smart pointer's own members are not deduced
7920+
const char code[] = "template<class T> int f(T x) { return x; }\n"
7921+
"int g(std::vector<int>& v, std::shared_ptr<int> sp, int* ip) { return v.f(ip) + sp.f(ip); }";
7922+
const char exp[] = "template < class T > int f ( T x ) { return x ; } "
7923+
"int g ( std :: vector < int > & v , std :: shared_ptr < int > sp , int * ip ) { return v . f ( ip ) + sp . f ( ip ) ; }";
7924+
ASSERT_EQUALS(exp, tok(code));
7925+
ASSERT_EQUALS("", errout_str());
7926+
}
7927+
}
7928+
78717929
void simplifyTemplateArgs1() {
78727930
ASSERT_EQUALS("foo<2> = 2 ; foo<2> ;", tok("template<int N> foo = N; foo < ( 2 ) >;\n"));
78737931
ASSERT_EQUALS("foo<2> = 2 ; foo<2> ;", tok("template<int N> foo = N; foo < 1 + 1 >;\n"));

0 commit comments

Comments
 (0)