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

Allow disambiguating class constructor definition followed by semicolon #346

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
Expand Up @@ -11531,4 +11531,14 @@ public void testResolveFunctionTemplateInDeferredClassArg() throws Exception {
public void testResolveFunctionTemplateInDeferredBaseArg() throws Exception {
parseAndCheckBindings();
}

// template<typename T>
// struct S {
// S(T) {};
// };
//
// S<int> s(1);
public void testRecognizeConstructorWithSemicolonAfterBody() throws Exception {
parseAndCheckImplicitNameBindings();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,9 @@ protected boolean isAtStartOfStructuredBinding(IASTDeclSpecifier declSpecifier)
&& LTcatchEOF(expectedBracketOffset + 1) == IToken.tIDENTIFIER;
}

protected abstract boolean maybeDeclaresNonStaticMemberOfSameClass(final DeclarationOptions option,
final IASTDeclSpecifier declSpec, final IASTDeclarator dtor);

/**
* Parses for two alternatives of a declspec sequence followed by a initDeclarator.
* A second alternative is accepted only, if it ends at the same point of the first alternative. Otherwise the
Expand Down Expand Up @@ -1770,10 +1773,16 @@ protected Decl declSpecifierSequence_initDeclarator(final DeclarationOptions opt
return result.set(declspec1, dtor1, dtorMark1);
}

// [class.mem] "... In particluar, a class C shall not contain a non-static member of class C"
// If type name in first variant matches current class name, disambiguate it later too;
// this allows to handle the following case where constructor declaration is shorter
// than variable declaration with initializer list
// template<typename T> class S { S(T){}; }
final IToken end2 = mark();
if (end1 == end2) {
if (end1 == end2 || maybeDeclaresNonStaticMemberOfSameClass(option, declspec1, dtor1)) {
return result.set(declspec1, dtor1, declspec2, dtor2);
}

if (end1.getEndOffset() > end2.getEndOffset()) {
backup(end1);
return result.set(declspec1, dtor1, dtorMark1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2224,4 +2224,10 @@ protected IASTAlignmentSpecifier createAmbiguousAlignmentSpecifier(IASTAlignment
IASTAlignmentSpecifier typeId) {
return new CASTAmbiguousAlignmentSpecifier(expression, typeId);
}

@Override
protected boolean maybeDeclaresNonStaticMemberOfSameClass(final DeclarationOptions option,
final IASTDeclSpecifier declSpec, final IASTDeclarator dtor) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4256,6 +4256,22 @@ protected IASTDeclarator initDeclarator(IASTDeclSpecifier declspec, DeclarationO
return dtor;
}

@Override
protected boolean maybeDeclaresNonStaticMemberOfSameClass(final DeclarationOptions option,
final IASTDeclSpecifier declSpec, final IASTDeclarator dtor) {
if (option == DeclarationOptions.CPP_MEMBER && currentClassName != null
&& declSpec.getStorageClass() != IASTDeclSpecifier.sc_static && dtor != null
&& dtor.getPointerOperators().length == 0
&& declSpec instanceof ICPPASTNamedTypeSpecifier namedTypeSpec) {
IASTName name = namedTypeSpec.getName();
if (CharArrayUtils.equals(name.getLookupKey(), currentClassName)) {
return true;
}
}

return false;
}

/**
* Tries to detect illegal versions of declarations
*/
Expand Down