Repository navigation
[clang][Modules] Complete the implementation of P2615: Meaningful exports #194201
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
base: main
Are you sure you want to change the base?
Changes from 10 commits
b8f38d5
b2e7ab4
fc4e1c3
012415d
a6d6b28
c8bee39
95c6d00
ac5f826
8270a02
a4c37e4
376bad9
8464a81
b94cd84
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -132,7 +132,7 @@ enum class EmbedResult { | |
| }; | ||
|
|
||
| struct CXXStandardLibraryVersionInfo { | ||
| enum Library { Unknown, LibStdCXX }; | ||
| enum Library { Unknown, LibStdCXX, MsvcStl }; | ||
| Library Lib; | ||
| std::uint64_t Version; | ||
| }; | ||
|
|
@@ -2823,9 +2823,29 @@ class Preprocessor { | |
| // Standard Library Identification | ||
| std::optional<CXXStandardLibraryVersionInfo> CXXStandardLibraryVersion; | ||
|
|
||
| void ComputeCXXStandardLibraryVersion(); | ||
|
|
||
| bool NeedsCXXStandardLibraryWorkaroundBefore( | ||
| uint64_t FixedVersion, CXXStandardLibraryVersionInfo::Library Lib) { | ||
| ComputeCXXStandardLibraryVersion(); | ||
| return CXXStandardLibraryVersion && CXXStandardLibraryVersion->Lib == Lib && | ||
| CXXStandardLibraryVersion->Version < FixedVersion; | ||
| } | ||
|
Comment on lines
+2826
to
+2833
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need these logics in this patch... |
||
|
|
||
| public: | ||
| std::optional<std::uint64_t> getStdLibCxxVersion(); | ||
| bool NeedsStdLibCxxWorkaroundBefore(std::uint64_t FixedVersion); | ||
| bool NeedsStdLibCxxWorkaroundBefore(std::uint64_t FixedVersion) { | ||
| assert(FixedVersion >= 2000'00'00 && FixedVersion <= 2100'00'00 && | ||
| "invalid value for __GLIBCXX__"); | ||
| return NeedsCXXStandardLibraryWorkaroundBefore( | ||
| FixedVersion, CXXStandardLibraryVersionInfo::LibStdCXX); | ||
| } | ||
|
|
||
| bool NeedsMsvcStlWorkaroundBefore(std::uint64_t FixedVersion) { | ||
| assert(FixedVersion >= 2000'00 && FixedVersion <= 2100'00 && | ||
| "invalid value for _MSVC_STL_UPDATE"); | ||
| return NeedsCXXStandardLibraryWorkaroundBefore( | ||
| FixedVersion, CXXStandardLibraryVersionInfo::MsvcStl); | ||
| } | ||
|
|
||
| private: | ||
| //===--------------------------------------------------------------------===// | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1913,3 +1913,19 @@ SourceRange ExplicitInstantiationDecl::getSourceRange() const { | |
| SourceLocation Begin = ExternLoc.isValid() ? ExternLoc : getLocation(); | ||
| return SourceRange(Begin, getEndLoc()); | ||
| } | ||
|
|
||
| TemplateSpecializationKind clang::getTemplateSpecializationKind(const Decl *D) { | ||
| if (!D) | ||
| return TSK_Undeclared; | ||
|
|
||
| if (const auto *Record = dyn_cast<CXXRecordDecl>(D)) | ||
| return Record->getTemplateSpecializationKind(); | ||
| if (const auto *Function = dyn_cast<FunctionDecl>(D)) | ||
| return Function->getTemplateSpecializationKind(); | ||
| if (const auto *Var = dyn_cast<VarDecl>(D)) | ||
| return Var->getTemplateSpecializationKind(); | ||
| if (const auto *EID = dyn_cast<ExplicitInstantiationDecl>(D)) | ||
| return EID->getTemplateSpecializationKind(); | ||
|
|
||
| return TSK_Undeclared; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Slightly odd to return TSK_Undeclared for declaration unrelated to templates.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, but it's consistent with all the node-specific |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -339,9 +339,11 @@ Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, DeclaratorContext Context) { | |
| // ... but anyway remember that such an "extern" was seen. | ||
| DS.setExternInLinkageSpec(true); | ||
| ParseExternalDeclaration(DeclAttrs, DeclSpecAttrs, &DS); | ||
| return LinkageSpec ? Actions.ActOnFinishLinkageSpecification( | ||
| getCurScope(), LinkageSpec, SourceLocation()) | ||
| : nullptr; | ||
| if (!LinkageSpec) | ||
| return nullptr; | ||
| CheckUnbracedLinkageOrExportDeclaration(LinkageSpec); | ||
| return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec, | ||
| SourceLocation()); | ||
| } | ||
|
|
||
| DS.abort(); | ||
|
|
@@ -420,6 +422,7 @@ Decl *Parser::ParseExportDeclaration() { | |
| MaybeParseCXX11Attributes(DeclAttrs); | ||
| ParsedAttributes EmptyDeclSpecAttrs(AttrFactory); | ||
| ParseExternalDeclaration(DeclAttrs, EmptyDeclSpecAttrs); | ||
| CheckUnbracedLinkageOrExportDeclaration(ExportDecl); | ||
| return Actions.ActOnFinishExportDecl(getCurScope(), ExportDecl, | ||
| SourceLocation()); | ||
| } | ||
|
|
@@ -440,6 +443,62 @@ Decl *Parser::ParseExportDeclaration() { | |
| T.getCloseLocation()); | ||
| } | ||
|
|
||
| void Parser::CheckUnbracedLinkageOrExportDeclaration( | ||
| Decl *LinkageOrExportDecl) { | ||
| const auto *DC = cast<DeclContext>(LinkageOrExportDecl); | ||
| if (DC->decls_empty()) | ||
| return; | ||
|
Comment on lines
+449
to
+450
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that can ever be true, we should assert on it instead
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That causes the following tests to fail: |
||
|
|
||
| const Decl *D = *DC->decls_begin(); | ||
|
|
||
| // Nested export declarations are diagnosed elsewhere. | ||
| if (isa<LinkageSpecDecl>(LinkageOrExportDecl) && isa<ExportDecl>(D)) { | ||
| Diag(LinkageOrExportDecl->getLocation(), | ||
| diag::err_invalid_decl_in_linkage_spec) | ||
| << /*export declaration*/ 2; | ||
| return; | ||
| } | ||
|
|
||
| // [module.interface]/1 says: | ||
| // | ||
| // The name-declaration of an export-declaration shall not declare a | ||
| // partial specialization. | ||
| // | ||
| // But there's no equivalent wording for linkage-specification. | ||
| if (isa<ClassTemplatePartialSpecializationDecl, | ||
| VarTemplatePartialSpecializationDecl>(D) && | ||
| isa<LinkageSpecDecl>(LinkageOrExportDecl)) | ||
| return; | ||
|
|
||
| TemplateSpecializationKind TSK = getTemplateSpecializationKind(D); | ||
| if (TSK == TSK_Undeclared) | ||
| return; | ||
|
|
||
| if (const auto *ED = dyn_cast<ExportDecl>(LinkageOrExportDecl)) { | ||
| Diag(ED->getExportLoc(), diag::err_meaningless_export) | ||
| << (TSK == TSK_ExplicitSpecialization) | ||
| << FixItHint::CreateRemoval(ED->getExportLoc()); | ||
|
localspook marked this conversation as resolved.
Resolved
|
||
| Diag(ED->getExportLoc(), diag::note_meaningless_export_explanation); | ||
| return; | ||
| } | ||
|
|
||
| if (const auto *LS = dyn_cast<LinkageSpecDecl>(LinkageOrExportDecl)) { | ||
| // Old versions of the MSVC STL used to have linkage specifications | ||
| // on some template specializations, but it would be too disruptive to | ||
| // reject them. This was fixed in | ||
| // https://github.com/microsoft/STL/pull/6074, merged on 2026-02-11. | ||
| if (LinkageOrExportDecl->isInStdNamespace() && | ||
| getPreprocessor().NeedsMsvcStlWorkaroundBefore(2026'03)) | ||
| return; | ||
|
|
||
| Diag(LS->getLocation(), diag::err_invalid_decl_in_linkage_spec) | ||
| << (TSK == TSK_ExplicitSpecialization); | ||
| return; | ||
| } | ||
|
|
||
| llvm_unreachable("Expected either an ExportDecl or a LinkageSpecDecl"); | ||
| } | ||
|
|
||
| Parser::DeclGroupPtrTy Parser::ParseUsingDirectiveOrDeclaration( | ||
| DeclaratorContext Context, const ParsedTemplateInfo &TemplateInfo, | ||
| SourceLocation &DeclEnd, ParsedAttributes &Attrs) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // RUN: %clang_cc1 -std=c++20 -fexceptions -fcxx-exceptions -pedantic-errors %s -verify -verify-directives | ||
| // RUN: %clang_cc1 -std=c++23 -fexceptions -fcxx-exceptions -pedantic-errors %s -verify -verify-directives | ||
| // RUN: %clang_cc1 -std=c++2c -fexceptions -fcxx-exceptions -pedantic-errors %s -verify -verify-directives | ||
|
|
||
| export module foo; | ||
|
|
||
| namespace cwg2443 { // cwg2443: 23 | ||
|
|
||
| export template <typename T> class s1 {}; | ||
| export template <typename T> class s1<T *> {}; | ||
| // expected-error@-1 {{a specialization cannot be marked 'export'}} | ||
| // expected-note@-2 {{it is exported if the primary template is exported}} | ||
| export template <> class s1<int> {}; | ||
| // expected-error@-1 {{a specialization cannot be marked 'export'}} | ||
| // expected-note@-2 {{it is exported if the primary template is exported}} | ||
| export template class s1<char>; | ||
| // expected-error@-1 {{an explicit instantiation cannot be marked 'export'}} | ||
| // expected-note@-2 {{it is exported if the primary template is exported}} | ||
| export extern template class s1<void>; | ||
| // expected-error@-1 {{an explicit instantiation cannot be marked 'export'}} | ||
| // expected-note@-2 {{it is exported if the primary template is exported}} | ||
|
|
||
| export template <typename T> int v1 = 0; | ||
| export template <typename T> int v1<T *> = 0; | ||
| // expected-error@-1 {{a specialization cannot be marked 'export'}} | ||
| // expected-note@-2 {{it is exported if the primary template is exported}} | ||
| export template <> int v1<int> = 0; | ||
| // expected-error@-1 {{a specialization cannot be marked 'export'}} | ||
| // expected-note@-2 {{it is exported if the primary template is exported}} | ||
| export template int v1<char>; | ||
| // expected-error@-1 {{an explicit instantiation cannot be marked 'export'}} | ||
| // expected-note@-2 {{it is exported if the primary template is exported}} | ||
| export extern template int v1<void>; | ||
| // expected-error@-1 {{an explicit instantiation cannot be marked 'export'}} | ||
| // expected-note@-2 {{it is exported if the primary template is exported}} | ||
|
|
||
| export template <typename T> void f1() {} | ||
| export template <> void f1<int>() {} | ||
| // expected-error@-1 {{a specialization cannot be marked 'export'}} | ||
| // expected-note@-2 {{it is exported if the primary template is exported}} | ||
| export template void f1<char>(); | ||
| // expected-error@-1 {{an explicit instantiation cannot be marked 'export'}} | ||
| // expected-note@-2 {{it is exported if the primary template is exported}} | ||
| export extern template void f1<void>(); | ||
| // expected-error@-1 {{an explicit instantiation cannot be marked 'export'}} | ||
| // expected-note@-2 {{it is exported if the primary template is exported}} | ||
|
|
||
|
|
||
| export { template <typename T> class s2 {}; } | ||
| export { template <typename T> class s2<T *> {}; } | ||
| export { template <> class s2<int> {}; } | ||
| export { template class s2<char>; } | ||
| export { extern template class s2<void>; } | ||
|
|
||
| export { template <typename T> int v2 = 0; } | ||
| export { template <typename T> int v2<T *> = 0; } | ||
| export { template <> int v2<int> = 0; } | ||
| export { template int v2<char>; } | ||
| export { extern template int v2<void>; } | ||
|
|
||
| export { template <typename T> void f2() {} } | ||
| export { template <> void f2<int>() {} } | ||
| export { template void f2<char>(); } | ||
| export { extern template void f2<void>(); } | ||
|
|
||
| } // namespace cwg2443 |
Uh oh!
There was an error while loading. Please reload this page.