[clang] dc61a53 - [Modules] Don't treat piped input as file input (#233371)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 19 17:59:29 PDT 2026
Author: Chuanqi Xu
Date: 2026-10-20T08:59:23+08:00
New Revision: dc61a534a58ddf2d7e2b5ebd6d4d60fa514254a7
URL: https://github.com/llvm/llvm-project/commit/dc61a534a58ddf2d7e2b5ebd6d4d60fa514254a7
DIFF: https://github.com/llvm/llvm-project/commit/dc61a534a58ddf2d7e2b5ebd6d4d60fa514254a7.diff
LOG: [Modules] Don't treat piped input as file input (#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.
Added:
clang/test/Modules/stdin-source-locations.cppm
Modified:
clang/lib/Serialization/ASTWriter.cpp
Removed:
################################################################################
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
diff er 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 at 2 {{member reference base type 'int' is not a structure or union}}
+// expected-error at 3 {{member reference base type 'int' is not a structure or union}}
More information about the cfe-commits
mailing list