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

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


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

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

Test Plan: check-clang

>From 300a567bd983754cc1187dc122f98c0f7d408b99 Mon Sep 17 00:00:00 2001
From: Dmitry Polukhin <dmitry.polukhin at gmail.com>
Date: Wed, 9 Oct 2024 04:01:42 -0700
Subject: [PATCH] [C++20][Modules] Allow import for a header unit after #pragma

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

Test Plan: check-clang
---
 clang/lib/Lex/Preprocessor.cpp                 | 13 ++++++++++---
 .../import_header_unit_after_pragma.cpp        | 18 ++++++++++++++++++
 2 files changed, 28 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Headers/import_header_unit_after_pragma.cpp

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}}



More information about the cfe-commits mailing list