Skip to content

Commit 6aa7ff9

Browse files
committed
Fix FP
1 parent 6db418b commit 6aa7ff9

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

‎lib/templatesimplifier.cpp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,29 @@ static bool scopeHasNonTemplateFunction(const Scope* scope, const std::string& n
11001100
});
11011101
}
11021102

1103+
// Does this executable scope declare a (non template) function with this name? Such a
1104+
// declaration hides the function templates of the enclosing scopes. The symbol database
1105+
// records only "extern" prototypes declared in a block scope, so the tokens of the scope
1106+
// are scanned for the other prototypes - nested blocks are separate scopes.
1107+
static bool executableScopeDeclaresFunction(const Scope* scope, const std::string& name)
1108+
{
1109+
if (scopeHasNonTemplateFunction(scope, name))
1110+
return true;
1111+
for (const Token* tok = scope->bodyStart->next(); tok && tok != scope->bodyEnd; tok = tok->next()) {
1112+
if (tok->str() == "{") {
1113+
tok = tok->link();
1114+
continue;
1115+
}
1116+
if (tok->str() != name || !Token::Match(tok, "%name% (") || !Token::simpleMatch(tok->linkAt(1), ") ;"))
1117+
continue;
1118+
// "int f ( long ) ;" is a declaration, "return f ( i ) ;" is a call
1119+
const Token* prev = tok->previous();
1120+
if (Token::Match(prev, "%type%|*|&|::") && !prev->isKeyword())
1121+
return true;
1122+
}
1123+
return false;
1124+
}
1125+
11031126
namespace {
11041127
// The scopes where a call name is looked up, in lookup order: the innermost scope
11051128
// with matching declarations wins. At class scopes the transitive base classes are
@@ -1402,6 +1425,9 @@ std::string TemplateSimplifier::deduceFunctionTemplateArguments(
14021425
} else {
14031426
// use the symbol database scopes
14041427
for (const Scope* scope = tok->scope(); scope; scope = scope->nestedIn) {
1428+
// a function declared in an enclosing block scope hides the templates of the outer scopes
1429+
if (scope->isExecutable() && executableScopeDeclaresFunction(scope, tok->str()))
1430+
return "";
14051431
if (scope->functionOf && scope->functionOf->isClassOrStructOrUnion()) {
14061432
// out of class member function body: the class, its base classes and
14071433
// its enclosing scopes come before the lexically enclosing scopes

‎test/testsimplifytemplate.cpp‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7022,6 +7022,31 @@ class TestSimplifyTemplate : public TestFixture {
70227022
"int :: type f<int> ( int x ) { return x ; }";
70237023
TODO_ASSERT_EQUALS(exp, act, tok(code));
70247024
}
7025+
{
7026+
// a function declared in a block scope hides the template: name lookup stops there
7027+
const char code[] = "template<class T> int f(T x) { return 100 / x; }\n"
7028+
"int g() { int f(long); int i = 0; return f(i); }";
7029+
const char exp[] = "template < class T > int f ( T x ) { return 100 / x ; } "
7030+
"int g ( ) { int f ( long ) ; int i ; i = 0 ; return f ( i ) ; }";
7031+
ASSERT_EQUALS(exp, tok(code));
7032+
}
7033+
{
7034+
// "extern" declaration in an enclosing block, the call in a nested block
7035+
const char code[] = "template<class T> int f(T x) { return 100 / x; }\n"
7036+
"int g(bool b) { extern int f(long); int i = 0; if (b) { return f(i); } return 0; }";
7037+
const char exp[] = "template < class T > int f ( T x ) { return 100 / x ; } "
7038+
"int g ( bool b ) { extern int f ( long ) ; int i ; i = 0 ; if ( b ) { return f ( i ) ; } return 0 ; }";
7039+
ASSERT_EQUALS(exp, tok(code));
7040+
}
7041+
{
7042+
// a declaration in a sibling block does not hide the template
7043+
const char code[] = "template<class T> int f(T x) { return x; }\n"
7044+
"int g() { { int f(long); } int i = 0; return f(i); }";
7045+
const char exp[] = "int f<int> ( int x ) ; "
7046+
"int g ( ) { { int f ( long ) ; } int i ; i = 0 ; return f<int> ( i ) ; } "
7047+
"int f<int> ( int x ) { return x ; }";
7048+
ASSERT_EQUALS(exp, tok(code));
7049+
}
70257050
}
70267051

70277052
void templateTypeDeduction15()

0 commit comments

Comments
 (0)