[C++] [Modules] Generate the initializer for modules if we compile a
authorChuanqi Xu <yedeng.yd@linux.alibaba.com>
Mon, 19 Sep 2022 03:03:46 +0000 (11:03 +0800)
committerChuanqi Xu <yedeng.yd@linux.alibaba.com>
Mon, 19 Sep 2022 03:30:34 +0000 (11:30 +0800)
module unit directly

Previously we lack a test which ensures that the module unit will
generate initializer if it is compiled directly (instead of from a pcm
file). Now we add the test back.

clang/lib/Parse/ParseAST.cpp
clang/test/CodeGenCXX/module-intializer.cpp

index 2e6d7ee..f442b62 100644 (file)
@@ -176,23 +176,21 @@ void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) {
   // and to be able to use a name based on the module name.
 
   // At this point, we should know if we are building a non-header C++20 module.
-  if (S.getLangOpts().CPlusPlusModules && !S.getLangOpts().IsHeaderFile &&
-      S.getLangOpts().isCompilingModuleInterface()) {
+  if (S.getLangOpts().CPlusPlusModules) {
     // If we are building the module from source, then the top level module
     // will be here.
     Module *CodegenModule = S.getCurrentModule();
     bool Interface = true;
     if (CodegenModule)
-      // We only use module initializers for interfaces (including partition
-      // implementation units).
+      // We only use module initializers for importable module (including
+      // partition implementation units).
       Interface = S.currentModuleIsInterface();
-    else
+    else if (S.getLangOpts().isCompilingModuleInterface())
       // If we are building the module from a PCM file, then the module can be
       // found here.
       CodegenModule = S.getPreprocessor().getCurrentModule();
-    // If neither. then ....
-    assert(CodegenModule && "codegen for a module, but don't know which?");
-    if (Interface)
+
+    if (Interface && CodegenModule)
       S.getASTContext().setModuleForCodeGen(CodegenModule);
   }
   Consumer->HandleTranslationUnit(S.getASTContext());
index 0b48e87..e514940 100644 (file)
 // RUN: -fmodule-file=M.pcm -S -emit-llvm  -o - \
 // RUN: | FileCheck %s --check-prefix=CHECK-IMPL
 
+// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 N.cpp -S -emit-llvm \
+// RUN:   -o - | FileCheck %s --check-prefix=CHECK-N
+
+// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 O.cpp -S -emit-llvm \
+// RUN:   -o - | FileCheck %s --check-prefix=CHECK-O
+
+// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 M-part.cpp -S -emit-llvm \
+// RUN:   -o - | FileCheck %s --check-prefix=CHECK-P
+
+// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 M.cpp \
+// RUN:   -fmodule-file=N.pcm -fmodule-file=O.pcm -fmodule-file=M-part.pcm \
+// RUN:   -S -emit-llvm -o - | FileCheck %s --check-prefix=CHECK-M
+
 //--- N-h.h
 
 struct Oink {