[clang] [clang][modules] Print library module manifest path. (PR #76451)

Mark de Wever via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 11 11:22:57 PST 2024


https://github.com/mordante updated https://github.com/llvm/llvm-project/pull/76451

>From f3f0db64da4d341f8e4a2054f9f25c87f8eda829 Mon Sep 17 00:00:00 2001
From: Mark de Wever <koraq at xs4all.nl>
Date: Wed, 27 Dec 2023 17:34:10 +0100
Subject: [PATCH 1/3] [clang][modules] Print library module manifest path.

This implements a way for the compiler to find the modules.json
associated with the C++23 Standard library modules.

This is based on a discussion in SG15. At the moment no Standard library
installs this manifest. #75741 adds this feature in libc++.
---
 clang/include/clang/Driver/Driver.h           | 10 +++++
 clang/include/clang/Driver/Options.td         |  3 ++
 clang/lib/Driver/Driver.cpp                   | 40 +++++++++++++++++++
 .../usr/lib/x86_64-linux-gnu/libc++.so        |  0
 .../usr/lib/x86_64-linux-gnu/modules.json     |  0
 ...dules-print-library-module-manifest-path.c | 15 +++++++
 ...arwin-print-library-module-manifest-path.c |  9 +++++
 7 files changed, 77 insertions(+)
 create mode 100644 clang/test/Driver/Inputs/cxx23_modules/usr/lib/x86_64-linux-gnu/libc++.so
 create mode 100644 clang/test/Driver/Inputs/cxx23_modules/usr/lib/x86_64-linux-gnu/modules.json
 create mode 100644 clang/test/Driver/cxx23-modules-print-library-module-manifest-path.c
 create mode 100644 clang/test/Driver/darwin-print-library-module-manifest-path.c

diff --git a/clang/include/clang/Driver/Driver.h b/clang/include/clang/Driver/Driver.h
index 3ee1bcf2a69c9b..2e1e3b128744ff 100644
--- a/clang/include/clang/Driver/Driver.h
+++ b/clang/include/clang/Driver/Driver.h
@@ -602,6 +602,16 @@ class Driver {
   // FIXME: This should be in CompilationInfo.
   std::string GetProgramPath(StringRef Name, const ToolChain &TC) const;
 
+  /// GetModuleManifestPath - Lookup the name of the Standard library manifest.
+  ///
+  /// \param C - The compilation.
+  /// \param TC - The tool chain for additional information on
+  /// directories to search.
+  //
+  // FIXME: This should be in CompilationInfo.
+  std::string GetModuleManifestPath(const Compilation &C,
+                                    const ToolChain &TC) const;
+
   /// HandleAutocompletions - Handle --autocomplete by searching and printing
   /// possible flags, descriptions, and its arguments.
   void HandleAutocompletions(StringRef PassedFlags) const;
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 2b93ddf033499c..890257e11485b6 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -5280,6 +5280,9 @@ def print_resource_dir : Flag<["-", "--"], "print-resource-dir">,
 def print_search_dirs : Flag<["-", "--"], "print-search-dirs">,
   HelpText<"Print the paths used for finding libraries and programs">,
   Visibility<[ClangOption, CLOption]>;
+def print_library_module_manifest_path : Flag<["-", "--"], "print-library-module-manifest-path">,
+  HelpText<"Print the path for the C++ Standard library module manifest">,
+  Visibility<[ClangOption, CLOption]>;
 def print_targets : Flag<["-", "--"], "print-targets">,
   HelpText<"Print the registered targets">,
   Visibility<[ClangOption, CLOption]>;
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index ff95c899c5f3d4..8d682f9238c6b8 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -2164,6 +2164,12 @@ bool Driver::HandleImmediateArgs(const Compilation &C) {
     return false;
   }
 
+  if (C.getArgs().hasArg(options::OPT_print_library_module_manifest_path)) {
+    llvm::outs() << "module: ="
+                 << GetModuleManifestPath(C, C.getDefaultToolChain()) << '\n';
+    return false;
+  }
+
   if (C.getArgs().hasArg(options::OPT_print_runtime_dir)) {
     if (std::optional<std::string> RuntimePath = TC.getRuntimePath())
       llvm::outs() << *RuntimePath << '\n';
@@ -6135,6 +6141,40 @@ std::string Driver::GetProgramPath(StringRef Name, const ToolChain &TC) const {
   return std::string(Name);
 }
 
+std::string Driver::GetModuleManifestPath(const Compilation &C,
+                                          const ToolChain &TC) const {
+
+  switch (TC.GetCXXStdlibType(C.getArgs())) {
+  case ToolChain::CST_Libcxx: {
+    std::string lib = "libc++.so";
+    std::string path = GetFilePath(lib, TC);
+
+    // Note when there are multiple flavours of libc++ the module json needs to
+    // look at the command-line arguments for the proper json.
+
+    // For example
+    /*
+        const SanitizerArgs &Sanitize = TC.getSanitizerArgs(C.getArgs());
+        if (Sanitize.needsAsanRt())
+          return path.replace(path.size() - lib.size(), lib.size(),
+                              "modules-asan.json");
+    */
+
+    path = path.replace(path.size() - lib.size(), lib.size(), "modules.json");
+    if (TC.getVFS().exists(path))
+      return path;
+
+    return "";
+  }
+
+  case ToolChain::CST_Libstdcxx:
+    // libstdc++ does not provide Standard library modules yet.
+    return "";
+  }
+
+  return "";
+}
+
 std::string Driver::GetTemporaryPath(StringRef Prefix, StringRef Suffix) const {
   SmallString<128> Path;
   std::error_code EC = llvm::sys::fs::createTemporaryFile(Prefix, Suffix, Path);
diff --git a/clang/test/Driver/Inputs/cxx23_modules/usr/lib/x86_64-linux-gnu/libc++.so b/clang/test/Driver/Inputs/cxx23_modules/usr/lib/x86_64-linux-gnu/libc++.so
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/Inputs/cxx23_modules/usr/lib/x86_64-linux-gnu/modules.json b/clang/test/Driver/Inputs/cxx23_modules/usr/lib/x86_64-linux-gnu/modules.json
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/cxx23-modules-print-library-module-manifest-path.c b/clang/test/Driver/cxx23-modules-print-library-module-manifest-path.c
new file mode 100644
index 00000000000000..7ad2b10081bc5b
--- /dev/null
+++ b/clang/test/Driver/cxx23-modules-print-library-module-manifest-path.c
@@ -0,0 +1,15 @@
+// Test that -print-library-module-manifest-path finds the correct file.
+
+// RUN: %clang -print-library-module-manifest-path \
+// RUN:     -stdlib=libc++ \
+// RUN:     --sysroot=%S/Inputs/cxx23_modules \
+// RUN:     --target=x86_64-linux-gnu 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-LIBCXX %s
+// CHECK-LIBCXX: module: ={{.*}}/Inputs/cxx23_modules/usr/lib/x86_64-linux-gnu/modules.json
+
+// RUN: %clang -print-library-module-manifest-path \
+// RUN:     -stdlib=libstdc++ \
+// RUN:     --sysroot=%S/Inputs/cxx23_modules \
+// RUN:     --target=x86_64-linux-gnu 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-LIBSTDCXX %s
+// CHECK-LIBSTDCXX: module: =
diff --git a/clang/test/Driver/darwin-print-library-module-manifest-path.c b/clang/test/Driver/darwin-print-library-module-manifest-path.c
new file mode 100644
index 00000000000000..39d28589b146ae
--- /dev/null
+++ b/clang/test/Driver/darwin-print-library-module-manifest-path.c
@@ -0,0 +1,9 @@
+// Test that -print-library-module-manifest-path finds the correct file.
+//
+// Note this file is currently not available on Apple platforms
+
+// RUN: %clang -print-library-module-manifest-path \
+// RUN:     -resource-dir=%S/Inputs/resource_dir \
+// RUN:     --target=x86_64-unknown-linux-gnu 2>&1 \
+// RUN:   | FileCheck %s
+// CHECK: module: =

>From 24faf65e51d54f35905e782e3bf7842c966e13e4 Mon Sep 17 00:00:00 2001
From: Mark de Wever <koraq at xs4all.nl>
Date: Wed, 3 Jan 2024 18:54:24 +0100
Subject: [PATCH 2/3] Adress review comments.

---
 clang/include/clang/Driver/Driver.h            |  7 ++++---
 clang/include/clang/Driver/Options.td          |  2 +-
 clang/lib/Driver/Driver.cpp                    | 18 ++++++++++--------
 ...les-print-library-module-manifest-path.cpp} |  4 ++--
 ...win-print-library-module-manifest-path.cpp} |  4 ++--
 5 files changed, 19 insertions(+), 16 deletions(-)
 rename clang/test/Driver/{cxx23-modules-print-library-module-manifest-path.c => cxx23-modules-print-library-module-manifest-path.cpp} (81%)
 rename clang/test/Driver/{darwin-print-library-module-manifest-path.c => darwin-print-library-module-manifest-path.cpp} (77%)

diff --git a/clang/include/clang/Driver/Driver.h b/clang/include/clang/Driver/Driver.h
index 2e1e3b128744ff..9c42650882d01a 100644
--- a/clang/include/clang/Driver/Driver.h
+++ b/clang/include/clang/Driver/Driver.h
@@ -602,15 +602,16 @@ class Driver {
   // FIXME: This should be in CompilationInfo.
   std::string GetProgramPath(StringRef Name, const ToolChain &TC) const;
 
-  /// GetModuleManifestPath - Lookup the name of the Standard library manifest.
+  /// GetStdModuleManifestPath - Lookup the path to the Standard library module
+  /// manifest.
   ///
   /// \param C - The compilation.
   /// \param TC - The tool chain for additional information on
   /// directories to search.
   //
   // FIXME: This should be in CompilationInfo.
-  std::string GetModuleManifestPath(const Compilation &C,
-                                    const ToolChain &TC) const;
+  std::string GetStdModuleManifestPath(const Compilation &C,
+                                       const ToolChain &TC) const;
 
   /// HandleAutocompletions - Handle --autocomplete by searching and printing
   /// possible flags, descriptions, and its arguments.
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 890257e11485b6..0ba81580686cf8 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -5280,7 +5280,7 @@ def print_resource_dir : Flag<["-", "--"], "print-resource-dir">,
 def print_search_dirs : Flag<["-", "--"], "print-search-dirs">,
   HelpText<"Print the paths used for finding libraries and programs">,
   Visibility<[ClangOption, CLOption]>;
-def print_library_module_manifest_path : Flag<["-", "--"], "print-library-module-manifest-path">,
+def print_std_module_manifest_path : Flag<["-", "--"], "print-library-module-manifest-path">,
   HelpText<"Print the path for the C++ Standard library module manifest">,
   Visibility<[ClangOption, CLOption]>;
 def print_targets : Flag<["-", "--"], "print-targets">,
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 8d682f9238c6b8..41315a6e10a252 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -2164,9 +2164,9 @@ bool Driver::HandleImmediateArgs(const Compilation &C) {
     return false;
   }
 
-  if (C.getArgs().hasArg(options::OPT_print_library_module_manifest_path)) {
-    llvm::outs() << "module: ="
-                 << GetModuleManifestPath(C, C.getDefaultToolChain()) << '\n';
+  if (C.getArgs().hasArg(options::OPT_print_std_module_manifest_path)) {
+    llvm::outs() << GetStdModuleManifestPath(C, C.getDefaultToolChain())
+                 << '\n';
     return false;
   }
 
@@ -6141,8 +6141,10 @@ std::string Driver::GetProgramPath(StringRef Name, const ToolChain &TC) const {
   return std::string(Name);
 }
 
-std::string Driver::GetModuleManifestPath(const Compilation &C,
-                                          const ToolChain &TC) const {
+std::string Driver::GetStdModuleManifestPath(const Compilation &C,
+                                             const ToolChain &TC) const {
+
+  std::string error = "<NOT PRESENT>";
 
   switch (TC.GetCXXStdlibType(C.getArgs())) {
   case ToolChain::CST_Libcxx: {
@@ -6164,15 +6166,15 @@ std::string Driver::GetModuleManifestPath(const Compilation &C,
     if (TC.getVFS().exists(path))
       return path;
 
-    return "";
+    return error;
   }
 
   case ToolChain::CST_Libstdcxx:
     // libstdc++ does not provide Standard library modules yet.
-    return "";
+    return error;
   }
 
-  return "";
+  return error;
 }
 
 std::string Driver::GetTemporaryPath(StringRef Prefix, StringRef Suffix) const {
diff --git a/clang/test/Driver/cxx23-modules-print-library-module-manifest-path.c b/clang/test/Driver/cxx23-modules-print-library-module-manifest-path.cpp
similarity index 81%
rename from clang/test/Driver/cxx23-modules-print-library-module-manifest-path.c
rename to clang/test/Driver/cxx23-modules-print-library-module-manifest-path.cpp
index 7ad2b10081bc5b..1467959c1a7574 100644
--- a/clang/test/Driver/cxx23-modules-print-library-module-manifest-path.c
+++ b/clang/test/Driver/cxx23-modules-print-library-module-manifest-path.cpp
@@ -5,11 +5,11 @@
 // RUN:     --sysroot=%S/Inputs/cxx23_modules \
 // RUN:     --target=x86_64-linux-gnu 2>&1 \
 // RUN:   | FileCheck --check-prefix=CHECK-LIBCXX %s
-// CHECK-LIBCXX: module: ={{.*}}/Inputs/cxx23_modules/usr/lib/x86_64-linux-gnu/modules.json
+// CHECK-LIBCXX: /{{.*}}/Inputs/cxx23_modules/usr/lib/x86_64-linux-gnu/modules.json
 
 // RUN: %clang -print-library-module-manifest-path \
 // RUN:     -stdlib=libstdc++ \
 // RUN:     --sysroot=%S/Inputs/cxx23_modules \
 // RUN:     --target=x86_64-linux-gnu 2>&1 \
 // RUN:   | FileCheck --check-prefix=CHECK-LIBSTDCXX %s
-// CHECK-LIBSTDCXX: module: =
+// CHECK-LIBSTDCXX: <NOT PRESENT>
diff --git a/clang/test/Driver/darwin-print-library-module-manifest-path.c b/clang/test/Driver/darwin-print-library-module-manifest-path.cpp
similarity index 77%
rename from clang/test/Driver/darwin-print-library-module-manifest-path.c
rename to clang/test/Driver/darwin-print-library-module-manifest-path.cpp
index 39d28589b146ae..4b0bd12454a0e0 100644
--- a/clang/test/Driver/darwin-print-library-module-manifest-path.c
+++ b/clang/test/Driver/darwin-print-library-module-manifest-path.cpp
@@ -2,8 +2,8 @@
 //
 // Note this file is currently not available on Apple platforms
 
-// RUN: %clang -print-library-module-manifest-path \
+// RUN: %clang --print-library-module-manifest-path \
 // RUN:     -resource-dir=%S/Inputs/resource_dir \
 // RUN:     --target=x86_64-unknown-linux-gnu 2>&1 \
 // RUN:   | FileCheck %s
-// CHECK: module: =
+// CHECK: <NOT PRESENT>

>From 6c5fb461a5a174179037f29a4f37261e51a2a57f Mon Sep 17 00:00:00 2001
From: Mark de Wever <koraq at xs4all.nl>
Date: Thu, 11 Jan 2024 20:22:44 +0100
Subject: [PATCH 3/3] Windows CI fix.

---
 .../Driver/cxx23-modules-print-library-module-manifest-path.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/Driver/cxx23-modules-print-library-module-manifest-path.cpp b/clang/test/Driver/cxx23-modules-print-library-module-manifest-path.cpp
index 1467959c1a7574..baaf8be76df903 100644
--- a/clang/test/Driver/cxx23-modules-print-library-module-manifest-path.cpp
+++ b/clang/test/Driver/cxx23-modules-print-library-module-manifest-path.cpp
@@ -5,7 +5,7 @@
 // RUN:     --sysroot=%S/Inputs/cxx23_modules \
 // RUN:     --target=x86_64-linux-gnu 2>&1 \
 // RUN:   | FileCheck --check-prefix=CHECK-LIBCXX %s
-// CHECK-LIBCXX: /{{.*}}/Inputs/cxx23_modules/usr/lib/x86_64-linux-gnu/modules.json
+// CHECK-LIBCXX: {{.*}}/Inputs/cxx23_modules/usr/lib/x86_64-linux-gnu/modules.json
 
 // RUN: %clang -print-library-module-manifest-path \
 // RUN:     -stdlib=libstdc++ \



More information about the cfe-commits mailing list