Skip to content

Commit

Permalink
[ExpressionParser] Add swift-lldb case for finding clang resource dir
Browse files Browse the repository at this point in the history
Summary:
I'm adding this to reduce the difference between swift-lldb and
llvm.org's lldb.

Reviewers: aprantl, davide, compnerd, JDevlieghere, jingham

Subscribers: lldb-commits

Differential Revision: https://reviews.llvm.org/D59708

llvm-svn: 357030
  • Loading branch information
bulbazord committed Mar 26, 2019
1 parent bb5cba3 commit 982726e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
45 changes: 32 additions & 13 deletions lldb/source/Plugins/ExpressionParser/Clang/ClangHost.cpp
Expand Up @@ -16,6 +16,7 @@
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Threading.h"

#include "lldb/Host/Config.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Utility/FileSpec.h"
Expand All @@ -40,25 +41,43 @@ static bool VerifyClangPath(const llvm::Twine &clang_path) {
/// This will compute the clang resource directory assuming that clang was
/// installed with the same prefix as lldb.
///
/// If verify is true, the first candidate resource directory will be returned.
/// This mode is only used for testing.
///
static bool DefaultComputeClangResourceDirectory(FileSpec &lldb_shlib_spec,
FileSpec &file_spec, bool verify) {
FileSpec &file_spec,
bool verify) {
Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
std::string raw_path = lldb_shlib_spec.GetPath();
llvm::StringRef parent_dir = llvm::sys::path::parent_path(raw_path);

llvm::SmallString<256> clang_dir(parent_dir);
llvm::SmallString<32> relative_path;
llvm::sys::path::append(relative_path,
llvm::Twine("lib") + CLANG_LIBDIR_SUFFIX, "clang",
CLANG_VERSION_STRING);

llvm::sys::path::append(clang_dir, relative_path);
if (!verify || VerifyClangPath(clang_dir)) {
file_spec.GetDirectory().SetString(clang_dir);
FileSystem::Instance().Resolve(file_spec);
return true;
static const llvm::StringRef kResourceDirSuffixes[] = {
// LLVM.org's build of LLDB uses the clang resource directory placed
// in $install_dir/lib{,64}/clang/$clang_version.
"lib" CLANG_LIBDIR_SUFFIX "/clang/" CLANG_VERSION_STRING,
// swift-lldb uses the clang resource directory copied from swift, which
// by default is placed in $install_dir/lib{,64}/lldb/clang. LLDB places
// it there, so we use LLDB_LIBDIR_SUFFIX.
"lib" LLDB_LIBDIR_SUFFIX "/lldb/clang",
};

for (const auto &Suffix : kResourceDirSuffixes) {
llvm::SmallString<256> clang_dir(parent_dir);
llvm::SmallString<32> relative_path(Suffix);
llvm::sys::path::native(relative_path);
llvm::sys::path::append(clang_dir, relative_path);
if (!verify || VerifyClangPath(clang_dir)) {
if (log)
log->Printf("DefaultComputeClangResourceDir: Setting ClangResourceDir "
"to \"%s\", verify = %s",
clang_dir.str().str().c_str(), verify ? "true" : "false");
file_spec.GetDirectory().SetString(clang_dir);
FileSystem::Instance().Resolve(file_spec);
return true;
}
}

return HostInfo::ComputePathRelativeToLibrary(file_spec, relative_path);
return false;
}

bool lldb_private::ComputeClangResourceDirectory(FileSpec &lldb_shlib_spec,
Expand Down
5 changes: 2 additions & 3 deletions lldb/unittests/Expression/ClangParserTest.cpp
Expand Up @@ -50,9 +50,8 @@ TEST_F(ClangHostTest, ComputeClangResourceDirectory) {
EXPECT_EQ(ComputeClangResourceDir(path_to_liblldb), path_to_clang_dir);

// The path doesn't really exist, so setting verify to true should make
// ComputeClangResourceDir to not give you path_to_clang_dir.
EXPECT_NE(ComputeClangResourceDir(path_to_liblldb, true),
ComputeClangResourceDir(path_to_liblldb));
// ComputeClangResourceDir not give you path_to_clang_dir.
EXPECT_NE(ComputeClangResourceDir(path_to_liblldb, true), path_to_clang_dir);
}

#if defined(__APPLE__)
Expand Down

0 comments on commit 982726e

Please sign in to comment.