[clang] [C++20] [Modules] Write comments in C++20 modules' module file (PR #192398)
Chuanqi Xu via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 15 23:21:37 PDT 2026
https://github.com/ChuanqiXu9 created https://github.com/llvm/llvm-project/pull/192398
Previously we avoid writing the comments in C++20 modules' module file.
But this prevents LSP tools to read the comments in it. Although we thought to add a new option for it and ask LSP to use the new option, the cost of comments seems to be low and new option raises complexity, so I prefer to write comments in C++20 modules' module file by default now.
>From b84878e36d4972270d185c834105df295a060151 Mon Sep 17 00:00:00 2001
From: Chuanqi Xu <yedeng.yd at linux.alibaba.com>
Date: Thu, 16 Apr 2026 14:20:16 +0800
Subject: [PATCH] [C++20] [Modules] Write comments in C++20 modules' module
file
Previously we avoid writing the comments in C++20 modules' module file.
But this prevents LSP tools to read the comments in it. Although we
thought to add a new option for it and ask LSP to use the new
option, the cost of comments seems to be low and new option raises
complexity, so I prefer to write comments in C++20 modules' module file by
default now.
---
clang/lib/Serialization/ASTWriter.cpp | 6 ------
clang/unittests/Serialization/CMakeLists.txt | 2 +-
.../{NoCommentsTest.cpp => CommentsTest.cpp} | 10 +++++-----
3 files changed, 6 insertions(+), 12 deletions(-)
rename clang/unittests/Serialization/{NoCommentsTest.cpp => CommentsTest.cpp} (94%)
diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp
index 632137f01d767..4d8f922da9044 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -3530,12 +3530,6 @@ void ASTWriter::WriteComments(ASTContext &Context) {
if (!PP->getPreprocessorOpts().WriteCommentListToPCH)
return;
- // Don't write comments to BMI to reduce the size of BMI.
- // If language services (e.g., clangd) want such abilities,
- // we can offer a special option then.
- if (isWritingStdCXXNamedModules())
- return;
-
RecordData Record;
for (const auto &FO : Context.Comments.OrderedComments) {
for (const auto &OC : FO.second) {
diff --git a/clang/unittests/Serialization/CMakeLists.txt b/clang/unittests/Serialization/CMakeLists.txt
index a5cc1ed83af49..f0837c10b3815 100644
--- a/clang/unittests/Serialization/CMakeLists.txt
+++ b/clang/unittests/Serialization/CMakeLists.txt
@@ -3,7 +3,7 @@ add_clang_unittest(SerializationTests
InMemoryModuleCacheTest.cpp
ModuleCacheTest.cpp
NamespaceLookupTest.cpp
- NoCommentsTest.cpp
+ CommentsTest.cpp
PreambleInNamedModulesTest.cpp
LoadSpecLazilyTest.cpp
SourceLocationEncodingTest.cpp
diff --git a/clang/unittests/Serialization/NoCommentsTest.cpp b/clang/unittests/Serialization/CommentsTest.cpp
similarity index 94%
rename from clang/unittests/Serialization/NoCommentsTest.cpp
rename to clang/unittests/Serialization/CommentsTest.cpp
index 444a082bba907..71d66075c76d2 100644
--- a/clang/unittests/Serialization/NoCommentsTest.cpp
+++ b/clang/unittests/Serialization/CommentsTest.cpp
@@ -1,4 +1,4 @@
-//===- unittests/Serialization/NoComments.cpp - CI tests -----===//
+//===- unittests/Serialization/Comments.cpp - CI tests -----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
@@ -27,7 +27,7 @@ using namespace clang;
namespace {
-class NoComments : public ::testing::Test {
+class Comments : public ::testing::Test {
void SetUp() override {
ASSERT_FALSE(
sys::fs::createUniqueDirectory("modules-no-comments-test", TestDir));
@@ -54,7 +54,7 @@ class NoComments : public ::testing::Test {
}
};
-TEST_F(NoComments, NonModulesTest) {
+TEST_F(Comments, NonModulesTest) {
std::unique_ptr<ASTUnit> AST = tooling::buildASTFromCodeWithArgs(
R"cpp(
/// Any comments
@@ -76,7 +76,7 @@ void foo() {}
"/// Any comments");
}
-TEST_F(NoComments, ModulesTest) {
+TEST_F(Comments, ModulesTest) {
addFile("Comments.cppm", R"cpp(
export module Comments;
@@ -124,7 +124,7 @@ import Comments;
EXPECT_TRUE(foo);
const RawComment *RC = getCompletionComment(Ctx, foo);
- EXPECT_FALSE(RC);
+ EXPECT_TRUE(RC);
}
} // anonymous namespace
More information about the cfe-commits
mailing list