19379d19fSRaphael Isemann //===-- CppModuleConfiguration.cpp ----------------------------------------===// 29379d19fSRaphael Isemann // 39379d19fSRaphael Isemann // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 49379d19fSRaphael Isemann // See https://llvm.org/LICENSE.txt for license information. 59379d19fSRaphael Isemann // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 69379d19fSRaphael Isemann // 79379d19fSRaphael Isemann //===----------------------------------------------------------------------===// 89379d19fSRaphael Isemann 99379d19fSRaphael Isemann #include "CppModuleConfiguration.h" 109379d19fSRaphael Isemann 119379d19fSRaphael Isemann #include "ClangHost.h" 129379d19fSRaphael Isemann #include "lldb/Host/FileSystem.h" 139379d19fSRaphael Isemann 149379d19fSRaphael Isemann using namespace lldb_private; 159379d19fSRaphael Isemann 169379d19fSRaphael Isemann bool CppModuleConfiguration::SetOncePath::TrySet(llvm::StringRef path) { 179379d19fSRaphael Isemann // Setting for the first time always works. 189379d19fSRaphael Isemann if (m_first) { 199379d19fSRaphael Isemann m_path = path.str(); 209379d19fSRaphael Isemann m_valid = true; 219379d19fSRaphael Isemann m_first = false; 229379d19fSRaphael Isemann return true; 239379d19fSRaphael Isemann } 249379d19fSRaphael Isemann // Changing the path to the same value is fine. 259379d19fSRaphael Isemann if (m_path == path) 269379d19fSRaphael Isemann return true; 279379d19fSRaphael Isemann 289379d19fSRaphael Isemann // Changing the path after it was already set is not allowed. 299379d19fSRaphael Isemann m_valid = false; 309379d19fSRaphael Isemann return false; 319379d19fSRaphael Isemann } 329379d19fSRaphael Isemann 339379d19fSRaphael Isemann bool CppModuleConfiguration::analyzeFile(const FileSpec &f) { 3408f90e3dSRaphael Isemann using namespace llvm::sys::path; 3508f90e3dSRaphael Isemann // Convert to slashes to make following operations simpler. 3608f90e3dSRaphael Isemann std::string dir_buffer = convert_to_slash(f.GetDirectory().GetStringRef()); 379379d19fSRaphael Isemann llvm::StringRef posix_dir(dir_buffer); 389379d19fSRaphael Isemann 399379d19fSRaphael Isemann // Check for /c++/vX/ that is used by libc++. 409379d19fSRaphael Isemann static llvm::Regex libcpp_regex(R"regex(/c[+][+]/v[0-9]/)regex"); 419379d19fSRaphael Isemann if (libcpp_regex.match(f.GetPath())) { 429379d19fSRaphael Isemann // Strip away libc++'s /experimental directory if there is one. 439379d19fSRaphael Isemann posix_dir.consume_back("/experimental"); 449379d19fSRaphael Isemann return m_std_inc.TrySet(posix_dir); 459379d19fSRaphael Isemann } 469379d19fSRaphael Isemann 479379d19fSRaphael Isemann // Check for /usr/include. On Linux this might be /usr/include/bits, so 489379d19fSRaphael Isemann // we should remove that '/bits' suffix to get the actual include directory. 499379d19fSRaphael Isemann if (posix_dir.endswith("/usr/include/bits")) 509379d19fSRaphael Isemann posix_dir.consume_back("/bits"); 519379d19fSRaphael Isemann if (posix_dir.endswith("/usr/include")) 529379d19fSRaphael Isemann return m_c_inc.TrySet(posix_dir); 539379d19fSRaphael Isemann 549379d19fSRaphael Isemann // File wasn't interesting, continue analyzing. 559379d19fSRaphael Isemann return true; 569379d19fSRaphael Isemann } 579379d19fSRaphael Isemann 589379d19fSRaphael Isemann bool CppModuleConfiguration::hasValidConfig() { 599379d19fSRaphael Isemann // We all these include directories to have a valid usable configuration. 609379d19fSRaphael Isemann return m_c_inc.Valid() && m_std_inc.Valid(); 619379d19fSRaphael Isemann } 629379d19fSRaphael Isemann 639379d19fSRaphael Isemann CppModuleConfiguration::CppModuleConfiguration( 649379d19fSRaphael Isemann const FileSpecList &support_files) { 659379d19fSRaphael Isemann // Analyze all files we were given to build the configuration. 66186f1c58SRaphael Isemann bool error = !llvm::all_of(support_files, 679379d19fSRaphael Isemann std::bind(&CppModuleConfiguration::analyzeFile, 689379d19fSRaphael Isemann this, std::placeholders::_1)); 699379d19fSRaphael Isemann // If we have a valid configuration at this point, set the 709379d19fSRaphael Isemann // include directories and module list that should be used. 719379d19fSRaphael Isemann if (!error && hasValidConfig()) { 729379d19fSRaphael Isemann // Calculate the resource directory for LLDB. 739379d19fSRaphael Isemann llvm::SmallString<256> resource_dir; 749379d19fSRaphael Isemann llvm::sys::path::append(resource_dir, GetClangResourceDir().GetPath(), 759379d19fSRaphael Isemann "include"); 76*adcd0268SBenjamin Kramer m_resource_inc = std::string(resource_dir.str()); 779379d19fSRaphael Isemann 789379d19fSRaphael Isemann // This order matches the way Clang orders these directories. 799379d19fSRaphael Isemann m_include_dirs = {m_std_inc.Get(), m_resource_inc, m_c_inc.Get()}; 809379d19fSRaphael Isemann m_imported_modules = {"std"}; 819379d19fSRaphael Isemann } 829379d19fSRaphael Isemann } 83