From 39b920e68f6449303e2aacc8751780a74dd6f72d Mon Sep 17 00:00:00 2001 From: Shreyas Atre Date: Sat, 3 Feb 2024 12:19:19 -0600 Subject: [PATCH] Remove nullptr comments and fix relative path for libraries Signed-off-by: Shreyas Atre --- lib/Interpreter/Paths.cpp | 16 +++++++++------- .../CppInterOp/DynamicLibraryManagerTest.cpp | 11 +++++++---- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/Interpreter/Paths.cpp b/lib/Interpreter/Paths.cpp index 367592a0..d4393402 100644 --- a/lib/Interpreter/Paths.cpp +++ b/lib/Interpreter/Paths.cpp @@ -107,12 +107,12 @@ namespace platform { } #if defined(_WIN32) - void* DLOpen(const std::string& Path, std::string& Err /* = nullptr */) { + void* DLOpen(const std::string& Path, std::string& Err) { auto lib = llvm::sys::DynamicLibrary::getLibrary(Path.c_str(), &Err); return lib.getOSSpecificHandle(); } - void DLClose(void* Lib, std::string& Err /* = nullptr*/) { + void DLClose(void* Lib, std::string& Err) { auto dl = llvm::sys::DynamicLibrary(Lib); llvm::sys::DynamicLibrary::closeLibrary(dl); if (Err.empty()) { @@ -121,19 +121,21 @@ namespace platform { } #else static void DLErr(std::string& Err) { - if (Err.empty()) - return; if (const char* DyLibError = ::dlerror()) Err = std::string(DyLibError); } - void* DLOpen(const std::string& Path, std::string& Err /* = nullptr */) { + void* DLOpen(const std::string& Path, std::string& Err) { void* Lib = ::dlopen(Path.c_str(), RTLD_LAZY | RTLD_GLOBAL); - DLErr(Err); + if (Lib == nullptr) + { + DLErr(Err); + return nullptr; + } return Lib; } - void DLClose(void* Lib, std::string& Err /* = nullptr*/) { + void DLClose(void* Lib, std::string& Err) { ::dlclose(Lib); DLErr(Err); } diff --git a/unittests/CppInterOp/DynamicLibraryManagerTest.cpp b/unittests/CppInterOp/DynamicLibraryManagerTest.cpp index 5050b908..b4f14c13 100644 --- a/unittests/CppInterOp/DynamicLibraryManagerTest.cpp +++ b/unittests/CppInterOp/DynamicLibraryManagerTest.cpp @@ -61,13 +61,16 @@ TEST(UtilsPlatform, DLTest) { std::string err = ""; #if defined(__APPLE__) auto dlopen_handle = Cpp::utils::platform::DLOpen( - "./unittests/CppInterOp/libTestSharedLib.dylib", err); + "./libTestSharedLib.dylib", err); #elif defined(_WIN32) auto dlopen_handle = Cpp::utils::platform::DLOpen( - "./unittests/CppInterOp/libTestSharedLib.dll", err); + "./libTestSharedLib.dll", err); #else auto dlopen_handle = Cpp::utils::platform::DLOpen( - "./unittests/CppInterOp/libTestSharedLib.so", err); + "./libTestSharedLib.so", err); #endif - (void)dlopen_handle; + EXPECT_TRUE(dlopen_handle); + EXPECT_TRUE(err.empty()); + Cpp::utils::platform::DLOpen("missing", err); + EXPECT_TRUE(err.find("no such file") != std::string::npos); }