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

Mark de Wever via cfe-commits cfe-commits at lists.llvm.org
Sun Jan 21 02:46:55 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/8] [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/8] 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/8] 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++ \

>From a8f9dc9edd98416a84d524ca6b233a27882f5085 Mon Sep 17 00:00:00 2001
From: Mark de Wever <koraq at xs4all.nl>
Date: Fri, 12 Jan 2024 17:42:28 +0100
Subject: [PATCH 4/8] Address review commands and attempt to fix the CI.

---
 clang/lib/Driver/Driver.cpp                   | 12 +++++------
 ...les-print-library-module-manifest-path.cpp | 20 +++++++++++++++----
 ...win-print-library-module-manifest-path.cpp |  9 ---------
 3 files changed, 22 insertions(+), 19 deletions(-)
 delete mode 100644 clang/test/Driver/darwin-print-library-module-manifest-path.cpp

diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 41315a6e10a252..a3cae06fc66b43 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -6153,14 +6153,14 @@ std::string Driver::GetStdModuleManifestPath(const Compilation &C,
 
     // Note when there are multiple flavours of libc++ the module json needs to
     // look at the command-line arguments for the proper json.
+	// These flavours do not exist at the moment, but there are plans to
+	// provide a variant that is built with sanitizer instrumentation enabled.
 
     // For example
-    /*
-        const SanitizerArgs &Sanitize = TC.getSanitizerArgs(C.getArgs());
-        if (Sanitize.needsAsanRt())
-          return path.replace(path.size() - lib.size(), lib.size(),
-                              "modules-asan.json");
-    */
+    //  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))
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 baaf8be76df903..a4241721181cb0 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
@@ -1,15 +1,27 @@
 // Test that -print-library-module-manifest-path finds the correct file.
 
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: split-file %s %t
+// RUN: cd %t
+
 // 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: {{.*}}/Inputs/cxx23_modules/usr/lib/x86_64-linux-gnu/modules.json
+// RUN:   | FileCheck libcxx.cpp
 
 // 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: <NOT PRESENT>
+// RUN:   | FileCheck -- libstdcxx.cpp
+
+//--- libcxx.cpp
+
+// The final path separator differs on Windows and Linux.
+// CHECK: {{.*}}/Inputs/cxx23_modules/usr/lib/x86_64-linux-gnu{{[\/]}}.modules.json
+
+//--- libstdcxx.cpp
+
+// CHECK: <NOT PRESENT>
diff --git a/clang/test/Driver/darwin-print-library-module-manifest-path.cpp b/clang/test/Driver/darwin-print-library-module-manifest-path.cpp
deleted file mode 100644
index 4b0bd12454a0e0..00000000000000
--- a/clang/test/Driver/darwin-print-library-module-manifest-path.cpp
+++ /dev/null
@@ -1,9 +0,0 @@
-// 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: <NOT PRESENT>

>From 506302ebd1bc1a13d5f9743fd32fd40adccf3397 Mon Sep 17 00:00:00 2001
From: Mark de Wever <koraq at xs4all.nl>
Date: Thu, 18 Jan 2024 18:55:34 +0100
Subject: [PATCH 5/8] Address review comments.

---
 .../usr/lib/x86_64-linux-gnu/libc++.so        |  0
 .../usr/lib/x86_64-linux-gnu/modules.json     |  0
 ...les-print-library-module-manifest-path.cpp | 19 ++++++++++++++++---
 3 files changed, 16 insertions(+), 3 deletions(-)
 delete mode 100644 clang/test/Driver/Inputs/cxx23_modules/usr/lib/x86_64-linux-gnu/libc++.so
 delete mode 100644 clang/test/Driver/Inputs/cxx23_modules/usr/lib/x86_64-linux-gnu/modules.json

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
deleted file mode 100644
index e69de29bb2d1d6..00000000000000
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
deleted file mode 100644
index e69de29bb2d1d6..00000000000000
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 a4241721181cb0..742f9a7ff9f3f2 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
@@ -2,12 +2,20 @@
 
 // RUN: rm -rf %t
 // RUN: mkdir -p %t
+// RUN: touch %t/libc++.so
 // RUN: split-file %s %t
 // RUN: cd %t
 
 // RUN: %clang -print-library-module-manifest-path \
 // RUN:     -stdlib=libc++ \
-// RUN:     --sysroot=%S/Inputs/cxx23_modules \
+// RUN:     --sysroot=%t \
+// RUN:     --target=x86_64-linux-gnu 2>&1 \
+// RUN:   | FileCheck libcxx-no-module-json.cpp
+
+// RUN: touch %t/modules.json
+// RUN: %clang -print-library-module-manifest-path \
+// RUN:     -stdlib=libc++ \
+// RUN:     --sysroot=%t \
 // RUN:     --target=x86_64-linux-gnu 2>&1 \
 // RUN:   | FileCheck libcxx.cpp
 
@@ -15,12 +23,17 @@
 // RUN:     -stdlib=libstdc++ \
 // RUN:     --sysroot=%S/Inputs/cxx23_modules \
 // RUN:     --target=x86_64-linux-gnu 2>&1 \
-// RUN:   | FileCheck -- libstdcxx.cpp
+// RUN:   | FileCheck libstdcxx.cpp
+
+//--- libcxx-no-module-json.cpp
+
+// The final path separator differs on Windows and Linux.
+// CHECK: <NOT PRESENT>
 
 //--- libcxx.cpp
 
 // The final path separator differs on Windows and Linux.
-// CHECK: {{.*}}/Inputs/cxx23_modules/usr/lib/x86_64-linux-gnu{{[\/]}}.modules.json
+// CHECK: modules.json
 
 //--- libstdcxx.cpp
 

>From 4f5a7346a45f8a4f47b6f50b97e1d343d70ab207 Mon Sep 17 00:00:00 2001
From: Mark de Wever <koraq at xs4all.nl>
Date: Fri, 19 Jan 2024 17:45:06 +0100
Subject: [PATCH 6/8] address review comments.

---
 clang/include/clang/Driver/Driver.h           |  3 +--
 clang/lib/Driver/Driver.cpp                   | 27 ++++++++++---------
 ...es-print-library-module-manifest-path.cpp} | 20 ++++++--------
 3 files changed, 24 insertions(+), 26 deletions(-)
 rename clang/test/Driver/{cxx23-modules-print-library-module-manifest-path.cpp => modules-print-library-module-manifest-path.cpp} (64%)

diff --git a/clang/include/clang/Driver/Driver.h b/clang/include/clang/Driver/Driver.h
index 9c42650882d01a..595f104e406d37 100644
--- a/clang/include/clang/Driver/Driver.h
+++ b/clang/include/clang/Driver/Driver.h
@@ -602,8 +602,7 @@ class Driver {
   // FIXME: This should be in CompilationInfo.
   std::string GetProgramPath(StringRef Name, const ToolChain &TC) const;
 
-  /// GetStdModuleManifestPath - Lookup the path to the Standard library module
-  /// manifest.
+  /// Lookup the path to the Standard library module manifest.
   ///
   /// \param C - The compilation.
   /// \param TC - The tool chain for additional information on
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index a3cae06fc66b43..511cc80bdfa191 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -6143,28 +6143,31 @@ std::string Driver::GetProgramPath(StringRef Name, 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: {
-    std::string lib = "libc++.so";
-    std::string path = GetFilePath(lib, TC);
+    std::string lib = GetFilePath("libc++.so", TC);
 
     // Note when there are multiple flavours of libc++ the module json needs to
     // look at the command-line arguments for the proper json.
-	// These flavours do not exist at the moment, but there are plans to
-	// provide a variant that is built with sanitizer instrumentation enabled.
+    // These flavours do not exist at the moment, but there are plans to
+    // provide a variant that is built with sanitizer instrumentation enabled.
 
     // 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");
+    //  StringRef modules = [&] {
+    //    const SanitizerArgs &Sanitize = TC.getSanitizerArgs(C.getArgs());
+    //    if (Sanitize.needsAsanRt())
+    //      return "modules-asan.json";
+    //    return "modules.json";
+    //  }();
+    StringRef modules = "modules.json";
+
+    SmallString<128> path(lib.begin(), lib.end());
+    llvm::sys::path::remove_filename(path);
+    llvm::sys::path::append(path, modules);
     if (TC.getVFS().exists(path))
-      return path;
+      return static_cast<std::string>(path);
 
     return error;
   }
diff --git a/clang/test/Driver/cxx23-modules-print-library-module-manifest-path.cpp b/clang/test/Driver/modules-print-library-module-manifest-path.cpp
similarity index 64%
rename from clang/test/Driver/cxx23-modules-print-library-module-manifest-path.cpp
rename to clang/test/Driver/modules-print-library-module-manifest-path.cpp
index 742f9a7ff9f3f2..8e0069de3340dd 100644
--- a/clang/test/Driver/cxx23-modules-print-library-module-manifest-path.cpp
+++ b/clang/test/Driver/modules-print-library-module-manifest-path.cpp
@@ -1,39 +1,35 @@
 // Test that -print-library-module-manifest-path finds the correct file.
 
-// RUN: rm -rf %t
-// RUN: mkdir -p %t
-// RUN: touch %t/libc++.so
-// RUN: split-file %s %t
-// RUN: cd %t
+// RUN: rm -rf %t && split-file %s %t && cd %t
+// RUN: mkdir -p %t/Inputs/usr/lib/x86_64-linux-gnu
+// RUN: touch %t/Inputs/usr/lib/x86_64-linux-gnu/libc++.so
 
 // RUN: %clang -print-library-module-manifest-path \
 // RUN:     -stdlib=libc++ \
-// RUN:     --sysroot=%t \
+// RUN:     --sysroot=%t/Inputs \
 // RUN:     --target=x86_64-linux-gnu 2>&1 \
 // RUN:   | FileCheck libcxx-no-module-json.cpp
 
-// RUN: touch %t/modules.json
+// RUN: touch %t/Inputs/usr/lib/x86_64-linux-gnu/modules.json
 // RUN: %clang -print-library-module-manifest-path \
 // RUN:     -stdlib=libc++ \
-// RUN:     --sysroot=%t \
+// RUN:     --sysroot=%t/Inputs \
 // RUN:     --target=x86_64-linux-gnu 2>&1 \
 // RUN:   | FileCheck libcxx.cpp
 
 // RUN: %clang -print-library-module-manifest-path \
 // RUN:     -stdlib=libstdc++ \
-// RUN:     --sysroot=%S/Inputs/cxx23_modules \
+// RUN:     --sysroot=%t/Inputs \
 // RUN:     --target=x86_64-linux-gnu 2>&1 \
 // RUN:   | FileCheck libstdcxx.cpp
 
 //--- libcxx-no-module-json.cpp
 
-// The final path separator differs on Windows and Linux.
 // CHECK: <NOT PRESENT>
 
 //--- libcxx.cpp
 
-// The final path separator differs on Windows and Linux.
-// CHECK: modules.json
+// CHECK: {{.*}}/Inputs/usr/lib/x86_64-linux-gnu{{/|\\\\}}modules.json
 
 //--- libstdcxx.cpp
 

>From c1237662790b20da52bdf9ed66e3a2bda7a6052c Mon Sep 17 00:00:00 2001
From: Mark de Wever <koraq at xs4all.nl>
Date: Sat, 20 Jan 2024 21:12:19 +0100
Subject: [PATCH 7/8] Simpify test to check Windows

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

diff --git a/clang/test/Driver/modules-print-library-module-manifest-path.cpp b/clang/test/Driver/modules-print-library-module-manifest-path.cpp
index 8e0069de3340dd..f605cd5b12c076 100644
--- a/clang/test/Driver/modules-print-library-module-manifest-path.cpp
+++ b/clang/test/Driver/modules-print-library-module-manifest-path.cpp
@@ -29,7 +29,7 @@
 
 //--- libcxx.cpp
 
-// CHECK: {{.*}}/Inputs/usr/lib/x86_64-linux-gnu{{/|\\\\}}modules.json
+// CHECK: {{.*}}modules.json
 
 //--- libstdcxx.cpp
 

>From 7bfaf9736edc7a28be6e0f66d6ad4f40ab5b92b6 Mon Sep 17 00:00:00 2001
From: Mark de Wever <koraq at xs4all.nl>
Date: Sun, 21 Jan 2024 11:46:29 +0100
Subject: [PATCH 8/8] Try a longer path component.

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

diff --git a/clang/test/Driver/modules-print-library-module-manifest-path.cpp b/clang/test/Driver/modules-print-library-module-manifest-path.cpp
index f605cd5b12c076..bd9d19f21e4e82 100644
--- a/clang/test/Driver/modules-print-library-module-manifest-path.cpp
+++ b/clang/test/Driver/modules-print-library-module-manifest-path.cpp
@@ -29,7 +29,7 @@
 
 //--- libcxx.cpp
 
-// CHECK: {{.*}}modules.json
+// CHECK: {{.*}}{{/|\\\\}}modules.json
 
 //--- libstdcxx.cpp
 



More information about the cfe-commits mailing list