1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
//===-- CppModuleConfigurationTest.cpp ------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "Plugins/ExpressionParser/Clang/CppModuleConfiguration.h"
#include "Plugins/ExpressionParser/Clang/ClangHost.h"
#include "TestingSupport/SubsystemRAII.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/HostInfo.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using namespace lldb_private;
namespace {
struct CppModuleConfigurationTest : public testing::Test {
llvm::MemoryBufferRef m_empty_buffer;
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> m_fs;
CppModuleConfigurationTest()
: m_empty_buffer("", "<empty buffer>"),
m_fs(new llvm::vfs::InMemoryFileSystem()) {}
void SetUp() override {
FileSystem::Initialize(m_fs);
HostInfo::Initialize();
}
void TearDown() override {
HostInfo::Terminate();
FileSystem::Terminate();
}
/// Utility function turning a list of paths into a FileSpecList.
FileSpecList makeFiles(llvm::ArrayRef<std::string> paths) {
FileSpecList result;
for (const std::string &path : paths) {
result.Append(FileSpec(path, FileSpec::Style::posix));
if (!m_fs->addFileNoOwn(path, static_cast<time_t>(0), m_empty_buffer))
llvm_unreachable("Invalid test configuration?");
}
return result;
}
};
} // namespace
/// Returns the Clang resource include directory.
static std::string ResourceInc() {
llvm::SmallString<256> resource_dir;
llvm::sys::path::append(resource_dir, GetClangResourceDir().GetPath(),
"include");
return std::string(resource_dir);
}
TEST_F(CppModuleConfigurationTest, Linux) {
// Test the average Linux configuration.
std::string usr = "/usr/include";
std::string libcpp = "/usr/include/c++/v1";
std::vector<std::string> files = {// C library
usr + "/stdio.h",
// C++ library
libcpp + "/vector",
libcpp + "/module.modulemap"};
CppModuleConfiguration config(makeFiles(files), llvm::Triple());
EXPECT_THAT(config.GetImportedModules(), testing::ElementsAre("std"));
EXPECT_THAT(config.GetIncludeDirs(),
testing::ElementsAre(libcpp, ResourceInc(), usr));
}
TEST_F(CppModuleConfigurationTest, LinuxTargetSpecificInclude) {
// Test the average Linux configuration.
std::string usr = "/usr/include";
std::string usr_target = "/usr/include/x86_64-linux-gnu";
std::string libcpp = "/usr/include/c++/v1";
std::string libcpp_target = "/usr/include/x86_64-unknown-linux-gnu/c++/v1";
std::vector<std::string> files = {
// C library
usr + "/stdio.h", usr_target + "/sys/cdefs.h",
// C++ library
libcpp + "/vector", libcpp + "/module.modulemap"};
CppModuleConfiguration config(makeFiles(files),
llvm::Triple("x86_64-unknown-linux-gnu"));
EXPECT_THAT(config.GetImportedModules(), testing::ElementsAre("std"));
EXPECT_THAT(config.GetIncludeDirs(),
testing::ElementsAre(libcpp, ResourceInc(), usr, usr_target,
libcpp_target));
}
TEST_F(CppModuleConfigurationTest, Sysroot) {
// Test that having a sysroot for the whole system works fine.
std::string libcpp = "/home/user/sysroot/usr/include/c++/v1";
std::string usr = "/home/user/sysroot/usr/include";
std::vector<std::string> files = {// C library
usr + "/stdio.h",
// C++ library
libcpp + "/vector",
libcpp + "/module.modulemap"};
CppModuleConfiguration config(makeFiles(files), llvm::Triple());
EXPECT_THAT(config.GetImportedModules(), testing::ElementsAre("std"));
EXPECT_THAT(config.GetIncludeDirs(),
testing::ElementsAre(libcpp, ResourceInc(), usr));
}
TEST_F(CppModuleConfigurationTest, LinuxLocalLibCpp) {
// Test that a locally build libc++ is detected.
std::string usr = "/usr/include";
std::string libcpp = "/home/user/llvm-build/include/c++/v1";
std::vector<std::string> files = {// C library
usr + "/stdio.h",
// C++ library
libcpp + "/vector",
libcpp + "/module.modulemap"};
CppModuleConfiguration config(makeFiles(files), llvm::Triple());
EXPECT_THAT(config.GetImportedModules(), testing::ElementsAre("std"));
EXPECT_THAT(config.GetIncludeDirs(),
testing::ElementsAre(libcpp, ResourceInc(), usr));
}
TEST_F(CppModuleConfigurationTest, UnrelatedLibrary) {
// Test that having an unrelated library in /usr/include doesn't break.
std::string usr = "/usr/include";
std::string libcpp = "/home/user/llvm-build/include/c++/v1";
std::vector<std::string> files = {// C library
usr + "/stdio.h",
// unrelated library
usr + "/boost/vector",
// C++ library
libcpp + "/vector",
libcpp + "/module.modulemap"};
CppModuleConfiguration config(makeFiles(files), llvm::Triple());
EXPECT_THAT(config.GetImportedModules(), testing::ElementsAre("std"));
EXPECT_THAT(config.GetIncludeDirs(),
testing::ElementsAre(libcpp, ResourceInc(), usr));
}
TEST_F(CppModuleConfigurationTest, UnrelatedLibraryWithTargetSpecificInclude) {
// Test that having an unrelated library in /usr/include doesn't break.
std::string usr = "/usr/include";
std::string libcpp = "/home/user/llvm-build/include/c++/v1";
std::string libcpp_target =
"/home/user/llvm-build/include/x86_64-unknown-linux-gnu/c++/v1";
std::vector<std::string> files = {// C library
usr + "/stdio.h",
// unrelated library
usr + "/boost/vector",
// C++ library
libcpp + "/vector",
libcpp + "/module.modulemap"};
CppModuleConfiguration config(makeFiles(files),
llvm::Triple("x86_64-unknown-linux-gnu"));
EXPECT_THAT(config.GetImportedModules(), testing::ElementsAre("std"));
EXPECT_THAT(config.GetIncludeDirs(),
testing::ElementsAre(libcpp, ResourceInc(), usr, libcpp_target));
}
TEST_F(CppModuleConfigurationTest, Xcode) {
// Test detection of libc++ coming from Xcode with generic platform names.
std::string p = "/Applications/Xcode.app/Contents/Developer/";
std::string libcpp = p + "Toolchains/B.xctoolchain/usr/include/c++/v1";
std::string usr =
p + "Platforms/A.platform/Developer/SDKs/OSVers.sdk/usr/include";
std::vector<std::string> files = {
// C library
usr + "/stdio.h",
// C++ library
libcpp + "/vector",
libcpp + "/module.modulemap",
};
CppModuleConfiguration config(makeFiles(files), llvm::Triple());
EXPECT_THAT(config.GetImportedModules(), testing::ElementsAre("std"));
EXPECT_THAT(config.GetIncludeDirs(),
testing::ElementsAre(libcpp, ResourceInc(), usr));
}
TEST_F(CppModuleConfigurationTest, LibCppV2) {
// Test that a "v2" of libc++ is still correctly detected.
std::string libcpp = "/usr/include/c++/v2";
std::vector<std::string> files = {// C library
"/usr/include/stdio.h",
// C++ library
libcpp + "/vector",
libcpp + "/module.modulemap"};
CppModuleConfiguration config(makeFiles(files), llvm::Triple());
EXPECT_THAT(config.GetImportedModules(), testing::ElementsAre("std"));
EXPECT_THAT(config.GetIncludeDirs(),
testing::ElementsAre("/usr/include/c++/v2", ResourceInc(),
"/usr/include"));
}
TEST_F(CppModuleConfigurationTest, UnknownLibCppFile) {
// Test that having some unknown file in the libc++ path doesn't break
// anything.
std::string libcpp = "/usr/include/c++/v1";
std::vector<std::string> files = {// C library
"/usr/include/stdio.h",
// C++ library
libcpp + "/non_existing_file",
libcpp + "/module.modulemap",
libcpp + "/vector"};
CppModuleConfiguration config(makeFiles(files), llvm::Triple());
EXPECT_THAT(config.GetImportedModules(), testing::ElementsAre("std"));
EXPECT_THAT(config.GetIncludeDirs(),
testing::ElementsAre("/usr/include/c++/v1", ResourceInc(),
"/usr/include"));
}
TEST_F(CppModuleConfigurationTest, MissingUsrInclude) {
// Test that we don't load 'std' if we can't find the C standard library.
std::string libcpp = "/usr/include/c++/v1";
std::vector<std::string> files = {// C++ library
libcpp + "/vector",
libcpp + "/module.modulemap"};
CppModuleConfiguration config(makeFiles(files), llvm::Triple());
EXPECT_THAT(config.GetImportedModules(), testing::ElementsAre());
EXPECT_THAT(config.GetIncludeDirs(), testing::ElementsAre());
}
TEST_F(CppModuleConfigurationTest, MissingLibCpp) {
// Test that we don't load 'std' if we don't have a libc++.
std::string usr = "/usr/include";
std::vector<std::string> files = {
// C library
usr + "/stdio.h",
};
CppModuleConfiguration config(makeFiles(files), llvm::Triple());
EXPECT_THAT(config.GetImportedModules(), testing::ElementsAre());
EXPECT_THAT(config.GetIncludeDirs(), testing::ElementsAre());
}
TEST_F(CppModuleConfigurationTest, IgnoreLibStdCpp) {
// Test that we don't do anything bad when we encounter libstdc++ paths.
std::string usr = "/usr/include";
std::vector<std::string> files = {
// C library
usr + "/stdio.h",
// C++ library
usr + "/c++/8.0.1/vector",
};
CppModuleConfiguration config(makeFiles(files), llvm::Triple());
EXPECT_THAT(config.GetImportedModules(), testing::ElementsAre());
EXPECT_THAT(config.GetIncludeDirs(), testing::ElementsAre());
}
TEST_F(CppModuleConfigurationTest, AmbiguousCLib) {
// Test that we don't do anything when we are not sure where the
// right C standard library is.
std::string usr1 = "/usr/include";
std::string usr2 = "/usr/include/other/path";
std::string libcpp = usr1 + "c++/v1";
std::vector<std::string> files = {
// First C library
usr1 + "/stdio.h",
// Second C library
usr2 + "/stdio.h",
// C++ library
libcpp + "/vector",
libcpp + "/module.modulemap",
};
CppModuleConfiguration config(makeFiles(files), llvm::Triple());
EXPECT_THAT(config.GetImportedModules(), testing::ElementsAre());
EXPECT_THAT(config.GetIncludeDirs(), testing::ElementsAre());
}
TEST_F(CppModuleConfigurationTest, AmbiguousLibCpp) {
// Test that we don't do anything when we are not sure where the
// right libc++ is.
std::string usr = "/usr/include";
std::string libcpp1 = usr + "c++/v1";
std::string libcpp2 = usr + "c++/v2";
std::vector<std::string> files = {
// C library
usr + "/stdio.h",
// First C++ library
libcpp1 + "/vector",
libcpp1 + "/module.modulemap",
// Second C++ library
libcpp2 + "/vector",
libcpp2 + "/module.modulemap",
};
CppModuleConfiguration config(makeFiles(files), llvm::Triple());
EXPECT_THAT(config.GetImportedModules(), testing::ElementsAre());
EXPECT_THAT(config.GetIncludeDirs(), testing::ElementsAre());
}
|