Capture C++ modules compilation commands in compile_commands.json
This change ensures that .modulemap files are included in the exported
compilation database. It also implements the {{cc_module_name}}
substitution to correctly generate -fmodule-name flags in the compile
commands.
Modified compile_commands_writer_unittest.cc to verify that module
compilation commands are correctly generated and included.
This is to fix https://crbug.com/475763034#comment9
Bug: 475763034
Change-Id: I3693cb555a30df9082f611aa690e561d1427d4ff
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/21320
Reviewed-by: Sylvain Defresne <sdefresne@chromium.org>
Commit-Queue: Takuto Ikuta <tikuta@google.com>
diff --git a/src/gn/compile_commands_writer.cc b/src/gn/compile_commands_writer.cc
index 29d2a5a..bef0287 100644
--- a/src/gn/compile_commands_writer.cc
+++ b/src/gn/compile_commands_writer.cc
@@ -229,6 +229,8 @@
} else if (range.type == &CSubstitutionCFlagsObjCc) {
if (source_type == SourceFile::SOURCE_MM)
out << flags.cflags_objcc;
+ } else if (range.type == &CSubstitutionModuleName) {
+ EscapeStringToStream(out, target->module_name(), opts);
} else if (range.type == &SubstitutionLabel ||
range.type == &SubstitutionLabelName ||
range.type == &SubstitutionLabelNoToolchain ||
@@ -295,7 +297,8 @@
if (source_type != SourceFile::SOURCE_CPP &&
source_type != SourceFile::SOURCE_C &&
source_type != SourceFile::SOURCE_M &&
- source_type != SourceFile::SOURCE_MM)
+ source_type != SourceFile::SOURCE_MM &&
+ source_type != SourceFile::SOURCE_MODULEMAP)
continue;
const char* tool_name = Tool::kToolNone;
diff --git a/src/gn/compile_commands_writer_unittest.cc b/src/gn/compile_commands_writer_unittest.cc
index 9dff19d..d7fcad0 100644
--- a/src/gn/compile_commands_writer_unittest.cc
+++ b/src/gn/compile_commands_writer_unittest.cc
@@ -626,8 +626,8 @@
Tool::CreateTool(CTool::kCToolCxxModule);
TestWithScope::SetCommandForTool(
"c++ {{source}} {{cflags}} {{cflags_cc}} {{module_deps_no_self}} "
- "{{defines}} {{include_dirs}} -fmodule-name={{label}} -c -x c++ "
- "-Xclang -emit-module -o {{output}}",
+ "{{defines}} {{include_dirs}} -fmodule-name={{cc_module_name}} -c "
+ "-x c++ -Xclang -emit-module -o {{output}}",
cxx_module_tool.get());
cxx_module_tool->set_outputs(SubstitutionList::MakeForTest(
"{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.pcm"));
@@ -663,6 +663,13 @@
const char expected[] =
"[\r\n"
" {\r\n"
+ " \"file\": \"../../foo/foo.modulemap\",\r\n"
+ " \"directory\": \"out/Debug\",\r\n"
+ " \"command\": \"c++ ../../foo/foo.modulemap "
+ "-fmodule-name=module -c -x c++ -Xclang -emit-module -o "
+ "withmodules/obj/foo/module.foo.pcm\"\r\n"
+ " },\r\n"
+ " {\r\n"
" \"file\": \"../../foo/dep.cc\",\r\n"
" \"directory\": \"out/Debug\",\r\n"
" \"command\": \"c++ ../../foo/dep.cc "
@@ -674,6 +681,13 @@
const char expected[] =
"[\n"
" {\n"
+ " \"file\": \"../../foo/foo.modulemap\",\n"
+ " \"directory\": \"out/Debug\",\n"
+ " \"command\": \"c++ ../../foo/foo.modulemap "
+ "-fmodule-name=module -c -x c++ -Xclang -emit-module -o "
+ "withmodules/obj/foo/module.foo.pcm\"\n"
+ " },\n"
+ " {\n"
" \"file\": \"../../foo/dep.cc\",\n"
" \"directory\": \"out/Debug\",\n"
" \"command\": \"c++ ../../foo/dep.cc "