Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++: parse member variables using templates with forward declared types #2869

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--sort=no
--kinds-C++=m
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
p input.cpp /^ Class *p;$/;" m class:A typeref:typename:Class * file:
a_c_forward input.cpp /^ A<class C> a_c_forward;$/;" m class:B typeref:typename:A<class C> file:
a_s_forward input.cpp /^ A<struct S> a_s_forward;$/;" m class:B typeref:typename:A<struct S> file:
a_u_forward input.cpp /^ A<union U> a_u_forward;$/;" m class:B typeref:typename:A<union U> file:
16 changes: 16 additions & 0 deletions Units/parser-cxx.r/template-member-forward-declaration.d/input.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
template <class Class>
class A
{
public:
A() { p = new Class(); }
Class *p;
};

class B {
public:
// forward declaration okay because only used as pointer in Template
A<class C> a_c_forward;
A<struct S> a_s_forward;
A<union U> a_u_forward;
};

16 changes: 13 additions & 3 deletions parsers/cxx/cxx_parser_block.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ static bool cxxParserParseBlockInternal(bool bExpectClosingBracket)
cppBeginStatement();
}

int iNestedAngleBracketLevel = 0;
for(;;)
{
if(!cxxParserParseNextToken())
Expand Down Expand Up @@ -392,21 +393,24 @@ static bool cxxParserParseBlockInternal(bool bExpectClosingBracket)
}
break;
case CXXKeywordCLASS:
if(!cxxParserParseClassStructOrUnion(CXXKeywordCLASS,CXXTagCPPKindCLASS,CXXScopeTypeClass))
if(iNestedAngleBracketLevel == 0 &&
!cxxParserParseClassStructOrUnion(CXXKeywordCLASS,CXXTagCPPKindCLASS,CXXScopeTypeClass))
{
CXX_DEBUG_LEAVE_TEXT("Failed to parse class/struct/union");
return false;
}
break;
case CXXKeywordSTRUCT:
if(!cxxParserParseClassStructOrUnion(CXXKeywordSTRUCT,CXXTagKindSTRUCT,CXXScopeTypeStruct))
if(iNestedAngleBracketLevel == 0 &&
!cxxParserParseClassStructOrUnion(CXXKeywordSTRUCT,CXXTagKindSTRUCT,CXXScopeTypeStruct))
{
CXX_DEBUG_LEAVE_TEXT("Failed to parse class/struct/union");
return false;
}
break;
case CXXKeywordUNION:
if(!cxxParserParseClassStructOrUnion(CXXKeywordUNION,CXXTagKindUNION,CXXScopeTypeUnion))
if(iNestedAngleBracketLevel == 0 &&
!cxxParserParseClassStructOrUnion(CXXKeywordUNION,CXXTagKindUNION,CXXScopeTypeUnion))
{
CXX_DEBUG_LEAVE_TEXT("Failed to parse class/struct/union");
return false;
Expand Down Expand Up @@ -744,6 +748,12 @@ static bool cxxParserParseBlockInternal(bool bExpectClosingBracket)
else if (cxxScopeGetType() == CXXScopeTypeClass)
cxxSubparserUnknownIdentifierInClassNotify(g_cxx.pToken);
break;
case CXXTokenTypeSmallerThanSign:
iNestedAngleBracketLevel++;
break;
case CXXTokenTypeGreaterThanSign:
iNestedAngleBracketLevel--;
break;
default:
// something else we didn't handle
break;
Expand Down