https://github.com/ChuanqiXu9 created 
https://github.com/llvm/llvm-project/pull/233371

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

The root cause of the problem is, clang treat the filename from stdin as 
<stdin>. Then when there are multiple input files from stdin, the cache status 
is polluted.

This patch fixes this as not treating piped input as file input.

This may show another concern as if we have different file contents with the 
same name, it may be problematic.

>From fa810be9c72be6bfeeaba480266e9d8f25045a73 Mon Sep 17 00:00:00 2001
From: Chuanqi Xu <[email protected]>
Date: Mon, 19 Oct 2026 16:48:43 +0800
Subject: [PATCH] [Modules] Don't treat piped input as file input

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

The root cause of the problem is, clang treat the filename from stdin
as <stdin>. Then when there are multiple input files from stdin, the
cache status is polluted.

This patch fixes this as not treating piped input as file input.

This may show another concern as if we have different file contents
with the same name, it may be problematic.
---
 clang/lib/Serialization/ASTWriter.cpp         |  8 +++--
 .../test/Modules/stdin-source-locations.cppm  | 34 +++++++++++++++++++
 2 files changed, 39 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Modules/stdin-source-locations.cppm

diff --git a/clang/lib/Serialization/ASTWriter.cpp 
b/clang/lib/Serialization/ASTWriter.cpp
index 0afba1c84fd62..520d7a4644f33 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -1897,7 +1897,9 @@ void ASTWriter::WriteInputFiles(SourceManager &SourceMgr) 
{
       continue;
     const SrcMgr::FileInfo &File = SLoc->getFile();
     const SrcMgr::ContentCache *Cache = &File.getContentCache();
-    if (!Cache->OrigEntry)
+    // Named pipes, including stdin, are serialized as buffers rather than
+    // files, since their contents can differ between AST files.
+    if (!Cache->OrigEntry || Cache->OrigEntry->isNamedPipe())
       continue;
 
     // Do not emit input files that do not affect current module.
@@ -2411,7 +2413,7 @@ void ASTWriter::WriteSourceManagerBlock(SourceManager 
&SourceMgr) {
     unsigned Code;
     if (SLoc->isFile()) {
       const SrcMgr::ContentCache *Cache = &SLoc->getFile().getContentCache();
-      if (Cache->OrigEntry) {
+      if (Cache->OrigEntry && !Cache->OrigEntry->isNamedPipe()) {
         Code = SM_SLOC_FILE_ENTRY;
       } else
         Code = SM_SLOC_BUFFER_ENTRY;
@@ -2433,7 +2435,7 @@ void ASTWriter::WriteSourceManagerBlock(SourceManager 
&SourceMgr) {
       Record.push_back(File.hasLineDirectives());
 
       bool EmitBlob = false;
-      if (Content->OrigEntry) {
+      if (Code == SM_SLOC_FILE_ENTRY) {
         assert(Content->OrigEntry == Content->ContentsEntry &&
                "Writing to AST an overridden file is not supported");
 
diff --git a/clang/test/Modules/stdin-source-locations.cppm 
b/clang/test/Modules/stdin-source-locations.cppm
new file mode 100644
index 0000000000000..79e1a38c57a9e
--- /dev/null
+++ b/clang/test/Modules/stdin-source-locations.cppm
@@ -0,0 +1,34 @@
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: split-file %s %t
+//
+// RUN: %clang_cc1 -std=c++20 -x c++ -emit-module-interface -o %t/A.pcm - < 
%t/a.cppm
+// RUN: %clang_cc1 -std=c++20 -x c++ -emit-module-interface -o %t/B.pcm - < 
%t/b.cppm
+// RUN: %clang_cc1 -std=c++20 -fmodule-file=A=%t/A.pcm 
-fmodule-file=B=%t/B.pcm -fsyntax-only -verify %t/use.cpp
+// RUN: %clang_cc1 -std=c++20 -fmodule-file=A=%t/A.pcm 
-fmodule-file=B=%t/B.pcm -x c++ -fsyntax-only -verify - < %t/use.cpp
+//
+// Test again with reduced BMI.
+// RUN: %clang_cc1 -std=c++20 -x c++ -emit-reduced-module-interface 
-fmodules-embed-all-files -o %t/A.pcm - < %t/a.cppm
+// RUN: %clang_cc1 -std=c++20 -x c++ -emit-reduced-module-interface 
-fmodules-embed-all-files -o %t/B.pcm - < %t/b.cppm
+// RUN: %clang_cc1 -std=c++20 -fmodule-file=A=%t/A.pcm 
-fmodule-file=B=%t/B.pcm -fsyntax-only -verify %t/use.cpp
+// RUN: %clang_cc1 -std=c++20 -fmodule-file=A=%t/A.pcm 
-fmodule-file=B=%t/B.pcm -x c++ -fsyntax-only -verify - < %t/use.cpp
+
+//--- a.cppm
+export module A;
+export template <class T> void fail_a(T t) { t.missing_a(); }
+
+//--- b.cppm
+export module B;
+
+export template <class T> void fail_b(T t) { t.missing_b(); }
+
+//--- use.cpp
+import A;
+import B;
+void test() {
+  fail_a(0); // expected-note {{in instantiation of function template 
specialization 'fail_a<int>' requested here}}
+  fail_b(0); // expected-note {{in instantiation of function template 
specialization 'fail_b<int>' requested here}}
+}
+
+// expected-error@2 {{member reference base type 'int' is not a structure or 
union}}
+// expected-error@3 {{member reference base type 'int' is not a structure or 
union}}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to