Skip to content

Commit bbff1d2

Browse files
committed
Use reference instead of ptr and remove unused function
Signed-off-by: Shreyas Atre <[email protected]>
1 parent e61e7ca commit bbff1d2

File tree

5 files changed

+39
-43
lines changed

5 files changed

+39
-43
lines changed

lib/Interpreter/DynamicLibraryManager.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ namespace Cpp {
365365
// TODO: !permanent case
366366

367367
std::string errMsg;
368-
DyLibHandle dyLibHandle = platform::DLOpen(canonicalLoadedLib, &errMsg);
368+
DyLibHandle dyLibHandle = platform::DLOpen(canonicalLoadedLib, errMsg);
369369
if (!dyLibHandle) {
370370
// We emit callback to LibraryLoadingFailed when we get error with error message.
371371
//TODO: Implement callbacks
@@ -403,7 +403,7 @@ namespace Cpp {
403403
// TODO: !permanent case
404404

405405
std::string errMsg;
406-
platform::DLClose(dyLibHandle, &errMsg);
406+
platform::DLClose(dyLibHandle, errMsg);
407407
if (!errMsg.empty()) {
408408
LLVM_DEBUG(dbgs() << "cling::DynamicLibraryManager::unloadLibrary(): "
409409
<< errMsg << '\n');

lib/Interpreter/Paths.cpp

+21-36
Original file line numberDiff line numberDiff line change
@@ -106,53 +106,38 @@ namespace platform {
106106
return std::string(Buffer.str());
107107
}
108108

109-
#if defined(LLVM_ON_UNIX)
110-
static void DLErr(std::string* Err) {
111-
if (!Err)
112-
return;
113-
if (const char* DyLibError = ::dlerror())
114-
*Err = DyLibError;
109+
#if defined(_WIN32)
110+
void* DLOpen(const std::string& Path, std::string& Err) {
111+
auto lib = llvm::sys::DynamicLibrary::getLibrary(Path.c_str(), &Err);
112+
return lib.getOSSpecificHandle();
115113
}
116114

117-
void* DLOpen(const std::string& Path, std::string* Err /* = nullptr */) {
118-
void* Lib = ::dlopen(Path.c_str(), RTLD_LAZY | RTLD_GLOBAL);
119-
DLErr(Err);
120-
return Lib;
115+
void DLClose(void* Lib, std::string& Err) {
116+
auto dl = llvm::sys::DynamicLibrary(Lib);
117+
llvm::sys::DynamicLibrary::closeLibrary(dl);
118+
if (Err.empty()) {
119+
Err = std::string();
120+
}
121+
}
122+
#elif defined(LLVM_ON_UNIX)
123+
static void DLErr(std::string& Err) {
124+
if (const char* DyLibError = ::dlerror())
125+
Err = std::string(DyLibError);
121126
}
122127

123-
void* DLSym(const std::string& Name, std::string* Err /* = nullptr*/) {
124-
if (void* Self = ::dlopen(nullptr, RTLD_GLOBAL)) {
125-
// get dlopen error if there is one
126-
DLErr(Err);
127-
void* Sym = ::dlsym(Self, Name.c_str());
128-
// overwrite error if dlsym caused one
128+
void* DLOpen(const std::string& Path, std::string& Err) {
129+
void* Lib = ::dlopen(Path.c_str(), RTLD_LAZY | RTLD_GLOBAL);
130+
if (Lib == nullptr) {
129131
DLErr(Err);
130-
// only get dlclose error if dlopen & dlsym haven't emited one
131-
DLClose(Self, Err && Err->empty() ? Err : nullptr);
132-
return Sym;
132+
return nullptr;
133133
}
134-
DLErr(Err);
135-
return nullptr;
134+
return Lib;
136135
}
137136

138-
void DLClose(void* Lib, std::string* Err /* = nullptr*/) {
137+
void DLClose(void* Lib, std::string& Err) {
139138
::dlclose(Lib);
140139
DLErr(Err);
141140
}
142-
#elif defined(_WIN32)
143-
144-
void* DLOpen(const std::string& Path, std::string* Err /* = nullptr */) {
145-
auto lib = llvm::sys::DynamicLibrary::getLibrary(Path.c_str(), Err);
146-
return lib.getOSSpecificHandle();
147-
}
148-
149-
void DLClose(void* Lib, std::string* Err /* = nullptr*/) {
150-
auto dl = llvm::sys::DynamicLibrary(Lib);
151-
llvm::sys::DynamicLibrary::closeLibrary(dl);
152-
if (Err) {
153-
*Err = std::string();
154-
}
155-
}
156141
#endif
157142

158143
} // namespace platform

lib/Interpreter/Paths.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ std::string NormalizePath(const std::string& Path);
4747
///
4848
/// \returns the library handle
4949
///
50-
void* DLOpen(const std::string& Path, std::string* Err = nullptr);
51-
52-
void* DLSym(const std::string& Name, std::string* Err = nullptr);
50+
void* DLOpen(const std::string& Path, std::string& Err);
5351

5452
///\brief Close a handle to a shared library.
5553
///
@@ -58,7 +56,7 @@ void* DLSym(const std::string& Name, std::string* Err = nullptr);
5856
///
5957
/// \returns the library handle
6058
///
61-
void DLClose(void* Lib, std::string* Err = nullptr);
59+
void DLClose(void* Lib, std::string& Err);
6260
} // namespace platform
6361

6462
///\brief Replace all $TOKENS in a string with environent variable values.

unittests/CppInterOp/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export_executable_symbols(CppInterOpTests)
2222

2323
unset(LLVM_LINK_COMPONENTS)
2424

25-
add_cppinterop_unittest(DynamicLibraryManagerTests DynamicLibraryManagerTest.cpp)
25+
add_cppinterop_unittest(DynamicLibraryManagerTests DynamicLibraryManagerTest.cpp ${CMAKE_SOURCE_DIR}/lib/Interpreter/Paths.cpp)
2626
target_link_libraries(DynamicLibraryManagerTests
2727
PRIVATE
2828
clangCppInterOp

unittests/CppInterOp/DynamicLibraryManagerTest.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "llvm/Support/FileSystem.h"
66
#include "llvm/Support/Path.h"
77

8+
#include "../../lib/Interpreter/Paths.h"
9+
810
// This function isn't referenced outside its translation unit, but it
911
// can't use the "static" keyword because its address is used for
1012
// GetMainExecutable (since some platforms don't support taking the
@@ -40,6 +42,17 @@ TEST(DynamicLibraryManagerTest, Sanity) {
4042
<< "Cannot find: '" << PathToTestSharedLib << "' in '" << Dir.str()
4143
<< "'";
4244

45+
// DLOPEN DLCLOSE Test
46+
std::string err = "";
47+
auto* dlopen_handle = Cpp::utils::platform::DLOpen(PathToTestSharedLib, err);
48+
EXPECT_TRUE(dlopen_handle) << "Error occurred: " << err << "\n";
49+
Cpp::utils::platform::DLClose(dlopen_handle, err);
50+
EXPECT_TRUE(err.empty()) << "Error occurred: " << err << "\n";
51+
Cpp::utils::platform::DLOpen("missing", err);
52+
EXPECT_TRUE(err.find("no such file") != std::string::npos ||
53+
err.find("No such file") != std::string::npos);
54+
// DLOPEN DLCLOSE Test end
55+
4356
EXPECT_TRUE(Cpp::LoadLibrary(PathToTestSharedLib.c_str()));
4457
// Force ExecutionEngine to be created.
4558
Cpp::Process("");

0 commit comments

Comments
 (0)