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 
11 #include "Plugins/ExpressionParser/Clang/ClangHost.h"
12 #include "TestingSupport/SubsystemRAII.h"
13 #include "TestingSupport/TestUtilities.h"
14 #include "lldb/Host/FileSystem.h"
15 #include "lldb/Host/HostInfo.h"
16 #include "lldb/Utility/FileSpec.h"
17 #include "lldb/lldb-defines.h"
18 #include "gtest/gtest.h"
19 
20 using namespace lldb_private;
21 
22 namespace {
23 struct ClangHostTest : public testing::Test {
24   SubsystemRAII<FileSystem, HostInfo> subsystems;
25 };
26 } // namespace
27 
28 static std::string ComputeClangResourceDir(std::string lldb_shlib_path,
29                                            bool verify = false) {
30   FileSpec clang_dir;
31   FileSpec lldb_shlib_spec(lldb_shlib_path);
32   ComputeClangResourceDirectory(lldb_shlib_spec, clang_dir, verify);
33   return clang_dir.GetPath();
34 }
35 
36 TEST_F(ClangHostTest, ComputeClangResourceDirectory) {
37 #if !defined(_WIN32)
38   std::string path_to_liblldb = "/foo/bar/lib/";
39   std::string path_to_clang_dir = "/foo/bar/lib/clang/" CLANG_VERSION_STRING;
40 #else
41   std::string path_to_liblldb = "C:\\foo\\bar\\lib";
42   std::string path_to_clang_dir = "C:\\foo\\bar\\lib\\clang\\" CLANG_VERSION_STRING;
43 #endif
44   EXPECT_EQ(ComputeClangResourceDir(path_to_liblldb), path_to_clang_dir);
45 
46   // The path doesn't really exist, so setting verify to true should make
47   // ComputeClangResourceDir not give you path_to_clang_dir.
48   EXPECT_NE(ComputeClangResourceDir(path_to_liblldb, true), path_to_clang_dir);
49 }
50 
51 #if defined(__APPLE__)
52 TEST_F(ClangHostTest, MacOSX) {
53   // This returns whatever the POSIX fallback returns.
54   std::string posix = "/usr/lib/liblldb.dylib";
55   EXPECT_FALSE(ComputeClangResourceDir(posix).empty());
56 
57   std::string build =
58       "/lldb-macosx-x86_64/Library/Frameworks/LLDB.framework/Versions/A";
59   std::string build_clang =
60       "/lldb-macosx-x86_64/Library/Frameworks/LLDB.framework/Resources/Clang";
61   EXPECT_EQ(ComputeClangResourceDir(build), build_clang);
62 
63   std::string xcode = "/Applications/Xcode.app/Contents/SharedFrameworks/"
64                       "LLDB.framework/Versions/A";
65   std::string xcode_clang =
66       "/Applications/Xcode.app/Contents/Developer/Toolchains/"
67       "XcodeDefault.xctoolchain/usr/lib/swift/clang";
68   EXPECT_EQ(ComputeClangResourceDir(xcode), xcode_clang);
69 
70   std::string toolchain =
71       "/Applications/Xcode.app/Contents/Developer/Toolchains/"
72       "Swift-4.1-development-snapshot.xctoolchain/System/Library/"
73       "PrivateFrameworks/LLDB.framework";
74   std::string toolchain_clang =
75       "/Applications/Xcode.app/Contents/Developer/Toolchains/"
76       "Swift-4.1-development-snapshot.xctoolchain/usr/lib/swift/clang";
77   EXPECT_EQ(ComputeClangResourceDir(toolchain), toolchain_clang);
78 
79   std::string cltools = "/Library/Developer/CommandLineTools/Library/"
80                         "PrivateFrameworks/LLDB.framework";
81   std::string cltools_clang =
82       "/Library/Developer/CommandLineTools/Library/PrivateFrameworks/"
83       "LLDB.framework/Resources/Clang";
84   EXPECT_EQ(ComputeClangResourceDir(cltools), cltools_clang);
85 
86   // Test that a bogus path is detected.
87   EXPECT_NE(ComputeClangResourceDir(GetInputFilePath(xcode), true),
88             ComputeClangResourceDir(GetInputFilePath(xcode)));
89 }
90 #endif // __APPLE__
91