| 1 | //===-- ClangParserTest.cpp -----------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "clang/Basic/Version.h" |
| 10 | #include "clang/Config/config.h" |
| 11 | #include "clang/Driver/Driver.h" |
| 12 | |
| 13 | #include "Plugins/ExpressionParser/Clang/ClangHost.h" |
| 14 | #include "TestingSupport/SubsystemRAII.h" |
| 15 | #include "TestingSupport/TestUtilities.h" |
| 16 | #include "lldb/Host/Config.h" |
| 17 | #include "lldb/Host/FileSystem.h" |
| 18 | #include "lldb/Host/HostInfo.h" |
| 19 | #include "lldb/Utility/FileSpec.h" |
| 20 | #include "lldb/lldb-defines.h" |
| 21 | #include "gtest/gtest.h" |
| 22 | |
| 23 | using namespace lldb_private; |
| 24 | |
| 25 | namespace { |
| 26 | struct ClangHostTest : public testing::Test { |
| 27 | SubsystemRAII<FileSystem, HostInfo> subsystems; |
| 28 | }; |
| 29 | } // namespace |
| 30 | |
| 31 | static std::string ComputeClangResourceDir(std::string lldb_shlib_path, |
| 32 | bool verify = false) { |
| 33 | FileSpec clang_dir; |
| 34 | FileSpec lldb_shlib_spec(lldb_shlib_path); |
| 35 | ComputeClangResourceDirectory(lldb_shlib_spec, file_spec&: clang_dir, verify); |
| 36 | return clang_dir.GetPath(); |
| 37 | } |
| 38 | |
| 39 | TEST_F(ClangHostTest, ComputeClangResourceDirectory) { |
| 40 | #if !defined(_WIN32) |
| 41 | std::string path_to_liblldb = "/foo/bar/lib/" ; |
| 42 | #else |
| 43 | std::string path_to_liblldb = "C:\\foo\\bar\\lib\\" ; |
| 44 | #endif |
| 45 | std::string path_to_clang_dir = |
| 46 | clang::driver::Driver::GetResourcesPath(BinaryPath: path_to_liblldb + "liblldb" ); |
| 47 | llvm::SmallString<256> path_to_clang_lib_dir_real; |
| 48 | llvm::sys::fs::real_path(path: path_to_clang_dir, output&: path_to_clang_lib_dir_real); |
| 49 | |
| 50 | std::string computed_path = ComputeClangResourceDir(lldb_shlib_path: path_to_liblldb); |
| 51 | llvm::SmallString<256> computed_path_real; |
| 52 | llvm::sys::fs::real_path(path: computed_path, output&: computed_path_real); |
| 53 | |
| 54 | // When CLANG_RESOURCE_DIR is set, both the functions we use here behave in |
| 55 | // such a way that leads to one path being lib/ and the other bin/. Check that |
| 56 | // they are equivalent after any ".." have been resolved. |
| 57 | EXPECT_EQ(path_to_clang_lib_dir_real, computed_path_real); |
| 58 | |
| 59 | // The path doesn't really exist, so setting verify to true should make |
| 60 | // ComputeClangResourceDir not give you path_to_clang_dir. |
| 61 | EXPECT_NE(ComputeClangResourceDir(path_to_liblldb, true), path_to_clang_dir); |
| 62 | } |
| 63 | |
| 64 | #if defined(__APPLE__) |
| 65 | TEST_F(ClangHostTest, MacOSX) { |
| 66 | // This returns whatever the POSIX fallback returns. |
| 67 | std::string posix = "/usr/lib/liblldb.dylib" ; |
| 68 | EXPECT_FALSE(ComputeClangResourceDir(posix).empty()); |
| 69 | |
| 70 | std::string build = |
| 71 | "/lldb-macosx-x86_64/Library/Frameworks/LLDB.framework/Versions/A" ; |
| 72 | std::string build_clang = |
| 73 | "/lldb-macosx-x86_64/Library/Frameworks/LLDB.framework/Resources/Clang" ; |
| 74 | EXPECT_EQ(ComputeClangResourceDir(build), build_clang); |
| 75 | |
| 76 | std::string xcode = "/Applications/Xcode.app/Contents/SharedFrameworks/" |
| 77 | "LLDB.framework/Versions/A" ; |
| 78 | std::string xcode_clang = |
| 79 | "/Applications/Xcode.app/Contents/Developer/Toolchains/" |
| 80 | "XcodeDefault.xctoolchain/usr/lib/swift/clang" ; |
| 81 | EXPECT_EQ(ComputeClangResourceDir(xcode), xcode_clang); |
| 82 | |
| 83 | std::string toolchain = |
| 84 | "/Applications/Xcode.app/Contents/Developer/Toolchains/" |
| 85 | "Swift-4.1-development-snapshot.xctoolchain/System/Library/" |
| 86 | "PrivateFrameworks/LLDB.framework" ; |
| 87 | std::string toolchain_clang = |
| 88 | "/Applications/Xcode.app/Contents/Developer/Toolchains/" |
| 89 | "Swift-4.1-development-snapshot.xctoolchain/usr/lib/swift/clang" ; |
| 90 | EXPECT_EQ(ComputeClangResourceDir(toolchain), toolchain_clang); |
| 91 | |
| 92 | std::string cltools = "/Library/Developer/CommandLineTools/Library/" |
| 93 | "PrivateFrameworks/LLDB.framework" ; |
| 94 | std::string cltools_clang = |
| 95 | "/Library/Developer/CommandLineTools/Library/PrivateFrameworks/" |
| 96 | "LLDB.framework/Resources/Clang" ; |
| 97 | EXPECT_EQ(ComputeClangResourceDir(cltools), cltools_clang); |
| 98 | |
| 99 | // Test that a bogus path is detected. |
| 100 | EXPECT_NE(ComputeClangResourceDir(GetInputFilePath(xcode), true), |
| 101 | ComputeClangResourceDir(GetInputFilePath(xcode))); |
| 102 | } |
| 103 | #endif // __APPLE__ |
| 104 | |