Index: packages/Python/lldbsuite/test/functionalities/paths/TestPaths.py =================================================================== --- packages/Python/lldbsuite/test/functionalities/paths/TestPaths.py +++ packages/Python/lldbsuite/test/functionalities/paths/TestPaths.py @@ -32,6 +32,15 @@ # No directory path types should have the filename set self.assertTrue(f.GetFilename() is None) + # TODO: Merge into test_path when GetClangResourceDir is implemented on + # windows + @expectedFailureAll(oslist=["windows"]) + @no_debug_info_test + def test_clang_dir_path(self): + '''Test to make sure clang dir is set correctly''' + clang_dir = lldb.SBHostOS.GetLLDBPath(lldb.ePathTypeClangDir) + self.assertFalse(clang_dir.GetDirectory() is None) + @no_debug_info_test def test_directory_doesnt_end_with_slash(self): current_directory_spec = lldb.SBFileSpec(os.path.curdir) Index: source/Plugins/ExpressionParser/Clang/ClangHost.h =================================================================== --- source/Plugins/ExpressionParser/Clang/ClangHost.h +++ source/Plugins/ExpressionParser/Clang/ClangHost.h @@ -13,9 +13,9 @@ class FileSpec; -#if defined(__APPLE__) -bool ComputeClangDirectory(FileSpec &lldb_shlib_spec, FileSpec &file_spec, - bool verify); +#if !defined(_WIN32) +bool ComputeClangResourceDirectory(FileSpec &lldb_shlib_spec, + FileSpec &file_spec, bool verify); #endif FileSpec GetClangResourceDir(); Index: source/Plugins/ExpressionParser/Clang/ClangHost.cpp =================================================================== --- source/Plugins/ExpressionParser/Clang/ClangHost.cpp +++ source/Plugins/ExpressionParser/Clang/ClangHost.cpp @@ -29,17 +29,8 @@ using namespace lldb_private; #if defined(_WIN32) -static bool ComputeClangDirectory(FileSpec &file_spec) { return false; } +static bool ComputeClangResourceDirectory(FileSpec &file_spec) { return false; } #else -static bool DefaultComputeClangDirectory(FileSpec &file_spec) { - return HostInfoPosix::ComputePathRelativeToLibrary( - file_spec, (llvm::Twine("/lib") + CLANG_LIBDIR_SUFFIX + "/clang/" + - CLANG_VERSION_STRING) - .str()); -} - -#if defined(__APPLE__) - static bool VerifyClangPath(const llvm::Twine &clang_path) { if (FileSystem::Instance().IsDirectory(clang_path)) return true; @@ -51,9 +42,38 @@ return false; } -bool lldb_private::ComputeClangDirectory(FileSpec &lldb_shlib_spec, +/// +/// This will compute the clang resource directory assuming that clang was +/// installed with the same prefix as lldb. +/// +static bool DefaultComputeClangResourceDirectory(FileSpec &lldb_shlib_spec, FileSpec &file_spec, bool verify) { 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; + } + + return HostInfoPosix::ComputePathRelativeToLibrary(file_spec, relative_path); +} + +bool lldb_private::ComputeClangResourceDirectory(FileSpec &lldb_shlib_spec, + FileSpec &file_spec, bool verify) { +#if !defined(__APPLE__) + return DefaultComputeClangResourceDirectory(lldb_shlib_spec, file_spec, + verify); +#else + std::string raw_path = lldb_shlib_spec.GetPath(); auto rev_it = llvm::sys::path::rbegin(raw_path); auto r_end = llvm::sys::path::rend(raw_path); @@ -65,8 +85,10 @@ ++rev_it; } + // Posix-style of LLDB detected. if (rev_it == r_end) - return DefaultComputeClangDirectory(file_spec); + return DefaultComputeClangResourceDirectory(lldb_shlib_spec, file_spec, + verify); // Inside Xcode and in Xcode toolchains LLDB is always in lockstep // with the Swift compiler, so it can reuse its Clang resource @@ -114,27 +136,17 @@ file_spec.SetFile(raw_path.c_str(), FileSpec::Style::native); FileSystem::Instance().Resolve(file_spec); return true; -} - -static bool ComputeClangDirectory(FileSpec &file_spec) { - if (FileSpec lldb_file_spec = HostInfo::GetShlibDir()) - return ComputeClangDirectory(lldb_file_spec, file_spec, true); - return false; -} -#else // __APPLE__ - -// All non-Apple posix systems. -static bool ComputeClangDirectory(FileSpec &file_spec) { - return DefaultComputeClangDirectory(file_spec); -} #endif // __APPLE__ +} #endif // _WIN32 FileSpec lldb_private::GetClangResourceDir() { static FileSpec g_cached_resource_dir; static llvm::once_flag g_once_flag; llvm::call_once(g_once_flag, []() { - ::ComputeClangDirectory(g_cached_resource_dir); + if (FileSpec lldb_file_spec = HostInfo::GetShlibDir()) + ComputeClangResourceDirectory(lldb_file_spec, g_cached_resource_dir, + true); Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST); if (log) log->Printf("GetClangResourceDir() => '%s'", Index: unittests/Expression/ClangParserTest.cpp =================================================================== --- unittests/Expression/ClangParserTest.cpp +++ unittests/Expression/ClangParserTest.cpp @@ -6,6 +6,8 @@ // //===----------------------------------------------------------------------===// +#include "clang/Basic/Version.h" + #include "Plugins/ExpressionParser/Clang/ClangHost.h" #include "TestingSupport/TestUtilities.h" #include "lldb/Host/FileSystem.h" @@ -29,32 +31,44 @@ }; } // namespace -#ifdef __APPLE__ -static std::string ComputeClangDir(std::string lldb_shlib_path, - bool verify = false) { +#if !defined(_WIN32) +static std::string ComputeClangResourceDir(std::string lldb_shlib_path, + bool verify = false) { FileSpec clang_dir; FileSpec lldb_shlib_spec(lldb_shlib_path); - ComputeClangDirectory(lldb_shlib_spec, clang_dir, verify); + ComputeClangResourceDirectory(lldb_shlib_spec, clang_dir, verify); return clang_dir.GetPath(); } +TEST_F(ClangHostTest, ComputeClangResourceDirectory) { + std::string path_to_liblldb = "/foo/bar/lib/"; + std::string path_to_clang_dir = "/foo/bar/lib/clang/" CLANG_VERSION_STRING; + 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)); +} + +#if defined(__APPLE__) TEST_F(ClangHostTest, MacOSX) { // This returns whatever the POSIX fallback returns. std::string posix = "/usr/lib/liblldb.dylib"; - EXPECT_FALSE(ComputeClangDir(posix).empty()); + EXPECT_FALSE(ComputeClangResourceDir(posix).empty()); std::string build = "/lldb-macosx-x86_64/Library/Frameworks/LLDB.framework/Versions/A"; std::string build_clang = "/lldb-macosx-x86_64/Library/Frameworks/LLDB.framework/Resources/Clang"; - EXPECT_EQ(ComputeClangDir(build), build_clang); + EXPECT_EQ(ComputeClangResourceDir(build), build_clang); std::string xcode = "/Applications/Xcode.app/Contents/SharedFrameworks/" "LLDB.framework/Versions/A"; std::string xcode_clang = "/Applications/Xcode.app/Contents/Developer/Toolchains/" "XcodeDefault.xctoolchain/usr/lib/swift/clang"; - EXPECT_EQ(ComputeClangDir(xcode), xcode_clang); + EXPECT_EQ(ComputeClangResourceDir(xcode), xcode_clang); std::string toolchain = "/Applications/Xcode.app/Contents/Developer/Toolchains/" @@ -63,17 +77,18 @@ std::string toolchain_clang = "/Applications/Xcode.app/Contents/Developer/Toolchains/" "Swift-4.1-development-snapshot.xctoolchain/usr/lib/swift/clang"; - EXPECT_EQ(ComputeClangDir(toolchain), toolchain_clang); + EXPECT_EQ(ComputeClangResourceDir(toolchain), toolchain_clang); std::string cltools = "/Library/Developer/CommandLineTools/Library/" "PrivateFrameworks/LLDB.framework"; std::string cltools_clang = "/Library/Developer/CommandLineTools/Library/PrivateFrameworks/" "LLDB.framework/Resources/Clang"; - EXPECT_EQ(ComputeClangDir(cltools), cltools_clang); + EXPECT_EQ(ComputeClangResourceDir(cltools), cltools_clang); // Test that a bogus path is detected. - EXPECT_NE(ComputeClangDir(GetInputFilePath(xcode), true), - ComputeClangDir(GetInputFilePath(xcode))); + EXPECT_NE(ComputeClangResourceDir(GetInputFilePath(xcode), true), + ComputeClangResourceDir(GetInputFilePath(xcode))); } -#endif +#endif // __APPLE__ +#endif // !_WIN32