[clang] [C++20][Modules] Allow import for a header unit after #pragma (PR #111662)

via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 9 04:29:59 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Dmitry Polukhin (dmpolukhin)

<details>
<summary>Changes</summary>

Summary:
`#pragma` and headers that finish with them shouldn't prevent `import "header_unit.h"` syntax.

Test Plan: check-clang

---
Full diff: https://github.com/llvm/llvm-project/pull/111662.diff


2 Files Affected:

- (modified) clang/lib/Lex/Preprocessor.cpp (+10-3) 
- (added) clang/test/Headers/import_header_unit_after_pragma.cpp (+18) 


``````````diff
diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp
index f0b4593e0cc22e..258154d1e7f45e 100644
--- a/clang/lib/Lex/Preprocessor.cpp
+++ b/clang/lib/Lex/Preprocessor.cpp
@@ -951,9 +951,16 @@ void Preprocessor::Lex(Token &Result) {
         break;
       [[fallthrough]];
     default:
-      TrackGMFState.handleMisc();
-      StdCXXImportSeqState.handleMisc();
-      ModuleDeclState.handleMisc();
+      if (tok::isPragmaAnnotation(Result.getKind())) {
+        // For `#pragma ...` mimic ';'.
+        TrackGMFState.handleSemi();
+        StdCXXImportSeqState.handleSemi();
+        ModuleDeclState.handleSemi();
+      } else {
+        TrackGMFState.handleMisc();
+        StdCXXImportSeqState.handleMisc();
+        ModuleDeclState.handleMisc();
+      }
       break;
     }
   }
diff --git a/clang/test/Headers/import_header_unit_after_pragma.cpp b/clang/test/Headers/import_header_unit_after_pragma.cpp
new file mode 100644
index 00000000000000..b1ad3b07fea29c
--- /dev/null
+++ b/clang/test/Headers/import_header_unit_after_pragma.cpp
@@ -0,0 +1,18 @@
+// RUN: rm -fR %t
+// RUN: split-file %s %t
+// RUN: cd %t
+// RUN: %clang_cc1 -verify -std=c++20 -emit-header-unit -xc++-user-header bz0.h
+// RUN: %clang_cc1 -verify -std=c++20 -emit-header-unit -xc++-user-header -fmodule-file=bz0.pcm bz.cpp
+
+//--- compare
+#pragma GCC visibility push(default)
+#pragma GCC visibility pop
+
+//--- bz0.h
+#include "compare"
+// expected-no-diagnostics
+
+//--- bz.cpp
+#include "compare"
+
+import "bz0.h"; // expected-warning {{the implementation of header units is in an experimental phase}}

``````````

</details>


https://github.com/llvm/llvm-project/pull/111662


More information about the cfe-commits mailing list