[PATCH] D126694: [C++20][Modules] Initial implementation of GMF decl elision.

Iain Sandoe via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 31 05:43:26 PDT 2022


iains created this revision.
Herald added a project: All.
iains added reviewers: urnathan, ChuanqiXu.
iains added a subscriber: clang-modules.
iains published this revision for review.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

The basis for removal of GMF decls is that they have no use
in the module interface.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D126694

Files:
  clang/lib/Sema/Sema.cpp
  clang/test/CXX/basic/basic.scope/basic.scope.namespace/p2.cpp


Index: clang/test/CXX/basic/basic.scope/basic.scope.namespace/p2.cpp
===================================================================
--- clang/test/CXX/basic/basic.scope/basic.scope.namespace/p2.cpp
+++ clang/test/CXX/basic/basic.scope/basic.scope.namespace/p2.cpp
@@ -29,8 +29,7 @@
 #endif
 
 void test_early() {
-  in_header = 1; // expected-error {{missing '#include "foo.h"'; 'in_header' must be declared before it is used}}
-  // expected-note@*{{not visible}}
+  in_header = 1; // expected-error {{use of undeclared identifier 'in_header'}}
 
   global_module_fragment = 1; // expected-error {{missing '#include'; 'global_module_fragment' must be declared before it is used}}
   // expected-note at p2.cpp:16 {{not visible}}
@@ -54,8 +53,7 @@
 #endif
 
 void test_late() {
-  in_header = 1; // expected-error {{missing '#include "foo.h"'; 'in_header' must be declared before it is used}}
-  // expected-note@*{{not visible}}
+  in_header = 1; // expected-error {{use of undeclared identifier 'in_header'}}
 
   global_module_fragment = 1; // expected-error {{missing '#include'; 'global_module_fragment' must be declared before it is used}}
   // expected-note at p2.cpp:16 {{not visible}}
Index: clang/lib/Sema/Sema.cpp
===================================================================
--- clang/lib/Sema/Sema.cpp
+++ clang/lib/Sema/Sema.cpp
@@ -1128,6 +1128,33 @@
   if (TUKind != TU_Prefix) {
     DiagnoseUseOfUnimplementedSelectors();
 
+    // For C++20 modules, we are permitted to elide decls in the Global
+    // Module Fragment of a module interface if they are unused in the body
+    // of the interface.
+    if (!ModuleScopes.empty() && ModuleScopes.back().ModuleInterface &&
+        getLangOpts().CPlusPlusModules) {
+      auto *DC = getASTContext().getTranslationUnitDecl();
+      SmallVector<Decl *, 2> WorkList;
+      for (DeclContext::decl_iterator DI = DC->decls_begin(),
+                                      DEnd = DC->decls_end();
+           DI != DEnd; ++DI) {
+        Decl *D = *DI;
+        Module *M;
+        if (D->isImplicit() || !isa<NamedDecl>(D))
+          continue;
+        M = D->getOwningModule();
+        if (!M || !D->getOwningModule()->isGlobalModule())
+          continue;
+        if (getASTContext().moduleInitializerContains(M, D))
+          continue;
+        if (!D->isUsed(false) && !D->isReferenced())
+          WorkList.push_back(D);
+      }
+
+      for (auto *D : WorkList)
+        DC->removeDecl(D);
+    }
+
     ActOnEndOfTranslationUnitFragment(
         !ModuleScopes.empty() && ModuleScopes.back().Module->Kind ==
                                      Module::PrivateModuleFragment


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126694.433061.patch
Type: text/x-patch
Size: 2660 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220531/5e669fae/attachment-0001.bin>


More information about the cfe-commits mailing list