Skip to content

Commit b845dd3

Browse files
committed
Check for array and bail
1 parent c31f6ea commit b845dd3

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

‎lib/templatesimplifier.cpp‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -899,8 +899,14 @@ static bool isUndecayedArray(const Token* tok, bool decay, std::size_t consumed
899899
{
900900
if (!tok)
901901
return false;
902-
if (tok->variable()) {
903-
const std::size_t dimensions = tok->variable()->dimensions().size();
902+
if (const Variable* var = tok->variable()) {
903+
const std::size_t dimensions = var->dimensions().size();
904+
if (var->isPointer() && dimensions > 0) {
905+
// pointer to an array, "int ( * p ) [ 3 ]": the first "[" or "*" gives the array
906+
if (consumed == 0)
907+
return true;
908+
--consumed;
909+
}
904910
if (dimensions <= consumed)
905911
return false;
906912
return !decay || dimensions - consumed >= 2;
@@ -911,6 +917,11 @@ static bool isUndecayedArray(const Token* tok, bool decay, std::size_t consumed
911917
return isUndecayedArray(tok->astOperand1(), false, consumed);
912918
if (Token::Match(tok, ".|::|?"))
913919
return isUndecayedArray(tok->astOperand2(), decay, consumed);
920+
if (Token::Match(tok, "+|-|++|--|=|+=|-=")) { // pointer arithmetic decays an array operand
921+
const bool decayed = decay || consumed == 0;
922+
return isUndecayedArray(tok->astOperand1(), decayed, consumed) ||
923+
isUndecayedArray(tok->astOperand2(), decayed, consumed);
924+
}
914925
if (tok->str() == ":")
915926
return isUndecayedArray(tok->astOperand1(), decay, consumed) ||
916927
isUndecayedArray(tok->astOperand2(), decay, consumed);

‎test/testsimplifytemplate.cpp‎

Lines changed: 34 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(templateTypeDeductionPointerToArray); // a pointer to an array is not a pointer to a pointer
328329
TEST_CASE(templateTypeDeductionMemberCall); // "obj . f ( x )": lookup in the class of the object, not at the call site
329330

330331
TEST_CASE(simplifyTemplateArgs1);
@@ -7943,6 +7944,39 @@ class TestSimplifyTemplate : public TestFixture {
79437944
ASSERT_EQUALS("", errout_str());
79447945
}
79457946

7947+
void templateTypeDeductionPointerToArray()
7948+
{ // the ValueType of a pointer to an array ("int ( * p ) [ 3 ]") is "int * *", the same
7949+
// as for a pointer to a pointer, and pointer arithmetic decays only the outermost
7950+
// dimension of an array
7951+
const char code[] = "template<class T> void f(T x) {}\n"
7952+
"template<class T> void h(T* x) {}\n"
7953+
"template<class T> void r(T& x) {}\n"
7954+
"struct S { int (*pa)[3]; int* ap[2]; };\n"
7955+
"void test(S s, bool c) {\n"
7956+
" int a[3]; int b[2][3]; int* q[2]; int (*p)[3] = &a;\n"
7957+
" f(p); h(p); r(*p); f(s.pa); f(b + 1); f(p++); f(c ? p : p); f(&*p);\n"
7958+
" f(*p); r((*p)[0]); f(a + 1); h(b[0] + 1); f(q + 1); f(s.ap); f(*(b + 1));\n"
7959+
"}";
7960+
// the generic definitions stay because of the calls that are not deduced
7961+
const char exp[] = "void f<int*> ( int * x ) ; void f<int**> ( int * * x ) ; "
7962+
"template < class T > void f ( T x ) { } "
7963+
"void h<int> ( int * x ) ; "
7964+
"template < class T > void h ( T * x ) { } "
7965+
"void r<int> ( int & x ) ; "
7966+
"template < class T > void r ( T & x ) { } "
7967+
"struct S { int ( * pa ) [ 3 ] ; int * ap [ 2 ] ; } ; "
7968+
"void test ( S s , bool c ) { "
7969+
"int a [ 3 ] ; int b [ 2 ] [ 3 ] ; int * q [ 2 ] ; int ( * p ) [ 3 ] = & a ; "
7970+
"f ( p ) ; h ( p ) ; r ( * p ) ; f ( s . pa ) ; f ( b + 1 ) ; f ( p ++ ) ; f ( c ? p : p ) ; f ( & * p ) ; "
7971+
"f<int*> ( * p ) ; r<int> ( ( * p ) [ 0 ] ) ; f<int*> ( a + 1 ) ; h<int> ( b [ 0 ] + 1 ) ; f<int**> ( q + 1 ) ; f<int**> ( s . ap ) ; f<int*> ( * ( b + 1 ) ) ; "
7972+
"} "
7973+
"void r<int> ( int & x ) { } "
7974+
"void h<int> ( int * x ) { } "
7975+
"void f<int*> ( int * x ) { } void f<int**> ( int * * x ) { }";
7976+
ASSERT_EQUALS(exp, tok(code));
7977+
ASSERT_EQUALS("", errout_str());
7978+
}
7979+
79467980
void templateTypeDeductionMemberCall()
79477981
{
79487982
{ // a free template with the same name and arity is not what "r . check ( p )" calls

0 commit comments

Comments
 (0)