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
23using namespace lldb_private;
24
25namespace {
26struct ClangHostTest : public testing::Test {
27 SubsystemRAII<FileSystem, HostInfo> subsystems;
28};
29} // namespace
30
31static 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
39TEST_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 = clang::driver::Driver::GetResourcesPath(
46 BinaryPath: path_to_liblldb + "liblldb", CLANG_RESOURCE_DIR);
47 EXPECT_EQ(ComputeClangResourceDir(path_to_liblldb), path_to_clang_dir);
48
49 // The path doesn't really exist, so setting verify to true should make
50 // ComputeClangResourceDir not give you path_to_clang_dir.
51 EXPECT_NE(ComputeClangResourceDir(path_to_liblldb, true), path_to_clang_dir);
52}
53
54#if defined(__APPLE__)
55TEST_F(ClangHostTest, MacOSX) {
56 // This returns whatever the POSIX fallback returns.
57 std::string posix = "/usr/lib/liblldb.dylib";
58 EXPECT_FALSE(ComputeClangResourceDir(posix).empty());
59
60 std::string build =
61 "/lldb-macosx-x86_64/Library/Frameworks/LLDB.framework/Versions/A";
62 std::string build_clang =
63 "/lldb-macosx-x86_64/Library/Frameworks/LLDB.framework/Resources/Clang";
64 EXPECT_EQ(ComputeClangResourceDir(build), build_clang);
65
66 std::string xcode = "/Applications/Xcode.app/Contents/SharedFrameworks/"
67 "LLDB.framework/Versions/A";
68 std::string xcode_clang =
69 "/Applications/Xcode.app/Contents/Developer/Toolchains/"
70 "XcodeDefault.xctoolchain/usr/lib/swift/clang";
71 EXPECT_EQ(ComputeClangResourceDir(xcode), xcode_clang);
72
73 std::string toolchain =
74 "/Applications/Xcode.app/Contents/Developer/Toolchains/"
75 "Swift-4.1-development-snapshot.xctoolchain/System/Library/"
76 "PrivateFrameworks/LLDB.framework";
77 std::string toolchain_clang =
78 "/Applications/Xcode.app/Contents/Developer/Toolchains/"
79 "Swift-4.1-development-snapshot.xctoolchain/usr/lib/swift/clang";
80 EXPECT_EQ(ComputeClangResourceDir(toolchain), toolchain_clang);
81
82 std::string cltools = "/Library/Developer/CommandLineTools/Library/"
83 "PrivateFrameworks/LLDB.framework";
84 std::string cltools_clang =
85 "/Library/Developer/CommandLineTools/Library/PrivateFrameworks/"
86 "LLDB.framework/Resources/Clang";
87 EXPECT_EQ(ComputeClangResourceDir(cltools), cltools_clang);
88
89 // Test that a bogus path is detected.
90 EXPECT_NE(ComputeClangResourceDir(GetInputFilePath(xcode), true),
91 ComputeClangResourceDir(GetInputFilePath(xcode)));
92}
93#endif // __APPLE__
94

source code of lldb/unittests/Expression/ClangParserTest.cpp