diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h --- a/clang/include/clang/Parse/Parser.h +++ b/clang/include/clang/Parse/Parser.h @@ -2846,7 +2846,8 @@ void ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS, bool IsInterface, SourceLocation FriendLoc); - bool isCXX11FinalKeyword() const; + bool isCXX11FinalKeyword(const Token &Tok) const; + bool isCXX11FinalKeyword() const { return isCXX11FinalKeyword(Tok); } /// DeclaratorScopeObj - RAII object used in Parser::ParseDirectDeclarator to /// enter a new C++ declarator scope and exit it when the function is diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp --- a/clang/lib/Parse/ParseDecl.cpp +++ b/clang/lib/Parse/ParseDecl.cpp @@ -2998,7 +2998,7 @@ // the token as an identifier. if (getLangOpts().MSVCCompat && Tok.is(tok::kw__Atomic) && DS.getStorageClassSpec() == clang::DeclSpec::SCS_typedef && - !DS.hasTypeSpecifier() && GetLookAheadToken(1).is(tok::less)) + !DS.hasTypeSpecifier() && NextToken().is(tok::less)) Tok.setKind(tok::identifier); SourceLocation Loc = Tok.getLocation(); diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp --- a/clang/lib/Parse/ParseDeclCXX.cpp +++ b/clang/lib/Parse/ParseDeclCXX.cpp @@ -1770,14 +1770,6 @@ } } - // If this is an elaborated type specifier, and we delayed - // diagnostics before, just merge them into the current pool. - if (shouldDelayDiagsInTag) { - diagsFromTag.done(); - if (TUK == Sema::TUK_Reference) - diagsFromTag.redelay(); - } - if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error || TUK != Sema::TUK_Definition)) { if (DS.getTypeSpecType() != DeclSpec::TST_error) { @@ -1950,6 +1942,14 @@ } } + // If this is an elaborated type specifier, and we delayed + // diagnostics before, just merge them into the current pool. + if (shouldDelayDiagsInTag) { + diagsFromTag.done(); + if (TUK == Sema::TUK_Reference) + diagsFromTag.redelay(); + } + // If there is a body, parse it and inform the actions module. if (TUK == Sema::TUK_Definition) { assert(Tok.is(tok::l_brace) || @@ -2292,8 +2292,8 @@ /// isCXX11FinalKeyword - Determine whether the next token is a C++11 /// 'final' or Microsoft 'sealed' contextual keyword. -bool Parser::isCXX11FinalKeyword() const { - VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(); +bool Parser::isCXX11FinalKeyword(const Token &Tok) const { + VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok); return Specifier == VirtSpecifiers::VS_Final || Specifier == VirtSpecifiers::VS_GNU_Final || Specifier == VirtSpecifiers::VS_Sealed; diff --git a/clang/lib/Parse/ParseTemplate.cpp b/clang/lib/Parse/ParseTemplate.cpp --- a/clang/lib/Parse/ParseTemplate.cpp +++ b/clang/lib/Parse/ParseTemplate.cpp @@ -167,6 +167,52 @@ LastParamListWasEmpty), DeclEnd); + // Check whether it is a partial specialization. + + // TODO. This can produce wrong detection in case of a later class + // declaration. Example: + // // forward declaration of a nested class C2 + // template C1 {template C2;}; + // //later declaration of a class C2 End example. + // template template C1::C2 {}; + + // Keyword `using` means that it's an alias declaration. + if (!isSpecialization && !ParamLists.empty() && Tok.isNot(tok::kw_using) && + Tok.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union)) { + // Next sequences of tokens can surely identify a partial specialization: + // `>::` in `class A::C{};` + // `>{` in `class A::B{};` + // `> final` in `class A::B final {};` + // `>;` in `class A::B;` + // `> :` in `class A::B : D {};` + // `>>::` in `class A>::C{};` + // `>>{` in `class A::B>{};` + // `>> final` in `class A::B> final {};` + // `>>;` in `class A::B>;` + // `>> :` in `class A::B> : E {};` + // `>>>::` in `class A>>::C{};` + // `>>>{` in `class A::B>>{};` + // `>>> final` in `class A::B>> final {};` + // `>>>;` in `class A::B>>;` + // `>>> :` in `class A::B>> : F {};` + int TokIdx = 0; + Token NextTok; + Token NextNextTok = GetLookAheadToken(TokIdx++); + // Look ahead until `{` or `;`. + while (!NextNextTok.isOneOf(tok::l_brace, tok::semi)) { + NextTok = NextNextTok; + NextNextTok = GetLookAheadToken(TokIdx++); + + if (NextTok.isOneOf(tok::greater, tok::greatergreater, + tok::greatergreatergreater) && + (NextNextTok.isOneOf(tok::coloncolon, tok::l_brace, tok::semi, + tok::colon) || + (NextNextTok.is(tok::identifier) && + isCXX11FinalKeyword(NextNextTok)))) + isSpecialization = true; + } + } + return ParseSingleDeclarationAfterTemplate( Context, ParsedTemplateInfo(&ParamLists, isSpecialization, LastParamListWasEmpty), @@ -245,6 +291,11 @@ else DS.takeAttributesFrom(prefixAttrs); + const bool NeedSuppressAccessChecks = + (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || + TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); + SuppressAccessChecks SuppressAccessGuard(*this, NeedSuppressAccessChecks); + // Parse the declarator. ParsingDeclarator DeclaratorInfo(*this, DS, (DeclaratorContext)Context); if (TemplateInfo.TemplateParams) @@ -259,6 +310,9 @@ return nullptr; } + if (NeedSuppressAccessChecks) + SuppressAccessGuard.done(); + llvm::TimeTraceScope TimeScope("ParseTemplate", [&]() { return std::string(DeclaratorInfo.getIdentifier() != nullptr ? DeclaratorInfo.getIdentifier()->getName() diff --git a/clang/test/CXX/drs/dr1xx.cpp b/clang/test/CXX/drs/dr1xx.cpp --- a/clang/test/CXX/drs/dr1xx.cpp +++ b/clang/test/CXX/drs/dr1xx.cpp @@ -934,12 +934,12 @@ template void C::g() {} class A { - class B {}; // expected-note {{here}} + class B {}; void f(); }; template void C::f(); - template <> void C::g(); // expected-error {{private}} + template <> void C::g(); void A::f() { C cb; diff --git a/clang/test/CXX/temp/temp.spec/func.spec.cpp b/clang/test/CXX/temp/temp.spec/func.spec.cpp new file mode 100644 --- /dev/null +++ b/clang/test/CXX/temp/temp.spec/func.spec.cpp @@ -0,0 +1,121 @@ +// RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify %s + +// C++20 [temp.explicit] 17.8/6: +// The usual access checking rules do not apply to names in a declaration +// of an explicit instantiation or explicit specialization, with +// the exception of names appearing in a function body, default argument, +// base-clause, member-specification, enumerator-list, or static data member +// or variable template initializer. + +class A { + // expected-note@+1 14{{implicitly declared private here}} + template class B {}; + // expected-note@+1 {{implicitly declared private here}} + static constexpr int num1 = 42; + +protected: + // expected-note@+1 9{{declared protected here}} + class C {}; + // expected-note@+1 {{declared protected here}} + static constexpr int num2 = 43; + static int num3; + +public: + template class D {}; +}; +int A::num3 = 44; + +class E : public A { + template A::C func1(); + // expected-error@+1 {{is a private member of}} + template A::B func2(); + template A::D func3(); + // expected-error@+1 {{is a private member of}} + template class A::B func4(); + template void func5(); + + template <> A::C func1(); + // expected-error@+1 {{is a private member of}} + template <> A::B func2(); + template <> A::D func3(); + // expected-error@+1 {{is a private member of}} + template <> class A::B func4(); + template <> void func5(); + // expected-error@+1 {{is a private member of}} + template <> void func5>(); + template <> void func5>(); + template <> void func5(); +}; + +template class StealClass { + friend int stealFunc() { return *x; } +}; + +template class StealClass<&A::num3>; +int stealFunc(); + +int stealFunc2() { + return stealFunc(); +} + +//----------------------------------------------------------// + +// explicit specializations + +// expected-error@+1 {{is a protected member of}} +template A::C func1(); +// expected-error@+1 {{is a private member of}} +template A::B func2(); +template A::D func3(); +// expected-error@+1 {{is a private member of}} +template class A::B func4(); +template void func5(); +// expected-error@+1 {{is a private member of}} +template void func6(); +// expected-error@+1 {{is a protected member of}} +template void func7(); + +// expected-error@+1 2{{is a protected member of}} +template A::C func1() { A::C x; } +// expected-error@+2 {{is a private member of}} +// expected-error@+1 {{is a protected member of}} +template A::B func2() { A::D x; } +template A::D func3() { A::D x; } +// expected-error@+2 2{{is a private member of}} +// expected-error@+1 {{is a protected member of}} +template class A::B func4() { A::B x; } template void func5() { + // expected-error@+2 {{is a private member of}} + // expected-error@+1 {{is a protected member of}} + A::B> x; + // expected-error@+1 {{is a private member of}} + A::B x2; +} + +// expected-error@+1 {{is a protected member of}} +template <> A::C func1(); +// expected-error@+2 {{is a private member of}} +// expected-error@+1 {{is a protected member of}} +template <> A::B func2(); +// expected-error@+1 {{is a protected member of}} +template <> A::D func3(); +// expected-error@+1 {{is a private member of}} +template <> class A::B func4(); +template <> void func5(); +template <> void func5>(); +template <> void func5>(); +template <> void func5(); + +//----------------------------------------------------------// + +// explicit instantiations + +template void func8() {} +template void func9() {} +template void func10() {} +template