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