| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| USE_LIBCPP := 1 | ||
| CXX_SOURCES := main.cpp | ||
| include Makefile.rules |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| USE_LIBCPP := 1 | ||
| CXX_SOURCES := main.cpp | ||
| include Makefile.rules |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| USE_LIBCPP := 1 | ||
| CXX_SOURCES := main.cpp | ||
| include Makefile.rules |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| //===-- CppModuleConfiguration.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 "CppModuleConfiguration.h" | ||
|
|
||
| #include "ClangHost.h" | ||
| #include "lldb/Host/FileSystem.h" | ||
|
|
||
| using namespace lldb_private; | ||
|
|
||
| bool CppModuleConfiguration::SetOncePath::TrySet(llvm::StringRef path) { | ||
| // Setting for the first time always works. | ||
| if (m_first) { | ||
| m_path = path.str(); | ||
| m_valid = true; | ||
| m_first = false; | ||
| return true; | ||
| } | ||
| // Changing the path to the same value is fine. | ||
| if (m_path == path) | ||
| return true; | ||
|
|
||
| // Changing the path after it was already set is not allowed. | ||
| m_valid = false; | ||
| return false; | ||
| } | ||
|
|
||
| bool CppModuleConfiguration::analyzeFile(const FileSpec &f) { | ||
| llvm::SmallString<256> dir_buffer = f.GetDirectory().GetStringRef(); | ||
| // Convert to posix style so that we can use '/'. | ||
| llvm::sys::path::native(dir_buffer, llvm::sys::path::Style::posix); | ||
| llvm::StringRef posix_dir(dir_buffer); | ||
|
|
||
| // Check for /c++/vX/ that is used by libc++. | ||
| static llvm::Regex libcpp_regex(R"regex(/c[+][+]/v[0-9]/)regex"); | ||
| if (libcpp_regex.match(f.GetPath())) { | ||
| // Strip away libc++'s /experimental directory if there is one. | ||
| posix_dir.consume_back("/experimental"); | ||
| return m_std_inc.TrySet(posix_dir); | ||
| } | ||
|
|
||
| // Check for /usr/include. On Linux this might be /usr/include/bits, so | ||
| // we should remove that '/bits' suffix to get the actual include directory. | ||
| if (posix_dir.endswith("/usr/include/bits")) | ||
| posix_dir.consume_back("/bits"); | ||
| if (posix_dir.endswith("/usr/include")) | ||
| return m_c_inc.TrySet(posix_dir); | ||
|
|
||
| // File wasn't interesting, continue analyzing. | ||
| return true; | ||
| } | ||
|
|
||
| bool CppModuleConfiguration::hasValidConfig() { | ||
| // We all these include directories to have a valid usable configuration. | ||
| return m_c_inc.Valid() && m_std_inc.Valid(); | ||
| } | ||
|
|
||
| CppModuleConfiguration::CppModuleConfiguration( | ||
| const FileSpecList &support_files) { | ||
| // Analyze all files we were given to build the configuration. | ||
| bool error = !std::all_of(support_files.begin(), support_files.end(), | ||
| std::bind(&CppModuleConfiguration::analyzeFile, | ||
| this, std::placeholders::_1)); | ||
| // If we have a valid configuration at this point, set the | ||
| // include directories and module list that should be used. | ||
| if (!error && hasValidConfig()) { | ||
| // Calculate the resource directory for LLDB. | ||
| llvm::SmallString<256> resource_dir; | ||
| llvm::sys::path::append(resource_dir, GetClangResourceDir().GetPath(), | ||
| "include"); | ||
| m_resource_inc = resource_dir.str(); | ||
|
|
||
| // This order matches the way Clang orders these directories. | ||
| m_include_dirs = {m_std_inc.Get(), m_resource_inc, m_c_inc.Get()}; | ||
| m_imported_modules = {"std"}; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| //===-- CppModuleConfiguration.h --------------------------------*- C++ -*-===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef liblldb_CppModuleConfiguration_h_ | ||
| #define liblldb_CppModuleConfiguration_h_ | ||
|
|
||
| #include <lldb/Core/FileSpecList.h> | ||
| #include <llvm/Support/Regex.h> | ||
|
|
||
| namespace lldb_private { | ||
|
|
||
| /// A Clang configuration when importing C++ modules. | ||
| /// | ||
| /// Includes a list of include paths that should be used when importing | ||
| /// and a list of modules that can be imported. Currently only used when | ||
| /// importing the 'std' module and its dependencies. | ||
| class CppModuleConfiguration { | ||
| /// Utility class for a path that can only be set once. | ||
| class SetOncePath { | ||
| std::string m_path; | ||
| bool m_valid = false; | ||
| /// True iff this path hasn't been set yet. | ||
| bool m_first = true; | ||
|
|
||
| public: | ||
| /// Try setting the path. Returns true if the path was set and false if | ||
| /// the path was already set. | ||
| LLVM_NODISCARD bool TrySet(llvm::StringRef path); | ||
| /// Return the path if there is one. | ||
| std::string Get() const { | ||
| assert(m_valid && "Called Get() on an invalid SetOncePath?"); | ||
| return m_path; | ||
| } | ||
| /// Returns true iff this path was set exactly once so far. | ||
| bool Valid() const { return m_valid; } | ||
| }; | ||
|
|
||
| /// If valid, the include path used for the std module. | ||
| SetOncePath m_std_inc; | ||
| /// If valid, the include path to the C library (e.g. /usr/include). | ||
| SetOncePath m_c_inc; | ||
| /// The Clang resource include path for this configuration. | ||
| std::string m_resource_inc; | ||
|
|
||
| std::vector<std::string> m_include_dirs; | ||
| std::vector<std::string> m_imported_modules; | ||
|
|
||
| /// Analyze a given source file to build the current configuration. | ||
| /// Returns false iff there was a fatal error that makes analyzing any | ||
| /// further files pointless as the configuration is now invalid. | ||
| bool analyzeFile(const FileSpec &f); | ||
|
|
||
| public: | ||
| /// Creates a configuraiton by analyzing the given list of used source files. | ||
| /// | ||
| /// Currently only looks at the used paths and doesn't actually access the | ||
| /// files on the disk. | ||
| explicit CppModuleConfiguration(const FileSpecList &support_files); | ||
| /// Creates an empty and invalid configuration. | ||
| CppModuleConfiguration() {} | ||
|
|
||
| /// Returns true iff this is a valid configuration that can be used to | ||
| /// load and compile modules. | ||
| bool hasValidConfig(); | ||
|
|
||
| /// Returns a list of include directories that should be used when using this | ||
| /// configuration (e.g. {"/usr/include", "/usr/include/c++/v1"}). | ||
| llvm::ArrayRef<std::string> GetIncludeDirs() const { return m_include_dirs; } | ||
|
|
||
| /// Returns a list of (top level) modules that should be imported when using | ||
| /// this configuration (e.g. {"std"}). | ||
| llvm::ArrayRef<std::string> GetImportedModules() const { | ||
| return m_imported_modules; | ||
| } | ||
| }; | ||
|
|
||
| } // namespace lldb_private | ||
|
|
||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| //===-- CppModuleConfigurationTest.cpp ---------------------------*- C++-*-===// | ||
| // | ||
| // 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 "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 { | ||
| static void SetUpTestCase() { | ||
| // Getting the resource directory uses those subsystems, so we should | ||
| // initialize them. | ||
| FileSystem::Initialize(); | ||
| HostInfo::Initialize(); | ||
| } | ||
| static void TearDownTestCase() { | ||
| HostInfo::Terminate(); | ||
| FileSystem::Terminate(); | ||
| } | ||
| }; | ||
| } // 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 resource_dir.str().str(); | ||
| } | ||
|
|
||
| /// Utility function turningn a list of paths into a FileSpecList. | ||
| static FileSpecList makeFiles(llvm::ArrayRef<std::string> paths) { | ||
| FileSpecList result; | ||
| for (const std::string &path : paths) | ||
| result.Append(FileSpec(path)); | ||
| return result; | ||
| } | ||
|
|
||
| TEST_F(CppModuleConfigurationTest, Linux) { | ||
| // Test the average Linux configuration. | ||
| std::string libcpp = "/usr/include/c++/v1"; | ||
| std::string usr = "/usr/include"; | ||
| CppModuleConfiguration config( | ||
| makeFiles({usr + "/bits/types.h", libcpp + "/vector"})); | ||
| EXPECT_THAT(config.GetImportedModules(), testing::ElementsAre("std")); | ||
| EXPECT_THAT(config.GetIncludeDirs(), | ||
| testing::ElementsAre(libcpp, ResourceInc(), usr)); | ||
| } | ||
|
|
||
| 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"; | ||
| CppModuleConfiguration config( | ||
| makeFiles({usr + "/bits/types.h", libcpp + "/vector"})); | ||
| 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 libcpp = "/home/user/llvm-build/include/c++/v1"; | ||
| std::string usr = "/usr/include"; | ||
| CppModuleConfiguration config( | ||
| makeFiles({usr + "/bits/types.h", libcpp + "/vector"})); | ||
| 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 libcpp = "/home/user/llvm-build/include/c++/v1"; | ||
| std::string usr = "/usr/include"; | ||
| CppModuleConfiguration config(makeFiles( | ||
| {usr + "/bits/types.h", libcpp + "/vector", usr + "/boost/vector"})); | ||
| EXPECT_THAT(config.GetImportedModules(), testing::ElementsAre("std")); | ||
| EXPECT_THAT(config.GetIncludeDirs(), | ||
| testing::ElementsAre(libcpp, ResourceInc(), usr)); | ||
| } | ||
|
|
||
| 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"; | ||
| CppModuleConfiguration config( | ||
| makeFiles({libcpp + "/unordered_map", usr + "/stdio.h"})); | ||
| 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. | ||
| CppModuleConfiguration config( | ||
| makeFiles({"/usr/include/bits/types.h", "/usr/include/c++/v2/vector"})); | ||
| 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. | ||
| CppModuleConfiguration config(makeFiles( | ||
| {"/usr/include/bits/types.h", "/usr/include/c++/v1/non_existing_file"})); | ||
| 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. | ||
| CppModuleConfiguration config(makeFiles({"/usr/include/c++/v1/vector"})); | ||
| 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++. | ||
| CppModuleConfiguration config(makeFiles({"/usr/include/bits/types.h"})); | ||
| 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. | ||
| CppModuleConfiguration config(makeFiles( | ||
| {"/usr/include/bits/types.h", "/usr/include/c++/8.0.1/vector"})); | ||
| 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. | ||
| CppModuleConfiguration config( | ||
| makeFiles({"/usr/include/bits/types.h", "/usr/include/c++/v1/vector", | ||
| "/sysroot/usr/include/bits/types.h"})); | ||
| 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. | ||
| CppModuleConfiguration config( | ||
| makeFiles({"/usr/include/bits/types.h", "/usr/include/c++/v1/vector", | ||
| "/usr/include/c++/v2/vector"})); | ||
| EXPECT_THAT(config.GetImportedModules(), testing::ElementsAre()); | ||
| EXPECT_THAT(config.GetIncludeDirs(), testing::ElementsAre()); | ||
| } |