1*061da546Spatrick //===-- CppModuleConfiguration.cpp ----------------------------------------===// 2*061da546Spatrick // 3*061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*061da546Spatrick // See https://llvm.org/LICENSE.txt for license information. 5*061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*061da546Spatrick // 7*061da546Spatrick //===----------------------------------------------------------------------===// 8*061da546Spatrick 9*061da546Spatrick #include "CppModuleConfiguration.h" 10*061da546Spatrick 11*061da546Spatrick #include "ClangHost.h" 12*061da546Spatrick #include "lldb/Host/FileSystem.h" 13*061da546Spatrick 14*061da546Spatrick using namespace lldb_private; 15*061da546Spatrick 16*061da546Spatrick bool CppModuleConfiguration::SetOncePath::TrySet(llvm::StringRef path) { 17*061da546Spatrick // Setting for the first time always works. 18*061da546Spatrick if (m_first) { 19*061da546Spatrick m_path = path.str(); 20*061da546Spatrick m_valid = true; 21*061da546Spatrick m_first = false; 22*061da546Spatrick return true; 23*061da546Spatrick } 24*061da546Spatrick // Changing the path to the same value is fine. 25*061da546Spatrick if (m_path == path) 26*061da546Spatrick return true; 27*061da546Spatrick 28*061da546Spatrick // Changing the path after it was already set is not allowed. 29*061da546Spatrick m_valid = false; 30*061da546Spatrick return false; 31*061da546Spatrick } 32*061da546Spatrick 33*061da546Spatrick bool CppModuleConfiguration::analyzeFile(const FileSpec &f) { 34*061da546Spatrick using namespace llvm::sys::path; 35*061da546Spatrick // Convert to slashes to make following operations simpler. 36*061da546Spatrick std::string dir_buffer = convert_to_slash(f.GetDirectory().GetStringRef()); 37*061da546Spatrick llvm::StringRef posix_dir(dir_buffer); 38*061da546Spatrick 39*061da546Spatrick // Check for /c++/vX/ that is used by libc++. 40*061da546Spatrick static llvm::Regex libcpp_regex(R"regex(/c[+][+]/v[0-9]/)regex"); 41*061da546Spatrick if (libcpp_regex.match(f.GetPath())) { 42*061da546Spatrick // Strip away libc++'s /experimental directory if there is one. 43*061da546Spatrick posix_dir.consume_back("/experimental"); 44*061da546Spatrick return m_std_inc.TrySet(posix_dir); 45*061da546Spatrick } 46*061da546Spatrick 47*061da546Spatrick // Check for /usr/include. On Linux this might be /usr/include/bits, so 48*061da546Spatrick // we should remove that '/bits' suffix to get the actual include directory. 49*061da546Spatrick if (posix_dir.endswith("/usr/include/bits")) 50*061da546Spatrick posix_dir.consume_back("/bits"); 51*061da546Spatrick if (posix_dir.endswith("/usr/include")) 52*061da546Spatrick return m_c_inc.TrySet(posix_dir); 53*061da546Spatrick 54*061da546Spatrick // File wasn't interesting, continue analyzing. 55*061da546Spatrick return true; 56*061da546Spatrick } 57*061da546Spatrick 58*061da546Spatrick bool CppModuleConfiguration::hasValidConfig() { 59*061da546Spatrick // We all these include directories to have a valid usable configuration. 60*061da546Spatrick return m_c_inc.Valid() && m_std_inc.Valid(); 61*061da546Spatrick } 62*061da546Spatrick 63*061da546Spatrick CppModuleConfiguration::CppModuleConfiguration( 64*061da546Spatrick const FileSpecList &support_files) { 65*061da546Spatrick // Analyze all files we were given to build the configuration. 66*061da546Spatrick bool error = !llvm::all_of(support_files, 67*061da546Spatrick std::bind(&CppModuleConfiguration::analyzeFile, 68*061da546Spatrick this, std::placeholders::_1)); 69*061da546Spatrick // If we have a valid configuration at this point, set the 70*061da546Spatrick // include directories and module list that should be used. 71*061da546Spatrick if (!error && hasValidConfig()) { 72*061da546Spatrick // Calculate the resource directory for LLDB. 73*061da546Spatrick llvm::SmallString<256> resource_dir; 74*061da546Spatrick llvm::sys::path::append(resource_dir, GetClangResourceDir().GetPath(), 75*061da546Spatrick "include"); 76*061da546Spatrick m_resource_inc = resource_dir.str(); 77*061da546Spatrick 78*061da546Spatrick // This order matches the way Clang orders these directories. 79*061da546Spatrick m_include_dirs = {m_std_inc.Get(), m_resource_inc, m_c_inc.Get()}; 80*061da546Spatrick m_imported_modules = {"std"}; 81*061da546Spatrick } 82*061da546Spatrick } 83