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