[clang] 3eb2da7 - [NFC] [C++20] [Modules] Simplify ActOnModuleImport by merging Path and Parition

Chuanqi Xu via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 2 07:06:59 PST 2022


Author: Chuanqi Xu
Date: 2022-03-02T23:06:36+08:00
New Revision: 3eb2da76d77044802135350668da16d433a7c7b6

URL: https://github.com/llvm/llvm-project/commit/3eb2da76d77044802135350668da16d433a7c7b6
DIFF: https://github.com/llvm/llvm-project/commit/3eb2da76d77044802135350668da16d433a7c7b6.diff

LOG: [NFC] [C++20] [Modules] Simplify ActOnModuleImport by merging Path and Parition

Reviewed By: iains

Differential Revision: https://reviews.llvm.org/D120793

Added: 
    

Modified: 
    clang/include/clang/Sema/Sema.h
    clang/lib/Parse/Parser.cpp
    clang/lib/Sema/SemaModule.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index d905db93ccfff..9496d72cadcab 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -2994,11 +2994,11 @@ class Sema final {
   /// \param ExportLoc The location of the 'export' keyword, if any.
   /// \param ImportLoc The location of the 'import' keyword.
   /// \param Path The module toplevel name as an access path.
-  /// \param Partition The module partition name as an access path.
+  /// \param IsPartition If the name is for a partition.
   DeclResult ActOnModuleImport(SourceLocation StartLoc,
                                SourceLocation ExportLoc,
                                SourceLocation ImportLoc, ModuleIdPath Path,
-                               ModuleIdPath Partition = {});
+                               bool IsPartition = false);
   DeclResult ActOnModuleImport(SourceLocation StartLoc,
                                SourceLocation ExportLoc,
                                SourceLocation ImportLoc, Module *M,

diff  --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp
index b7e4be6e436e3..bc5b504ad1b8f 100644
--- a/clang/lib/Parse/Parser.cpp
+++ b/clang/lib/Parse/Parser.cpp
@@ -2420,7 +2420,7 @@ Decl *Parser::ParseModuleImport(SourceLocation AtLoc,
 
   // For C++20 modules, we can have "name" or ":Partition name" as valid input.
   SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
-  SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Partition;
+  bool IsPartition = false;
   Module *HeaderUnit = nullptr;
   if (Tok.is(tok::header_name)) {
     // This is a header import that the preprocessor decided we should skip
@@ -2435,10 +2435,12 @@ Decl *Parser::ParseModuleImport(SourceLocation AtLoc,
     SourceLocation ColonLoc = ConsumeToken();
     if (!getLangOpts().CPlusPlusModules)
       Diag(ColonLoc, diag::err_unsupported_module_partition)
-          << SourceRange(ColonLoc, Partition.back().second);
+          << SourceRange(ColonLoc, Path.back().second);
     // Recover by leaving partition empty.
-    else if (ParseModuleName(ColonLoc, Partition, /*IsImport*/ true))
+    else if (ParseModuleName(ColonLoc, Path, /*IsImport*/ true))
       return nullptr;
+    else
+      IsPartition = true;
   } else {
     if (ParseModuleName(ImportLoc, Path, /*IsImport*/ true))
       return nullptr;
@@ -2457,7 +2459,6 @@ Decl *Parser::ParseModuleImport(SourceLocation AtLoc,
 
   // Diagnose mis-imports.
   bool SeenError = true;
-  bool HasPart = !Partition.empty();
   switch (ImportState) {
   case Sema::ModuleImportState::ImportAllowed:
     SeenError = false;
@@ -2465,7 +2466,7 @@ Decl *Parser::ParseModuleImport(SourceLocation AtLoc,
   case Sema::ModuleImportState::FirstDecl:
   case Sema::ModuleImportState::NotACXX20Module:
     // We can only import a partition within a module purview.
-    if (HasPart)
+    if (IsPartition)
       Diag(ImportLoc, diag::err_partition_import_outside_module);
     else
       SeenError = false;
@@ -2474,7 +2475,7 @@ Decl *Parser::ParseModuleImport(SourceLocation AtLoc,
     // We can only have pre-processor directives in the global module
     // fragment.  We can, however have a header unit import here.
     if (!HeaderUnit)
-      Diag(ImportLoc, diag::err_import_in_wrong_fragment) << HasPart << 0;
+      Diag(ImportLoc, diag::err_import_in_wrong_fragment) << IsPartition << 0;
     else
       SeenError = false;
     break;
@@ -2485,7 +2486,7 @@ Decl *Parser::ParseModuleImport(SourceLocation AtLoc,
       SeenError = false;
     break;
   case Sema::ModuleImportState::PrivateFragment:
-    Diag(ImportLoc, diag::err_import_in_wrong_fragment) << HasPart << 1;
+    Diag(ImportLoc, diag::err_import_in_wrong_fragment) << IsPartition << 1;
     break;
   }
   if (SeenError) {
@@ -2497,9 +2498,9 @@ Decl *Parser::ParseModuleImport(SourceLocation AtLoc,
   if (HeaderUnit)
     Import =
         Actions.ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, HeaderUnit);
-  else if (!Path.empty() || !Partition.empty())
+  else if (!Path.empty())
     Import = Actions.ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Path,
-                                       Partition);
+                                       IsPartition);
   ExpectAndConsumeSemi(diag::err_module_expected_semi);
   if (Import.isInvalid())
     return nullptr;

diff  --git a/clang/lib/Sema/SemaModule.cpp b/clang/lib/Sema/SemaModule.cpp
index 6bb886bfe53c6..d12de22241f2c 100644
--- a/clang/lib/Sema/SemaModule.cpp
+++ b/clang/lib/Sema/SemaModule.cpp
@@ -365,13 +365,10 @@ Sema::ActOnPrivateModuleFragmentDecl(SourceLocation ModuleLoc,
 DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc,
                                    SourceLocation ExportLoc,
                                    SourceLocation ImportLoc, ModuleIdPath Path,
-                                   ModuleIdPath Partition) {
+                                   bool IsPartition) {
 
-  bool IsPartition = !Partition.empty();
   bool Cxx20Mode = getLangOpts().CPlusPlusModules || getLangOpts().ModulesTS;
   assert((!IsPartition || Cxx20Mode) && "partition seen in non-C++20 code?");
-  assert((!IsPartition || Path.empty()) &&
-         "trying to import a partition with its named module specified?");
 
   // For a C++20 module name, flatten into a single identifier with the source
   // location of the first component.
@@ -386,9 +383,9 @@ DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc,
     // otherwise, the name of the importing named module.
     ModuleName = NamedMod->getPrimaryModuleInterfaceName().str();
     ModuleName += ":";
-    ModuleName += stringFromPath(Partition);
-    ModuleNameLoc = {PP.getIdentifierInfo(ModuleName), Partition[0].second};
-    Partition = ModuleIdPath(ModuleNameLoc);
+    ModuleName += stringFromPath(Path);
+    ModuleNameLoc = {PP.getIdentifierInfo(ModuleName), Path[0].second};
+    Path = ModuleIdPath(ModuleNameLoc);
   } else if (Cxx20Mode) {
     ModuleName = stringFromPath(Path);
     ModuleNameLoc = {PP.getIdentifierInfo(ModuleName), Path[0].second};
@@ -410,13 +407,11 @@ DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc,
   }
 
   Module *Mod = getModuleLoader().loadModule(
-      ImportLoc, IsPartition ? Partition : Path, Module::AllVisible,
-      /*IsInclusionDirective=*/false);
+      ImportLoc, Path, Module::AllVisible, /*IsInclusionDirective=*/false);
   if (!Mod)
     return true;
 
-  return ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Mod,
-                           IsPartition ? Partition : Path);
+  return ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Mod, Path);
 }
 
 /// Determine whether \p D is lexically within an export-declaration.


        


More information about the cfe-commits mailing list