aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/clang/lib/Sema/SemaModule.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2023-12-18 20:30:12 +0000
committerDimitry Andric <dim@FreeBSD.org>2024-04-06 20:11:55 +0000
commit5f757f3ff9144b609b3c433dfd370cc6bdc191ad (patch)
tree1b4e980b866cd26a00af34c0a653eb640bd09caf /contrib/llvm-project/clang/lib/Sema/SemaModule.cpp
parent3e1c8a35f741a5d114d0ba670b15191355711fe9 (diff)
parent312c0ed19cc5276a17bacf2120097bec4515b0f1 (diff)
Diffstat (limited to 'contrib/llvm-project/clang/lib/Sema/SemaModule.cpp')
-rw-r--r--contrib/llvm-project/clang/lib/Sema/SemaModule.cpp74
1 files changed, 45 insertions, 29 deletions
diff --git a/contrib/llvm-project/clang/lib/Sema/SemaModule.cpp b/contrib/llvm-project/clang/lib/Sema/SemaModule.cpp
index cd38cd4cf69d..db0cbd5ec6d6 100644
--- a/contrib/llvm-project/clang/lib/Sema/SemaModule.cpp
+++ b/contrib/llvm-project/clang/lib/Sema/SemaModule.cpp
@@ -28,11 +28,11 @@ static void checkModuleImportContext(Sema &S, Module *M,
if (auto *LSD = dyn_cast<LinkageSpecDecl>(DC)) {
switch (LSD->getLanguage()) {
- case LinkageSpecDecl::lang_c:
+ case LinkageSpecLanguageIDs::C:
if (ExternCLoc.isInvalid())
ExternCLoc = LSD->getBeginLoc();
break;
- case LinkageSpecDecl::lang_cxx:
+ case LinkageSpecLanguageIDs::CXX:
break;
}
DC = LSD->getParent();
@@ -103,7 +103,8 @@ void Sema::HandleStartOfHeaderUnit() {
StringRef HUName = getLangOpts().CurrentModule;
if (HUName.empty()) {
- HUName = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID())->getName();
+ HUName =
+ SourceMgr.getFileEntryRefForID(SourceMgr.getMainFileID())->getName();
const_cast<LangOptions &>(getLangOpts()).CurrentModule = HUName.str();
}
@@ -124,7 +125,6 @@ void Sema::HandleStartOfHeaderUnit() {
ModuleScopes.push_back({}); // No GMF
ModuleScopes.back().BeginLoc = StartOfTU;
ModuleScopes.back().Module = Mod;
- ModuleScopes.back().ModuleInterface = true;
VisibleModules.setVisible(Mod, StartOfTU);
// From now on, we have an owning module for all declarations we see.
@@ -268,7 +268,7 @@ Sema::ActOnModuleDecl(SourceLocation StartLoc, SourceLocation ModuleLoc,
StringRef FirstComponentName = Path[0].first->getName();
if (!getSourceManager().isInSystemHeader(Path[0].second) &&
(FirstComponentName == "std" ||
- (FirstComponentName.startswith("std") &&
+ (FirstComponentName.starts_with("std") &&
llvm::all_of(FirstComponentName.drop_front(3), &llvm::isDigit))))
Diag(Path[0].second, diag::warn_reserved_module_name) << Path[0].first;
@@ -374,7 +374,6 @@ Sema::ActOnModuleDecl(SourceLocation StartLoc, SourceLocation ModuleLoc,
// Switch from the global module fragment (if any) to the named module.
ModuleScopes.back().BeginLoc = StartLoc;
ModuleScopes.back().Module = Mod;
- ModuleScopes.back().ModuleInterface = MDK != ModuleDeclKind::Implementation;
VisibleModules.setVisible(Mod, ModuleLoc);
// From now on, we have an owning module for all declarations we see.
@@ -469,7 +468,6 @@ Sema::ActOnPrivateModuleFragmentDecl(SourceLocation ModuleLoc,
ModuleScopes.push_back({});
ModuleScopes.back().BeginLoc = ModuleLoc;
ModuleScopes.back().Module = PrivateModuleFragment;
- ModuleScopes.back().ModuleInterface = true;
VisibleModules.setVisible(PrivateModuleFragment, ModuleLoc);
// All declarations created from now on are scoped to the private module
@@ -522,7 +520,7 @@ DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc,
if (getLangOpts().CPlusPlusModules && isCurrentModulePurview() &&
getCurrentModule()->Name == ModuleName) {
Diag(ImportLoc, diag::err_module_self_import_cxx20)
- << ModuleName << !ModuleScopes.back().ModuleInterface;
+ << ModuleName << currentModuleIsImplementation();
return true;
}
@@ -531,6 +529,12 @@ DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc,
if (!Mod)
return true;
+ if (!Mod->isInterfaceOrPartition() && !ModuleName.empty()) {
+ Diag(ImportLoc, diag::err_module_import_non_interface_nor_parition)
+ << ModuleName;
+ return true;
+ }
+
return ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Mod, Path);
}
@@ -602,10 +606,7 @@ DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc,
Mod->Kind == Module::ModuleKind::ModulePartitionImplementation) {
Diag(ExportLoc, diag::err_export_partition_impl)
<< SourceRange(ExportLoc, Path.back().second);
- } else if (!ModuleScopes.empty() &&
- (ModuleScopes.back().ModuleInterface ||
- (getLangOpts().CPlusPlusModules &&
- ModuleScopes.back().Module->isGlobalModule()))) {
+ } else if (!ModuleScopes.empty() && !currentModuleIsImplementation()) {
// Re-export the module if the imported module is exported.
// Note that we don't need to add re-exported module to Imports field
// since `Exports` implies the module is imported already.
@@ -765,7 +766,7 @@ Decl *Sema::ActOnStartExportDecl(Scope *S, SourceLocation ExportLoc,
Diag(ExportLoc, diag::err_export_not_in_module_interface) << 0;
D->setInvalidDecl();
return D;
- } else if (!ModuleScopes.back().ModuleInterface) {
+ } else if (currentModuleIsImplementation()) {
Diag(ExportLoc, diag::err_export_not_in_module_interface) << 1;
Diag(ModuleScopes.back().BeginLoc,
diag::note_not_module_interface_add_export)
@@ -837,7 +838,7 @@ static bool checkExportedDecl(Sema &S, Decl *D, SourceLocation BlockStart) {
// Don't diagnose anonymous union objects; we'll diagnose their members
// instead.
HasName = (bool)ND->getDeclName();
- if (HasName && ND->getFormalLinkage() == InternalLinkage) {
+ if (HasName && ND->getFormalLinkage() == Linkage::Internal) {
S.Diag(ND->getLocation(), diag::err_export_internal) << ND;
if (BlockStart.isValid())
S.Diag(BlockStart, diag::note_export);
@@ -851,9 +852,9 @@ static bool checkExportedDecl(Sema &S, Decl *D, SourceLocation BlockStart) {
if (auto *USD = dyn_cast<UsingShadowDecl>(D)) {
NamedDecl *Target = USD->getUnderlyingDecl();
Linkage Lk = Target->getFormalLinkage();
- if (Lk == InternalLinkage || Lk == ModuleLinkage) {
+ if (Lk == Linkage::Internal || Lk == Linkage::Module) {
S.Diag(USD->getLocation(), diag::err_export_using_internal)
- << (Lk == InternalLinkage ? 0 : 1) << Target;
+ << (Lk == Linkage::Internal ? 0 : 1) << Target;
S.Diag(Target->getLocation(), diag::note_using_decl_target);
if (BlockStart.isValid())
S.Diag(BlockStart, diag::note_export);
@@ -925,7 +926,6 @@ Module *Sema::PushGlobalModuleFragment(SourceLocation BeginLoc) {
// Enter the scope of the global module.
ModuleScopes.push_back({BeginLoc, TheGlobalModuleFragment,
- /*ModuleInterface=*/false,
/*OuterVisibleModules=*/{}});
VisibleModules.setVisible(TheGlobalModuleFragment, BeginLoc);
@@ -939,23 +939,20 @@ void Sema::PopGlobalModuleFragment() {
ModuleScopes.pop_back();
}
-Module *Sema::PushImplicitGlobalModuleFragment(SourceLocation BeginLoc,
- bool IsExported) {
- Module **M = IsExported ? &TheExportedImplicitGlobalModuleFragment
- : &TheImplicitGlobalModuleFragment;
- if (!*M) {
+Module *Sema::PushImplicitGlobalModuleFragment(SourceLocation BeginLoc) {
+ if (!TheImplicitGlobalModuleFragment) {
ModuleMap &Map = PP.getHeaderSearchInfo().getModuleMap();
- *M = Map.createImplicitGlobalModuleFragmentForModuleUnit(
- BeginLoc, IsExported, getCurrentModule());
+ TheImplicitGlobalModuleFragment =
+ Map.createImplicitGlobalModuleFragmentForModuleUnit(BeginLoc,
+ getCurrentModule());
}
- assert(*M && "module creation should not fail");
+ assert(TheImplicitGlobalModuleFragment && "module creation should not fail");
// Enter the scope of the global module.
- ModuleScopes.push_back({BeginLoc, *M,
- /*ModuleInterface=*/false,
+ ModuleScopes.push_back({BeginLoc, TheImplicitGlobalModuleFragment,
/*OuterVisibleModules=*/{}});
- VisibleModules.setVisible(*M, BeginLoc);
- return *M;
+ VisibleModules.setVisible(TheImplicitGlobalModuleFragment, BeginLoc);
+ return TheImplicitGlobalModuleFragment;
}
void Sema::PopImplicitGlobalModuleFragment() {
@@ -964,3 +961,22 @@ void Sema::PopImplicitGlobalModuleFragment() {
"left the wrong module scope, which is not global module fragment");
ModuleScopes.pop_back();
}
+
+bool Sema::isCurrentModulePurview() const {
+ if (!getCurrentModule())
+ return false;
+
+ /// Does this Module scope describe part of the purview of a standard named
+ /// C++ module?
+ switch (getCurrentModule()->Kind) {
+ case Module::ModuleInterfaceUnit:
+ case Module::ModuleImplementationUnit:
+ case Module::ModulePartitionInterface:
+ case Module::ModulePartitionImplementation:
+ case Module::PrivateModuleFragment:
+ case Module::ImplicitGlobalModuleFragment:
+ return true;
+ default:
+ return false;
+ }
+}