[clang] f9558c6 - [C++20] [Modules] Handle import decl before module declaration without being in GMF

Chuanqi Xu via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 27 19:28:40 PDT 2023


Author: Chuanqi Xu
Date: 2023-09-28T10:24:50+08:00
New Revision: f9558c691128cbc1660f69bd3b5f547be90ed18b

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

LOG: [C++20] [Modules] Handle import decl before module declaration without being in GMF

Close https://github.com/llvm/llvm-project/issues/67627

In a module unit, all the declaration before the modoule declaration
should live in the GMF.

Added: 
    clang/test/Modules/pr67627.cppm

Modified: 
    clang/lib/Parse/Parser.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp
index dda1dbaa0c21aa9..9d2c2fd9a5fa5d6 100644
--- a/clang/lib/Parse/Parser.cpp
+++ b/clang/lib/Parse/Parser.cpp
@@ -2564,6 +2564,10 @@ Decl *Parser::ParseModuleImport(SourceLocation AtLoc,
     SeenError = false;
     break;
   case Sema::ModuleImportState::FirstDecl:
+    // If we found an import decl as the first declaration, we must be not in
+    // a C++20 module unit or we are in an invalid state.
+    ImportState = Sema::ModuleImportState::NotACXX20Module;
+    [[fallthrough]];
   case Sema::ModuleImportState::NotACXX20Module:
     // We can only import a partition within a module purview.
     if (IsPartition)

diff  --git a/clang/test/Modules/pr67627.cppm b/clang/test/Modules/pr67627.cppm
new file mode 100644
index 000000000000000..3d4410229080a98
--- /dev/null
+++ b/clang/test/Modules/pr67627.cppm
@@ -0,0 +1,13 @@
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: split-file %s %t
+//
+// RUN: %clang_cc1 -std=c++20 %t/A.cppm -emit-module-interface -o %t/A.pcm
+// RUN: %clang_cc1 -std=c++20 %t/B.cppm -fmodule-file=A=%t/A.pcm -fsyntax-only -verify
+
+//--- A.cppm
+export module A;
+
+//--- B.cppm
+import A; // expected-note {{add 'module;' to the start of the file to introduce a global module fragment}}
+export module B; // expected-error {{module declaration must occur at the start of the translation unit}}


        


More information about the cfe-commits mailing list