vedgy updated this revision to Diff 481913.
vedgy edited the summary of this revision.
vedgy added a comment.

Extract identical code from the two Path.inc files into Path.cpp

One of the Path.inc files is #include-d into this Path.cpp file and nowhere 
else.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D139774/new/

https://reviews.llvm.org/D139774

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang-c/Index.h
  clang/tools/libclang/CIndex.cpp
  clang/tools/libclang/libclang.map
  llvm/include/llvm/Support/Path.h
  llvm/lib/Support/Path.cpp
  llvm/lib/Support/Unix/Path.inc
  llvm/lib/Support/Windows/Path.inc
  llvm/unittests/Support/Path.cpp

Index: llvm/unittests/Support/Path.cpp
===================================================================
--- llvm/unittests/Support/Path.cpp
+++ llvm/unittests/Support/Path.cpp
@@ -591,6 +591,26 @@
   EXPECT_TRUE(!TempDir.empty());
 }
 
+TEST(Support, SetTempDirectory) {
+  SmallString<64> DefaultTempDir;
+  path::system_temp_directory(true, DefaultTempDir);
+  EXPECT_TRUE(!DefaultTempDir.empty());
+
+  auto CustomTempDir = DefaultTempDir;
+  path::append(CustomTempDir, "/llvm/test_temp_dir");
+  path::native(CustomTempDir);
+  path::set_system_temp_directory_erased_on_reboot(CustomTempDir.c_str());
+
+  SmallString<64> TempDir;
+  path::system_temp_directory(true, TempDir);
+  EXPECT_EQ(CustomTempDir, TempDir);
+
+  path::set_system_temp_directory_erased_on_reboot(nullptr);
+  TempDir.clear();
+  path::system_temp_directory(true, TempDir);
+  EXPECT_EQ(DefaultTempDir, TempDir);
+}
+
 #ifdef _WIN32
 static std::string path2regex(std::string Path) {
   size_t Pos = 0;
Index: llvm/lib/Support/Windows/Path.inc
===================================================================
--- llvm/lib/Support/Windows/Path.inc
+++ llvm/lib/Support/Windows/Path.inc
@@ -1472,11 +1472,16 @@
   (void)ErasedOnReboot;
   Result.clear();
 
+  if (tempDirErasedOnRebootUtf8) {
+    const auto len = strlen(tempDirErasedOnRebootUtf8);
+    Result.append(tempDirErasedOnRebootUtf8, tempDirErasedOnRebootUtf8 + len);
+  }
+
   // Check whether the temporary directory is specified by an environment var.
   // This matches GetTempPath logic to some degree. GetTempPath is not used
   // directly as it cannot handle evn var longer than 130 chars on Windows 7
   // (fixed on Windows 8).
-  if (getTempDirEnvVar(Result)) {
+  if (!Result.empty() || getTempDirEnvVar(Result)) {
     assert(!Result.empty() && "Unexpected empty path");
     native(Result); // Some Unix-like shells use Unix path separator in $TMP.
     fs::make_absolute(Result); // Make it absolute if not already.
Index: llvm/lib/Support/Unix/Path.inc
===================================================================
--- llvm/lib/Support/Unix/Path.inc
+++ llvm/lib/Support/Unix/Path.inc
@@ -1451,8 +1451,11 @@
   Result.clear();
 
   if (ErasedOnReboot) {
+    const char *RequestedDir = tempDirErasedOnRebootUtf8;
     // There is no env variable for the cache directory.
-    if (const char *RequestedDir = getEnvTempDir()) {
+    if (!RequestedDir)
+      RequestedDir = getEnvTempDir();
+    if (RequestedDir) {
       Result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
       return;
     }
Index: llvm/lib/Support/Path.cpp
===================================================================
--- llvm/lib/Support/Path.cpp
+++ llvm/lib/Support/Path.cpp
@@ -780,6 +780,12 @@
   return true;
 }
 
+static const char *tempDirErasedOnRebootUtf8 = nullptr;
+
+void set_system_temp_directory_erased_on_reboot(const char *tempDirUtf8) {
+  tempDirErasedOnRebootUtf8 = tempDirUtf8;
+}
+
 } // end namespace path
 
 namespace fs {
Index: llvm/include/llvm/Support/Path.h
===================================================================
--- llvm/include/llvm/Support/Path.h
+++ llvm/include/llvm/Support/Path.h
@@ -412,6 +412,16 @@
 /// @param result Holds the resulting path name.
 void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result);
 
+/// Override the temporary directory path returned by
+/// system_temp_directory(true, result).
+///
+/// @param tempDirUtf8 UTF-8-encoded path to the desired temporary directory.
+/// The pointer is owned by the caller and must be always valid. Pass nullptr to
+/// this function in order to reset the temporary directory to the default value
+/// from the environment. Such a resetting should be done before deleting a
+/// tempDirUtf8 pointer previously passed to this function.
+void set_system_temp_directory_erased_on_reboot(const char *tempDirUtf8);
+
 /// Get the user's home directory.
 ///
 /// @param result Holds the resulting path name.
Index: clang/tools/libclang/libclang.map
===================================================================
--- clang/tools/libclang/libclang.map
+++ clang/tools/libclang/libclang.map
@@ -407,6 +407,7 @@
 
 LLVM_16 {
   global:
+    clang_setTemporaryDirectory;
     clang_getUnqualifiedType;
     clang_getNonReferenceType;
     clang_CXXMethod_isDeleted;
Index: clang/tools/libclang/CIndex.cpp
===================================================================
--- clang/tools/libclang/CIndex.cpp
+++ clang/tools/libclang/CIndex.cpp
@@ -48,6 +48,7 @@
 #include "llvm/Support/Format.h"
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Path.h"
 #include "llvm/Support/Program.h"
 #include "llvm/Support/SaveAndRestore.h"
 #include "llvm/Support/Signals.h"
@@ -3637,6 +3638,12 @@
 static llvm::ManagedStatic<RegisterFatalErrorHandler>
     RegisterFatalErrorHandlerOnce;
 
+void clang_setTemporaryDirectory(const char *tempDirUtf8) {
+  // libclang uses only erased-on-reboot temporary directory, so only it needs
+  // to be set here.
+  llvm::sys::path::set_system_temp_directory_erased_on_reboot(tempDirUtf8);
+}
+
 CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
                           int displayDiagnostics) {
   // We use crash recovery to make some of our APIs more reliable, implicitly
Index: clang/include/clang-c/Index.h
===================================================================
--- clang/include/clang-c/Index.h
+++ clang/include/clang-c/Index.h
@@ -34,7 +34,7 @@
  * compatible, thus CINDEX_VERSION_MAJOR is expected to remain stable.
  */
 #define CINDEX_VERSION_MAJOR 0
-#define CINDEX_VERSION_MINOR 62
+#define CINDEX_VERSION_MINOR 63
 
 #define CINDEX_VERSION_ENCODE(major, minor) (((major)*10000) + ((minor)*1))
 
@@ -69,6 +69,17 @@
  * @{
  */
 
+/**
+ * Override the temporary directory path used by Clang.
+ *
+ * \param tempDirUtf8 UTF-8-encoded path to the desired temporary directory.
+ * The pointer is owned by the caller and must be always valid. Pass nullptr to
+ * this function in order to reset the temporary directory to the default value
+ * from the environment. Such a resetting should be done before deleting a
+ * tempDirUtf8 pointer previously passed to this function.
+ */
+CINDEX_LINKAGE void clang_setTemporaryDirectory(const char *tempDirUtf8);
+
 /**
  * An "index" that consists of a set of translation units that would
  * typically be linked together into an executable or library.
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -859,6 +859,8 @@
 - Introduced the new function ``clang_CXXMethod_isMoveAssignmentOperator``,
   which identifies whether a method cursor is a move-assignment
   operator.
+- Introduced the new function ``clang_setTemporaryDirectory``,
+  which allows to override the temporary directory path used by Clang.
 - ``clang_Cursor_getNumTemplateArguments``, ``clang_Cursor_getTemplateArgumentKind``,
   ``clang_Cursor_getTemplateArgumentType``, ``clang_Cursor_getTemplateArgumentValue`` and
   ``clang_Cursor_getTemplateArgumentUnsignedValue`` now work on struct, class,
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to